[R] getting the p-value from lm as a list object

Marc Schwartz marc_schwartz at comcast.net
Fri Oct 31 17:21:23 CET 2008


on 10/31/2008 10:59 AM eric lee wrote:
> Hi,
> 
> I'm trying to get the p-value from the 'lm' regression function as a list
> object.  For example, I can get r^2 from the following code by entering
> summary(fm)$r.squared.  Is there a way to get the p-value?  If not, is there
> a function where I can enter the f-value and degrees of freedom to get the
> p-value?  Thanks.
> 
> x <- c(1,2,3,4,5,6,7,8,9,10)
> y <- c(1,2,3,4,4,5,6,8,1,9)
> 
> fm <- lm(y ~ x)
> str(summary(fm))


The default output is created in stats:::print.summary.lm().

The basic incantation is:

  pf(x$fstatistic[1], x$fstatistic[2], x$fstatistic[3],
     lower.tail = FALSE)

where:

  pf() is the F distribution function

  x = the summary.lm model object

  x$fstatistic[1] = model F statistic

  x$fstatistic[2] = model numerator DF

  x$fstatistic[3] = model denominator DF



Thus, using lm.D9 from example(lm):


> summary(lm.D9)

Call:
lm(formula = weight ~ group)

Residuals:
    Min      1Q  Median      3Q     Max
-1.0710 -0.4938  0.0685  0.2462  1.3690

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   5.0320     0.2202  22.850 9.55e-15 ***
groupTrt     -0.3710     0.3114  -1.191    0.249
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.6964 on 18 degrees of freedom
Multiple R-squared: 0.07308,	Adjusted R-squared: 0.02158
F-statistic: 1.419 on 1 and 18 DF,  p-value: 0.249


> summary(lm.D9)$fstatistic
    value     numdf     dendf
 1.419101  1.000000 18.000000


> pf(1.419, 1, 18, lower = FALSE)
[1] 0.2490394


See ?pf

HTH,

Marc Schwartz



More information about the R-help mailing list