分散分析(ANOVA)で「少なくとも1組の群に有意差がある」とわかっても, 「どの群とどの群が異なるのか」はわかりません。
→ 多重比較(post-hoc test) でペアごとの差を検定します。
単純な t検定の繰り返しでは第一種の過誤率が膨らむ(第13回で解説)ため, 検定全体の誤り率を制御する方法を使います。
| 方法 | 特徴 | Rの関数 |
|---|---|---|
| Tukey HSD | 全ペア比較,バランスのとれた設計に適す | TukeyHSD() |
| Bonferroni | 保守的,検定回数で α を割る | pairwise.t.test(..., p.adjust="bonferroni") |
| Holm | Bonferroni より検出力が高い | pairwise.t.test(..., p.adjust="holm") |
| Scheffé | 任意の対比を検定,最も保守的 | 専用パッケージ |
本授業では Tukey HSD 法 を中心に扱います。
## Df Sum Sq Mean Sq F value Pr(>F)
## Species 2 63.21 31.606 119.3 <2e-16 ***
## Residuals 147 38.96 0.265
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = Sepal.Length ~ Species, data = iris)
##
## $Species
## diff lwr upr p adj
## versicolor-setosa 0.930 0.6862273 1.1737727 0
## virginica-setosa 1.582 1.3382273 1.8257727 0
## virginica-versicolor 0.652 0.4082273 0.8957727 0
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = Sepal.Length ~ Species, data = iris)
$Species
diff lwr upr p adj
versicolor-setosa 0.930 0.6862... 1.1737... 0.0000000
virginica-setosa 1.582 1.3382... 1.8257... 0.0000000
virginica-versicolor 0.652 0.4082... 0.8957... 0.0000000
| 列 | 意味 |
|---|---|
diff |
平均差 |
lwr / upr |
95%信頼区間の下限・上限 |
p adj |
調整済みp値(< .05 で有意) |
# confint() でデータフレームに変換して描く
tukey_df <- as.data.frame(result_tukey$Species)
tukey_df$comparison <- rownames(tukey_df)
ggplot(tukey_df, aes(x = comparison, y = diff)) +
geom_point(size = 3, color = "steelblue") +
geom_errorbar(aes(ymin = lwr, ymax = upr), width = 0.1, color = "steelblue") +
geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
labs(title = "Tukey HSD:群間の平均差と95%CI",
x = "比較ペア", y = "平均差(cm)") +
coord_flip() +
theme_bw()信頼区間が 0 をまたがっていなければ有意差あり。
# ggplot2で箱ひげ図を作成
p_box <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_boxplot(outlier.shape = NA, alpha = 0.7) +
geom_jitter(width = 0.1, alpha = 0.3, size = 1.5) +
scale_fill_brewer(palette = "Pastel1") +
labs(title = "種ごとのがく片の長さ(Tukey HSD: すべての群間に p < .001)",
x = "種", y = "がく片の長さ (cm)") +
theme_bw() +
theme(legend.position = "none")
p_boxiris |>
group_by(Species) |>
summarise(n = n(), M = mean(Sepal.Length), SD = sd(Sepal.Length)) |>
mutate(across(where(is.numeric), ~ round(., 2)))##
## Bartlett test of homogeneity of variances
##
## data: Sepal.Length by Species
## Bartlett's K-squared = 16.006, df = 2, p-value = 0.0003345
## Df Sum Sq Mean Sq F value Pr(>F)
## Species 2 63.21 31.606 119.3 <2e-16 ***
## Residuals 147 38.96 0.265
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = Sepal.Length ~ Species, data = iris)
##
## $Species
## diff lwr upr p adj
## versicolor-setosa 0.930 0.6862273 1.1737727 0
## virginica-setosa 1.582 1.3382273 1.8257727 0
## virginica-versicolor 0.652 0.4082273 0.8957727 0
方法:3種のアヤメ(setosa, versicolor, virginica)のがく片の長さを 一元配置分散分析によって比較した。多重比較には Tukey HSD 法を用いた。
結果:種の主効果が有意であった,F(2, 147) = 119.3, p < .001, η² = .62。 Tukey HSD 法による多重比較の結果, setosa と versicolor(p < .001),setosa と virginica(p < .001), versicolor と virginica(p < .001)の間でそれぞれ有意差が認められた。
iris データで Petal.Length
の一元配置分散分析を実行し, Tukey HSD で多重比較を行ってください。
どの群間に有意差がありましたか?
演習1の Tukey HSD の結果を,tukey_df
を使って信頼区間のプロットで表示してください。
以下の問いに対して,Step 1〜5 の分析フローを実行し, 結果を論文の「方法・結果」の節として1段落にまとめてください。
問い:mtcars
データで,シリンダー数(cyl:4・6・8)によって
馬力(hp)に違いがあるかを検討してください。
aov())TukeyHSD() で調整済みp値と信頼区間を確認