---
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 >}}
# 最初の表:`tbl_summary()` の基本 {#sec-basics}
```{webr}
#| include: false
source("R/setup.R")
```
## データを眺める
```{webr}
head(trial)
```
## 1行で Table 1
```{webr}
trial |> tbl_summary()
```
これだけで、
- 連続変数(`age`, `marker` など)→ **中央値 (Q1, Q3)**
- カテゴリ変数(`stage`, `grade` など)→ **n (%)**
- 0/1 の変数(`response`, `death`)→ **1 の人数と割合** を1行で
- 欠測値 → **Unknown** 行
が自動で判定・集計されます。表の下の脚注に、使った要約統計量が書かれています。
## 変数を選ぶ:`include`
```{webr}
trial |>
tbl_summary(include = c(trt, age, grade, response))
```
`include` には、列名をそのまま並べるほか、`starts_with("t")`, `where(is.numeric)` などの選択関数も使えます。
## ラベルを変える:`label`
```{webr}
trial |>
tbl_summary(
include = c(age, grade, response),
label = list(
age ~ "年齢(歳)",
grade ~ "グレード",
response ~ "腫瘍縮小"
)
)
```
gtsummary では **`変数 ~ 値`** という書き方(formula)で、変数ごとの設定をします。
`list()` で複数まとめて指定できます。これから何度も出てくる書き方です。
## 統計量を変える:`statistic`
連続変数を **平均 (SD)** にしたい場合:
```{webr}
trial |>
tbl_summary(
include = c(age, marker, grade),
statistic = list(
all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} / {N} ({p}%)"
)
)
```
- `all_continuous()`:連続変数すべて
- `all_categorical()`:カテゴリ変数すべて(0/1 変数を含む)
- `{ }` の中に統計量の名前を書き、それ以外の文字はそのまま表示されます
| 書ける統計量(連続) | 意味 |
|---|---|
| `{mean}`, `{sd}` | 平均、標準偏差 |
| `{median}`, `{p25}`, `{p75}` | 中央値、25・75 パーセンタイル |
| `{min}`, `{max}` | 最小、最大 |
| `{N_nonmiss}`, `{N_miss}` | 欠測でない数、欠測数 |
| 書ける統計量(カテゴリ) | 意味 |
|---|---|
| `{n}`, `{N}`, `{p}` | 該当数、分母、割合 (%) |
::: {.callout-tip}
## 変数ごとに統計量を変える
`statistic = list(age ~ "{mean} ({sd})", marker ~ "{median} ({p25}, {p75})")` のように、
個別の変数名で指定することもできます。正規分布に近い変数は平均 (SD)、歪んだ変数は中央値 (IQR) にする、などの使い分けができます。
:::
## 複数行で表示する:`continuous2`
1つの変数に複数の統計量を **行を分けて** 表示したいときは、型を `continuous2` にします。
```{webr}
trial |>
tbl_summary(
include = c(age, marker),
type = all_continuous() ~ "continuous2",
statistic = all_continuous() ~ c(
"{mean} ({sd})",
"{median} ({p25}, {p75})",
"{min}, {max}"
)
)
```
## 小数点の桁数:`digits`
```{webr}
trial |>
tbl_summary(
include = c(age, marker, grade),
statistic = all_continuous() ~ "{mean} ({sd})",
digits = list(
age ~ 1, # 平均・SD とも小数1桁
marker ~ c(2, 3), # 平均は2桁、SD は3桁
all_categorical() ~ c(0, 1) # n は整数、% は小数1桁
)
)
```
## 変数の型を指定する:`type`
gtsummary は値の種類から型を自動判定しますが、思い通りでないときは `type` で直します。
```{webr}
trial |>
tbl_summary(
include = c(response, grade),
type = list(
response ~ "categorical", # 0 と 1 を両方表示する
grade ~ "dichotomous" # 特定の水準だけを1行で
),
value = grade ~ "III" # dichotomous で表示する水準
)
```
| 型 | 表示 |
|---|---|
| `"continuous"` | 1行に要約統計量 |
| `"continuous2"` | 複数行に要約統計量 |
| `"categorical"` | 水準ごとに n (%) |
| `"dichotomous"` | 1つの水準(既定は 1 や TRUE, "Yes")だけを1行で |
## 欠測値の表示:`missing`
```{webr}
trial |>
tbl_summary(
include = c(age, response),
missing = "always", # "ifany"(既定), "no", "always"
missing_text = "欠測"
)
```
::: {.callout-warning}
`missing = "no"` にすると欠測の行は消えますが、**割合の分母は欠測を除いた数** になります。
論文では欠測数の報告を求められることが多いので、消す場合は脚注などで補いましょう。
:::
## 割合の向き・並べ替え
```{webr}
trial |>
tbl_summary(
by = trt, # 次章で詳しく
include = c(stage, grade),
percent = "row", # "column"(既定), "row", "cell"
sort = all_categorical() ~ "frequency" # 度数の多い順に並べる
)
```
::: {.callout-important}
## この章のまとめ
- `tbl_summary()` だけで Table 1 の下地ができる
- `include`(変数選択)、`label`(ラベル)、`statistic`(統計量)、`digits`(桁数)、`type`(型)、`missing`(欠測)
- 設定は **`変数 ~ 値`** と `all_continuous()` / `all_categorical()` の組み合わせで書く
:::