Open
Description
library(datasets)
library(margins)
mtcars$cyl = as.factor(mtcars$cyl)
mod = lm(mpg ~ cyl + hp + (cyl + hp):carb, data = mtcars)
ame.effects = summary(margins(mod))$AME
ame.effects
# Your plots seem to have a bug as the slope is not always the same as the slope of the ame (which, however, should be the case, right?)
cplot(mod, x = "carb")
abline(a = mean(mtcars$mpg), # use anything as intercept
b = ame.effects["carb"], col = "red")
# here the slope is equivalent to the ame value as the slope of the green and red curves are the same
library("pdp")
pdp = partial(mod, pred.var = "carb", grid.resolution = nrow(mtcars))
lines(pdp$carb, pdp$yhat, col = "green")