如何从R中的摘要anova测试中获得p值?

时间:2014-03-21 14:44:34

标签: r

我有这个R代码

As = rnorm(5, mean = 0, sd = 5)
Bs = rnorm(5, mean = 0, sd = 5)
Cs = rnorm(5, mean = 0, sd = 5)

dat = data.frame(factor   = c("A","A","A","A","A","B","B","B","B","B", 
                              "C","C","C","C","C"),
                 response = c(As, Bs, Cs))

summary(aov(response ~ factor, data = dat))

返回此结果

> summary(aov(response ~ factor, data = dat))
            Df Sum Sq Mean Sq F value Pr(>F)
factor       2  36.08   18.04   0.807  0.469
Residuals   12 268.22   22.

我想访问Pr(>F)的值0.469。我怎么能这样做?

2 个答案:

答案 0 :(得分:3)

如果您查看结果的结构(请参阅?str函数),您将了解如何使用索引运算符([)来获取Pr(>F)

> str(result)
List of 1
 $ :Classes ‘anova’ and 'data.frame':   2 obs. of  5 variables:
  ..$ Df     : num [1:2] 2 12
  ..$ Sum Sq : num [1:2] 35.7 261.3
  ..$ Mean Sq: num [1:2] 17.9 21.8
  ..$ F value: num [1:2] 0.82 NA
  ..$ Pr(>F) : num [1:2] 0.464 NA
 - attr(*, "class")= chr [1:2] "summary.aov" "listof"

你可以这样做

> result[[1]][["Pr(>F)"]][1]
[1] 0.4636054

> result[[1]][[5]][1]
[1] 0.4636054

答案 1 :(得分:3)

s<-summary(aov(response ~ factor, data = dat))
unlist(s)['Pr(>F)1']

Pr(>F)1 
0.1125695