OK, what is the trick to extracting the overall p value from an lm object? It shows up in the summary(lm(model)) output but I can't seem to extract it:> test2 = apply(aa, 1, function(x) summary(lm(x[,1] ~ 0 + x[,3] + x[,6]))) > test2[[1]]Call: lm(formula = x[, 1] ~ 0 + x[, 3] + x[, 6]) [omitted summary output] F-statistic: 40.94 on 2 and 7 DF, p-value: 0.0001371 It does not seem to be obtainable from anova(lm(model)) either, only the p values for the individual predictors. Stumped. Jim Bouldin Research Ecologist [[alternative HTML version deleted]]
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:> OK, what is the trick to extracting the overall p value from an lm object?
> It shows up in the summary(lm(model)) output but I can't seem to
extract it:
>
>> test2 = apply(aa, 1, function(x) summary(lm(x[,1] ~ 0 + x[,3] +
x[,6])))
>> test2[[1]]
>
> Call:
> lm(formula = x[, 1] ~ 0 + x[, 3] + x[, 6])
>
> [omitted summary output]
> F-statistic: 40.94 on 2 and 7 DF, ?p-value: 0.0001371
>
> It does not seem to be obtainable from anova(lm(model)) either, only the p
> values for the individual predictors.
> Stumped.
>
> Jim Bouldin
> Research Ecologist
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/
It's not directly extractable since it's calculated on the fly in the
printing method. If you type stats:::print.summary.lm, you can see
the code the leads to the calculation: It's basically (I'm leaving out
some formatting stuff):
pf(x$fstatistic[1L], x$fstatistic[2L], x$fstatistic[3L], lower.tail = FALSE)
where the fstatistic is calculated in summary.lm. You can write a
little helper to calculate this yourself -- just something like
getModelPValue <- function(m) {
stopifnot(inherits(m, "lm"))
s <- summary.lm(m)
pf(s$fstatistic[1L], s$fstatistic[2L], s$fstatistic[3L], lower.tail = FALSE)
}
And you can just extract the necessary parts of summary.lm if speed is
a concern.
Hope this helps,
Michael
On Mon, Oct 24, 2011 at 1:47 PM, Jim Bouldin <bouldinjr at gmail.com>
wrote:> OK, what is the trick to extracting the overall p value from an lm object?
> It shows up in the summary(lm(model)) output but I can't seem to
extract it:
>
>> test2 = apply(aa, 1, function(x) summary(lm(x[,1] ~ 0 + x[,3] +
x[,6])))
>> test2[[1]]
>
> Call:
> lm(formula = x[, 1] ~ 0 + x[, 3] + x[, 6])
>
> [omitted summary output]
> F-statistic: 40.94 on 2 and 7 DF, ?p-value: 0.0001371
>
> It does not seem to be obtainable from anova(lm(model)) either, only the p
> values for the individual predictors.
> Stumped.
>
> Jim Bouldin
> Research Ecologist
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
On 24/10/2011 1:47 PM, Jim Bouldin wrote:> OK, what is the trick to extracting the overall p value from an lm object? > It shows up in the summary(lm(model)) output but I can't seem to extract it:It's not part of the object, it is computed when the object is printed. To see the print method, do this: class(test2[[1]]) (which will print "summary.lm" if I'm reading your code properly). Then print.summary.lm is the print method: getAnywhere(print.summary.lm) It's fairly long, but I think the part you want is 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") Here, x is the summary object. Duncan Murdoch> > test2 = apply(aa, 1, function(x) summary(lm(x[,1] ~ 0 + x[,3] + x[,6]))) > > test2[[1]] > > Call: > lm(formula = x[, 1] ~ 0 + x[, 3] + x[, 6]) > > [omitted summary output] > F-statistic: 40.94 on 2 and 7 DF, p-value: 0.0001371 > > It does not seem to be obtainable from anova(lm(model)) either, only the p > values for the individual predictors. > Stumped. > > Jim Bouldin > Research Ecologist > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.