付録BのRコード

付録B 最尤法と2値選択モデル

サンプルデータ

union.csvMales.csvを簡略化したデータ.

パッケージの読み込み

library(dplyr)
library(broom)

B.3 Rによる推定

uniondata <- readr::read_csv("union.csv")

uniondata %>% 
  glm(union ~ school + black + married + wage,
      family = binomial(link = "logit"),
      data = .) %>% 
  tidy()

result <- uniondata %>% 
  glm(union ~ school + black + married + wage,
      family = binomial(link = "logit"),
      data = .)

result$fitted.values
result %>% 
  augment()
# A tibble: 4,360 × 11
   union school black married  wage .fitted .resid .std.resid     .hat .sigma
   <dbl>  <dbl> <dbl>   <dbl> <dbl>   <dbl>  <dbl>      <dbl>    <dbl>  <dbl>
 1     0     14     0       0 3.31    -1.68 -0.585     -0.585 0.000907   1.04
 2     1     14     0       0 6.38    -1.41  1.80       1.80  0.000791   1.04
 3     0     14     0       0 3.84    -1.63 -0.597     -0.598 0.000869   1.04
 4     0     14     0       0 4.19    -1.60 -0.606     -0.606 0.000846   1.04
 5     0     14     0       0 4.80    -1.55 -0.621     -0.622 0.000816   1.04
 6     0     14     0       0 5.47    -1.49 -0.639     -0.639 0.000795   1.04
 7     0     14     0       0 0.487   -1.93 -0.520     -0.521 0.00120    1.04
 8     0     14     0       0 5.31    -1.50 -0.634     -0.635 0.000798   1.04
 9     0     13     0       0 5.34    -1.45 -0.650     -0.650 0.000566   1.04
10     0     13     0       0 4.56    -1.52 -0.630     -0.630 0.000590   1.04
# ℹ 4,350 more rows
# ℹ 1 more variable: .cooksd <dbl>