2.1.11.15 ocmath_linear_fit


Description

Function to perform simple linear regression. using g02cac, The computational engine is the NAG function nag_simple_linear_regression (g02cac).

Syntax

int ocmath_linear_fit( const double * pX, const double * pY, int nSize, FitParameter * psFitParameter, const double * pWT = NULL, UINT nWTSize = 0, const LROptions * psLROptions = NULL, RegStats * psRegStats = NULL, RegANOVA * psRegANOVA = NULL, RegCalcInternal * psInternal = NULL, double * pCov = NULL, double * pCorr = NULL, uint nCovCorrRowSize = 0, uint nCovCorrColSize = 0 )

Parameters

pX
[Input] matrix containing data points of the independent variables
pY
[Input] vector containing data points of dependent variable, size of pY = n
nSize
[Input] size of pX and pY, the number of observations, n. n>=2
psFitParameter
[Output] pointer to structs to receive information on each fitting parameter
pWT
[Optional Input] vector containing weight of the data, all data in pWT should not less than zero
nWTSize
[Optional Input] the size of the weight, should be the same as nOSizeN
psLROptions
[Optional Input] pointer to struct for linear regression options.
psRegStats
[Optional Output] pointer to struct containing Regression statistics
psRegANOVA
[Optional Output] pointer to struct containing ANOVA statistics.
psInternal
[Optional Output] pointer to struct containing Sxx, Sxy, Syy ect
pCov
[Optional Output] pointer to covariance matrix of estimate
pCorr
[Optional 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

Return

Returns STATS_NO_ERROR on successful exit and a non-zero STATS error code on failure.

Examples

EX1

void ocmath_linear_fit_ex1()
{
    // regression data input directly by vector
    vector vx = {1,0,4,7.5,2.5,0,10,5}; 
    vector vy = {20,15.5,28.3,45,24.5,10,99,31.2};
    vector vw = {1,1,1,1,1,1,1,1};

    // or obtain regression data from an Origin Worksheet
    //Worksheet wks=Project.ActiveLayer();
    //if(!wks)
    //    printf("no active worksheet found\n");    
    //Dataset dsSrcX(wks,0);
    //Dataset dsSrcY(wks,1);
    //Dataset dsSrcW(wks,2);
    //vector vx(nSize);
    //vector vy(nSize);
    //vector vw(nSize);
    //vx=dsSrcX;
    //vy=dsSrcY;
    //vw=dsSrcW;

    int  n;
    n = vx.GetSize();
    
    double a, b, err_a, err_b, rsq, rss, df;

    FitParameter sFitParameter[2];
    LROptions sLROption;
    RegStats sRegStats;
    ocmath_linear_fit(vx, vy, n, sFitParameter, vw, n, NULL, &sRegStats);
    a = sFitParameter[0].Value;
    err_a = sFitParameter[0].Error;
    b = sFitParameter[1].Value;
    err_b = sFitParameter[1].Error;
    
    rsq = sRegStats.RSqCOD;
    rss = sRegStats.SSR;
    df = sRegStats.DOF;
    
    // output intepretation: a+b*x  b is the slope, a is the intercept

    printf("\nRegression constant a = %6.4f\n\n", a);
    printf("Standard error of the regression constant a = %6.4f\n\n",
              err_a); 
    
    printf("Regression coefficient b = %6.4f\n\n", b);
      printf("Standard error of the regression coefficient b = %6.4f\n\n",
          err_b);
    
      printf("The regression coefficient of determination = %6.4f\n\n", rsq);
      printf("The sum of squares of the residuals about the "
          "regression = %6.4f\n\n", rss);
      printf("Number of degrees of freedom about the "
          "regression = %6.4f\n\n",df);

    //Expected Result:      
    //Regression constant a = 7.5982

    //Standard error of the regression constant a = 6.6858
    //
    //Regression coefficient b = 7.0905
    //
    //Standard error of the regression coefficient b = 1.3224
    //
    //The regression coefficient of determination = 0.8273
    //
    //The sum of squares of the residuals about the regression = 965.2454
    //
    //Number of degrees of freedom about the regression = 6.0000
}

Remark

See Also

Header to Include

origin.h

Reference