library(foreign) library(dplyr) data_URL <- "https://github.com/mca91/EconometricsWithR/blob/master/data/fastfood.dta?raw=true" dat <- read.dta(data_URL) dat <- dat %>% mutate(FTE = nmgrs + empft + (0.5 * emppt), FTE2 = nmgrs2 + empft2 + (0.5 * emppt2)) dat_NJ <- subset(dat, state == 1) dat_PA <- subset(dat, state == 0) reg_dat <- data.frame( rbind( data.frame(id = dat$sheet, chain = dat$chain, state = dat$state, empl = dat$FTE, D = 0), data.frame(id = dat$sheet, chain = dat$chain, state = dat$state, empl = dat$FTE2, D = 1))) # estimate the regression model # obtain a robust summary # estimate the regression model emp_mod <- lm(empl ~ D, data = reg_dat, subset = state == 1) # obtain a robust summary library(AER) coeftest(emp_mod, vcov. = vcovHC, type = "HC1") ex() %>% check_predefined_objects("reg_dat") ex() %>% check_library("AER") ex() %>% check_object("emp_mod") %>% check_equal(undefined_msg = "Make sure to not remove `emp_mod`!") check_or( ex() %>% check_function(., "lm") %>% { check_arg(., "formula") %>% check_equal(incorrect_msg = "Looks like the `formula` argument in `lm()` is wrong.") check_arg(., "data") %>% check_equal(incorrect_msg = "Looks like the `data` argument in `lm()` is wrong.") }, ex() %>% override_solution("emp_mod <- lm(dat_NJ$empl ~ dat_NJ$D)") %>% check_function("lm") %>% check_arg("formula") %>% check_equal(incorrect_msg = "Looks like the `data` argument in `lm()` is wrong.") ) check_or( ex() %>% check_function("coeftest") %>% { check_arg(., "x") %>% check_equal() check_arg(., "vcov.") %>% check_equal() }, ex() %>% override_solution("emp_mod <- lm(dat_NJ$empl ~ dat_NJ$D); coeftest(emp_mod, vcov. = vcovHC, type = 'HC1')") %>% check_function("coeftest") %>% { check_arg(., "x") %>% check_equal() check_arg(., "vcov.") %>% check_equal() } ) success_msg(msg = "Nicely done! The estimate of the coefficient on `D` as well as the t-statistic and thus also the p-value match the results reported by `t.test()`. We cannot reject the hypothesis that the coefficient on `D` is zero.")