t.test() を実行し,結果を解釈できる→ t検定(独立2標本t検定) で統計的に判断します
\[t = \frac{\bar{x}_1 - \bar{x}_2}{SE_{差}}\]
→ t値が大きいほど「差が大きく,偶然とは言いにくい」
# mtcars データ:am(0=オートマ, 1=マニュアル)ごとの燃費(mpg)
mtcars |>
group_by(am) |>
summarise(n = n(), mean_mpg = mean(mpg), sd_mpg = sd(mpg))# 箱ひげ図で確認
mtcars |>
mutate(am = factor(am, labels = c("オートマ", "マニュアル"))) |>
ggplot(aes(x = am, y = mpg, fill = am)) +
geom_boxplot() +
geom_jitter(width = 0.1, alpha = 0.5) +
scale_fill_brewer(palette = "Pastel1") +
labs(title = "トランスミッション別の燃費", x = "トランスミッション", y = "燃費 (mpg)") +
theme_bw() +
theme(legend.position = "none")##
## Welch Two Sample t-test
##
## data: mpg by am
## t = -3.7671, df = 18.332, p-value = 0.001374
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
## -11.280194 -3.209684
## sample estimates:
## mean in group 0 mean in group 1
## 17.14737 24.39231
Welch Two Sample t-test
t = -3.7671, df = 18.332, p-value = 0.001374
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-11.280194 -3.209684
sample estimates:
mean in group 0 mean in group 1
17.14737 24.39231
| 項目 | 意味 |
|---|---|
t |
t統計量(負の値 = group0 < group1) |
df |
自由度 |
p-value |
p値(< 0.05 で有意) |
95% CI |
平均差の95%信頼区間(0を含まなければ有意) |
mean in group 0/1 |
各群の平均値 |
Welch法(分散が等しくない場合に対応)とStudent法(等分散を仮定)の使い分け:
##
## F test to compare two variances
##
## data: mpg by am
## F = 0.38656, num df = 18, denom df = 12, p-value = 0.06691
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.1243721 1.0703429
## sample estimates:
## ratio of variances
## 0.3865615
##
## Two Sample t-test
##
## data: mpg by am
## t = -4.1061, df = 30, p-value = 0.000285
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
## -10.84837 -3.64151
## sample estimates:
## mean in group 0 mean in group 1
## 17.14737 24.39231
現代的には,Welch法をデフォルトで使うことが推奨されています(前提条件が少ないため)。
p値は「差が有意かどうか」しか教えてくれません。 「どのくらいの差か」 を表すのが効果量です。
\[d = \frac{\bar{x}_1 - \bar{x}_2}{SD_{プール}}\]
# Cohenの d を手動で計算
auto <- mtcars |> filter(am == 0) |> pull(mpg)
manual <- mtcars |> filter(am == 1) |> pull(mpg)
mean_diff <- mean(manual) - mean(auto)
sd_pooled <- sqrt(((length(auto)-1)*var(auto) + (length(manual)-1)*var(manual)) /
(length(auto) + length(manual) - 2))
d <- mean_diff / sd_pooled
round(d, 2)## [1] 1.48
| d の値 | 効果の大きさ |
|---|---|
| 0.2 | 小 |
| 0.5 | 中 |
| 0.8以上 | 大 |
論文・レポートでの記述例:
マニュアル車(M = 24.39, SD = 6.17)の燃費は, オートマ車(M = 17.15, SD = 3.83)に比べて有意に高かった, t(18.33) = 3.77, p = .001, d = 1.48。
報告の要素:各群の M と SD,t値,df,p値,効果量 d
iris データを使って,setosa と
versicolor の Petal.Length を比較します。
まず,2群の平均・標準偏差・サンプルサイズを確認してください。
演習1の2群を箱ひげ図で比較してください。
iris の setosa と versicolor
の Petal.Length を比べるt検定を実行し,
結果を解釈してください。
演習3の結果を,論文の結果の節として1〜2文で記述してください。 (M, SD, t値, df, p値を含める)
t.test() で t値・df・p値・信頼区間を取得