Function to perform multiple linear regression.
OCMATH_API int ocmath_multiple_linear_regression(const double* pX, UINT nOSizeN, UINT nVSizeM, const double* pY, const double* pWT, UINT nWTSize, const LROptions* psLROptions, FitParameter* psFitParameter, UINT nFitSize, RegStats* psRegStats = NULL, RegANOVA* psRegANOVA = NULL, double* pCov = NULL, double* pCorr = NULL, uint nCovCorrRowSize = 0, uint nCovCorrColSize = 0, double* pH = NULL, double* pSE = NULL, int* pAuxErrCode = NULL ,LackStat* pLack = NULL , bool bCalXIntercept = false, , double* pFittedY = NULL, double* pResiduals = NULL )
Returns STATS_NO_ERROR on successful exit and a non-zero STATS error code on failure.
Error Codes:
STATS_ERROR_SETTING(-151): when psLROptions->Confidence<0 or psLROptions->Confidence > 1.
STATS_INPUT_NULL_POINTER(-156): pX is NULL or pY is NULL.
STATS_ERROR_WEIGHT_DIFF_SIZE(-174): if nWTSize is not 0, nWTSize should equal to nOSizeN
STATS_PARAMS_ARRAY_TOO_SMALL(-98): nCovCorrRowSize < nVSizeM + 1 or nCovCorrColSize < nVSizeM + 1, or nFitSize < nVSizeM.
STATS_ERROR_DATA_PTS_LESS_THAN_PARA_NUM(-212): the number of input data cannot less than the number of parameters.
Other error code please refer to g02dac.
EX1
void ocmath_multiple_linear_regression_ex1() { 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); dr.Add("Y", wks, 0, 3, -1, 3); matrix mX; dr.GetData(mX, 0, 0); vector vY; dr.GetData(&vY, 1); // prepare input and output variables UINT nOSizeN = mX.GetNumRows(); UINT nVSizeM = mX.GetNumCols(); LROptions stLROptions; stLROptions.UseReducedChiSq = 1; FitParameter stFitParameters[4]; // should be nVSizeM+1 UINT nFitSize = nVSizeM+1; RegStats stRegStats; RegANOVA stRegANOV; // calculate 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; } // output the parameter value to Script Window for(int nn = 0; nn < nFitSize; nn++) printf("Param %d=%g\n", nn+1, stFitParameters[nn].Value); // output statistics results to Result Log Tree tr; TreeNode trResult = tr.AddNode("MR"); TreeNode trStats = trResult.AddNode("Stats"); trStats += stRegStats; TreeNode trANOVA = trResult.AddNode("ANOVA"); trANOVA += stRegANOV; string str; tree_to_str(trResult, str); Project.OutStringToResultsLog(str); }
EX2
void ocmath_multiple_linear_regression_ex2() { 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); dr.Add("Y", wks, 0, 3, -1, 3); matrix mX; dr.GetData(mX, 0, 0); vector vY; dr.GetData(&vY, 1); // prepare input and output variables UINT nOSizeN = mX.GetNumRows(); UINT nVSizeM = mX.GetNumCols(); LROptions stLROptions; stLROptions.UseReducedChiSq = 1; FitParameter stFitParameters[4]; // should be nVSizeM+1 UINT nFitSize = nVSizeM+1; RegStats stRegStats; RegANOVA stRegANOV; //get fit and residual vector vF, vR; vF.SetSize(nOSizeN); vR.SetSize(nOSizeN); // calculate int nRet = ocmath_multiple_linear_regression(mX, nOSizeN, nVSizeM, vY, NULL, 0, &stLROptions, stFitParameters, nFitSize, &stRegStats, &stRegANOV, NULL, NULL, 0, 0, NULL, NULL, NULL, NULL, false, vF, vR); if( nRet != STATS_NO_ERROR ) { out_str("Error"); return; } // output the parameter value to Script Window for(int nn = 0; nn < nFitSize; nn++) printf("Param %d=%g\n", nn+1, stFitParameters[nn].Value); // output statistics results to Result Log Tree tr; TreeNode trResult = tr.AddNode("MR"); TreeNode trStats = trResult.AddNode("Stats"); trStats += stRegStats; TreeNode trANOVA = trResult.AddNode("ANOVA"); trANOVA += stRegANOV; string str; tree_to_str(trResult, str); Project.OutStringToResultsLog(str); }
The function finds a QR decomposition of X. If R is of full rank, solution is obtained from the QR decomposition. If R is not full rank, a solution is obtain by means of a singular value decomposion(SVD) of R. If R is not full rank, STATS_ERROR_RANK_DEFICIENT is returned in addition to the solutions. The computational engine is the NAG function nag_regsn_mult_linear (g02dac).
origin.h