付録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     .hat .sigma   .cooksd
   <dbl>  <dbl> <dbl>   <dbl> <dbl>   <dbl>  <dbl>    <dbl>  <dbl>     <dbl>
 1     0     14     0       0 3.31    -1.68 -0.585 0.000907   1.04 0.0000339
 2     1     14     0       0 6.38    -1.41  1.80  0.000791   1.04 0.000647 
 3     0     14     0       0 3.84    -1.63 -0.597 0.000869   1.04 0.0000340
 4     0     14     0       0 4.19    -1.60 -0.606 0.000846   1.04 0.0000342
 5     0     14     0       0 4.80    -1.55 -0.621 0.000816   1.04 0.0000348
 6     0     14     0       0 5.47    -1.49 -0.639 0.000795   1.04 0.0000360
 7     0     14     0       0 0.487   -1.93 -0.520 0.00120    1.04 0.0000347
 8     0     14     0       0 5.31    -1.50 -0.634 0.000798   1.04 0.0000356
 9     0     13     0       0 5.34    -1.45 -0.650 0.000566   1.04 0.0000266
10     0     13     0       0 4.56    -1.52 -0.630 0.000590   1.04 0.0000259
# ℹ 4,350 more rows
# ℹ 1 more variable: .std.resid <dbl>