[R] Fixed effects regression with state-specific trends

Jake Russ jake.w.russ at gmail.com
Tue Dec 15 15:24:24 CET 2015

Dear All,

I am trying to implement a regression with state-specific trends in R. I
can implement this in Stata with ease, but I am hoping to preserve my R
workflow. I suspect there is an "R formula trick" that I'm just not
understanding and I would be grateful to anyone who could help me
understand "the R way."

I posted a similar question to Stackoverflow, but the solution from there
is not playing nice with plm() and I am hoping for a better answer,

My panel data looks like the following,

df <- data.frame(state = rep(c("a", "b", "c"), each = 10), time = seq(1,
10, 1), stringsAsFactors = FALSE)

I would like to create state-specific trends that look like these three new

df2 <- cbind(df, model.matrix( ~ state - 1, data = df) * df$time)

To achieve this in Stata, I would simply write

xi: i.state i.state*time
reg y x _I*

The problem I have with the SO model.matrix solution is that I have greater
than 200 "states." Currently, I am generating more than 200 trend columns
with model.matrix and then I am building a very long formula string with
each column by name, I then turn that string into a formula with
as.formula(). Passing the resulting formula to lm() works but it is
cumbersome, and it is also choking the fixed effects version of plm().

Is there an R formula trick to this that I am missing?

Thank you,


	[[alternative HTML version deleted]]

More information about the R-help mailing list