Origin uses the ocmath_multiple_linear_regression function to perform multiple linear regression. This function allows to specify the weight of data and linear regression options. After running this function successfully, output details will include fitting parameters, regression statistics, ANOVA statistics, covariance and correlation matrix of estimate, and etc.
In the following sections, we will learn how to perform multiple linear regression by using this function.
To perform multiple linear regression, please import the desired data, here will use three independents and one dependent.
1. Load data for multiple linear regression. All the independent data should be stored in a matrix, and dependent data in a vector.
// 1. get data for multiple linear regression Worksheet wks = Project.ActiveLayer(); if( !wks ) return; // please make sure a worksheet with data is active DataRange dr; dr.Add("X", wks, 0, 0, -1, 2); // first three columns dr.Add("Y", wks, 0, 3, -1, 3); // the fourth column matrix mX; dr.GetData(mX, 0, 0); // get data of first three columns to matrix vector vY; dr.GetData(&vY, 1); // get data of the fourth column
2. Declare and initialize the parameters that will be passed to the function.
// 2. prepare input and output variables UINT nOSizeN = mX.GetNumRows(); // number of observations UINT nVSizeM = mX.GetNumCols(); // total number of independent variables LROptions stLROptions; // use to set linear regression options stLROptions.UseReducedChiSq = 1; FitParameter stFitParameters[4]; // should be nVSizeM+1 UINT nFitSize = nVSizeM+1; // size of FitParameter RegStats stRegStats; // use to get regression statistics RegANOVA stRegANOV; // use to get anova statistics
3. Pass the prepared parameters to the function and perform multiple linear regression.
// 3. perform multiple linear regression, here we are not going to get // the covariance and correlation matrix of estimate, and no weight is used. int nRet = ocmath_multiple_linear_regression(mX, nOSizeN, nVSizeM, vY, NULL, 0, &stLROptions, stFitParameters, nFitSize, &stRegStats, &stRegANOV); if( nRet != STATS_NO_ERROR ) { out_str("Error"); return; }
After finishing the calculation, the results may need to output to somewhere for presentation, such as Script Window, Result Log, Worksheet, etc.
Please refer to the Result to Output Window and Result to Worksheet section in the chapter Analysis and Applications: Curve Fitting: Linear Fitting for more details about how to output the results.