library(AER)
data(CollegeDistance)
# compute the correlation
# perform the first stage regression and compute the fraction of explained variation
# regress `log(wage)` on `education` and save the result to `wage_mod_1`
# regress `log(wage)` on `education` and controls and save the result to `wage_mod_2`
# obtain robust coefficient summaries on both models
# compute the correlation
cor(CollegeDistance$distance, CollegeDistance$education)
# perform the first stage regression and compute the fraction of explained variation
R2 <- summary(lm(education ~ distance, data = CollegeDistance))$r.squared
# estimate the IV regression of `log(wage)` on `education` using distance as the instrument and save the result to `wage_mod_iv1`
wage_mod_iv1 <- ivreg(log(wage) ~ education | distance, data = CollegeDistance)
# perform TSLS regression of `log(wage)` on `education` and controls using distance as the instrument and save the result to `wage_mod_iv2`
wage_mod_iv2 <- ivreg(log(wage) ~ unemp + ethnicity + gender + urban + education | . - education + distance, data = CollegeDistance)
# obtain robust coefficient summaries on both models
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("Nice! In the multiple regression model the estimated coefficient on education is positive, of reasonable size and highly significant when education is instrumented by college distance. ")