2
$\begingroup$

I've searched quite a while for the answer and still have no clue how I would go about setting up this regression in R.

I have estimated a difference-in-difference treatment effect, simply y ~ treatment*after. I have panel data for 5 years pre-treatment and 5 years post-treatment. Now I additionally would like to estimate yearly treatment effects for all pre- and post-treatment years just as Atkin, Faber and Navarro (2016) did it with monthly data in their paper. They define their regression as follows: desired regression (photo)

In my data-frame did, the treatment happens after 5 years and for simplicity I want to normalize the coefficient on year 5 to zero. What I tried so far is to create an indicator variable (dummy) for each year 1 to 10, and then create a new data matrix did2 including these year dummies:

dy1  <- as.numeric(did$t==1)
dy1m <- matrix(dy1, ncol=1, nrow=5000)
dy2  <- as.numeric(did$t==2)
...
...

did2 <- cbind(did, dy1m, ... , dy10m)

Then, I did the following regression (leaving out fixed effects, clustered errors, etc. for now):

lm(y ~ dy1m + ... + dy10m -1, data = did)

The resulting coefficients do not make sense at all. The problem is that I do not even fully understand what this regression model should look like in my case (when we take the model in Atkin, Faber and Navarro (2016) as an example).

How do I program this model?

$\endgroup$
1
$\begingroup$

It looks like you are trying to fit a regression model with a factor variable. The regression equation in the paper you cite is treating the variable months-since-entry as a factor, with possible values between $-13$ up to $36$. In your coding you appear to be trying to do a regression using a factor variable, by manually creating a set of indicator variables to use in the model. Fortunately, all this is unnecessary, since R has simple functionality to fit regression models with factor variables with a much simpler syntax, without the user having to program the design matrix manually.

To fit your regression, with the variable t treated as a factor variable, you can use your original data-frame did via the following code:

MODEL <- lm(y ~ factor(t), data = did);

This code will recognise t as a factor variable and will automatically create a design matrix for the regression that consists of indicators for the outcomes of this variable. There is no need to remove the intercept from the model with this syntax, since R will automatically see that the regression should exclude a baseline category for the factor variable from the model.

This should give you a baseline piece of code that allows you to fit a linear regression for your data. You can then build on this by adding other relevant variables. It appears from your question that you are a novice in R programming, so I would also recommend that you do some study on how to manipulate data frames (e.g., with the dplyr package) and how you fit regression models in cases where you have different types of variables.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.