library("marginaleffects")
library(marginaleffects)
mod <- lm(mpg ~ hp * drat * factor(am), data = mtcars)
plot_slopes(mod, variables = "hp", condition = "drat")
plot_slopes(mod, variables = "hp", condition = c("drat", "am"))
plot_slopes(mod, variables = "hp", condition = list("am", "drat" = 3:5))
plot_slopes(mod, variables = "am", condition = list("hp", "drat" = range))
plot_slopes(mod, variables = "am", condition = list("hp", "drat" = "threenum"))
# marginal slopes
plot_slopes(mod, variables = "hp", by = "am")
# marginal slopes on a counterfactual grid
plot_slopes(mod,
variables = "hp",
by = "am",
newdata = datagrid(am = 0:1, grid_type = "counterfactual")
)
Plot Conditional or Marginal Slopes
Description
Plot slopes on the y-axis against values of one or more predictors (x-axis, colors/shapes, and facets).
The by
argument is used to plot marginal slopes, that is, slopes made on the original data, but averaged by subgroups. This is analogous to using the by
argument in the slopes()
function.
The condition
argument is used to plot conditional slopes, that is, slopes computed on a user-specified grid. This is analogous to using the newdata
argument and datagrid()
function in a slopes()
call. All variables whose values are not specified explicitly are treated as usual by datagrid()
, that is, they are held at their mean or mode (or rounded mean for integers). This includes grouping variables in mixed-effects models, so analysts who fit such models may want to specify the groups of interest using the condition
argument, or supply model-specific arguments to compute population-level estimates. See details below. See the "Plots" vignette and website for tutorials and information on how to customize plots:
-
https://marginaleffects.com/bonus/plot.html
-
https://marginaleffects.com
Usage
plot_slopes(
model,
variables = NULL,
condition = NULL,
by = NULL,
newdata = NULL,
type = NULL,
vcov = NULL,
conf_level = 0.95,
wts = FALSE,
slope = "dydx",
rug = FALSE,
gray = getOption("marginaleffects_plot_gray", default = FALSE),
draw = TRUE,
...
)
Arguments
model
|
Model object |
variables
|
Name of the variable whose marginal effect (slope) we want to plot on the y-axis. |
condition
|
Conditional slopes
|
by
|
Aggregate unit-level estimates (aka, marginalize, average over). Valid inputs:
|
newdata
|
When newdata is NULL , the grid is determined by the condition argument. When newdata is not NULL , the argument behaves in the same way as in the slopes() function.
|
type
|
string indicates the type (scale) of the predictions used to compute contrasts or slopes. This can differ based on the model type, but will typically be a string such as: "response", "link", "probs", or "zero". When an unsupported string is entered, the model-specific list of acceptable values is returned in an error message. When type is NULL , the first entry in the error message is used by default.
|
vcov
|
Type of uncertainty estimates to report (e.g., for robust standard errors). Acceptable values:
|
conf_level
|
numeric value between 0 and 1. Confidence level to use to build a confidence interval. |
wts
|
logical, string or numeric: weights to use when computing average predictions, contrasts or slopes. These weights only affect the averaging in
|
slope
|
string indicates the type of slope or (semi-)elasticity to compute:
|
rug
|
TRUE displays tick marks on the axes to mark the distribution of raw data. |
gray
|
FALSE grayscale or color plot |
draw
|
TRUE returns a ggplot2 plot. FALSE returns a data.frame of the underlying data.
|
…
|
Additional arguments are passed to the predict() method supplied by the modeling package.These arguments are particularly useful for mixed-effects or bayesian models (see the online vignettes on the marginaleffects website). Available arguments can vary from model to model, depending on the range of supported arguments by each modeling package. See the "Model-Specific Arguments" section of the ?slopes documentation for a non-exhaustive list of available arguments.
|
Value
A ggplot2
object
Model-Specific Arguments
Some model types allow model-specific arguments to modify the nature of marginal effects, predictions, marginal means, and contrasts. Please report other package-specific predict()
arguments on Github so we can add them to the table below.
https://github.com/vincentarelbundock/marginaleffects/issues
Package | Class | Argument | Documentation |
brms
|
brmsfit
|
ndraws
|
brms::posterior_predict |
re_formula
|
brms::posterior_predict | ||
lme4
|
merMod
|
re.form
|
lme4::predict.merMod |
allow.new.levels
|
lme4::predict.merMod | ||
glmmTMB
|
glmmTMB
|
re.form
|
glmmTMB::predict.glmmTMB |
allow.new.levels
|
glmmTMB::predict.glmmTMB | ||
zitype
|
glmmTMB::predict.glmmTMB | ||
mgcv
|
bam
|
exclude
|
mgcv::predict.bam |
gam
|
exclude
|
mgcv::predict.gam | |
robustlmm
|
rlmerMod
|
re.form
|
robustlmm::predict.rlmerMod |
allow.new.levels
|
robustlmm::predict.rlmerMod | ||
MCMCglmm
|
MCMCglmm
|
ndraws
|
|
sampleSelection
|
selection
|
part
|
sampleSelection::predict.selection |