18.104.22.168 Linear, Polynomial and Multiple Regression
In LabTalk scripts, three simple quick use X-Functions, fitLR, fitPoly, and fitMR, are available for performing linear regression, polynomial regression, and multiple linear regression, respectively. And the -h switch can be used to see the argument list.
fitLR finds a best fit straight line to a given dataset.
newbook; // create a new book
// file name
string strFile$ = system.path.program$ + "Samples\Curve Fitting\Linear Fit.dat";
impasc fname:=strFile$; // import the data
int nn = wks.ncols;
wks.addcol(); // Add a new column
wks.col$(nn+1).lname$ = FitData; // Assign a long name to the new column
// perform linear fit on the first ten points of column 1 (X) and column 2 (Y)
// and the fitted data is output to FitData column
fitLR iy:=(1,2) oy:=col("FitData");
// a tree object named fitLR is created, and contains the output values
fitLR.a = ; // output the fitted intercept
fitLR.b = ; // output the fitted slope
fitLR.= ; // output all the results, which include fitted intercept and slope
More examples about linear regression can be found in Curve Fitting sample page, or under Fitting category in XF Script Dialog (press F11 to open).
fitPoly is used to control polynomial fitting.
Polynomial fitting is a special case wherein the fitting function is mathematically non-linear, but an analytical (non-iterative) solution is obtained. In LabTalk,
newbook; // create a new book;
// file name
string strFile$ = system.path.program$ + "Samples\Curve Fitting\Polynomial Fit.dat";
impasc fname:=strFile$; // import data
page.xlcolname = 0; // turn off Spreadsheet Cell Notation firstly
wks.addcol(PolyCoef); // add a new column for polynomial coefficients
wks.addcol(FittedX); // add a new column for fitted X values
wks.addcol(FittedY); // add a new column for fitted Y values
// perform polynomial fitting on column 1 (X) and column 3 (Y)
// polynomial order is 3
fitPoly iy:=(1,3) polyorder:=3 coef:=col(PolyCoef) oy:=(col(FittedX),col(FittedY));
// the results are stored in the tree named fitPoly, output it
Additionally, fitPoly provides the outputs for adjusted residual sum of squares, coefficient of determination, and errors in polynomial coefficients. For more detailed examples, please refer to Curve Fitting sample page, or Fitting category in XF Script Dialog (press F11 to open).
Multiple Linear Regression
Multiple linear regression studies the relationship between several predictor variables and a response variable, which is an extension of simple linear regression.
// create a new book and import some data
fn$ = system.path.program$ + "Samples\Curve Fitting\Multiple Linear Regression.dat";
page.xlcolname = 0; //turn off Spreadsheet Cell Notation firstly
wks.addcol(FitValue); // add a column for fitted values of dependent
// perform multiple linear regression
// column D is dependent, and column A, B, and C are independents
// the output results are stored in a tree, tr
fitMR dep:=col(D) indep:=col(A):col(C) mrtree:=tr odep:=col(FitValue);
tr.= ; // output the result tree
For more examples, please refer to Curve Fitting sample page, or Fitting category in XF Script Dialog (press F11 to open).
Run Operation Classes to Perform Regression
The X-Functions depicted above are for simple quick use only to perform linear, polynomial and multiple regression. That is to say, some quantities are not available when using these three X-Functions. For full access to all quantities, the X-Function xop is provided to invoke the internal menu commands (operation commands), so to run the corresponding operation classes to perform regression. The following example shows how to use the X-Function xop to perform linear fit, and generate a report.
// create a new book and import data
fname$ = system.path.program$ + "Samples\Curve Fitting\Linear Fit.dat";
tree lrGUI; // GUI tree for linear fit
// initialize the GUI tree, with the FitLinear class
xop execute:=init classname:=FitLinear iotrgui:=lrGUI;
// specify the input data in the GUI tree
lrGUI.GUI.InputData.Range1.X$ = col(A);
lrGUI.GUI.InputData.Range1.Y$ = col(C);
// perform linear fit and generate a report with the prepared GUI tree
xop execute:=report iotrgui:=lrGUI;
xop execute:=cleanup; // clean up linear fit operation objects after fitting