Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign up[Request] Allowing custom leads and lags in interact() #23
Comments
|
Thanks for your very clear request! I've just released a new version of the package (0.5.0) so a few comments are in order:
data(base_did)
df = base_did ; df$window = ifelse(df$period %in% 3:7 & df$treat, 1, 0)
est1 = feols(y ~ x1 + window::period | id + period, df)
#> Variables 'window:period::1', 'window:period::2' and 3 others have been removed because of collinearity (see $collin.var).
est2 = feols(y ~ x1 + treat::period(c(1:2, 8:10)) | id + period, base_did)
etable(est1, est2)
#> est1 est2
#> x1 0.9907*** (0.04843) 0.9907*** (0.04843)
#> window:period::3 -2.789** (0.8627)
#> window:period::4 -4.314*** (0.9298)
#> window:period::5 -2.502** (0.9101)
#> window:period::6 -1.725* (0.8379)
#> window:period::7 1.083 (0.7922)
#> treat:period::3 -2.789** (0.8627)
#> treat:period::4 -4.314*** (0.9298)
#> treat:period::5 -2.502** (0.9101)
#> treat:period::6 -1.725* (0.8379)
#> treat:period::7 1.083 (0.7922)
#> Fixed-Effects: ------------------- -------------------
#> id Yes Yes
#> period Yes Yes
#> ___________________ ___________________ ___________________
#> Observations 1,080 1,080
#> S.E. type: Clustered by: id by: id
#> R2 0.50713 0.50713
#> Within R2 0.33496 0.33496The big problem is that by doing these changes I introduced bugs in coefplot(est2)But then all the references are represented as data points. coefplot(est1, only.inter = FALSE, drop = "x1")You can also locate the coefficients where you want on the x-axis using the argument coefplot(est1, only.inter = FALSE, drop = "x1", x = c(0, 3.5, 4, 4.5, 6.5, 7), xlim.add = c(-0.1,0.1))Note that you still have to provide a value for the On Your suggestionsAs you noticed, the factor to be interacted with can be of any type (logical, numeric, character, factor) and can represent anything, not only time periods. So the I think I'll introduce the two arguments
What do you think? Would these changes be OK? In any case, thanks a lot for the effort, very appreciated! |
|
Thank you for your fast answer, It now works as expected ! I agree with you about using |
|
Great then! :-) I'll close the issue when I add the new arguments. |
|
That's done! Still haven't fixed the |
|
Fyi: just got rid of the bug when interacted variables were removed because of collinearity. |



Problem
The
interactfunction is very handy for differences in differences setups and allows to quickly plot the estimated coefficients withcoefplot. However, by default the function interacts every values of thefeparameter. This is problematic when one wants to have only some leads and lags.Example
The basic diff in diff setup presented in the vignette is
However, one often wants to set custom number of leads and lags. For instance one would want to have only two pre-treatment coefficients (ie 2 leads) and two post-treatment coefficients (ie 2 lags).
What I tried
I tried to create a third dummy (hereby called
window) that is equal to 1 if the observation is treated AND in the focus window such that the interaction between thewindowandperiodgives the right set of dummies. However it creates interaction even whenwindowis equal to 0 and I end up with collinearity error.Code to reproduce :
My hacky solution
The solution is simply to filter out the null dummies when I interact
windowandperiod.result:
Proposed interface
Maybe
leadandlagare too economics-centered and usingpreandpostis clearer.rangeparameter to theinteractfunction.Final words
I can try implementing the function with a little help from the developer.