Y
to be for any given value of X
X
is a logical or a factor, regression and our method are exactly the same! This will make doing most of our methods easy!library(Ecdat)
data(Airline)
# Our method
AirlineOurMethod <- Airline %>% group_by(airline) %>%
mutate(output.r = output - mean(output),
cost.r = cost - mean(cost))
AirlineReg <- Airline %>%
mutate(output.reg = residuals(lm(output~factor(airline))),
cost.reg = residuals(lm(cost~factor(airline))))
c(cor(AirlineOurMethod$output.r,AirlineOurMethod$cost.r),
cor(AirlineReg$output.reg,AirlineReg$cost.reg))
## [1] 0.9272297 0.9272297
load('mariel.RData')
#(some data-cleaning omitted here, see the code for the slides)
#Then we can do our difference in difference with our method
means <- df %>% group_by(after,miami) %>% summarize(lwage = mean(lwage),unemp=mean(unemp))
(means$lwage[4] - means$lwage[2]) - (means$lwage[3]-means$lwage[1])
#or by regression, using an "interaction term"
lm(lwage~after*miami,data=df)
## [1] 0.02740653
##
## Call:
## lm(formula = lwage ~ after * miami, data = df)
##
## Coefficients:
## (Intercept) afterTRUE miamiTRUE
## 1.88186 -0.04606 -0.14674
## afterTRUE:miamiTRUE
## 0.02741