Classical difference-in-differences: Coding the time (post) variable when treatment starts at different times I have panel data with 40 treated cases and 40 control cases. I thought about the application of the 'classical' difference-in-differences (DiD) equation with the following linear regression model:
$y = \alpha + \gamma dtreated + \lambda dtime + \delta(dtreated \times dtime) + e$
where $dtime$ is the dummy variable for the pre/post time period and $dtreated$ is the dummy variable for treatment/control group, and I want to estimate the coefficient $\delta$ related to their interaction.
The problem I have is that for my treated cases the treatment begin in different years, and I tried to solve this issue by taking into account three years before treatment and three years after treatment by coding periods 123456 and creating the treatment dummy variable from values >=4.




Cases
Years
Times
dtime
dtreated
did
Y




1
2006
1
0
1
0
602


1
2007
2
0
1
0
548


1
2008
3
0
1
0
578


1
2009
4
1
1
1
633


1
2010
5
1
1
1
633


1
2011
6
1
1
1
604


2
2013
1
0
1
0
1682


2
2014
2
0
1
0
1566


2
2015
3
0
1
0
1566


2
2016
4
1
1
1
1566


2
2017
5
1
1
1
1566


2
2018
6
1
1
1
1566




Thus, I have 20 years of data with cases that have treatment in different years. In the example above, the starting point for case 1 is 2009 and the starting point for case 2 is 2016. In the model I consider period 4 the starting point and I want to use the 'classical' DiD method with two groups and two periods.
My questions are:

*

*Can I apply the classical DiD method with these conditions and this way of coding times of treatment?


*How can I consider the control cases in relation to the interval of time that I have to select?
To construct a balanced panel, I have to choose 6 years of observations for my control cases but I don't know how to select this interval.



*

*Can I apply the classical DiD method with these conditions and this way of coding times of treatment?


No.
The "post-period" varies across cases. To proceed with the 'classical' difference-in-differences (DiD) estimator, you'd have to make some arbitrary choice as to which year distinguishes pre- versus post-treatment for your controls, a position which is generally indefensible in the absence of some matching procedure.
I suppose you could use the 'classical' DiD approach on subsets of early-/late-adopters, assuming the cohort of early-/late-adopters have the same start dates. For example, say 20 cases become exposed to a treatment in 2009, while the remaining start in 2016. In this setting, you could use the 'classical' method, separately, on the subset of early-/late-adopter cases, focusing on the immediate adoption periods. This approach is defensible assuming a well-defined start date for the first and second waves of treatment, and assuming the exposure epochs don't overlap.
However, we rarely observe this in practice. In staggered adoption designs, the "start date" may vary widely across cases. To overcome this, we must define the interaction term in a different way to account for the different exposure epochs.



*How can I consider the control cases in relation to the interval of time that I have to select?


The control cases do not have a well-defined post-treatment period.
In canonical DiD applications, the post-treatment variable is case-invariant. In more specific words, the "post" or "time" variable is equal to 1 in all periods after treatment in both the treatment group and the control group. But what is "post-treatment" for the control group when treated cases become exposed to a treatment/policy at widely different times? In your setting, a term representing pre-/post-event has no well-defined meaning. The more defensible approach is to use the 'generalized' DiD estimator with all available data:
$$
y_{it} = \eta_i + \lambda_t + \delta D_{it} + u_{it},
$$
where $y_{it}$ is your outcome for case $i$ in year $t$. The parameters $\eta_i$ and $\lambda_t$ denote fixed effects for cases and years, respectively. $D_{it}$ is a dichotomous treatment variable. It equals 1 if case $i$ is in the treatment group and is in the $t$ periods after treatment. In the example data frame, $D_{it}$ should switch from 0 to 1 for 'Case 1' in 2009 onward. Similarly, $D_{it}$ should switch from 0 to 1 for 'Case 2' in 2016 onward. Unless treatment reverses, then for any treated case, $D_{it}$ 'turns on' in adoption year $t$ and 'stays on' in all subsequent periods. A control case, on the other hand, should consistently equal 0 in all years. Thus, if hypothetical 'Case 3' was never treated over the 20-year period, then it equals 0 in every single year. Note how the interaction term is implicit in the coding of $D_{it}$, and there's no requirement for you to discard observations beyond the 3-year pre-/post-treatment effect window. Here, $\delta$ identifies a weighted sum of the treatment effect. It may require re-weighting, and several papers propose new estimands to address the problem of negative weights. Peruse this recent NBER working paper for more information!
In short, I would proceed with the 'generalized' DiD estimator. It's amenable to settings with staggered adoption periods and you do not have to discard observations unnecessarily. Just give the paper I cited a quick read to understand the downsides of using this estimator.

Coding Suggestions
The plm package is very useful. Once you specify the proper indexes, then all that's required is the treatment dummy, which I will call did. Here is some code:
library(plm)

plm(y ~ did, index = c("cases", "years"), method = "within", effect = "twoways", data = ...)

Again, the variable did is only switching from 0 to 1 in those case-year combinations where the treatment is in effect, 0 otherwise! I recommend instantiating this variable manually to account for the irregular start (end) times. A standard interaction term isn't going to work as we don't have a unique pre/post indicator to properly demarcate the before/after epochs.
As a final recommendation, consider including time-varying covariates at the case level, if any. I also recommend clustering your standard errors at the case level to address the within-unit (i.e., within-case) dependence among your observations.
