Reputation: 794
My apologies for not including enough information to properly access the issue. Anyway,I want to compare different pairs of linear models and record their p-values into a table. Where my troubles lie is that I cannot extract only the p-value.
TC <- matrix(data=NA, nrow=1, ncol=6)
ML5 <- 1:5
for(B in ML5) {
Coop <- anova(M6,Models5[[B]])$"Pr(>F)"
TC[1,B] <- Coop
}
>Coop
NA 2.041767e-05
So how do I only put the number into the table and not the NA into the table?
Thanks a million!
Upvotes: 1
Views: 15088
Reputation: 1
Using aov():
anova_a <- aov(response ~ time + Error(subject), data = all_data)
f_value <- unlist(summary(anova_a)[[2]])[9]
Upvotes: 0
Reputation: 15458
Using mtcars
data from R (I would prefer the solution suggested by @Drew Steen)
myanova<-anova(lm(mpg~cyl,mtcars),lm(mpg~cyl+disp,mtcars))
pval<-summary(myanova)[1,6]
Updated as per question:
models1<-lm(mpg~cyl,mtcars)
models2<-list(lm(mpg~cyl+disp,mtcars),lm(mpg~cyl+disp+hp,mtcars),lm(mpg~cyl+disp+hp+drat,mtcars))
myp1<-lapply(models2,function(x) anova(models1,x))
myp2<-lapply(myp1,function(x)summary(x)[1,6])
> myp3
[1] " 0.05419 " " 0.09891 " " 0.09661 "
Add:If you are using "$"Pr(>F)"
myp2<-lapply(myp1,function(x)x$"Pr(>F)")
myp3<-sapply(myp2,function(x) as.data.frame(x)[2,1])
> myp3
[1] 0.05418572 0.09891469 0.09661222
Upvotes: 0
Reputation: 16617
You just need quotation marks:
pvalue <- anova(m2,m1)$"Pr(>F)"
You can access the second element of pvalue using normal bracket subsetting:
pvalue[2]
So in your example I believe you'll use
Coop <- anova(M6,Models5[[B]])$"Pr(>F)"[2]
(Although without access to M6, I can't be totally sure).
The str
command is very useful in this sort of situation, to figure out what kind of object you're dealing with:
str(myanova$"Pr(>F)")
Upvotes: 7