使用mtcars
来自 R 的数据(我更喜欢@Drew Steen 建议的解决方案)
myanova<-anova(lm(mpg~cyl,mtcars),lm(mpg~cyl+disp,mtcars))
pval<-summary(myanova)[1,6]
根据问题更新:
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 "
添加:如果您使用“$”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