繁体   English   中英

如何在R中对事后测试结果进行分类?

[英]How can I classify post-hoc test results in R?

我试图理解如何在R中使用ANOVA和事后测试。到目前为止,我已经使用aov()和TukeyHSD()来分析我的数据。 例:

uni2.anova <- aov(Sum_Uni ~ Micro, data= uni2)

uni2.anova

Call:
aov(formula = Sum_Uni ~ Micro, data = uni2)

Terms:
                    Micro  Residuals
Sum of Squares  0.04917262 0.00602925
Deg. of Freedom         15         48

Residual standard error: 0.01120756 
Estimated effects may be unbalanced

我的问题是,现在我有一个巨大的成对比较列表,但不能用它做任何事情:

 TukeyHSD(uni2.anova)
 Tukey multiple comparisons of means
   95% family-wise confidence level

Fit: aov(formula = Sum_Uni ~ Micro, data = uni2)

$Micro
                               diff          lwr           upr     p adj
Act_Glu2-Act_Ala2     -0.0180017863 -0.046632157  0.0106285840 0.6448524
Ana_Ala2-Act_Ala2     -0.0250134285 -0.053643799  0.0036169417 0.1493629
NegI_Ala2-Act_Ala2     0.0702274527  0.041597082  0.0988578230 0.0000000

这个数据集有40行...想要,我想得到一个看起来像这样的数据集:

  • Act_Glu2:a
  • Act_Ala2:a
  • NegI_Ala2:b ......

我希望你明白这一点。 到目前为止,我没有发现任何类似的在线...我也试图在TukeyHSD文件中只选择重要的对,但文件不“承认”它由行和列组成,使选择不可能.. 。

也许我的方法存在根本性的错误?

我认为OP希望这些字母可以看到比较。

library(multcompView)
multcompLetters(extract_p(TukeyHSD(uni2.anova)))

那会给你的信件。

您也可以使用multcomp包

library(multcomp)
cld(glht(uni2.anova, linct = mcp(Micro = "Tukey")))

我希望这就是你所需要的。

TukeyHSD的结果是一个列表。 使用str来查看结构。 在你的情况下,你会看到它是一个项目的列表,该项目基本上是一个矩阵。 因此,要提取第一列,您需要保存TukeyHSD结果

hsd <- TukeyHSD(uni2.anova)

如果你看看str(hsd) ,那么你可以得到一些......

hsd$Micro[,1]

这将为您提供差异列。 你应该能够提取你想要的东西。

很难说没有示例数据,但假设Micro只是4级的因素而uni2看起来像

n = 40
Micro = c('Act_Glu2', 'Act_Ala2', 'Ana_Ala2', 'NegI_Ala2')[sample(4, 40, rep=T)]
Sum_Uni = rnorm(n, 5, 0.5)
Sum_Uni[Micro=='Act_Glu2'] = Sum_Uni[Micro=='Act_Glu2'] + 0.5

uni2 = data.frame(Sum_Uni, Micro)
> uni2
   Sum_Uni     Micro
1 4.964061  Ana_Ala2
2 4.807680  Ana_Ala2
3 4.643279 NegI_Ala2
4 4.793383  Act_Ala2
5 5.307951 NegI_Ala2
6 5.171687  Act_Glu2
...

那么我认为你真正想要的是基本的多元回归输出:

fit = lm(Sum_Uni ~ Micro, data = uni2)

summary(fit)
anova(fit)
> summary(fit)

Call:
lm(formula = Sum_Uni ~ Micro, data = uni2)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.26301 -0.35337 -0.04991  0.29544  1.07887 

Coefficients:
               Estimate Std. Error t value Pr(>|t|)    
(Intercept)      4.8364     0.1659  29.157  < 2e-16 ***
MicroAct_Glu2    0.9542     0.2623   3.638 0.000854 ***
MicroAna_Ala2    0.1844     0.2194   0.841 0.406143    
MicroNegI_Ala2   0.1937     0.2158   0.898 0.375239    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.4976 on 36 degrees of freedom
Multiple R-squared: 0.2891, Adjusted R-squared: 0.2299 
F-statistic:  4.88 on 3 and 36 DF,  p-value: 0.005996 

> anova(fit)
Analysis of Variance Table

Response: Sum_Uni
          Df Sum Sq Mean Sq F value   Pr(>F)   
Micro      3 3.6254 1.20847  4.8801 0.005996 **
Residuals 36 8.9148 0.24763                    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

您可以访问任何这些表中的数字,例如,

> summary(fit)$coef[2,4]
[1] 0.0008536287

要查看每个对象中存储的内容列表,请使用names()

> names(summary(fit))
 [1] "call"          "terms"         "residuals"     "coefficients" 
 [5] "aliased"       "sigma"         "df"            "r.squared"    
 [9] "adj.r.squared" "fstatistic"    "cov.unscaled" 

除了您发现的TukeyHSD()函数之外,还有许多其他选项可用于进一步查看成对测试,并根据需要更正p值。 这些措施包括pairwise.table() estimable()gmodels中, resamplingboot包,和其他...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM