I am trying to extract the marginal effects from an interactive term that captures for the effects of a treatment X (X is coded as 1 or 0) on outcome Y (Y is coded in a scale from -10 to 10) moderated by variable A (A is coded between 0 and 10). However, I am not sure how to extract the marginal effects from the interaction terms at the highest and lowest measure of A
m<-lm(Y~ X*A, data = combined)
Overall, I managed to generate plots for the marginal effect using the interplot function:
interplot(m = m, var1 = "X", var2 = "A", ci = 0.90)+
ylab("X")+
xlab("A")+
theme_bw()+
ggtitle("Figure 1. Effect of X on Y Moderated by A")+
theme(plot.title = element_text(face = "bold"))+
geom_hline(yintercept = 0, linetype = "dashed")
Additionally, I tried to use ggpredict to extract the marginal effects with 90% confidence interval at different levels of A:
margin1<- ggpredict(m, c ("X", "A"), ci = 0.90)
margin1
However, using ggpredict produces marginal coefficients that do not align with what I see in the summary for m, and do not align with the marginal effects plot. Instead, I get estimates that are clearly not accurate or precise. How can I extract the marginal effects as seen in the interplot?
Since you unfortunately don't provide sample data, here is a minimal example using the R built-in state.x77
dataset.
fit <- lm(Income ~ Illiteracy * Murder, data = as.data.frame(state.x77))
We are interested in the marginal effect of Illiteracy
on Income
library(sjPlot)
plot_model(fit, type = "int")
Here plot_model
uses the minimal and maximal values of Murder
as the grouping levels (this is the default behavious, see the Plotting linteraction effects of Regression Models vignette for details).