library(AER) data(CollegeDistance) # calcular la correlación # realizar la regresión de primera etapa y calcular la fracción de variación explicada # regresar `log(wage)` en `education` y guardar el resultado en `wage_mod_1` # regresar `log(wage)` en `education` y controles y guardar el resultado en `wage_mod_2` # obtener resúmenes robustos de coeficientes en ambos modelos # calcular la correlación cor(CollegeDistance$distance, CollegeDistance$education) # realizar la regresión de primera etapa y calcular la fracción de variación explicada R2 <- summary(lm(education ~ distance, data = CollegeDistance))$r.squared # estimar la regresión IV de `log(wage)` en `education` usando la distancia como instrumento y guardar el resultado en `wage_mod_iv1` wage_mod_iv1 <- ivreg(log(wage) ~ education | distance, data = CollegeDistance) # realizar la regresión TSLS de `log(wage)` en `education` y controles usando la distancia como instrumento y guardar el resultado en `wage_mod_iv2` wage_mod_iv2 <- ivreg(log(wage) ~ unemp + ethnicity + gender + urban + education | . - education + distance, data = CollegeDistance) # obtener resúmenes robustos de coeficientes en ambos modelos coeftest(wage_mod_iv1, vcov. = vcovHC, type = "HC1") coeftest(wage_mod_iv2, vcov. = vcovHC, type = "HC1") test_output_contains("cor(CollegeDistance$distance, CollegeDistance$education)") test_object("R2") test_or({ f <- ex() %>% override_solution("attach(CollegeDistance); wage_mod_iv1 <- ivreg(log(wage) ~ education | distance); wage_mod_iv2 <- ivreg(log(wage) ~ unemp + ethnicity + gender + urban + education | . - education + distance); coeftest(wage_mod_iv1, vcov. = vcovHC, type = \"HC1\"); coeftest(wage_mod_iv2, vcov. = vcovHC, type = \"HC1\")") f %>% check_function("ivreg", index = 1) %>% check_arg("formula") %>% check_equal() f %>% check_function("ivreg", index = 2) %>% check_arg("formula") %>% check_equal() f %>% check_object("wage_mod_iv1") f %>% check_object("wage_mod_iv2") f %>% check_function("coeftest", index = 1) %>% check_arg("vcov.") %>% check_equal() f %>% check_function("coeftest", index = 2) %>% check_arg("vcov.") %>% check_equal() },{ f <- ex() %>% override_solution("wage_mod_iv1 <- ivreg(log(CollegeDistance$wage) ~ CollegeDistance$education | CollegeDistance$distance); wage_mod_iv2 <- ivreg(log(CollegeDistance$wage) ~ CollegeDistance$unemp + CollegeDistance$ethnicity + CollegeDistance$gender + CollegeDistance$urban + CollegeDistance$education | . - CollegeDistance$education + CollegeDistance$distance); coeftest(wage_mod_iv1, vcov. = vcovHC, type = \"HC1\"); coeftest(wage_mod_iv2, vcov. = vcovHC, type = \"HC1\")") f %>% check_function("ivreg", index = 1) %>% check_arg("formula") %>% check_equal() f %>% check_function("ivreg", index = 2) %>% check_arg("formula") %>% check_equal() f %>% check_object("wage_mod_iv1") f %>% check_object("wage_mod_iv2") f %>% check_function("coeftest", index = 1) %>% check_arg("vcov.") %>% check_equal() f %>% check_function("coeftest", index = 2) %>% check_arg("vcov.") %>% check_equal() },{ test_function("ivreg", index = 1, args = "formula") test_function("ivreg", index = 2, args = "formula") test_function("coeftest", index = 1, args = c("x", "vcov.")) test_function("coeftest", index = 2, args = c("x", "vcov.")) }) success_msg("¡Lindo! En el modelo de regresión múltiple, el coeficiente de educación estimado es positivo, de tamaño razonable y altamente significativo cuando la educación está instrumentada por la distancia universitaria.")