if (require("emmeans")) {
# Get an idea of the data
if (require("ggplot2")) {
ggplot(iris, aes(x = Petal.Length, y = Sepal.Width)) +
geom_point(aes(color = Species)) +
geom_smooth(color = "black", se = FALSE) +
geom_smooth(aes(color = Species), linetype = "dotted", se = FALSE) +
geom_smooth(aes(color = Species), method = "lm", se = FALSE)
}
# Model it
model <- lm(Sepal.Width ~ Species * Petal.Length, data = iris)
# Compute the marginal effect of Petal.Length at each level of Species
slopes <- estimate_slopes(model, trend = "Petal.Length", at = "Species")
slopes
if (require("see")) {
plot(slopes)
}
standardize(slopes)
if (FALSE) {
# TODO: fails with latest emmeans (1.8.0)
if (require("mgcv") && require("see")) {
model <- mgcv::gam(Sepal.Width ~ s(Petal.Length), data = iris)
slopes <- estimate_slopes(model, at = "Petal.Length", length = 50)
summary(slopes)
plot(slopes)
model <- mgcv::gam(Sepal.Width ~ s(Petal.Length, by = Species), data = iris)
slopes <- estimate_slopes(model,
trend = "Petal.Length",
at = c("Petal.Length", "Species"), length = 20
)
summary(slopes)
plot(slopes)
}}
}
Run the code above in your browser using DataLab