% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/bin_layer.R
\name{bin_layer}
\alias{bin_layer}
\title{Add a Binning Diagnostic to an interplot}
\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.
}
\usage{
bin_layer(
  m,
  var1,
  var2,
  ci = 0.95,
  bins = 3,
  point_color = "#BD472A",
  point_shape = 18
)
}
\arguments{
\item{m}{A model object of class \code{lm} or \code{glm} including the
interaction of \code{var1} and \code{var2}.}

\item{var1}{The name (as a string) of the variable whose conditional effect is plotted.}

\item{var2}{The name (as a string) of the moderating variable.}

\item{ci}{A numeric value defining the confidence level. The default is 0.95.}

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

\item{point_color}{Color of the binned points and whiskers. Default \code{"#BD472A"}.}

\item{point_shape}{Plotting shape of the binned points. Default \code{18} (filled diamond).}
}
\value{
A list of \code{ggplot2} layers, to be added to an \code{interplot}
  plot with \code{+}.
}
\description{
Overlays within-bin estimates of the conditional effect of \code{var1} on an
\code{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.
}
\details{
For each quantile bin of \code{var2}, the model is refitted on the
  observations in that bin with \code{var2} centered at the bin median. The
  coefficient on \code{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.
}
\examples{
m <- lm(mpg ~ wt * cyl, data = mtcars)
interplot(m, "cyl", "wt") + bin_layer(m, "cyl", "wt")

}
