从 aov 中提取 p 值

发布于 2024-09-12 09:31:55 字数 3750 浏览 5 评论 0原文

我正在寻找从 R 中的方差分析生成的 p 值。

这是我正在运行的:

test <- aov(asq[,9] ~ asq[,187])
summary(test)

产量:

              Df Sum Sq Mean Sq F value    Pr(>F)    
asq[, 187]     1   3.02 3.01951  12.333 0.0004599 ***
Residuals   1335 326.85 0.24483                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
12 observations deleted due to missingness

当我查看结构时,这就是我看到的。我通常可以通过列表来获得我需要的东西,但我在这个方面遇到了困难。谷歌搜索似乎也揭示了比我得到的简单得多的结构。

注意:ASQ 是我的数据框。

str(test)

List of 13
 $ coefficients : Named num [1:2] 0.2862 0.0973
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "asq[, 187]"
 $ residuals    : Named num [1:1337] 0.519 0.519 -0.481 -0.481 -0.481 ...
  ..- attr(*, "names")= chr [1:1337] "1" "2" "3" "4" ...
 $ effects      : Named num [1:1337] -16.19 -1.738 -0.505 -0.505 -0.505 ...
  ..- attr(*, "names")= chr [1:1337] "(Intercept)" "asq[, 187]" "" "" ...
 $ rank         : int 2
 $ fitted.values: Named num [1:1337] 0.481 0.481 0.481 0.481 0.481 ...
  ..- attr(*, "names")= chr [1:1337] "1" "2" "3" "4" ...
 $ assign       : int [1:2] 0 1
 $ qr           :List of 5
  ..$ qr   : num [1:1337, 1:2] -36.565 0.0273 0.0273 0.0273 0.0273 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:1337] "1" "2" "3" "4" ...
  .. .. ..$ : chr [1:2] "(Intercept)" "asq[, 187]"
  .. ..- attr(*, "assign")= int [1:2] 0 1
  ..$ qraux: num [1:2] 1.03 1.02
  ..$ pivot: int [1:2] 1 2
  ..$ tol  : num 1e-07
  ..$ rank : int 2
  ..- attr(*, "class")= chr "qr"
 $ df.residual  : int 1335
 $ na.action    :Class 'omit'  Named int [1:12] 26 257 352 458 508 624 820 874 1046 1082 ...
  .. ..- attr(*, "names")= chr [1:12] "26" "257" "352" "458" ...
 $ xlevels      : list()
 $ call         : language aov(formula = asq[, 9] ~ asq[, 187])
 $ terms        :Classes 'terms', 'formula' length 3 asq[, 9] ~ asq[, 187]
  .. ..- attr(*, "variables")= language list(asq[, 9], asq[, 187])
  .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "asq[, 9]" "asq[, 187]"
  .. .. .. ..$ : chr "asq[, 187]"
  .. ..- attr(*, "term.labels")= chr "asq[, 187]"
  .. ..- attr(*, "order")= int 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(asq[, 9], asq[, 187])
  .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. ..- attr(*, "names")= chr [1:2] "asq[, 9]" "asq[, 187]"
 $ model        :'data.frame':  1337 obs. of  2 variables:
  ..$ asq[, 9]  : int [1:1337] 1 1 0 0 0 1 1 1 0 0 ...
  ..$ asq[, 187]: int [1:1337] 2 2 2 2 2 2 2 2 2 2 ...
  ..- attr(*, "terms")=Classes 'terms', 'formula' length 3 asq[, 9] ~ asq[, 187]
  .. .. ..- attr(*, "variables")= language list(asq[, 9], asq[, 187])
  .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : chr [1:2] "asq[, 9]" "asq[, 187]"
  .. .. .. .. ..$ : chr "asq[, 187]"
  .. .. ..- attr(*, "term.labels")= chr "asq[, 187]"
  .. .. ..- attr(*, "order")= int 1
  .. .. ..- attr(*, "intercept")= int 1
  .. .. ..- attr(*, "response")= int 1
  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. .. ..- attr(*, "predvars")= language list(asq[, 9], asq[, 187])
  .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. .. ..- attr(*, "names")= chr [1:2] "asq[, 9]" "asq[, 187]"
  ..- attr(*, "na.action")=Class 'omit'  Named int [1:12] 26 257 352 458 508 624 820 874 1046 1082 ...
  .. .. ..- attr(*, "names")= chr [1:12] "26" "257" "352" "458" ...
 - attr(*, "class")= chr [1:2] "aov" "lm"

I am looking to extract the p-value generated from an anova in R.

Here is what I am running:

test <- aov(asq[,9] ~ asq[,187])
summary(test)

