Learn R Programming

car (version 2.1-6)

mmps: Marginal Model Plotting

Description

For a regression object, draw a plot of the response on the vertical axis versus a linear combination \(u\) of regressors in the mean function on the horizontal axis. Added to the plot are a smooth for the graph, along with a smooth from the plot of the fitted values on \(u\). mmps is an alias for marginalModelPlots, and mmp is an alias for marginalModelPlot.

Usage

marginalModelPlots(...)

mmps(model, terms= ~ ., fitted=TRUE, layout=NULL, ask, main, groups, key=TRUE, ...)

marginalModelPlot(...)

# S3 method for lm mmp(model, variable, sd = FALSE, xlab = deparse(substitute(variable)), smoother = loessLine, smoother.args=list(span=2/3), key=TRUE, pch, groups=NULL, ...) # S3 method for default mmp(model, variable, sd = FALSE, xlab = deparse(substitute(variable)), smoother=loessLine, smoother.args, key=TRUE, pch, groups=NULL, col.line = palette()[c(4, 2)], col=palette()[1], labels, id.method="y", id.n=if(id.method[1]=="identify") Inf else 0, id.cex=1, id.col=palette()[1], id.location="lr", grid=TRUE, ...) # S3 method for glm mmp(model, variable, sd = FALSE, xlab = deparse(substitute(variable)), smoother=gamLine, smoother.args=list(k=3), key=TRUE, pch, groups=NULL, col.line = palette()[c(4, 2)], col=palette()[1], labels, id.method="y", id.n=if(id.method[1]=="identify") Inf else 0, id.cex=1, id.col=palette()[1], id.location="lr", grid=TRUE, ...)

Arguments

model

A regression object, usually of class either lm or glm, for which there is a predict method defined.

terms

A one-sided formula. A marginal model plot will be drawn for each term on the right-side of this formula that is not a factor. The default is ~ ., which specifies that all the terms in formula(object) will be used. If a conditioning argument is given, eg terms = ~. | a, then separate colors and smoothers are used for each unique non-missing value of a. See examples below.

fitted

If the default TRUE, then a marginal model plot in the direction of the fitted values or linear predictor of a generalized linear model will be drawn.

layout

If set to a value like c(1, 1) or c(4, 3), the layout of the graph will have this many rows and columns. If not set, the program will select an appropriate layout. If the number of graphs exceed nine, you must select the layout yourself, or you will get a maximum of nine per page. If layout=NA, the function does not set the layout and the user can use the par function to control the layout, for example to have plots from two models in the same graphics window.

ask

If TRUE, ask before clearing the graph window to draw more plots.

main

Main title for the array of plots. Use main="" to suppress the title; if missing, a title will be supplied.

Additional arguments passed from mmps to mmp and then to plot. Users should generally use mmps, or equivalently marginalModelPlots.

variable

The quantity to be plotted on the horizontal axis. The default is the predicted values predict(object). It can be any other vector of length equal to the number of observations in the object. Thus the mmp function can be used to get a marginal model plot versus any regressor or predictor while the mmps function can be used only to get marginal model plots for the first-order regressors in the formula. In particular, terms defined by a spline basis are skipped by mmps, but you can use mmp to get the plot for the variable used to define the splines.

sd

If TRUE, display sd smooths. For a binomial regression with all sample sizes equal to one, this argument is ignored as the SD bounds don't make any sense.

xlab

label for horizontal axis

smoother

the name of the smoother to use, selected from the choices described at ScatterplotSmoothers. For linear models and the default method, the default smoother is the function is the function loessLine. For generalized linear models the default is gamLine, using the gam package and using splines.

smoother.args

arguments passed to the smoother. For linear models the defaults match the smoother used before September 2012, and may be changed later. See ScatterplotSmoothers.

groups

The name of a vector that specifies a grouping variable for separate colors/smoothers. This can also be specified as a conditioning argument on the terms argument.

key

If TRUE, include a key at the top of the plot, if FALSE omit the key. If grouping is present, the key is only printed for the upper-left plot.

id.method,labels,id.n,id.cex,id.col,id.location

Arguments for labeling points. The default id.n=0 suppresses labeling, and setting this argument greater than zero will include labeling. See showLabels for these arguments.

pch

plotting character to use if no grouping is present.

col.line

colors for data and model smooth, respectively. Using the default palette, these are blue and red.

col

color(s) for the plotted points.

grid

If TRUE, the default, a light-gray background grid is put on the graph

Value

Used for its side effect of producing plots.

Details

mmp and marginalModelPlot draw one marginal model plot against whatever is specified as the horizontal axis. mmps and marginalModelPlots draws marginal model plots versus each of the terms in the terms argument and versus fitted values. mmps skips factors and interactions if they are specified in the terms argument. Terms based on polynomials or on splines (or potentially any term that is represented by a matrix of regressors) will be used to form a marginal model plot by returning a linear combination of the terms. For example, if you specify terms = ~ X1 + poly(X2, 3) and poly(X2, 3) was part of the original model formula, the horizontal axis of the marginal model plot for X2 will be the value of predict(model, type="terms")[, "poly(X2, 3)"]). If the predict method for the model you are using doesn't support type="terms", then the polynomial/spline term is skipped. Adding a conditioning variable, e.g., terms = ~ a + b | c, will produce marginal model plots for a and b with different colors and smoothers for each unique non-missing value of c.

The smoothers used were changed in September 2012. For linear models, the default smoother is still loess with the same smoothing parameters as were used in the past, but these can be changed with the argument smoother.args. For generalized linear models, the default smoother uses gamLine, fitting a generalized additive model with the same family, link and weights as the fit of the model. SD smooths are not computed for for generalized linear models.

For generalized linear models the default number of elements in the spline basis is k=3; this is done to allow fitting for predictors with just a few support points. If you have many support points you may wish to set k to a higher number, or k=-1 for the default used by gam.

References

Cook, R. D., & Weisberg, S. (1997). Graphics for assessing the adequacy of regression models. Journal of the American Statistical Association, 92(438), 490-499.

Fox, J. and Weisberg, S. (2011) An R Companion to Applied Regression, Second Edition. Sage.

Weisberg, S. (2005) Applied Linear Regression, Third Edition, Wiley, Section 8.4.

See Also

ScatterplotSmoothers, plot

Examples

Run this code
# NOT RUN {
c1 <- lm(infant.mortality ~ gdp, UN)
mmps(c1)
c2 <- update(c1, ~ poly(gdp, 4), data=na.omit(UN))
# plot against predict(c2, type="terms")[, "poly(gdp, 4)"] and
# and against gdp
mmps(c2, ~ poly(gdp,4) + gdp)
# include SD lines
p1 <- lm(prestige ~ income + education, Prestige)
mmps(p1, sd=TRUE) 
# condition on type:
mmps(p1, ~. | type)
# logisitic regression example
# smoothers return warning messages.
# fit a separate smoother and color for each type of occupation.

m1 <- glm(lfp ~ ., family=binomial, data=Mroz)
mmps(m1)
# }

Run the code above in your browser using DataLab