Access or parse elements in summary() in R

ε祈祈猫儿з 提交于 2019-12-23 16:44:50

问题


I run the following R commands to make a Dunnett's test and get the summary. How can I access each row of the Linear Hypotheses below, which is part of summary output? Basically I do not know the structure of the summary. I attempted using names() but it seems not working as I do not see any named attribute to give that.

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)

回答1:


@MatthewLundberg started to say it, though I didn't find all of the values you wanted with str(Dunnet), so I reverted to str(summary(Dunnet)) and found it:

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



回答2:


The print method for summary.glht objects displays the desired items in a manner that obscures the underlying structure. After you look at the structure of the summary object with str you still need to extract the values.

The $test element of the summary object has several sub-items, each with the same length, so they can be assembled into a dataframe:

> 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

There are two items in the $test item that are not going to be of equal length (since they are functions), so they should not be included in the selections with a vector of names.



来源:https://stackoverflow.com/questions/25225480/access-or-parse-elements-in-summary-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!