【问题标题】:Get a single value out of any statistics tests (e.g. value of spearman rho out of cor.test)从任何统计测试中获取单个值(例如,从 cor.test 中获取 spearman rho 的值)
【发布时间】:2011-10-02 03:17:50
【问题描述】:

R 中的统计测试输出许多描述。虽然它们很有用,但我们如何才能只输出或提取单个值?

> cor.test(x,y,method="spearman", exact=F)

        Spearman's rank correlation rho

data:  x and y 
S = 12767993, p-value = 0.0001517
alternative hypothesis: true rho is not equal to 0 
sample estimates:
      rho 
-0.188074 

特别是,如何才能得到这些值 0.0001517-0.188074 以便我可以存储它们以供进一步分析?

【问题讨论】:

    标签: r


    【解决方案1】:
    test.res <- cor.test(x,y,method="spearman", exact=F)
    test.res[1:8]
    

    你要找的东西就在里面。

    为特定值添加另一个索引前缀,如下所示:

    test.res[1][1]
    

    要查找特定元素,您可以str(test.res) 查找其位置并在上面替换,例如test.res[1][5]

    【讨论】:

      【解决方案2】:

      您可以使用测试对象的$ 子集。相关名称为p.valueestimate

      > tst<-cor.test(1:10,rnorm(10),method="spearman")
      > tst
      
              Spearman's rank correlation rho
      
      data:  1:10 and rnorm(10) 
      S = 140, p-value = 0.6818
      alternative hypothesis: true rho is not equal to 0 
      sample estimates:
            rho 
      0.1515152 
      

      .

      > tst$p.value
      [1] 0.6818076
      > tst$estimate
            rho 
      0.1515152 
      

      编辑

      其他答案指出,您可以使用str 调查对象的结构,以找到与$ 子集一起使用的名称。你也可以通过names找到名字:

      > names(tst)
      [1] "statistic"   "parameter"   "p.value"     "estimate"    "null.value" 
      [6] "alternative" "method"      "data.name" 
      

      要考虑的另一件事是,您正在查看对象的打印版本,并且 print 方法可能正在执行一些计算(在这种情况下不是)。您可以使用class(tst) 检查对象类,这表明它属于htest 类。 print.htest是相关的打印方式,但是这是不可见的,所以使用getAnywhere(print.htest)查看。

      【讨论】:

      • 虽然它已经在其他答案中得到处理,但我认为您还可以指出 str() 将显示可以使用 $ 符号调用的变量。还有一个技巧 - 您有时只能在 summaryprint 中看到值(它们是根据来自对象的数据计算的),而不是在对象本身中。然后,您必须对 summary(object)$variable 进行子集化处理。
      • 非常感谢您的快速回复,但 estimate 值仍有问题。我只想将 estimate 的数值输出 0.1515152 隔离,从而进行存储。顺便说一句,只是好奇,有没有关于如何隔离打印输出的完整指南,可能就像涉及 getAnywhere(print.htest) 的指南一样?但我认为这只是许多步骤之一,不是吗?我在 R 中完全是新手 :(,但在 Perl 中。
      • 糟糕,我想我错过了什么。我只是不知道为什么即使在以不同的名称重新分配后仍会打印 rho 这个词,我认为它会影响值;事实上它没有,不是吗?所以,就像我之前的问题一样,这背后是什么?我想我问的问题可能也很琐碎。
      • @Rivo Suoth 对象的名称属性,见?names。数字输出与没有名称一样,但您可以使用 names(x) &lt;- NULL 删除对象 x 的名称。
      【解决方案3】:
      test.res <- cor.test(x,y,method="spearman", exact=F)
      

      使用 str(test.res) 查看对象的结构

      > str(test.res)
      List of 8
       $ statistic  : Named num 182
        ..- attr(*, "names")= chr "S"
       $ parameter  : NULL
       $ p.value    : num 0.785
       $ estimate   : Named num -0.103
        ..- attr(*, "names")= chr "rho"
       $ null.value : Named num 0
        ..- attr(*, "names")= chr "rho"
       $ alternative: chr "two.sided"
       $ method     : chr "Spearman's rank correlation rho"
       $ data.name  : chr "1:10 and rnorm(10)"
       - attr(*, "class")= chr "htest"
      

      其中任何一个都可以通过使用 $ 表示法获得。如果您正在寻找获取 p.value 使用以下内容:

      test.res$p.value
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-01-26
        • 2013-04-17
        • 2017-09-28
        • 2011-07-06
        • 1970-01-01
        • 1970-01-01
        • 2018-04-12
        相关资源
        最近更新 更多