I’ve run an individual-fixed effects panel model in R using the plm-package. I now want to plot the marginal effects. However, neither plot_model() nor effect_plot() work for plm-objects. plot_model() works for type = “est” but not for type = “pred”. My online search so far only suggests using ggplot (which however only displays OLS-regressions, not fixed effects) or outdated functions (i.e, sjp.lm())
Does anyone have any recommendations how I can visualize effects of plm-objects?
IFE_Aut_uc <- plm(LoC_Authorities_rec ~ Compassion_rec, index = c("id","wave"), model = "within", effect = "individual", data = D3_long2)
summary(IFE_Aut_uc)
plot_model(IFE_Aut_uc, type = "pred”)
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 50238, 82308
and:
effect_plot(IFE_Pol_uc, pred = Compassion_rec)
Error in `stop_wrap()`:
! ~does not appear to be a one- or two-sided formula.
LoC_Politicians_recdoes not appear to be a one- or two-sided formula.
Compassion_recdoes not appear to be a one- or two-sided formula.
Backtrace:
1. jtools::effect_plot(IFE_Pol_uc, pred = Compassion_rec)
2. jtools::get_data(model, warn = FALSE)
4. jtools:::get_lhs(formula)
Edit 2022-08-20: The latest version of plm
on CRAN now includes a predict()
method for within models. In principle, the commands illustrated below using fixest
should now work with plm
as well.
In my experience, plm
models are kind of tricky to deal with, and many of the packages which specialize in “post-processing” fail to handle these objects properly.
One alternative would be to estimate your “within” model using the fixest
package and to plot the results using the marginaleffects
package. (Disclaimer: I am the marginaleffects
author.)
Note that many of the models estimated by plm
are officially supported and tested with marginaleffects
(e.g., random effects, Amemiya, Swaymy-Arora). However, this is not the case of this specific "within" model, which is even trickier than the others to support.
First, we estimate two models to show that the plm
and fixest
versions are equivalent:
library(plm)
library(fixest)
library(marginaleffects)
library(modelsummary)
data("EmplUK")
mod1 <- plm(
emp ~ wage * capital,
index = c("firm", "year"),
model = "within",
effect = "individual",
data = EmplUK)
mod2 <- feols(
emp ~ wage * capital | firm,
se = "standard",
data = EmplUK)
models <- list("PLM" = mod1, "FIXEST" = mod2)
modelsummary(models)
PLM | FIXEST | |
---|---|---|
wage | 0.000 | 0.000 |
(0.034) | (0.034) | |
capital | 2.014 | 2.014 |
(0.126) | (0.126) | |
wage × capital | -0.043 | -0.043 |
(0.004) | (0.004) | |
Num.Obs. | 1031 | 1031 |
R2 | 0.263 | 0.986 |
R2 Adj. | 0.145 | 0.984 |
R2 Within | 0.263 | |
R2 Within Adj. | 0.260 | |
AIC | 4253.9 | 4253.9 |
BIC | 4273.7 | 4273.7 |
RMSE | 1.90 | 1.90 |
Std.Errors | IID | |
FE: firm | X |
Now, we use the marginaleffects
package to plot the results. There are two main functions for this:
plot_cap()
: plot conditional adjusted predictions. How does my predicted outcome change as a function of a covariate?plot_cme()
: plot conditional marginal effects. How does the slope of my model with respect to one variable (i.e., a derivative or “marginal effect”) change with respect to another variable?See the website for definitions and details: https://vincentarelbundock.github.io/marginaleffects/
plot_cap(mod2, condition = "capital")
plot_cme(mod2, effect = "wage", condition = "capital")