---
webr:
packages: [dplyr, broom, broom.helpers, cardx, car, parameters, smd, gtsummary] # gtsummary は最後(他パッケージに関数を上書きされないように)
resources: [R/setup.R]
cell-options:
autorun: true
---
{{< include ./_extensions/r-wasm/live/_knitr.qmd >}}
# 群間比較と p 値 {#sec-compare}
```{webr}
#| include: false
source("R/setup.R")
```
## 群ごとに集計する:`by`
```{webr}
trial |>
tbl_summary(
by = trt,
include = c(age, marker, stage, grade, response)
)
```
列が治療群ごとに分かれ、見出しに各群の人数 `N = ` が付きます。
## 全体の列・人数の列を足す
```{webr}
trial |>
tbl_summary(by = trt, include = c(age, grade, response)) |>
add_overall(last = FALSE) |> # 全体の列(TRUE なら右端に)
add_n() # 欠測でない人数の列
```
gtsummary は **`tbl_summary()` で表を作り、`|>` で `add_*()` 関数をつないで情報を足していく** という作りになっています。
## p 値を付ける:`add_p()`
```{webr}
trial |>
tbl_summary(by = trt, include = c(age, marker, stage, grade, response)) |>
add_p()
```
既定で使われる検定(2群の場合):
| 変数の型 | 既定の検定 |
|---|---|
| 連続 | Wilcoxon 順位和検定(Mann-Whitney U 検定) |
| カテゴリ | Pearson のカイ二乗検定(期待度数が 5 未満のセルがあれば Fisher の正確検定) |
3群以上なら連続変数は Kruskal-Wallis 検定になります。使われた検定は脚注に書かれます。
## 検定を指定する:`test`
平均 (SD) で要約したなら、検定も t 検定にそろえるのが自然です。
```{webr}
trial |>
tbl_summary(
by = trt,
include = c(age, marker, grade, response),
statistic = all_continuous() ~ "{mean} ({sd})"
) |>
add_p(
test = list(
all_continuous() ~ "t.test",
grade ~ "fisher.test"
),
test.args = all_tests("t.test") ~ list(var.equal = TRUE) # Student の t 検定
)
```
よく使う検定名:
| 指定 | 検定 |
|---|---|
| `"t.test"` | t 検定(既定は Welch) |
| `"wilcox.test"` | Wilcoxon 順位和検定 |
| `"aov"` | 一元配置分散分析(3群以上) |
| `"kruskal.test"` | Kruskal-Wallis 検定 |
| `"chisq.test"` | カイ二乗検定(連続性補正あり) |
| `"chisq.test.no.correct"` | カイ二乗検定(補正なし) |
| `"fisher.test"` | Fisher の正確検定 |
| `"paired.t.test"` など | 対応のある検定(`group` 引数が必要) |
一覧は `?gtsummary::tests` で見られます。
## p 値の表示桁数
```{webr}
trial |>
tbl_summary(by = trt, include = c(age, marker, grade)) |>
add_p(pvalue_fun = label_style_pvalue(digits = 3)) |>
bold_p(t = 0.05) # p < 0.05 を太字に
```
`label_style_pvalue(digits = 3)` は「小数3桁、0.001 未満は `<0.001`」という表示関数を作ります。
## 多重比較の補正:`add_q()`
たくさんの変数で検定するときは、偽発見率 (FDR) などで補正した q 値を添えることがあります。
```{webr}
trial |>
tbl_summary(by = trt, include = c(age, marker, stage, grade, response, death)) |>
add_p() |>
add_q(method = "fdr") # p.adjust() の method: "bonferroni", "holm", "fdr" など
```
## 差と信頼区間:`add_difference()`
p 値だけでなく、**群間差とその 95% 信頼区間** を示すよう求めるジャーナルも増えています。
```{webr}
trial |>
tbl_summary(
by = trt,
include = c(age, marker, response),
statistic = all_continuous() ~ "{mean} ({sd})",
missing = "no"
) |>
add_difference()
```
- 連続変数:平均の差(t 検定)
- 0/1 変数:割合の差
多水準のカテゴリ変数を含む場合は、**標準化平均差 (SMD)** を使います。観察研究で群のバランスを示すときによく使われます。
```{webr}
trial |>
tbl_summary(by = trt, include = c(age, stage, grade), missing = "no") |>
add_difference(everything() ~ "smd")
```
## 各群の信頼区間:`add_ci()`
```{webr}
trial |>
tbl_summary(
by = trt,
include = c(age, response),
statistic = list(age ~ "{mean}", response ~ "{p}%"),
missing = "no"
) |>
add_ci()
```
## 統計量のラベルを行に出す:`add_stat_label()`
脚注ではなく各行に「Median (Q1, Q3)」などを書きたいときに使います。
```{webr}
trial |>
tbl_summary(by = trt, include = c(age, grade)) |>
add_stat_label()
```
::: {.callout-important}
## この章のまとめ
- `by =` で群ごとの列、`add_overall()` / `add_n()` で全体・人数の列
- `add_p()` で p 値。`test =` で検定を変更、`pvalue_fun =` で桁数を変更
- `add_q()`(多重比較補正)、`add_difference()`(差と CI、SMD)、`add_ci()`(各群の CI)
:::