1\name{mcPlots}
2\alias{mcPlots}
3\alias{mcPlots.default}
4\alias{mcPlot}
5\alias{mcPlot.lm}
6\alias{mcPlot.glm}
7
8\title{Draw Linear Model Marginal and Conditional Plots in Parallel or Overlaid}
9\description{
10the \code{mcPlot} function draws two graphs or overlays the two graphs.  For a response Y and a regressor X, the first plot is the \emph{m}arginal plot of Y versus X with both variables centered, visualizing the conditional distribution of Y given X ignoring all other regressors.  The second plot is an added-variable for X after all other regressors, visualizing the \emph{c}onditional distribution of Y given X after adjusting for all other predictors.  The added variable plot by default is drawn using the same xlim and ylim as the centered marginal plot to emphasize that conditioning removes variation in both the regressor and the response.The plot is primarily intended as a pedagogical tool for understanding coefficients in first-order models.
11}
12
13\usage{
14mcPlots(model, ...)
15
16\method{mcPlots}{default}(model, terms=~., layout=NULL, ask, overlaid=TRUE, ...)
17
18mcPlot(model, ...)
19
20\method{mcPlot}{lm}(model, variable, id=FALSE,
21    col.marginal=carPalette()[2], col.conditional=carPalette()[3],
22    col.arrows="gray", pch = c(16,1), cex=par("cex"), pt.wts=FALSE,
23    lwd = 2, grid=TRUE, ellipse=FALSE, overlaid=TRUE, new=TRUE,
24    title=TRUE, ...)
25
26\method{mcPlot}{glm}(model, ...)
27}
28\arguments{
29  \item{model}{model object produced by \code{lm}; the \code{"glm"} method just
30  reports an error.}
31  \item{terms}{ A one-sided formula that specifies a subset of the predictors.
32One added-variable plot is drawn for each regressor and for each basis vector used to define a factor. For example, the
33specification \code{terms = ~ . - X3} would plot against all terms
34except for \code{X3}. If this argument is a quoted name of one of the regressors or factors, the
35added-variable plot is drawn for that regressor or factor only.  Unlike other car functions, the formula should include the names of regressors, not predictors.  That is, if \code{log(X4)} is used to represent a predictor \code{X4}, the formula should specify \code{terms = ~ log(X4)}.
36}
37\item{variable}{A quoted string giving the name of a numeric predictor in the model matrix for the horizontal
38  axis.  To plot against a factor, you need to specify the full name of one of the indicator variables that define the factor.  For example, for a factor called \code{type} with levels \code{A}, \code{B} and {C}, using the usual drop-first level parameterization of the factor, the regressors for \code{type} would be \code{typeB} or \code{typeC}.  Similarly, to plot against the regressor \code{log(X4)}, you must specify \code{"log((X4)"}, not \code{"X4"}.
39}
40  \item{layout}{
41If set to a value like \code{c(1, 2)} or \code{c(6, 2)}, the layout
42of the graph will have this many rows and columns.  If not set, behavior depends on the value of the \code{overlaid} argument; see the details
43}
44\item{ask}{If \code{TRUE}, ask the user before drawing the next plot; if \code{FALSE} don't ask.
45}
46\item{\dots}{\code{mcPlots} passes these arguments to \code{mcPlot}.
47  \code{mcPlot} passes arguments to \code{plot}.
48}
49\item{id}{controls point identification; if \code{FALSE} (the default), no points are identified;
50    can be a list of named arguments to the \code{\link{showLabels}} function;
51    \code{TRUE} is equivalent to \code{list(method=list(abs(residuals(model, type="pearson")), "x"), n=2, cex=1, col=carPalette()[1], location="lr")},
52    which identifies the 2 points with the largest residuals and the 2 points with the most extreme
53    horizontal (X) values.}
54  \item{overlaid}{If TRUE, the default, overlay the marginal and conditional plots on the same graph; otherwise
55  plot them side-by-side.  See the details below}
56  \item{col.marginal, col.conditional}{colors for points, lines,
57  ellipses in the marginal and conditional plots, respectively.  The defaults are determined by the \code{\link{carPalette}} function.}
58  \item{col.arrows}{color for the arrows with \code{overlaid=TRUE}}
59  \item{pch}{Plotting character for marginal and conditional plots, respectively.}
60  \item{cex}{size of plotted points; default is taken from \code{par("cex")}.}
61  \item{pt.wts}{if \code{TRUE} (the default is \code{FALSE}), the areas of
62  plotted points for a weighted least squares fit
63  are made proportional to the weights, with the average size taken from the
64  \code{cex} argument.}
65  \item{lwd}{line width; default is \code{2} (see \code{\link{par}}).}
66  \item{grid}{If \code{TRUE}, the default, a light-gray background grid is put on the graph.}
67  \item{ellipse}{Arguments to pass to the \code{\link{dataEllipse}} function, in the form of a list
68    with named elements; e.g., \code{ellipse.args=list(robust=TRUE))} will cause the ellipse to be plotted using
69    a robust covariance-matrix. if \code{FALSE}, the default, no ellipse is plotted. \code{TRUE} is equivalent to
70    \code{ellipse=list(levels=0.5)}, which plots a bivariate-normal 50 percent concentration ellipse.}
71  \item{new}{if \code{TRUE}, the default, the plot window is reset when \code{overlaid=FALSE} using \code{par{mfrow=c(1, 2)}}.  If \code{FALSE}, the layout of the plot window is not reset.
72  Users will ordinarily ignore this argument.}
73  \item{title}{If TRUE, the default, the standard main argument in plot is used to add a standard title to each plot.  If FALSE no title is used.}
74}
75
76\details{
77With an \code{lm} object, suppose the response is Y, X is a numeric regressor of interest, and Z is all the remaining predictors, possibly including interactions and factors.  This function produces two graphs.  The first graph is the marginal plot of Y versus X, with each variable centered around its mean.  The second conditional plot is the added-variable plot of e(Y|Z) versus e(X|Z) where e(a|b) means the Pearson residuals from the regression of a on b.  If \code{overlaid=TRUE}, these two plots are overlaid in one graph, with the points in different colors.  In addition, each point in the marginal plot is joined to its value in the conditional plot by an arrow.  Least squares regression lines fit to the marginal and conditional graphs are also shown; data ellipsoids can also be added.  If \code{overlaid=FALSE}, then the two graphs are shown in side-by-side plots as long as the second argument to \code{layout} is equal to \code{2}, or \code{layout} is set by the function. The arrows are omitted if the graphs are not overlaid.
78
79These graphs are primarily for teaching, as the marginal plot shows the relationship between Y and X ignoring Z, while the conditional is the relationship between Y and X given X.  By keeping the scales the same in both graphs the effect of conditioning on both X and Y can be visualized.
80
81This function is intended for first-order models with numeric predictors only.  For a factor,  one (pair) of mcPlots will be produced for each of the dummy variables in the basis for the factor, and the resulting plots are not generally meaningful because they depend on parameterization.  If the mean function includes interactions, then mcPlots for main effects may violate the hierarchy principle, and may also be of little interest.  mcPlots for interactions of numerical predictors, however, can be useful.
82
83These graphs are closely related to the ARES plots proposed by Cook and Weisberg (1989).
84This plot would benefit from animation.
85}
86
87\value{
88  These functions are used for their side effect of producing
89  plots.
90}
91
92\references{
93  Cook, R. D. and Weisberg, S. (1989)
94  \emph{Regression diagnostics with dynamic graphics,} Technometrics, 31, 277.
95
96  Fox, J. (2016)
97  \emph{Applied Regression Analysis and Generalized Linear Models},
98  Third Edition. Sage.
99
100  Fox, J. and Weisberg, S. (2019)
101  \emph{An R Companion to Applied Regression}, Third Edition, Sage.
102
103  Weisberg, S. (2014) \emph{Applied Linear Regression}, Fourth Edition, Wiley.
104}
105
106\author{John Fox \email{jfox@mcmaster.ca}, Sanford Weisberg \email{sandy@umn.edu}}
107
108\seealso{\code{\link{avPlots}}, \code{\link{residualPlots}}, \code{\link{crPlots}}, \code{\link{ceresPlots}},
109\code{\link{dataEllipse}}
110}
111
112\examples{
113m1 <- lm(partic ~ tfr + menwage + womwage + debt + parttime, data = Bfox)
114mcPlot(m1, "womwage")
115mcPlot(m1, "womwage", overlaid=FALSE, ellipse=TRUE)
116}
117
118\keyword{hplot}
119\keyword{regression}
120