# Fitting panel data with both variable and constant coefficients using R

I'm working with panel data and want to fit a model of the form:

$y_{ij} = \alpha_{i} + \beta_{1i} x_{1ij} + \beta_{2i} x_{2ij} + \beta_{3} treatment_{ij} + \epsilon_{ij}$

The data consist of (unbalanced) time series for a number of individuals, each of which received the treatment at a different time. For each individual in the data, the coefficents on $x_1$ and $x_2$ should be different, as well as the intercepts. However, I want to estimate the coefficient on treatment across all individuals. I'm having trouble figuring out which functions to use to get this done.

One possibility would be "least squares dummy variables", using lm:

lm(y ~ x1*individual + x2*individual + treatment)


where "individual" is a factor variable. My data has a large number of individuals and so this takes a lot of time and memory to run. Other than that, is there anything wrong with this approach?

I've also looked into the plm package, which would have the advantage of time-demeaning the data instead of fitting for the intercepts for each individual. But I can't figure out how to make it fit a model of the form that I want. The closest I can get is:

pvcm(y ~ x1 + x2, index=c("individual","date"), model="within")


That will fit for different slopes for each individual, but I can't find a way to include the treatment variable such that the coefficient on that is the same for all individuals.

I'm also reading up on lme4, but am finding the documentation difficult. Is there a way to do this using "lmer"? Any advice on which packages are applicable and how they might differ would be much appreciated!

• How large is your data? The number of events in a typical time series will be helpful, as well as the number of individuals. `lme4' may well be able to help; there are a number of sets of tutorial slides here; lme4.r-forge.r-project.org – guest Feb 28 '12 at 6:21
• The number of individuals is 1200 (and could be larger in future analyses that I'll be doing) and the number of data points per individual is about 40. I have looked at some of the material at that link, but am still having trouble figuring out how to do this with lme4. – gub Feb 28 '12 at 15:32
• Hi there, are x1 and x2 both repeated measures, and how do they relate to the timing of the treatment? It would be good to get more detail about your study design. – Michelle Feb 29 '12 at 5:32