Skip to content

Instantly share code, notes, and snippets.

@stephenturner
Created November 30, 2010 17:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stephenturner/722049 to your computer and use it in GitHub Desktop.
Save stephenturner/722049 to your computer and use it in GitHub Desktop.
pvalue-from-lm-object.r
# Function to extract the overall ANOVA p-value out of a linear model object
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)
}
# simulate some data
set.seed(42)
n=20
d=data.frame(x1=rbinom(n,2,.5), x2=rbinom(n,2,.5))
d=transform(d, y=x1+x2+rnorm(n))
#fit the linear model
fit=lm(y ~ x1 + x2, data=d)
summary(fit) #shows that the F-test is 0.006641
names(summary(fit)) #can't access that p-value using this!
names(fit) # this doesn't work either
lmp(fit) # uses the above function to capture the F-test p-value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment