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 made on a user-specified grid. This is analogous to using the newdata
argument and datagrid()
function in a slopes()
call. Unspecified variables are held at their mean or mode.
See the "Plots" vignette and website for tutorials and information on how to customize plots:
https://vincentarelbundock.github.io/marginaleffects/articles/plot.html
https://vincentarelbundock.github.io/marginaleffects
Usage
plot_slopes(
model,
variables = NULL,
condition = NULL,
by = NULL,
type = "response",
vcov = NULL,
conf_level = 0.95,
slope = "dydx",
rug = FALSE,
gray = 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
Character vector (max length 3): Names of the predictors to display.
Named list (max length 3): List names correspond to predictors. List elements can be:
Numeric vector
Function which returns a numeric vector or a set of unique categorical values
Shortcut strings for common reference values: "minmax", "quartile", "threenum"
1: x-axis. 2: color/shape. 3: facets.
Numeric variables in positions 2 and 3 are summarized by Tukey's five numbers
?stats::fivenum
.
- by
Aggregate unit-level estimates (aka, marginalize, average over). Valid inputs:
FALSE
: return the original unit-level estimates.TRUE
: aggregate estimates for each term.Character vector of column names in
newdata
or in the data frame produced by calling the function without theby
argument.Data frame with a
by
column of group labels, and merging columns shared bynewdata
or the data frame produced by calling the same function without theby
argument.See examples below.
- 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
isNULL
, the default value is used. This default is the first model-related row in themarginaleffects:::type_dictionary
dataframe.- vcov
Type of uncertainty estimates to report (e.g., for robust standard errors). Acceptable values:
FALSE: Do not compute standard errors. This can speed up computation considerably.
TRUE: Unit-level standard errors using the default
vcov(model)
variance-covariance matrix.String which indicates the kind of uncertainty estimates to return.
Heteroskedasticity-consistent:
"HC"
,"HC0"
,"HC1"
,"HC2"
,"HC3"
,"HC4"
,"HC4m"
,"HC5"
. See?sandwich::vcovHC
Heteroskedasticity and autocorrelation consistent:
"HAC"
Mixed-Models degrees of freedom: "satterthwaite", "kenward-roger"
Other:
"NeweyWest"
,"KernHAC"
,"OPG"
. See thesandwich
package documentation.
One-sided formula which indicates the name of cluster variables (e.g.,
~unit_id
). This formula is passed to thecluster
argument of thesandwich::vcovCL
function.Square covariance matrix
Function which returns a covariance matrix (e.g.,
stats::vcov(model)
)
- conf_level
numeric value between 0 and 1. Confidence level to use to build a confidence interval.
- slope
string indicates the type of slope or (semi-)elasticity to compute:
"dydx": dY/dX
"eyex": dY/dX * Y / X
"eydx": dY/dX * Y
"dyex": dY/dX / X
- rug
TRUE displays tick marks on the axes to mark the distribution of raw data.
- gray
FALSE grayscale or color plot
- draw
TRUE
returns aggplot2
plot.FALSE
returns adata.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 themarginaleffects
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?marginaleffects
documentation for a non-exhaustive list of available arguments.
Examples
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"))