그냥 넘어가기 섭섭하니, 실제 분석 사례를 하나 소개하기로 하자. 데이터는 울드리지 교수의 계량경제학 교과서에서 가져왔다. 데이터는 1975년 753명의 여성 노동자를 대상으로 임금, 나이, 자녀, 결혼 상태 등 각종 변수를 담고 있다.
먼저 자료의 csv를 다운 받아서 적절한 방식으로 통계 패키지에 로드하도록 하자. 여기서는 R의 AER 패키지에 담긴 ivreg
명령을 활용할 예정이다.1
library(tidyverse)
library(AER)
mydata <-
read_csv('data/mroz.csv') %>%
na_if('.') %>%
filter(!is.na(wage)) %>%
mutate(
lwage = as.numeric(lwage)
)
데이터에는 NA값이 ’.’으로 되어 있어 이를 NA로 바꾸고, 해당 값을 제외한다.
##
## Call:
## lm(formula = lwage ~ educ, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.10256 -0.31473 0.06434 0.40081 2.10029
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.1852 0.1852 -1.000 0.318
## educ 0.1086 0.0144 7.545 2.76e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.68 on 426 degrees of freedom
## Multiple R-squared: 0.1179, Adjusted R-squared: 0.1158
## F-statistic: 56.93 on 1 and 426 DF, p-value: 2.761e-13
로그 임금을 교육년수로 회귀했을 때 계수는 약 0.11이다. 즉, 교육년수가 1년 올라갈 때 임금이 약 11% 정도 증가하는 것으로 이해할 수 있다. 그런데 이 회귀분석은 문제가 있다. 임금수준과 교육년수에 모두 영향을 줄 수 있는 요인이 있다면, OLS 추정량은 일치성을 지니지 않는다.
##
## Call:
## ivreg(formula = lwage ~ educ | fatheduc, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.0870 -0.3393 0.0525 0.4042 2.0677
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.44110 0.44610 0.989 0.3233
## educ 0.05917 0.03514 1.684 0.0929 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6894 on 426 degrees of freedom
## Multiple R-Squared: 0.09344, Adjusted R-squared: 0.09131
## Wald test: 2.835 on 1 and 426 DF, p-value: 0.09294
해당 노동자의 아버지의 교육년수를 도구 변수로 활용해보았다. 교육년수는 10% 유의수준에서 통계적으로 유의미하고 그 크기 역시 6% 정도로 크게 낮아졌다.아버지의 교육년수는 도구 변수로 적절할까? 우선 아버지의 교육년수와 딸의 교육년수 사이에는 높은 상관성이 존재할 것 같다. 아버지의 교육년수와 딸의 임금 수준의 관계는 어떨까? 뭔가 애매한 느낌이다. 도구 변수의 적절성을 테스트해 볼 방법은 없을까? 도구 변수가 갖추어야 하는 두가지 특징, 즉 내생 변수와 충분한 상관성을 지닐 것 그리고 원래 회귀식의 오차항과 상관성이 없을 것, 을 테스트하기 위해서 ivreg
에 diagnoses = TRUE
옵션을 추가해보자.
##
## Call:
## ivreg(formula = lwage ~ educ | fatheduc, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.0870 -0.3393 0.0525 0.4042 2.0677
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.44110 0.44610 0.989 0.3233
## educ 0.05917 0.03514 1.684 0.0929 .
##
## Diagnostic tests:
## df1 df2 statistic p-value
## Weak instruments 1 426 88.84 <2e-16 ***
## Wu-Hausman 1 425 2.47 0.117
## Sargan 0 NA NA NA
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6894 on 426 degrees of freedom
## Multiple R-Squared: 0.09344, Adjusted R-squared: 0.09131
## Wald test: 2.835 on 1 and 426 DF, p-value: 0.09294
##
## Call:
## lm(formula = lwage ~ educ + age + exper + expersq, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.08521 -0.30587 0.04946 0.37523 2.37077
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.5333762 0.2778430 -1.920 0.05557 .
## educ 0.1075228 0.0141745 7.586 2.12e-13 ***
## age 0.0002836 0.0048553 0.058 0.95344
## exper 0.0415623 0.0131909 3.151 0.00174 **
## expersq -0.0008152 0.0003996 -2.040 0.04195 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6672 on 423 degrees of freedom
## Multiple R-squared: 0.1568, Adjusted R-squared: 0.1489
## F-statistic: 19.67 on 4 and 423 DF, p-value: 7.328e-15
통제변수를 추가해도 OLS에서 교육년수의 계수는 크게 변하지 않았다. 인과관계 추론에서는 통제변수를 더 넣는다고 해도 상황이 개선되지 않는 경우가 많다.
reg_iv1 <- ivreg(lwage~educ+exper+expersq|fatheduc+motheduc+exper+expersq,data=mydata)
summary(reg_iv1, vcov = sandwich, diagnostics = T)
##
## Call:
## ivreg(formula = lwage ~ educ + exper + expersq | fatheduc + motheduc +
## exper + expersq, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.0986 -0.3196 0.0551 0.3689 2.3493
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0481003 0.4277846 0.112 0.91053
## educ 0.0613966 0.0331824 1.850 0.06497 .
## exper 0.0441704 0.0154736 2.855 0.00452 **
## expersq -0.0008990 0.0004281 -2.100 0.03631 *
##
## Diagnostic tests:
## df1 df2 statistic p-value
## Weak instruments 2 423 50.112 <2e-16 ***
## Wu-Hausman 1 423 2.582 0.109
## Sargan 1 NA 0.378 0.539
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6747 on 424 degrees of freedom
## Multiple R-Squared: 0.1357, Adjusted R-squared: 0.1296
## Wald test: 6.204 on 3 and 424 DF, p-value: 0.0003934
이번에는 도구 변수로 여성 노동자의 부모 모두의 교육년수를 활용했다. ivreg
에서는 |
뒤에 도구 변수를 포함해서 오차항에 외생적이라고 간주할 수 있는 변수를 모두 넣어준다. IV를 통한 추정 결과 여성 노동자의 교육년수는 10% 유의 수준에서 임금에 대해 약 6%의 영향을 끼치고 있음을 알 수 있다.
IV 모형이 잘 설정되었는지 확인해보자.
그리고 vcov = sandwich
옵션은 검정에 활용되는 분산-공분산 행렬을 통해 이분산성을 고려하기 위해 것이다. 손해날 것이 없으니 왠만하면 이 옵션을 쓰는 편이 좋다.
reg_iv2 <- ivreg(lwage~educ+exper+expersq|.-educ+fatheduc+motheduc,data=mydata)
위와 같은 식으로 표현할 수도 있다. 위 식에서 .-
이하는 원래 회귀식에서 내생변수(educ
)과 이의 추정에 동원되는 IV들(fathereduc
, motheduc
)을 나타낸다.
이 결과에서 보듯이 IV 회귀분석은 분석 도구로서 OLS가 지닌 근본적인 한계를 교정할 수 있는 좋은 수단이다. 당신의 데이터에서 IV로 사용될 만한 것이 없는지 한번 고민해보실 때다!
분석은 이 곳을 참고했다. http://eclr.humanities.manchester.ac.uk/index.php/IV_in_R↩