Yields:

              Df Sum Sq Mean Sq F value    Pr(>F)    
asq[, 187]     1   3.02 3.01951  12.333 0.0004599 ***
Residuals   1335 326.85 0.24483                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
12 observations deleted due to missingness

When I look a the structure, this is what I see. I usually can work through lists to get what I need, but I am having a hard time with this one. A Google searched also seemed to reveal much simpler structures than I am getting.

NOTE: ASQ is my data frame.

str(test)

List of 13
 $ coefficients : Named num [1:2] 0.2862 0.0973
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "asq[, 187]"
 $ residuals    : Named num [1:1337] 0.519 0.519 -0.481 -0.481 -0.481 ...
  ..- attr(*, "names")= chr [1:1337] "1" "2" "3" "4" ...
 $ effects      : Named num [1:1337] -16.19 -1.738 -0.505 -0.505 -0.505 ...
  ..- attr(*, "names")= chr [1:1337] "(Intercept)" "asq[, 187]" "" "" ...
 $ rank         : int 2
 $ fitted.values: Named num [1:1337] 0.481 0.481 0.481 0.481 0.481 ...
  ..- attr(*, "names")= chr [1:1337] "1" "2" "3" "4" ...
 $ assign       : int [1:2] 0 1
 $ qr           :List of 5
  ..$ qr   : num [1:1337, 1:2] -36.565 0.0273 0.0273 0.0273 0.0273 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:1337] "1" "2" "3" "4" ...
  .. .. ..$ : chr [1:2] "(Intercept)" "asq[, 187]"
  .. ..- attr(*, "assign")= int [1:2] 0 1
  ..$ qraux: num [1:2] 1.03 1.02
  ..$ pivot: int [1:2] 1 2
  ..$ tol  : num 1e-07
  ..$ rank : int 2
  ..- attr(*, "class")= chr "qr"
 $ df.residual  : int 1335
 $ na.action    :Class 'omit'  Named int [1:12] 26 257 352 458 508 624 820 874 1046 1082 ...
  .. ..- attr(*, "names")= chr [1:12] "26" "257" "352" "458" ...
 $ xlevels      : list()
 $ call         : language aov(formula = asq[, 9] ~ asq[, 187])
 $ terms        :Classes 'terms', 'formula' length 3 asq[, 9] ~ asq[, 187]
  .. ..- attr(*, "variables")= language list(asq[, 9], asq[, 187])
  .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "asq[, 9]" "asq[, 187]"
  .. .. .. ..$ : chr "asq[, 187]"
  .. ..- attr(*, "term.labels")= chr "asq[, 187]"
  .. ..- attr(*, "order")= int 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(asq[, 9], asq[, 187])
  .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. ..- attr(*, "names")= chr [1:2] "asq[, 9]" "asq[, 187]"
 $ model        :'data.frame':  1337 obs. of  2 variables:
  ..$ asq[, 9]  : int [1:1337] 1 1 0 0 0 1 1 1 0 0 ...
  ..$ asq[, 187]: int [1:1337] 2 2 2 2 2 2 2 2 2 2 ...
  ..- attr(*, "terms")=Classes 'terms', 'formula' length 3 asq[, 9] ~ asq[, 187]
  .. .. ..- attr(*, "variables")= language list(asq[, 9], asq[, 187])
  .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : chr [1:2] "asq[, 9]" "asq[, 187]"
  .. .. .. .. ..$ : chr "asq[, 187]"
  .. .. ..- attr(*, "term.labels")= chr "asq[, 187]"
  .. .. ..- attr(*, "order")= int 1
  .. .. ..- attr(*, "intercept")= int 1
  .. .. ..- attr(*, "response")= int 1
  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. .. ..- attr(*, "predvars")= language list(asq[, 9], asq[, 187])
  .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. .. ..- attr(*, "names")= chr [1:2] "asq[, 9]" "asq[, 187]"
  ..- attr(*, "na.action")=Class 'omit'  Named int [1:12] 26 257 352 458 508 624 820 874 1046 1082 ...
  .. .. ..- attr(*, "names")= chr [1:12] "26" "257" "352" "458" ...
 - attr(*, "class")= chr [1:2] "aov" "lm"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

握住我的手 2024-09-19 09:31:55

这里:

summary(test)[[1]][["Pr(>F)"]][1]

Here:

summary(test)[[1]][["Pr(>F)"]][1]
渔村楼浪 2024-09-19 09:31:55

由于上面的建议对我不起作用,这就是我设法解决它的方法:

sum_test = unlist(summary(test))

然后查看带有

names(sum_test)

“Pr(>F)1”和“Pr(>F)2”的名称,当第一个它是请求的值,因此

sum_test["Pr(>F)1"]

将给出请求的值

since the suggest above didn't work for me this is how i managed to solve it:

sum_test = unlist(summary(test))

then looking at the names with

names(sum_test)

i have"Pr(>F)1" and "Pr(>F)2", when the first it the requested value, so

sum_test["Pr(>F)1"]

will give the requested value

擦肩而过的背影 2024-09-19 09:31:55

我知道这已经很旧了,但我在网上浏览了一下,没有找到解释或一般解决方案,而这个线程是谷歌搜索中首先出现的内容之一。

Aniko 是对的,最简单的方法是查看 summary(test)

tests <- summary(test)
str(tests)

这为您提供了一个独立测量 aov 对象的列表 1,但它可能有多个具有重复测量的项目。通过重复测量,列表中的每个项目都由列表中项目的误差项定义。很多新手感到困惑的是,如果它位于度量之间,则不会命名一个单独的列表项。因此,他们并没有真正注意到这一点,也不明白为什么使用典型的选择器不起作用。

在独立措施的情况下,类似以下的工作。

tests[[1]]

在重复测量中,它是类似的,但您也可以使用命名项,例如...

myModelSummary

请注意,我仍然必须执行该列表选择,因为重复测量模型中的每个列表项又都是 1 的列表。

Pr(>F)'

在重复测量中,它是类似的,但您也可以使用命名项,例如...


请注意,我仍然必须执行该列表选择,因为重复测量模型中的每个列表项又都是 1 的列表。

Error: subject:A'[[1]]

请注意,我仍然必须执行该列表选择,因为重复测量模型中的每个列表项又都是 1 的列表。

Pr(>F)'

在重复测量中,它是类似的,但您也可以使用命名项,例如...

请注意,我仍然必须执行该列表选择,因为重复测量模型中的每个列表项又都是 1 的列表。

Pr(>F)'

请注意,我仍然必须执行该列表选择,因为重复测量模型中的每个列表项又都是 1 的列表。

Pr(>F)'

在重复测量中,它是类似的,但您也可以使用命名项,例如...

请注意,我仍然必须执行该列表选择,因为重复测量模型中的每个列表项又都是 1 的列表。

I know this is old but I looked around online and didn't find an explanation or general solution and this thread is one of the first things that comes up in a Google search.

Aniko is right, the easiest way is looking in summary(test).

tests <- summary(test)
str(tests)

That gives you a list of 1 for an independent measures aov object but it could have multiple items with repeated measures. With the repeated measures each item in the list is defined by the error term for the item in the list. Where a lot of new people get confused is that if it's between measures the one lone list item isn't named. So, they don't really notice that and don't understand why using a typical selector doesn't work.

In the independent measures case something like the following works.

tests[[1]]

In repeated measures it's similar but you could also use named items like...

myModelSummary

Note there I still had to do that list selection because each one of the list items in the repeated measures model is again a list of 1.

Pr(>F)'

In repeated measures it's similar but you could also use named items like...


Note there I still had to do that list selection because each one of the list items in the repeated measures model is again a list of 1.

Error: subject:A'[[1]]

Note there I still had to do that list selection because each one of the list items in the repeated measures model is again a list of 1.

Pr(>F)'

In repeated measures it's similar but you could also use named items like...

Note there I still had to do that list selection because each one of the list items in the repeated measures model is again a list of 1.

Pr(>F)'

Note there I still had to do that list selection because each one of the list items in the repeated measures model is again a list of 1.

Pr(>F)'

In repeated measures it's similar but you could also use named items like...

Note there I still had to do that list selection because each one of the list items in the repeated measures model is again a list of 1.

沙与沫 2024-09-19 09:31:55

查看 str(summary(test)) - 您可以在其中看到 p 值。

Check out str(summary(test)) - that's where you see the p-value.

美男兮 2024-09-19 09:31:55

比 BurningLeo 的建议要短一些:

summary(test)[[1]][[1,"Pr(>F)"]]

Somewhat shorter, than in BurningLeo's advice:

summary(test)[[1]][[1,"Pr(>F)"]]
↘紸啶 2024-09-19 09:31:55
summary(aov(y~factor(x)))[[1]][[5]][1]
summary(aov(y~factor(x)))[[1]][[5]][1]
浅浅淡淡 2024-09-19 09:31:55
unlist(summary(myAOV)[[2]])[[9]]

2和9是p值在myAOV模型中的位置

unlist(summary(myAOV)[[2]])[[9]]

2 and 9 are the positions of p-value in myAOV model

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文