Using R and plm to estimate fixed-effects models that include interactions with time

I am using plm() to estimate fixed-effects models of the form

y ~ x + time + time:fixed_trait

where fixed_trait is a variable that varies across individuals but is constant within individuals.

The point of interacting time with fixed_trait is to permit the effect of fixed_trait to vary across time. (I am working here from Paul Allison’s recent booklet on fixed effects. Citation appended.)

plm() has no trouble estimating coefficients and standard errors for such models. But summary.plm() can’t calculate R^2 for these models. This is the problem that I would like to fix.

Here is a minimal example:

tmp <- data.frame(ID=rep(1:3, 2), year=rep(0:1, each=3), 
                  y=rnorm(6), const=rep(1:3, 2))
fe1 <- plm(y ~ year,              index=c('ID', 'year'), data=tmp)
fe2 <- plm(y ~ year + year:const, index=c('ID', 'year'), data=tmp)
summary(fe1)  # works fine
summary(fe2)  # Error in crossprod(t(X), beta) : non-conformable arguments

Delving into plm:::summary.plm makes the problem clearer. To calculate R^2, plm tries to do this:

beta <- coef(fe2)
X <- model.matrix(fe2)
crossprod(t(X), beta)

This doesn’t work because beta only includes estimates for year1 and year0:const, while X also includes a column for year1:const. In other words, X includes columns for both year0:const and year1:const, and it’s impossible to estimate both of those coefficients.

A workaround is to create the interaction term “by hand” before entering it into the formula:

tmp$yearXconst <- tmp$year*tmp$const
fe3 <- plm(y ~ year + yearXconst, index=c('ID', 'year'), data=tmp)
summary(fe3)  # works fine

But this is cumbersome. Short of this, is there anything that I can do to make summary.plm work with such models?


Allison, Paul D. 2009. Fixed Effects Regression Models. Los Angeles, CA: Sage. See especially pages 19-21.


Try using

fe3 <- plm(y ~ year * const, index = c('ID', 'year'), data = tmp)

instead. It even works if you are redundantly including time fixed effects in the * interaction (because time is being fixed effect-ed out):

fe4 <- plm(y ~ year * const, index = c('ID', 'year'), data = tmp, effect = "twoway")

Source : Link , Question Author : user697473 , Answer Author : Charlie

Leave a Comment