如何在base R中执行tukey HSD?
在进行事后分析时,您必须记住的第一件事是,必须拒绝方差分析的原假设,以便我们可以断言组均值存在差异。现在,一旦我们实现了可以通过使用基本R中的TukeyHSD函数简单地执行tukeyHSD。
示例
考虑以下数据帧-
x1<-rep(LETTERS[1:4],5) y1<-rep(c(5,2000,30,99),5) df1<-data.frame(x1,y1) df1
输出结果
x1 y1 1 A 5 2 B 2000 3 C 30 4 D 99 5 A 5 6 B 2000 7 C 30 8 D 99 9 A 5 10 B 2000 11 C 30 12 D 99 13 A 5 14 B 2000 15 C 30 16 D 99 17 A 5 18 B 2000 19 C 30 20 D 99
示例
进行方差分析-
ANOVA<-aov(y1~x1,data=df1)summary(ANOVA)
输出结果
Df Sum Sq Mean Sq F value Pr(>F) x1 3 14361185 4787062 1.07e+32 <2e-16 *** Residuals 16 0 0 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
示例
执行tukeyHSD-
TukeyHSD(ANOVA)
输出结果
Tukey multiple comparisons of means 95% family-wise confidence level Fit: aov(formula = y1 ~ x1, data = df1) $x1 diff lwr upr p adj B-A 1995 1995 1995 0 C-A 25 25 25 0 D-A 94 94 94 0 C-B -1970 -1970 -1970 0 D-B -1901 -1901 -1901 0 D-C 69 69 69 0
示例
考虑基数R中的PlantGrowth数据-
str(PlantGrowth)
输出结果
'data.frame': 30 obs. of 2 variables: $ weight: num 4.17 5.58 5.18 6.11 4.5 4.61 5.17 4.53 5.33 5.14 ... $ group : Factor w/ 3 levels "ctrl","trt1",..: 1 1 1 1 1 1 1 1 1 1 ...
示例
head(PlantGrowth,20)
输出结果
weight group 1 4.17 ctrl 2 5.58 ctrl 3 5.18 ctrl 4 6.11 ctrl 5 4.50 ctrl 6 4.61 ctrl 7 5.17 ctrl 8 4.53 ctrl 9 5.33 ctrl 10 5.14 ctrl 11 4.81 trt1 12 4.17 trt1 13 4.41 trt1 14 3.59 trt1 15 5.87 trt1 16 3.83 trt1 17 6.03 trt1 18 4.89 trt1 19 4.32 trt1 20 4.69 trt1
进行方差分析-
示例
ANOVA<-aov(weight~group,data=PlantGrowth)summary(ANOVA)
输出结果
Df Sum Sq Mean Sq F value Pr(>F) group 2 3.766 1.8832 4.846 0.0159 * Residuals 27 10.492 0.3886 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
示例
执行tukeyHSD-
TukeyHSD(ANOVA)
输出结果
Tukey multiple comparisons of means 95% family-wise confidence level Fit: aov(formula = weight ~ group, data = PlantGrowth) $group diff lwr upr p adj trt1-ctrl -0.371 -1.0622161 0.3202161 0.3908711 trt2-ctrl 0.494 -0.1972161 1.1852161 0.1979960 trt2-trt1 0.865 0.1737839 1.5562161 0.0120064