【问题标题】:Extract p-value from aov从 aov 中提取 p 值
【发布时间】:2011-03-22 22:39:48
【问题描述】:

我正在寻找从 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"

【问题讨论】:

  • 当你做str(test)时,它会列出aov对象的结构。您需要查看的是 print.aov() 的输出(参见方法(打印))! Aniko 的 str(summary(test)) 就是这样做的。

标签: r anova


【解决方案1】:

查看str(summary(test)) - 这就是您看到 p 值的地方。

【讨论】:

    【解决方案2】:

    这里:

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

    【讨论】:

    • 我需要另一个 [[1]] - 但是,嘿,这很酷 :) summary(test)[[1]][["Pr(&gt;F)"]][[1]]
    • 没有BurninLeo的额外[[1]],它给出了汇总表的最后一列,第二行是空白的——我们只希望第一行得到p值,即这就是额外的 [[1]] 所做的。
    • 如果您不喜欢输入概率陈述,类似的替代方法是summary(test)[[1]][1, 5]。但是您可能不仅想要 p 值,还想要 F 统计量,所以这将是 summary(test)[[1]][1, 4:5]
    • 嗨,我使用了 permuco 包,它的输出与 aov 的输出相似。但是 summary(test)[[1]][["Pr(>F)"]] 返回 NA
    【解决方案3】:

    我知道这很旧,但我在网上四处寻找,没有找到解释或一般解决方案,而且这个帖子是谷歌搜索中最先出现的内容之一。

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

    tests <- summary(test)
    str(tests)
    

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

    在独立测量的情况下,类似于以下工作。

    tests[[1]]$'Pr(>F)'
    

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

    myModelSummary$'Error: subject:A'[[1]]$'Pr(>F)'
    

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

    【讨论】:

      【解决方案4】:

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

      sum_test = unlist(summary(test))
      

      然后查看名称

      names(sum_test)
      

      我有“Pr(>F)1”和“Pr(>F)2”,当第一个它是请求的值,所以

      sum_test["Pr(>F)1"]
      

      将给出请求的值

      【讨论】:

      • 哇,太棒了!!!我已经能够从其他方法获得 p 值,但我从未见过“unlist”命令,这会生成完整 aov 输出的精简列表
      【解决方案5】:

      比 BurningLeo 的 advice 短一些:

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

      【讨论】:

        【解决方案6】:
        summary(aov(y~factor(x)))[[1]][[5]][1]
        

        【讨论】:

          【解决方案7】:
          unlist(summary(myAOV)[[2]])[[9]]
          

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-10-17
            • 2015-02-06
            • 2015-04-11
            • 1970-01-01
            • 1970-01-01
            • 2010-11-28
            • 1970-01-01
            • 2022-01-04
            相关资源
            最近更新 更多