Skip to content
Prev 275377 / 398506 Next

extract the p value

Hi Jim,

Its a bit of a trick question.  There isn't actually any overall p
value stored in the lm object, or even in the summary.lm object.  It
is calculated from the f statistic by the print methods for
summary.lm.  Of course, none of that helps, per se.  Try this:

summary(lm(mpg ~ hp, data = mtcars))$fstatistic

f <- summary(lm(mpg ~ hp, data = mtcars))$fstatistic

## get p-value
pf(q = f[1], df1 = f[2], df2 = f[3], lower.tail = FALSE)

If you are interested in tracking things "under the hood":

# show methods for summary
methods(summary)
# leads to
summary.lm

## shows the structure of the summary
str(summary(lm(mpg ~ hp, data = mtcars)))

hmm no p value

how is the summary shown? it must be printed, which happens silently

methods(print)

leading us to: print.summary.lm*

the asterisk means it is a non-exported object, so to view the source
will take a bit more work.  From experience I know it is in the stats
package, so:

stats:::print.summary.lm

shows the code for the print method for summary.lm objects.  Towards
the end, you'll see:

    cat(",\tAdjusted R-squared:", formatC(x$adj.r.squared,
            digits = digits), "\nF-statistic:", formatC(x$fstatistic[1L],
            digits = digits), "on", x$fstatistic[2L], "and",
            x$fstatistic[3L], "DF,  p-value:", format.pval(pf(x$fstatistic[1L],
                x$fstatistic[2L], x$fstatistic[3L], lower.tail = FALSE),
                digits = digits), "\n")
    }

which is where it gets and outputs to the console the p value.

HTH,

Josh
On Mon, Oct 24, 2011 at 10:47 AM, Jim Bouldin <bouldinjr at gmail.com> wrote: