plot_cco: Plot Conditional Contrasts

View source: R/plot_cco.R

plot_ccoR Documentation

Plot Conditional Contrasts


This function plots contrasts (y-axis) against values of predictor(s) variable(s) (x-axis and colors). This is especially useful in models with interactions, where the values of contrasts depend on the values of "condition" variables.


  effect = NULL,
  condition = NULL,
  type = "response",
  vcov = NULL,
  conf_level = 0.95,
  transform_pre = "difference",
  transform_post = NULL,
  draw = TRUE,



Model object


Name of the variable whose contrast we want to plot on the y-axis


character vector or named list of length smaller than 3. Character vectors must be the names of the predictor variables to display. The names of the list must The first element is displayed on the x-axis. The second element determines the colors. The third element creates facets. Unspecified variables are held at their means or modes. Lists can include these types of values (see Examples section below):

  • Numeric vector

  • Function which returns a numeric vector or a set of unique categorical values

  • Shortcut strings for common reference values: "minmax", "quartile", "threenum"


string indicates the type (scale) of the predictions used to compute marginal effects or contrasts. 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 default value is used. This default is the first model-related row in the marginaleffects:::type_dictionary dataframe.


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 the sandwich package documentation.

  • One-sided formula which indicates the name of cluster variables (e.g., ~unit_id). This formula is passed to the cluster argument of the sandwich::vcovCL function.

  • Square covariance matrix

  • Function which returns a covariance matrix (e.g., stats::vcov(model))


numeric value between 0 and 1. Confidence level to use to build a confidence interval.


string or function. How should pairs of adjusted predictions be contrasted?

  • string: shortcuts to common contrast functions.

    • Supported shortcuts strings: difference, differenceavg, differenceavgwts, dydx, eyex, eydx, dyex, dydxavg, eyexavg, eydxavg, dyexavg, dydxavgwts, eyexavgwts, eydxavgwts, dyexavgwts, ratio, ratioavg, ratioavgwts, lnratio, lnratioavg, lnratioavgwts, lnor, lnoravg, lnoravgwts, expdydx, expdydxavg, expdydxavgwts

    • See the Transformations section below for definitions of each transformation.

  • function: accept two equal-length numeric vectors of adjusted predictions (hi and lo) and returns a vector of contrasts of the same length, or a unique numeric value.

    • See the Transformations section below for examples of valid functions.


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"


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 ?marginaleffects documentation for a non-exhaustive list of available arguments.


A ggplot2 object

See Also

Other plot: plot.marginaleffects(), plot_cap(), plot_cme()


mod <- lm(mpg ~ hp * drat * factor(am), data = mtcars)

plot_cco(mod, effect = "hp", condition = "drat")

plot_cco(mod, effect = "hp", condition = c("drat", "am"))

plot_cco(mod, effect = "hp", condition = list("am", "drat" = 3:5))

plot_cco(mod, effect = "am", condition = list("hp", "drat" = range))

marginaleffects documentation built on Nov. 24, 2022, 1:06 a.m.