如何在 R 中執行 Tukey HSD?


在進行後設分析時,必須記住的第一件事是方差分析的零假設必須被拒絕,這樣我們才能聲稱組均值存在差異。現在,一旦我們實現了這一點,就可以簡單地使用基本 R 中的 TukeyHSD 函式來執行 Tukey HSD。

示例

考慮以下資料幀 -

 線上演示

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

示例

執行 Tukey HSD -

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

示例

執行 Tukey HSD -

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

更新於: 08-12-2020

829 瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.