在R中的summary()中访问或解析元素

时间:2014-08-10 04:15:55

标签: r statistics output elements

我运行以下R命令进行Dunnett测试并获得摘要。如何访问下面的线性假设的每一行,这是摘要输出的一部分?基本上我不知道摘要的结构。我尝试使用names(),但似乎没有工作,因为我没有看到任何命名属性给予它。

library("multcomp")
Group <- factor(c("A","A","B","B","B","C","C","C","D","D","D","E","E","F","F","F"))
Value <- c(5,5.09901951359278,4.69041575982343,4.58257569495584,4.79583152331272,5,5.09901951359278,4.24264068711928,5.09901951359278,5.19615242270663,4.58257569495584,6.16441400296898,6.85565460040104,7.68114574786861,7.07106781186548,6.48074069840786)
data <- data.frame(Group, Value)

fit <- aov(Value ~ Group, data)
set.seed(20140123)
Dunnet <- glht(fit, linfct=mcp(Group="Dunnett"))
summary(Dunnet)

        Simultaneous Tests for General Linear Hypotheses

Multiple Comparisons of Means: Dunnett Contrasts


Fit: aov(formula = Value ~ Group, data = data)

Linear Hypotheses:
       Estimate Std. Error t value Pr(>|t|)   
B - A == 0 -0.35990    0.37009  -0.972  0.76536   
C - A == 0 -0.26896    0.37009  -0.727  0.90012   
D - A == 0 -0.09026    0.37009  -0.244  0.99895   
E - A == 0  1.46052    0.40541   3.603  0.01794 * 
F - A == 0  2.02814    0.37009   5.480  0.00112 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
   (Adjusted p values reported -- single-step method)

2 个答案:

答案 0 :(得分:5)

@MatthewLundberg开始说出来,虽然我没有找到str(Dunnet)所需的所有值,所以我回到了str(summary(Dunnet))并找到了它:

str(summary(Dunnet))
## List of 10
##  $ model      :List of 13
##   ...
##  $ linfct     : num [1:5, 1:6] 0 0 0 0 0 1 0 0 0 0 ...
##   ...
##  $ rhs        : num [1:5] 0 0 0 0 0
##  $ coef       : Named num [1:6] 5.0495 -0.3599 -0.269 -0.0903 1.4605 ...
##   ...
##  $ vcov       : num [1:6, 1:6] 0.0822 -0.0822 -0.0822 -0.0822 -0.0822 ...
##   ...
##  $ df         : int 10
##  $ alternative: chr "two.sided"
##  $ type       : chr "Dunnett"
##  $ focus      : chr "Group"
##  $ test       :List of 7
##   ..$ pfunction   :function (type = c("univariate", "adjusted", p.adjust.methods), ...)  
##   ..$ qfunction   :function (conf.level, adjusted = TRUE, ...)  
##   ..$ coefficients: Named num [1:5] -0.3599 -0.269 -0.0903 1.4605 2.0281
##   .. ..- attr(*, "names")= chr [1:5] "B - A" "C - A" "D - A" "E - A" ...
##   ..$ sigma       : Named num [1:5] 0.37 0.37 0.37 0.405 0.37
##   .. ..- attr(*, "names")= chr [1:5] "B - A" "C - A" "D - A" "E - A" ...
##   ..$ tstat       : Named num [1:5] -0.972 -0.727 -0.244 3.603 5.48
##   .. ..- attr(*, "names")= chr [1:5] "B - A" "C - A" "D - A" "E - A" ...
##   ..$ pvalues     : atomic [1:5] 0.7655 0.9001 0.9989 0.0176 0.0011
##   .. ..- attr(*, "error")= num 0.000755
##   ..$ type        : chr "single-step"
##   ..- attr(*, "class")= chr "mtest"
##  - attr(*, "class")= chr [1:2] "summary.glht" "glht"

summary(Dunnet)$test[c('coefficients', 'sigma', 'tstat', 'pvalues')]
## $coefficients
##       B - A       C - A       D - A       E - A       F - A 
## -0.35990210 -0.26895636 -0.09026055  1.46052454  2.02814166 
## 
## $sigma
##     B - A     C - A     D - A     E - A     F - A 
## 0.3700867 0.3700867 0.3700867 0.4054096 0.3700867 
## 
## $tstat
##      B - A      C - A      D - A      E - A      F - A 
## -0.9724806 -0.7267388 -0.2438903  3.6025896  5.4801802 
## 
## $pvalues
## [1] 0.765312271 0.900150707 0.998946706 0.017585149 0.001147749
## attr(,"error")
## [1] 0.0007157397

答案 1 :(得分:1)

print对象的summary.glht方法以隐藏底层结构的方式显示所需的项目。使用str查看摘要对象的结构后,仍需要提取值。

摘要对象的$test元素有几个子项,每个子项具有相同的长度,因此可以将它们组合成一个数据帧:

> as.data.frame(summary(Dunnet)$test[ c("coefficients", "sigma", "tstat", "pvalues")] )

      coefficients     sigma      tstat      pvalues
B - A  -0.35990210 0.3700867 -0.9724806 0.7654036572
C - A  -0.26895636 0.3700867 -0.7267388 0.9001056962
D - A  -0.09026055 0.3700867 -0.2438903 0.9989441213
E - A   1.46052454 0.4054096  3.6025896 0.0172171050
F - A   2.02814166 0.3700867  5.4801802 0.0009869786

$test项目中有两个项目长度不等(因为它们是函数),所以它们不应该包含在带有名称向量的选择中。