I am running a regression on two independent variables and their interaction, along with district fixed effects and clustering the standard errors at the observation level using lm_robust
command from estimatr
(version 0.22.0) with R version 3.6.0.
I want to visualize the predicted regression results using the plot
command from ggpredict
(version 0.14.3) but am getting an error that appears to be due to the inclusion of the fixed effects.
The specific error I get is:
Error in X[, !beta_na, drop = FALSE] %*% coefs[!beta_na, ] : non-conformable arguments
If I use ggpredict
after running a regression that only clusters the standard errors but does not include fixed effects, the code runs just fine. I get the same error when using the wrapper commands from sjPlot
instead of ggpredict
.
Below is a MWE:
library(ggeffects)
library(estimatr)
library(sjPlot)
N <- 1000
df <- data.frame(id = rep(1:N),
district = as.factor(rep(1:20, times = 50)),
x = rpois(N, lambda = 4),
y = rnorm(N),
z = factor(rbinom(N, 1, prob = 0.5)))
mod1 <- lm_robust(y ~ x*z,
clusters = id,
fixed_effects = ~district,
data = df)
summary(mod1)
predDF <- ggpredict(mod1, terms = c("x", "z")) # use ggpredict from ggeffects
plot_model(mod1, type = "pred", terms = c("x", "z")) # using plot_model from sjPlot
Wondering either how to get ggeffects
/sjPlot
to work lm_robust
models containing both clustered standard errors and fixed effects - or alternative packages? I already switched from using felm
in the lfe
library for the fixed effects and clustering because ggeffects
doesn't work on felm
objects.