2.1.11.16 ocmath_multiple_linear_regression
Description
Function to perform multiple linear regression.
Syntax
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)
Parameters
- pX
- [Input] matrix containing data points of the independent variables
- nOSizeN
- [Input] the number of observations, nOSizeN>=2
- nVSizeM
- [Input] the total number of independent variables in the data set, nVSizeM>=1
- pY
- [Input] vector containing data points of dependent variable, size of pY = nOSizeN
- pWT
- [Input] vector containing weight of the data, all date in pWT should not less than zero
- nWTSize
- [Input] the size of the weight, should be the same as nOSizeN
- psLROptions
- [Input] struct for linear regression options
- psFitParameter
- [Output] pointer to structs to receive information on each fitting parameter
- nFitSize
- [Input] the size of psFitParameter, should be nVSizeM + 1
- psRegStats
- [Output] pointer to struct containing Regression statistics
- psRegANOVA
- [Output] pointer to struct containing ANOVA statistics
- pCov
- [Output] pointer to covariance matrix of estimate
- pCorr
- [Output] pointer to correlation matrix of estimate
- nCovCorrRowSize
- [Input] row size of Covariance and Correlation matrix, which should be nVSizeM + 1
- nCovCorrColSize
- [Input] column size of Covariance and Correlation matrix, which should be nVSizeM + 1
- pH
- [Output] pointer to diagonal element of H, leverage Size of the pH should be nOSizeN
- pSE
- [Output] pointer to standard error of mean predicted values. Size of the pSE should be nOSizeN
Return
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.
Examples
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);
}
Remark
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).
See Also
Header to Include
origin.h
Reference
|