ocmath_diagnostic_residuals

 

Description

Function to compute diagnostic residual for fits(NLSF, LR, PR and MR)

Syntax

int ocmath_diagnostic_residuals( const int nSize, const int nParam, const double dSSE, int nResid, const double * pResid, const double * pX, double * pStadRes, double * pStudRes, double * pStudDelRes, double * pCook, double * pAtkinson, double * pH = NULL )

Parameters

nSize
[input] the number of data points
nParam
[input] the number of parameters fit by the regression.
dSSE
[input] the sum of square of residuals
nResid
[input] size of residuals
pResid
[input] pointer to residuals in the fits
pX
[input] pointer to matix of X, which is X in MR and partial derivatives matrix in NLSF
the matrix should have nParam columns and nSize rows. The variable won't be used if pH is specified
pStadRes
[output] pointer to stardardized residual. Its size is nResid
pStudRes
[output] pointer to studentized residual. Its size is nResid
pStudDelRes
[output] pointer to studentized deleted residual. Its size is nResid
pCook
[output] pointer to Cook'sD statistic. Its size is nResid
pAtkinson
[output] pointer to Atkinson's T statistic. Its size is nResid
pH
[input] optional output pointer to leverage. Only needed in MR. Its size is nSize

Return

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

Examples

EX1

//The example find studentized residuals for 2 order polynomial fit
#include <FDFTree.h>
#include <ONLSF.h>
#include <ocStats.h>
#include <stats_utils.h>
void ocmath_diagnostic_residuals_ex1()
{
    string        strFDF = okutil_get_origin_path(ORIGIN_PATH_SYSTEM, "FitFunc")+"PARABOLA.FDF";
    int nParam = 3; //2th Order polynomial has 3 parameters. 
    Tree tr;
    if( !nlsf_FDF_to_tree( strFDF, &tr ))
        return;
    
    NumericFunction    NF;
    NF.SetTree(tr);

    vector        vParas;
    vParas.SetSize(nParam);
    vParas[0] = 11.2717;        // a
    vParas[1] = -3.81714;        // b
    vParas[2] = 0.4619;    // c
    
    // X-values:
    //vector vY={4, 3.3, 3.5, 3.8, 4.2, 5};
    vector vX={3,4,4.5,5,5.5, 6}; 
    int    nNumPts = vX.GetSize();

    vector vA, vB, vC;
    vA.SetSize(nNumPts);
    vB.SetSize(nNumPts);
    vC.SetSize(nNumPts);
    
    for (int ipt = 0; ipt < nNumPts; ipt++)
    {
        double            arrPartialDerivs[3];
        BOOL            bb = NF.EvalPartialDerivatives(vParas, arrPartialDerivs, vX[ipt]);
        if (!bb)
        {
            out_str("Failed!");
            return;
        }
        
        vA[ipt] = arrPartialDerivs[0];
        vB[ipt] = arrPartialDerivs[1];
        vC[ipt] = arrPartialDerivs[2];
    }
    
    matrix mDerivX; //Patial derivative matrix
    mDerivX.SetSize(nNumPts, nParam);
    mDerivX.SetColumn(vA, 0);
    mDerivX.SetColumn(vB, 1);
    mDerivX.SetColumn(vC, 2);
    
    double dSSE = 0.00629;
    vector vRes = {0.0226, -0.0936, 0.0519, 0.0664, -0.0500, 0.0026};
    DiagResid psDiagResid_1[8];
    vector vStad(nNumPts), vStud(nNumPts), vStudDel(nNumPts), vCook(nNumPts), vAtkinson(nNumPts);
    int nRet = ocmath_diagnostic_residuals(nNumPts, nParam, dSSE, vRes.GetSize(), vRes, mDerivX, vStad, vStud, vStudDel, vCook, vAtkinson);
}

EX2

#include <stats_utils.h>
void ocmath_diagnostic_residuals_ex2()
{
    int nRet;
    
    UINT nPts,nM;
    uint nSizeFitParams;
    LROptions psLROptions;
    RegStats stRegStats;
    RegANOVA stRegANOVA;
    matrix mX(6,3)={{1,   2,   3},
                     {3.2, 1.1, 3.4},
                     {1,   2.6, 5},
                     {5,   5,   2.1},
                     {1,   6,   7},
                     {2,    8,    10}};
    vector vY ={4,2,6, 7, 8, 10};
    vector vWeight = {1, 1, 1, 1, 1, 1};
    psLROptions.FixIntercept = false;
    psLROptions.Confidence = 0.95;
    FitParameter psFitParameter_1[4];
    vector vH, vSE;
    matrix mCov, mCorr;
    nRet = stats_multiple_linear_regression(mX, vY, vWeight, psLROptions, psFitParameter_1, 4, &stRegStats, &stRegANOVA, mCov, mCorr, vH, vSE);
    if (nRet != STATS_NO_ERROR)
        printf("error in stats_multiple_linear_regression");

    vector vRes = {-0.46398, -0.49247, 1.1874, 0.26653, -0.5148, 0.01732};
    int nNumPts = mX.GetNumRows();
    int nParam = mX.GetNumCols() + 1;
    vector vStad(nNumPts), vStud(nNumPts), vStudDel(nNumPts), vCook(nNumPts), vAtkinson(nNumPts);
    nRet = ocmath_diagnostic_residuals(nNumPts, nParam, stRegStats.ReducedChiSq, vRes.GetSize(), vRes, mX, vStad, vStud, vStudDel, vCook, vAtkinson, vH);
    if (nRet != STATS_NO_ERROR)
        printf("error in ocmath_diagnostic_residuals");
}

Remark

See Also

header to Include

origin.h

Reference