title: How do I calculate p values of a linear regression given the covariance matrix and fit coefficients
body: <p>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. </p>
  
  <p>In C, I have the covariance matrix, sum of squared residuals value and fit coefficients. Using these, how do I calculate the p values?</p>
  
  <p>I have attempted this approach using "<a href="https://stackoverflow.com/questions/5503733/getting-p-value-for-linear-regression-in-c-gsl-fit-linear-function-from-gsl-li">Getting p-value for linear regression in C gsl_fit_linear() function from GSL library</a>"</p>
  
  <p>Can anyone confirm it's validity? The results it gives differ for me compared to R.</p>
  
  <p>I have isolated this line of C code as incorrect, but I can't see why:</p>
  
  <pre><code>double pv0=t0&lt;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
  </code></pre>
  
  <p>Results given by R:</p>
  
  <p>Coefficients:</p>
  
  <pre><code>            Estimate Std. Error t value Pr(&gt;|t|)   
  (Intercept) -700.000    226.569  -3.090  0.05373 . 
  x             60.000      6.831   8.783  0.00311 **
  </code></pre>
  
  <p>Results given by C:</p>
  
  <pre><code>Coefficients    Estimate Std. Error   t value   Pr(&gt;|t|)
  (Intercept) -700.000000  226.568606 -3.089572 -550099700.000000
            x   60.000000    6.831301  8.783101 -4.000000
  </code></pre>