pull out p-values and r-squared from a linear regression
You can see the structure of the object returned by summary()
by calling str(summary(fit))
. Each piece can be accessed using $
. The p-value for the F statistic is more easily had from the object returned by anova
.
Concisely, you can do this:
rSquared <- summary(fit)$r.squared
pVal <- anova(fit)$'Pr(>F)'[1]
r-squared: You can return the r-squared value directly from the summary object summary(fit)$r.squared
. See names(summary(fit))
for a list of all the items you can extract directly.
Model p-value: If you want to obtain the p-value of the overall regression model, this blog post outlines a function to return the p-value:
lmp <- function (modelobject) {
if (class(modelobject) != "lm") stop("Not an object of class 'lm' ")
f <- summary(modelobject)$fstatistic
p <- pf(f[1],f[2],f[3],lower.tail=F)
attributes(p) <- NULL
return(p)
}
> lmp(fit)
[1] 1.622665e-05
In the case of a simple regression with one predictor, the model p-value and the p-value for the coefficient will be the same.
Coefficient p-values: If you have more than one predictor, then the above will return the model p-value, and the p-value for coefficients can be extracted using:
summary(fit)$coefficients[,4]
Alternatively, you can grab the p-value of coefficients from the anova(fit)
object in a similar fashion to the summary object above.
Notice that summary(fit)
generates an object with all the information you need. The beta, se, t and p vectors are stored in it. Get the p-values by selecting the 4th column of the coefficients matrix (stored in the summary object):
summary(fit)$coefficients[,4]
summary(fit)$r.squared
Try str(summary(fit))
to see all the info that this object contains.
Edit: I had misread Chase's answer which basically tells you how to get to what I give here.