Search code examples
crregressionp-value

How do I calculate p values of a linear regression given the covariance matrix and fit coefficients


I have performed a linear regression in C using the GSL library. I've performed the same regression in R. I can access the p values for this regression in R using the "summary" command.

In C, I have the covariance matrix, sum of squared residuals value and fit coefficients. Using these, how do I calculate the p values?

I have attempted this approach using "Getting p-value for linear regression in C gsl_fit_linear() function from GSL library"

Can anyone confirm it's validity? The results it gives differ for me compared to R.

I have isolated this line of C code as incorrect, but I can't see why:

double pv0=t0<0?2*(1-gsl_cdf_tdist_P(-t0,n-2)):2*(1-gsl_cdf_tdist_P(t0,n-2));//This is the p-value of the constant term

Results given by R:

Coefficients:

            Estimate Std. Error t value Pr(>|t|)   
(Intercept) -700.000    226.569  -3.090  0.05373 . 
x             60.000      6.831   8.783  0.00311 **

Results given by C:

Coefficients    Estimate Std. Error   t value   Pr(>|t|)
(Intercept) -700.000000  226.568606 -3.089572 -550099700.000000
          x   60.000000    6.831301  8.783101 -4.000000

Solution

  • gsl_cdf_tdist_P should expect two double arguments and return a double between 0 and 1.

    Check all your types first.

    If it's returning values outside 0-1 when all the types are correct, there's a very serious problem somewhere.