library(dplyr)
library(broom)
library(fixest)
第11章のRコード
第11章 操作変数法
パッケージの読み込み
11.2 操作変数法の実施:2段階最小二乗法
11.2.1 2段階最小二乗法
<- readr::read_csv("cigarette.csv")
cigdata
%>% # 通常の最小二乗法
cigdata lm(bw ~ cigpacks,
data = .) %>%
tidy()
# A tibble: 2 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 3423. 17.0 201. 0
2 cigpacks -62.4 1.54 -40.4 2.94e-212
%>%
cigdata lm(cigpacks ~ price,
data = .) %>%
augment(newdata = cigdata) %>%
lm(bw ~ .fitted,
data = .) %>%
tidy()
# A tibble: 2 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 3332. 22.2 150. 0
2 .fitted -53.3 2.03 -26.2 1.63e-115
%>%
cigdata lm(cigpacks ~ price,
data = .) %>%
tidy()
# A tibble: 2 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 42.1 0.340 124. 0
2 price -5.01 0.0526 -95.2 0
Rによるデータ演習
<- readr::read_csv("proximity.csv")
proxdata
%>%
proxdata print(n = 4)
# A tibble: 3,010 × 7
lwage educ exper black south smsa nearc4
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 6.31 7 16 1 0 1 0
2 6.18 12 9 0 0 1 0
3 6.58 12 16 0 0 1 0
4 5.52 11 10 0 0 1 1
# ℹ 3,006 more rows
<- proxdata %>% # 最小二乗法
OLS feols(lwage ~ educ + exper + black + south + smsa)
OLS
OLS estimation, Dep. Var.: lwage
Observations: 3,010
Standard-errors: IID
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.913331 0.063121 77.83968 < 2.2e-16 ***
educ 0.073807 0.003534 20.88707 < 2.2e-16 ***
exper 0.039313 0.002196 17.90611 < 2.2e-16 ***
black -0.188223 0.017768 -10.59349 < 2.2e-16 ***
south -0.129053 0.015229 -8.47442 < 2.2e-16 ***
smsa 0.164741 0.015692 10.49851 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 0.376836 Adj. R2: 0.277561
<- proxdata %>% # 2段階最小二乗法
TSLS feols(lwage ~ exper + black + south + smsa | educ ~ nearc4)
TSLS
TSLS estimation, Dep. Var.: lwage, Endo.: educ, Instr.: nearc4
Second stage: Dep. Var.: lwage
Observations: 3,010
Standard-errors: IID
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3.939822 0.831763 4.73671 2.2729e-06 ***
fit_educ 0.131850 0.049573 2.65970 7.8625e-03 **
exper 0.062270 0.019686 3.16313 1.5764e-03 **
black -0.129601 0.053262 -2.43326 1.5022e-02 *
south -0.109252 0.023176 -4.71394 2.5403e-06 ***
smsa 0.134826 0.030291 4.45105 8.8561e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 0.393395 Adj. R2: 0.212674
F-test (1st stage), educ: stat = 16.7 , p = 4.43e-5 , on 1 and 3,004 DoF.
Wu-Hausman: stat = 1.50259, p = 0.220369, on 1 and 3,003 DoF.
etable(OLS, TSLS)
OLS TSLS
Dependent Var.: lwage lwage
Constant 4.913*** (0.0631) 3.940*** (0.8318)
educ 0.0738*** (0.0035) 0.1318** (0.0496)
exper 0.0393*** (0.0022) 0.0623** (0.0197)
black -0.1882*** (0.0178) -0.1296* (0.0533)
south -0.1291*** (0.0152) -0.1093*** (0.0232)
smsa 0.1647*** (0.0157) 0.1348*** (0.0303)
_______________ ___________________ ___________________
S.E. type IID IID
Observations 3,010 3,010
R2 0.27876 0.21398
Adj. R2 0.27756 0.21267
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1