bin_layer: Add a Binning Diagnostic to an interplot

View source: R/bin_layer.R

bin_layerR Documentation

Add a Binning Diagnostic to an interplot

Description

Overlays within-bin estimates of the conditional effect of var1 on an interplot output, following the binning estimator of Hainmueller, Mummolo, and Xu (2019). It provides a visual check of the linear-interaction- effect (LIE) assumption: if the moderating relationship is truly linear, the binned point estimates fall on the interplot line; systematic departures signal a nonlinear conditional effect.

Usage

bin_layer(
  m,
  var1,
  var2,
  ci = 0.95,
  bins = 3,
  point_color = "#BD472A",
  point_shape = 18
)

Arguments

m

A model object of class lm or glm including the interaction of var1 and var2.

var1

The name (as a string) of the variable whose conditional effect is plotted.

var2

The name (as a string) of the moderating variable.

ci

A numeric value defining the confidence level. The default is 0.95.

bins

The number of moderator bins (quantile groups). The default is 3 (low / medium / high terciles).

point_color

Color of the binned points and whiskers. Default "#BD472A".

point_shape

Plotting shape of the binned points. Default 18 (filled diamond).

Details

For each quantile bin of var2, the model is refitted on the observations in that bin with var2 centered at the bin median. The coefficient on var1 is then its marginal effect evaluated at the bin median, estimated from only that bin's data; this is algebraically the Hainmueller-Mummolo-Xu L-estimator. Each estimate is drawn as a dot-and-whisker at the bin median.

Bins with singular or failed fits are dropped with a warning.

Value

A list of ggplot2 layers, to be added to an interplot plot with +.

Source

Hainmueller, Jens, Jonathan Mummolo, and Yiqing Xu. 2019. "How Much Should We Trust Estimates from Multiplicative Interaction Models? Simple Tools to Improve Empirical Practice." Political Analysis 27(2): 163–192.

Examples

m <- lm(mpg ~ wt * cyl, data = mtcars)
interplot(m, "cyl", "wt") + bin_layer(m, "cyl", "wt")


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