Rのロジスティック回帰によるオッズ比と95%信頼区間の算出

R

Rのロジスティック回帰ではglmが一般的に使用されますが,summaryではオッズ比と95%信頼区間が算出されないためbroomパッケージのtidyを使用するのが便利です.

通常の一般化線形モデルでロジスティック回帰

通常通りにロジスティック回帰を行った際にはsummary()で結果を出す.

fit <- glm(
  case ~ age + parity + education + spontaneous + induced,
  data = infert,
  family = binomial()
)
summary(fit)

すると結果は次のように出力されるはず.

Call:
glm(formula = case ~ age + parity + education + spontaneous +
induced, family = binomial(), data = infert)

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.14924 1.41220 -0.814 0.4158
age 0.03958 0.03120 1.269 0.2046
parity -0.82828 0.19649 -4.215 2.49e-05 ***
education6-11yrs -1.04424 0.79255 -1.318 0.1876
education12+ yrs -1.40321 0.83416 -1.682 0.0925 .
spontaneous 2.04591 0.31016 6.596 4.21e-11 ***
induced 1.28876 0.30146 4.275 1.91e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 316.17 on 247 degrees of freedom
Residual deviance: 257.80 on 241 degrees of freedom
AIC: 271.8

Number of Fisher Scoring iterations: 4

Coefficients:以下に各変数の回帰係数(Estimate)と標準誤差(Std.Error),またp値が出力されている.

つまりこれらはオッズ比ではなく,いわゆるオッズの自然対数(対数オッズ),つまりロジット(Logit)が表記されているため,オッズ比と95%信頼区間を算出するには自身でべき乗しなければならない.

これを簡単にわかりやすく表示させるにはbroomパッケージのtidyが便利.

broom::tidyを使用してオッズ比と95%信頼区間を出す

broomパッケージはあらかじめインストールしておく.よく使われるtidymodelsに含まれているのでそちらでインストールしておくのがよいかも.

library(broom)
tidy(fit, exponentiate = TRUE, conf.int = TRUE)

tidyを使用して,引数としてexponentiate = TRUE(べき乗してオッズ比とする), conf.int = TRUE(信頼区間を出す)を追加することで下記のように出力される.

# A tibble: 7 × 7
term estimate std.error statistic p.value conf.low conf.high
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 0.317 1.41 -0.814 4.16e- 1 0.0192 5.01
2 age 1.04 0.0312 1.27 2.05e- 1 0.979 1.11
3 parity 0.437 0.196 -4.22 2.49e- 5 0.291 0.631
4 education6-11yrs 0.352 0.793 -1.32 1.88e- 1 0.0733 1.74
5 education12+ yrs 0.246 0.834 -1.68 9.25e- 2 0.0467 1.30
6 spontaneous 7.74 0.310 6.60 4.21e-11 4.33 14.7
7 induced 3.63 0.301 4.28 1.91e- 5 2.05 6.70

なお,conf.level = 0.95を引数で変更することで信頼区間を変えることも可能.

コメント

タイトルとURLをコピーしました