interplot.brmsfit: Plot Conditional Coefficients in Bayesian Models with...

View source: R/Interplot_brms.R

interplot.brmsfitR Documentation

Plot Conditional Coefficients in Bayesian Models with Interaction Terms

Description

interplot.brmsfit is a method to calculate conditional coefficient estimates from the posterior draws of a Bayesian regression model fitted with brm that includes a two-way interaction term.

Usage

## S3 method for class 'brmsfit'
interplot(
  m,
  var1,
  var2,
  plot = TRUE,
  steps = NULL,
  ci = 0.95,
  adjCI = FALSE,
  hist = FALSE,
  var2_dt = NA,
  predPro = FALSE,
  var2_vals = NULL,
  point = FALSE,
  sims = 1000,
  xmin = NA,
  xmax = NA,
  ercolor = NA,
  esize = 0.5,
  ralpha = 0.5,
  rfill = "grey70",
  stats_cp = "none",
  txt_caption = NULL,
  facet_labs = NULL,
  var3 = NULL,
  var3_vals = NULL,
  facet = TRUE,
  ...
)

Arguments

m

A model object of class brmsfit including an interaction term.

var1

The name (as a string) of the variable of interest in the interaction term; its conditional coefficient estimates will be plotted.

var2

The name (as a string) of the other variable in the interaction term.

plot

A logical value indicating whether the output is a plot or a dataframe including the conditional coefficient estimates of var1, their upper and lower bounds, and the corresponding values of var2.

steps

Desired length of the sequence. A non-negative number, which for seq and seq.int will be rounded up if fractional. The default is 100 or the unique categories in the var2 (when it is less than 100. Also see unique).

ci

A numeric value defining the credible interval. The default value is 95% (0.95). For brmsfit objects the interval summarizes the posterior draws, so it is an (equal-tailed) Bayesian credible interval rather than a frequentist confidence interval.

adjCI

The false-discovery-rate adjustment of Esarey and Sumner (2017) is a frequentist correction and does not apply to Bayesian posteriors. The argument is ignored (with a warning) for brmsfit objects.

hist

A logical value indicating if there is a histogram of 'var2' added at the bottom of the conditional effect plot.

var2_dt

A numerical value indicating the frequency distribution of 'var2'. It is only used when 'hist == TRUE'. When the object is a model, the default is the distribution of 'var2' of the model.

predPro

A logical value with default of 'FALSE'. When the 'm' is fitted with a Bernoulli or binomial family and the argument is set to 'TRUE', the function plots the posterior expected predicted probabilities at the values given by 'var2_vals', computed with posterior_epred.

var2_vals

A numerical value indicating the values the predicted probabilities are estimated, when 'predPro' is 'TRUE'.

point

A logical value determining the format of plot. By default, the function produces a line plot when var2 takes on ten or more distinct values and a point (dot-and-whisker) plot otherwise; option TRUE forces a point plot.

sims

Ignored for brmsfit objects: the posterior draws produced by the sampler are used directly instead of re-simulating the coefficients.

xmin

A numerical value indicating the minimum value shown of x shown in the graph. Rarely used.

xmax

A numerical value indicating the maximum value shown of x shown in the graph. Rarely used.

ercolor

A character value indicating the outline color of the whisker or ribbon.

esize

A numerical value indicating the size of the whisker or ribbon.

ralpha

A numerical value indicating the transparency of the ribbon.

rfill

A character value indicating the filling color of the ribbon.

stats_cp

A character value indicating what statistics to present as the plot note. Three options are available: "none", "ci", and "ks". The default is "none". See the Details for more information.

txt_caption

A character string to add a note for the plot, a value will sending to ggplot2::labs(caption = txt_caption)).

facet_labs

An optional character vector of facet labels to be used when plotting an interaction with a factor variable.

var3

An optional name (as a string) of a third variable for a three-way interaction var1 * var2 * var3. When supplied, the conditional effect of var1 across var2 is shown at several values/levels of var3. The default NULL gives the standard two-way behavior. Requires continuous var1 and var2.

var3_vals

An optional numeric vector giving the values of a continuous var3 to condition on. The default is the mean and the mean plus or minus one standard deviation. Ignored when var3 is a factor.

facet

A logical value, used only with var3. TRUE (default) draws one panel per value/level of var3; FALSE overlays the curves colored by var3.

...

Other ggplot aesthetics arguments for points in the dot-whisker plot or lines in the line-ribbon plots. Not currently used.

Details

interplot.brmsfit is an S3 method of interplot for models fitted with brm. Unlike the frequentist methods, it does not call arm::sim: the posterior draws of the population-level (fixed) effects are extracted directly with as.matrix and the conditional coefficient b_{var1} + b_{var1:var2} \cdot var2 is computed for every draw. Point estimates are posterior means and the bounds are posterior quantiles.

Because the output function is based on ggplot, any additional arguments and layers supported by ggplot2 can be added with the +.

The 'brms' package is only suggested by 'interplot'; it must be installed for this method to run.

Value

The function returns a ggplot object, or a list with the data frame of conditional coefficients when plot = FALSE.

Examples

## Not run: 
library(brms)
data(mtcars)

# A Bayesian linear model with a two-way interaction
m_brms <- brm(mpg ~ wt * cyl, data = mtcars, chains = 2, refresh = 0)

# Identical interface; the band is a 95% posterior credible interval
interplot(m_brms, var1 = "cyl", var2 = "wt")

# Posterior predicted probabilities for a Bernoulli model
m_brms_bin <- brm(am ~ wt * cyl, data = mtcars,
                  family = bernoulli(), chains = 2, refresh = 0)
interplot(m_brms_bin, var1 = "wt", var2 = "cyl",
          predPro = TRUE, var2_vals = c(4, 6, 8))

## End(Not run)


interplot documentation built on June 24, 2026, 5:08 p.m.