I have a dataset with individual id
(factor), time t
(factor), a dependent variable y
(continuous) and an independent variable x
(continuous), which can be measured at time t xt
or can be set at the individual level xi
.
set.seed(100)
df <- data.frame(id=as.factor(rep(1:20, each = 5)),
t=as.factor(rep(1:5, 20)),
y=rnorm(100, 5, 2))
df$xt <- rep(rnorm(100, 0, 1))
df$xi <- rep(rnorm(20, 0, 1), each = 5)
I want to estimate the marginal effects (and plot) of the interaction of time and the individual level IV (t:xi
) while controlling for individual fixed effects (id
). I know that the FEs of id
absorb the effects of xi
, but I want to see the effect of the interaction t:xi
. Below I show how this works with t:xt
but does not work with t:xi
.
m1 <- lm(y ~ t + xt + t:xt + id, df)
m2 <- lm(y ~ t + xi + t:xi + id, df)
Effect(focal.predictors = c("t", "xt"), mod = m1)
Effect(focal.predictors = c("t", "xi"), mod = m2)
I have tried different combinations to write the interaction term (t + t:xi
, t*xi
, etc.), and using different packages (effects
, ggeffects
, interplot
, margins
, etc.). Since there are coefficients for t
and t:xi
, I think there should be a way to estimate and plot these effects (using base-0/change). How could this be done?
Found a way to do it. It's a little clunky but it works. Basically, you have to create new variable for each interaction except for the first. It's not only that the individual FEs absorb the direct effect of
xi
, but they also absorb the interaction betweenxi
and timet1
. In other words, the baseline effect ofxi
in time 1 can be estimated based on the individual FEs.