5.11 Statistics with Labtalk

Summary

This tutorial demonstrates using labtalk for statistical analysis. We will start with datasets which consists of daily percent change of nine stocks in three sectors in 2012. We will compare the performance between the three stock sector during the year.

What Will You Learn

This tutorial will show you how to use Labtalk script to:

  • Calculate the average daily data of each sector with rowstats to perform statistics on rows
  • Perform statistics on column with grouping to calculate the monthly data.
  • Plot line+symbol plot and box chart with calculated data
  • Perform Descriptive Stats on matrix object with theme

Steps

Calculate the means on the rows

The x-function rowstats is used to perform descriptive statistics on each row of data in a specific range. Here, calculate the Mean value of the stock percent change on the same date for three sectors respectively, and put the results on specified columns.

// Define the file name  
string fname$ = system.path.program$ + "Samples\Statistics\Stocks.dat";
//Use the impfile x-function to import the specified file
newbook;
impASC fname:=fname$ options.ImpMode:=3 options.FileStruct.NumericSeparator:=0;

//Save the active workbook short name
string bk$=%H;

// Turn off Spreadsheet Cell Notation firstly
page.xlcolname = 0;
// Add four new columns with short name;
wks.addcol(M);
wks.addcol(M1);
wks.addcol(M2);
wks.addcol(M3);
 
// Set the "Long Name" for four new columns;
col(M)[L]$=Month;
col(M1)[L]$=Technology;
col(M2)[L]$=Electronics Manufacturer;
col(M3)[L]$=Consumer Products;

// Copy Date column to Month column
col(M)=col(Date);
// Set column 11 (Month) to date format
wks.col11.format = 4; 
// Set column 11 (Month) display as "May"
wks.col11.subformat = 17; 
 
// Calculate the means on the rows for three sectors. 
rowstats -r 1 irng:=col(2):col(4) mean:=col(M1) sd:=<optional>;
rowstats -r 1 irng:=col(5):col(7) mean:=col(M2) sd:=<optional>;
rowstats -r 1 irng:=col(8):col(10) mean:=col(M3) sd:=<optional>;

For the Spreadsheet Cell Notation in the workbook, please see FAQ-849 for more information.

Create box graphs with template

Create box graph using the plotxy x-function. All of the possible values for the plot type option can be found in the Plot Type IDs

// Create Box plots with "box" template by the means
plotxy iy:=((1,12),(1,13),(1,14)) plot:=206 ogl:=<new template:=box>;
 
// Rotate 30 degree on the x label of active layer
range ll = !;
ll.x.label.rotate = 30;

Statistics on columns with grouping

To get the monthly Mean value of the stock percent change in the three sectors, perform descriptive statistics on columns with grouping in the active sheet and generate a report sheet.

Statistics on Columns analysis is quite complicated and it has been implemented through a framework of Origin C called the operation framework. The operation framework allows these operation based classes to be called from Origin C, and thus an xop X-Function has been created to run the operation classes.

// Now the newly created graph is the active window
// But we need to activate original workbook 
win -a %(bk$);

// Use the DescStats class to create a GUI tree "statGUI"
tree statGUI;
xop execute:=init classname:=DescStats iotrgui:=statGUI;

// Specify the input data in the GUI tree
statGUI.GUI.InputData.Range1.X$=Col(M1:M3);
// Specify the grouping data
statGUI.GUI.InputData.Range1.F$=Col(Month);
 
// Specify some settings of the Statistics on Columns,
// Set the value of N total, Standard Deviation, Sum, Max, Min and  Median not show in the result.
statGUI.GUI.Quantities.moments.N=0;
statGUI.GUI.Quantities.moments.SD=0;
statGUI.GUI.Quantities.moments.Sum=0;
statGUI.GUI.Quantities.quantiles.Min=0;
statGUI.GUI.Quantities.quantiles.Median=0;
statGUI.GUI.Quantities.quantiles.Max=0;

// Generate a report tree with the prepared GUI tree
xop execute:=report iotrgui:=statGUI otrresult:=statOut;

// Clean up operation objects after statistics
xop execute:=cleanup;

After we create a GUI tree "statGUI" with the script,

xop execute:=init classname:=DescStats iotrgui:=statGUI;

we can run script below for the detailed settings in "statGUI" tree

statGUI.=


Unstack columns

To plot the graph, unstack columns with three sectors (Data column in the DescStatsQuantities1 sheet). The wunstackcol X-Function is the most convenient way to perform this task from script. The columns are unstacked by a particular category, which we specify with the input range irng2. The data to be displayed (by category) is referenced by input range irng1.

// Unstack all other columns using Data column
// Place "Data" in "Long Name" row of output sheet
page.active$ ="DescStatsQuantities1";
wunstackcol -r 2 irng1:=col(Mean) irng2:=col(Data) nonstack:=1 other:=col(Month) pos:=lname ow:="UnstackCols1";

Create Line+Symbol graph

Create Line+Symbol graph, using the plotxy x-function. All of the possible values for the plot type option can be found in the Plot Type IDs

page.active$ ="UnstackCols1";
// Create Line plots by columns 2, 3 and 4 in the active Worksheet
plotxy iy:=((1,2),(1,3),(1,4)) plot:=202;
//Update Legend as the Long Name of the source data. 
legendupdate mode:=lname;

Perform Descriptive Stats on matrix object with theme

To prepare the theme, open Statistics on Columns dialog, and then go to Output tab

  • under Report Tables branch, set Book = <new>. Then it will create a new workbook for report table.
  • under Quantities branch, set Book = <report>. Then it will put Quantities to the same book as report table.
  • click the triangle button on top of the dialog, and select Save As to save these change to a theme named "reportnewbook"
// use the DescStats class to create a GUI tree "statGUI" with theme "reportnewbook"
tree statGUI;
xop execute:=init classname:=DescStats iotrgui:=statGUI theme:=reportnewbook;

// set input data
statGUI.GUI.InputData.Range1.X$=[MBook1]MSheet1!1:0; //1:0 means all matrix objects

// change report output to Book1, it was set to <new> in theme "reportnewbook"
statGUI.GUI.Output.Report.Book$ = "Book1";

// generate report with the prepared GUI tree, and clean up operation objects 
xop execute:=report iotrgui:=statGUI;
xop execute:=cleanup;