plot_comparisons {marginaleffects} | R Documentation |
Plot comparisons 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 comparisons, that is, comparisons made on the original data, but averaged by subgroups. This is analogous to using the by
argument in the comparisons()
function.
The condition
argument is used to plot conditional comparisons, that is, comparisons made on a user-specified grid. This is analogous to using the newdata
argument and datagrid()
function in a comparisons()
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
plot_comparisons(
model,
variables = NULL,
condition = NULL,
by = NULL,
type = "response",
vcov = NULL,
conf_level = 0.95,
transform_pre = "difference",
transform_post = NULL,
rug = FALSE,
gray = FALSE,
draw = TRUE,
...
)
model |
Model object |
variables |
Name of the variable whose contrast we want to plot on the y-axis. |
condition |
Conditional slopes
|
by |
Aggregate unit-level estimates (aka, marginalize, average over). Valid inputs:
|
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 |
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. |
transform_pre |
string or function. How should pairs of adjusted predictions be contrasted?
|
transform_post |
string or function. Transformation applied to unit-level estimates and confidence intervals just before the function returns results. Functions must accept a vector and return a vector of the same length. Support string shortcuts: "exp", "ln" |
rug |
TRUE displays tick marks on the axes to mark the distribution of raw data. |
gray |
FALSE grayscale or color plot |
draw |
|
... |
Additional arguments are passed to the |
A ggplot2
object
mod <- lm(mpg ~ hp * drat * factor(am), data = mtcars)
plot_comparisons(mod, variables = "hp", condition = "drat")
plot_comparisons(mod, variables = "hp", condition = c("drat", "am"))
plot_comparisons(mod, variables = "hp", condition = list("am", "drat" = 3:5))
plot_comparisons(mod, variables = "am", condition = list("hp", "drat" = range))
plot_comparisons(mod, variables = "am", condition = list("hp", "drat" = "threenum"))