Car PDF
Car PDF
Car PDF
URL https://r-forge.r-project.org/projects/car/,
https://CRAN.R-project.org/package=car,
https://socialsciences.mcmaster.ca/jfox/Books/Companion/index.html
VignetteBuilder knitr
Author John Fox [aut, cre],
Sanford Weisberg [aut],
Brad Price [aut],
Daniel Adler [ctb],
Douglas Bates [ctb],
Gabriel Baud-Bovy [ctb],
Ben Bolker [ctb],
Steve Ellison [ctb],
David Firth [ctb],
Michael Friendly [ctb],
Gregor Gorjanc [ctb],
Spencer Graves [ctb],
Richard Heiberger [ctb],
1
2 R topics documented:
R topics documented:
Anova . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
avPlots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
bcPower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Boot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
boxCox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
boxCoxVariable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Boxplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
boxTidwell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
brief . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
car-defunct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
car-deprecated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
car-internal.Rd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
carHexsticker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
carPalette . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
carWeb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
ceresPlots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
compareCoefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Contrasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
crPlots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
deltaMethod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
densityPlot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
dfbetaPlots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
durbinWatsonTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
R topics documented: 3
Ellipses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Export . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
hccm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
hist.boot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
infIndexPlot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
influence.mixed.models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
influencePlot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
invResPlot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
invTranPlot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
leveneTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
leveragePlots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
linearHypothesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
logit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
mcPlots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
mmps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
ncvTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
outlierTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
panel.car . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
poTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
powerTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Predict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
qqPlot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
recode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
regLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
residualPlots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
S. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
scatter3d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
scatterplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
scatterplotMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
ScatterplotSmoothers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
showLabels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
sigmaHat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
some . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
spreadLevelPlot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
strings2factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
symbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Tapply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
testTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
TransformationAxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
vif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
wcrossprod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
whichNames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Index 147
4 Anova
Description
Calculates type-II or type-III analysis-of-variance tables for model objects produced by lm, glm,
multinom (in the nnet package), polr (in the MASS package), coxph (in the survival package),
coxme (in the coxme pckage), svyglm (in the survey package), rlm (in the MASS package), lmer
in the lme4 package, lme in the nlme package, and (by the default method) for most models with
a linear predictor and asymptotically normal coefficients (see details below). For linear models,
F-tests are calculated; for generalized linear models, likelihood-ratio chisquare, Wald chisquare,
or F-tests are calculated; for multinomial logit and proportional-odds logit models, likelihood-ratio
tests are calculated. Various test statistics are provided for multivariate linear models produced
by lm or manova. Partial-likelihood-ratio tests or Wald tests are provided for Cox models. Wald
chi-square tests are provided for fixed effects in linear and generalized linear mixed-effects models.
Wald chi-square or F tests are provided in the default case.
Usage
Anova(mod, ...)
Manova(mod, ...)
Anova(mod, ...)
## Default S3 method:
Anova(mod, type=c("II", "III", 2, 3),
test.statistic=c("Chisq", "F"), vcov.=vcov(mod, complete=FALSE),
singular.ok, ...)
Arguments
mod lm, aov, glm, multinom, polr mlm, coxph, coxme, lme, mer, merMod, svyglm,
rlm, or other suitable model object.
error for a linear model, an lm model object from which the error sum of squares
and degrees of freedom are to be calculated. For F-tests for a generalized lin-
ear model, a glm object from which the dispersion is to be estimated. If not
specified, mod is used.
type type of test, "II", "III", 2, or 3. Roman numerals are equivalent to the corre-
sponding Arabic numerals.
singular.ok defaults to TRUE for type-II tests, and FALSE for type-III tests where the tests for
models with aliased coefficients will not be straightforwardly interpretable; if
FALSE, a model with aliased coefficients produces an error.
test.statistic for a generalized linear model, whether to calculate "LR" (likelihood-ratio),
"Wald", or "F" tests; for a Cox or Cox mixed-effects model, whether to calculate
"LR" (partial-likelihood ratio) or "Wald" tests; in the default case or for linear
mixed models fit by lmer, whether to calculate Wald "Chisq" or Kenward-
Roger "F" tests with Satterthwaite degrees of freedom (warning: the KR F-tests
can be very time-consuming). For a multivariate linear model, the multivariate
test statistic to compute — one of "Pillai", "Wilks", "Hotelling-Lawley",
or "Roy", with "Pillai" as the default. The summary method for Anova.mlm
objects permits the specification of more than one multivariate test statistic, and
the default is to report all four.
error.estimate for F-tests for a generalized linear model, base the dispersion estimate on the
Pearson residuals ("pearson", the default); use the dispersion estimate in the
model object ("dispersion"); or base the dispersion estimate on the residual
deviance ("deviance"). For binomial or Poisson GLMs, where the dispersion
is fixed to 1, setting error.estimate="dispersion" is changed to "pearson",
with a warning.
white.adjust if not FALSE, the default, tests use a heteroscedasticity-corrected coefficient co-
variance matrix; the various values of the argument specify different corrections.
See the documentation for hccm for details. If white.adjust=TRUE then the
"hc3" correction is selected.
SSPE For Anova for a multivariate linear model, the error sum-of-squares-and-products
matrix; if missing, will be computed from the residuals of the model; for the
print method for the summary of an Anova of a multivariate linear model,
whether or not to print the error SSP matrix (defaults to TRUE).
Anova 7
SSP if TRUE (the default), print the sum-of-squares and cross-products matrix for the
hypothesis and the response-transformation matrix.
error.df The degrees of freedom for error; if missing, will be taken from the model.
idata an optional data frame giving a factor or factors defining the intra-subject model
for multivariate repeated-measures data. See Details for an explanation of the
intra-subject design and for further explanation of the other arguments relating
to intra-subject factors.
idesign a one-sided model formula using the “data” in idata and specifying the intra-
subject design.
icontrasts names of contrast-generating functions to be applied by default to factors and
ordered factors, respectively, in the within-subject “data”; the contrasts must
produce an intra-subject model matrix in which different terms are orthogonal.
The default is c("contr.sum","contr.poly").
imatrix as an alternative to specifying idata, idesign, and (optionally) icontrasts,
the model matrix for the within-subject design can be given directly in the form
of list of named elements. Each element gives the columns of the within-subject
model matrix for a term to be tested, and must have as many rows as there are
responses; the columns of the within-subject model matrix for different terms
must be mutually orthogonal.
x, object object of class "Anova.mlm" to print or summarize.
multivariate, univariate
compute and print multivariate and univariate tests for a repeated-measures ANOVA
or multivariate linear model; the default is TRUE for both for repeated measures
and TRUE for multivariate for a multivariate linear model.
p.adjust.method
if given for a multivariate linear model when univariate tests are requested, the
univariate tests are corrected for simultaneous inference by term; if specified,
should be one of the methods recognized by p.adjust or TRUE, in which case
the default (Holm) adjustment is used.
digits minimum number of significant digits to print.
style for printing univariate tests if requested for a multivariate linear model; one of
"wide", the default, or "long".
by if univariate tests are printed in "long" style, they can be ordered by "response",
the default, or by "term".
row.names, optional
not used.
vcov. in the default method, an optional coefficient-covariance matrix or function
to compute a covariance matrix, computed by default by applying the generic
vcov function to the model object. A similar argument may be supplied to the
lm method, and the default (NULL) is to ignore the argument; if both vcov. and
white.adjust are supplied to the lm method, the latter is used.
... do not use.
8 Anova
Details
The designations "type-II" and "type-III" are borrowed from SAS, but the definitions used here do
not correspond precisely to those employed by SAS. Type-II tests are calculated according to the
principle of marginality, testing each term after all others, except ignoring the term’s higher-order
relatives; so-called type-III tests violate marginality, testing each term in the model after all of the
others. This definition of Type-II tests corresponds to the tests produced by SAS for analysis-of-
variance models, where all of the predictors are factors, but not more generally (i.e., when there
are quantitative predictors). Be very careful in formulating the model for type-III tests, or the
hypotheses tested will not make sense.
As implemented here, type-II Wald tests are a generalization of the linear hypotheses used to gen-
erate these tests in linear models.
For tests for linear models, multivariate linear models, and Wald tests for generalized linear models,
Cox models, mixed-effects models, generalized linear models fit to survey data, and in the default
case, Anova finds the test statistics without refitting the model. The svyglm method simply calls the
default method and therefore can take the same arguments.
The standard R anova function calculates sequential ("type-I") tests. These rarely test interesting
hypotheses in unbalanced designs.
A MANOVA for a multivariate linear model (i.e., an object of class "mlm" or "manova") can op-
tionally include an intra-subject repeated-measures design. If the intra-subject design is absent (the
default), the multivariate tests concern all of the response variables. To specify a repeated-measures
design, a data frame is provided defining the repeated-measures factor or factors via idata, with
default contrasts given by the icontrasts argument. An intra-subject model-matrix is generated
from the formula specified by the idesign argument; columns of the model matrix corresponding to
different terms in the intra-subject model must be orthogonal (as is insured by the default contrasts).
Note that the contrasts given in icontrasts can be overridden by assigning specific contrasts to the
factors in idata. As an alternative, the within-subjects model matrix can be specified directly via
the imatrix argument. Manova is essentially a synonym for Anova for multivariate linear models.
If univariate tests are requested for the summary of a multivariate linear model, the object returned
contains a univaov component of "univaov"; print and as.data.frame methods are provided
for the "univaov" class.
For the default method to work, the model object must contain a standard terms element, and must
respond to the vcov, coef, and model.matrix functions. If any of these requirements is missing,
then it may be possible to supply it reasonably simply (e.g., by writing a missing vcov method for
the class of the model object).
Value
An object of class "anova", or "Anova.mlm", which usually is printed. For objects of class
"Anova.mlm", there is also a summary method, which provides much more detail than the print
method about the MANOVA, including traditional mixed-model univariate F-tests with Greenhouse-
Geisser and Huynh-Feldt corrections.
Warning
Be careful of type-III tests: For a traditional multifactor ANOVA model with interactions, for ex-
ample, these tests will normally only be sensible when using contrasts that, for different terms, are
Anova 9
orthogonal in the row-basis of the model, such as those produced by contr.sum, contr.poly, or
contr.helmert, but not by the default contr.treatment. In a model that contains factors, numeric
covariates, and interactions, main-effect tests for factors will be for differences over the origin. In
contrast (pun intended), type-II tests are invariant with respect to (full-rank) contrast coding. If you
don’t understand this issue, then you probably shouldn’t use Anova for type-III tests.
Author(s)
John Fox <[email protected]>; the code for the Mauchly test and Greenhouse-Geisser and Huynh-
Feldt corrections for non-spericity in repeated-measures ANOVA are adapted from the functions
stats:::stats:::mauchly.test.SSD and stats:::sphericity by R Core; summary.Anova.mlm
and print.summary.Anova.mlm incorporates code contributed by Gabriel Baud-Bovy.
References
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Hand, D. J., and Taylor, C. C. (1987) Multivariate Analysis of Variance and Repeated Measures: A
Practical Approach for Behavioural Scientists. Chapman and Hall.
O’Brien, R. G., and Kaiser, M. K. (1985) MANOVA method for analyzing repeated measures de-
signs: An extensive primer. Psychological Bulletin 97, 316–333.
See Also
linearHypothesis, anova anova.lm, anova.glm, anova.mlm, anova.coxph, svyglm.
Examples
## Two-Way Anova
## One-Way MANOVA
## See ?Pottery for a description of the data set used in this example.
summary(av.ok, multivariate=FALSE)
## Not run:
library(nlme)
example(lme)
Anova(fm2)
## End(Not run)
## Not run:
library(lme4)
example(glmer)
Anova(gm1)
## End(Not run)
avPlots 11
Description
These functions construct added-variable, also called partial-regression, plots for linear and gener-
alized linear models.
Usage
avPlots(model, ...)
## Default S3 method:
avPlots(model, terms=~., intercept=FALSE,
layout=NULL, ask, main, ...)
avp(...)
avPlot(model, ...)
Arguments
model model object produced by lm or glm.
terms A one-sided formula that specifies a subset of the predictors. One added-variable
plot is drawn for each term. For example, the specification terms = ~.-X3 would
plot against all terms except for X3. If this argument is a quoted name of one of
the terms, the added-variable plot is drawn for that term only.
intercept Include the intercept in the plots; default is FALSE.
variable A quoted string giving the name of a regressor in the model matrix for the hori-
zontal axis.
12 avPlots
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.
main The title of the plot; if missing, one will be supplied.
ask If TRUE, ask the user before drawing the next plot; if FALSE don’t ask.
... avPlots passes these arguments to avPlot. avPlot passes them to plot.
id controls point identification; if FALSE, no points are identified; can be a list of
named arguments to the showLabels function; TRUE, the default, is equivalent to
list(method=list(abs(residuals(model,type="pearson")),"x"),n=2,cex=1,col=carPalette(
which identifies the 2 points with the largest residuals and the 2 points with the
most extreme horizontal values (i.e., largest partial leverage).
col color for points; the default is the second entry in the current car palette (see
carPalette and par).
col.lines color for the fitted line.
pch plotting character for points; default is 1 (a circle, see par).
lwd line width; default is 2 (see par).
xlab x-axis label. If omitted a label will be constructed.
ylab y-axis label. If omitted a label will be constructed.
type if "Wang" use the method of Wang (1985); if "Weisberg" use the method in the
Arc software associated with Cook and Weisberg (1999).
grid If TRUE, the default, a light-gray background grid is put on the graph.
ellipse controls plotting data-concentration ellipses. If FALSE (the default), no ellipses
are plotted. Can be a list of named values giving levels, a vector of one or
more bivariate-normal probability-contour levels at which to plot the ellipses;
and robust, a logical value determing whether to use the cov.trob function in
the MASS package to calculate the center and covariance matrix for the data
ellipses. TRUE is equivalent to list(levels=c(.5,.95),robust=TRUE).
marginal.scale Consider an added-variable plot of Y versus X given Z. If this argument is FALSE
then the limits on the horizontal axis are determined by the range of the residuals
from the regression of X on Z and the limits on the vertical axis are determined
by the range of the residuals from the regressnio of Y on Z. If the argument
is TRUE, then the limits on the horizontal axis are determined by the range of
X minus it mean, and on the vertical axis by the range of Y minus its means;
adjustment is made if necessary to include outliers. This scaling allows visual-
ization of the correlations between Y and Z and between X and Z. For example,
if the X and Z are highly correlated, then the points will be concentrated on the
middle of the plot.
Details
The function intended for direct use is avPlots (for which avp is an abbreviation).
bcPower 13
Value
These functions are used for their side effect id producing plots, but also invisibly return the coor-
dinates of the plotted points.
Author(s)
John Fox <[email protected]>, Sanford Weisberg <[email protected]>
References
Cook, R. D. and Weisberg, S. (1999) Applied Regression, Including Computing and Graphics.
Wiley.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Wang, P C. (1985) Adding a variable in generalized linear models. Technometrics 27, 273–276.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley.
See Also
residualPlots, crPlots, ceresPlots, link{dataEllipse}, showLabels, dataEllipse.
Examples
avPlots(lm(prestige ~ income + education + type, data=Duncan))
Description
Transform the elements of a vector or columns of a matrix using, the Box-Cox, Box-Cox with
negatives allowed, Yeo-Johnson, or simple power transformations.
14 bcPower
Usage
bcPower(U, lambda, jacobian.adjusted=FALSE, gamma=NULL)
basicPower(U,lambda, gamma=NULL)
Arguments
U A vector, matrix or data.frame of values to be transformed
lambda Power transformation parameter with one element for each column of U, usual-
lly in the range from −2 to 2.
jacobian.adjusted
If TRUE, the transformation is normalized to have Jacobian equal to one. The
default FALSE is almost always appropriate.
gamma For bcPower or basicPower, the transformation is of U + gamma, where gamma
is a positive number called a start that must be large enough so that U + gamma
is strictly positive. For the bcnPower, Box-cox power with negatives allowed,
see the details below.
z a numeric vector the result of a call to bcnPower with jacobian.adjusted=FALSE.
Details
The Box-Cox family of scaled power transformations equals (xλ − 1)/λ for λ 6= 0, and log(x) if
λ = 0. The bcPower function computes the scaled power transformation of x = U + γ, where γ is
set by the user so U + γ is strictly positive for these transformations to make sense.
The Box-Cox family with negatives allowed was proposed by Hawkins and Weisberg (2017). It is
the Box-Cox power transformation of
p
z = .5(U + U 2 + γ 2 ))
where for this family γ is either user selected or is estimated. gamma must be positive if U includes
negative values and non-negative otherwise, ensuring that z is always positive. The bcnPower
transformations behave similarly to the bcPower transformations, and introduce less bias than is
introduced by setting the parameter γ to be non-zero in the Box-Cox family.
The function bcnPowerInverse computes the inverse of the bcnPower function, so U = bcnPowerInverse(bcnPower(U,lamb
is true for any permitted value of gam and lam.
If family="yeo.johnson" then the Yeo-Johnson transformations are used. This is the Box-Cox
transformation of U + 1 for nonnegative values, and of |U | + 1 with parameter 2 − λ for U negative.
The basic power transformation returns U λ if λ is not 0, and log(λ) otherwise for U strictly positive.
If jacobian.adjusted is TRUE, then the scaled transformations are divided by the Jacobian, which
is a function of the geometric mean of U for skewPower and yjPower and of U + gamma for
Boot 15
bcPower. With this adjustment, the Jacobian of the transformation is always equal to 1. Jacobian
adjustment facilitates computing the Box-Cox estimates of the transformation parameters.
Missing values are permitted, and return NA where ever U is equal to NA.
Value
Returns a vector or matrix of transformed values.
Author(s)
Sanford Weisberg, <[email protected]>
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Hawkins, D. and Weisberg, S. (2017) Combining the Box-Cox Power and Generalized Log Trans-
formations to Accomodate Nonpositive Responses In Linear and Mixed-Effects Linear Models
South African Statistics Journal, 51, 317-328.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley Wiley, Chapter 7.
Yeo, In-Kwon and Johnson, Richard (2000) A new family of power transformations to improve
normality or symmetry. Biometrika, 87, 954-959.
See Also
powerTransform, testTransform
Examples
U <- c(NA, (-3:3))
## Not run: bcPower(U, 0) # produces an error as U has negative values
bcPower(U, 0, gamma=4)
bcPower(U, .5, jacobian.adjusted=TRUE, gamma=4)
bcnPower(U, 0, gamma=2)
basicPower(U, lambda = 0, gamma=4)
yjPower(U, 0)
V <- matrix(1:10, ncol=2)
bcPower(V, c(0, 2))
basicPower(V, c(0,1))
Description
This function provides a simple front-end to the boot function in the boot package that is tailored
to bootstrapping based on regression models. Whereas boot is very general and therefore has many
arguments, the Boot function has very few arguments.
16 Boot
Usage
Boot(object, f=coef, labels=names(f(object)), R=999,
method=c("case", "residual"), ncores=1, ...)
## Default S3 method:
Boot(object, f=coef, labels=names(f(object)),
R=999, method=c("case", "residual"), ncores=1,
start = FALSE, ...)
Arguments
object A regression object of class "lm", "glm" or "nls". The function may work with
other regression objects that support the update method and have a subset
argument. See discussion of the default method in the details below.
f A function whose one argument is the name of a regression object that will be
applied to the updated regression object to compute the statistics of interest.
The default is coef, to return regression coefficient estimates. For example, f
= function(obj) coef(obj)[1]/coef(obj)[2] will bootstrap the ratio of the
first and second coefficient estimates.
labels Provides labels for the statistics computed by f. Default labels are obtained from
a call to f, or generic labels if f does not return names.
R Number of bootstrap samples. The number of bootstrap samples actually com-
puted may be smaller than this value if either the fitting method is iterative and
fails to converge for some boothstrap samples, or if the rank of a fitted model is
different in a bootstrap replication than in the original data.
method The bootstrap method, either “case” for resampling cases or “residuals” for a
residual bootstrap. See the details below. The residual bootstrap is available
only for lm and nls objects and will return an error for glm objects.
... Arguments passed to the boot function, see boot.
start Should the estimates returned by f be passed as starting values for each bootstrap
iteration? Alternatively, start can be a numeric vector of starting values. The
default is to use the estimates from the last bootstrap iteration as starting values
for the next iteration.
Boot 17
ncores A numeric argument that specifies the number of cores for parallel processing
for unix systems. If less than or equal to 1, no parallel processing wiill be used.
Note in a Windows platform will produce a warning and set this argument to 1.
Details
Boot uses a regression object and the choice of method, and creates a function that is passed as
the statistic argument to the boot function in the boot package. The argument R is also passed
to boot. If ncores is greater than 1, then the parallel and ncpus arguments to boot are set
appropriately to use multiple codes, if available, on your computer. All other arguments to boot are
kept at their default values unless you pass values for them.
The methods available for lm and nls objects are “case” and “residual”. The case bootstrap resam-
ples from the joint distribution of the terms in the model and the response. The residual bootstrap
fixes the fitted values from the original data, and creates bootstraps by adding a bootstrap sample of
the residuals to the fitted values to get a bootstrap response. It is an implementation of Algorithm
6.3, page 271, of Davison and Hinkley (1997). For nls objects ordinary residuals are used in the
resampling rather than the standardized residuals used in the lm method. The residual bootstrap for
generalized linear models has several competing approaches, but none are without problems. If you
want to do a residual bootstrap for a glm, you will need to write your own call to boot.
For the default object to work with other types of regression models, the model must have methods
for the the following generic functions: residuals(object,type="pearson") must return Pear-
son residuals; fitted(object) must return fitted values; hatvalues(object) should return the
leverages, or perhaps the value 1 which will effectively ignore setting the hatvalues. In addition,
the data argument should contain no missing values among the columns actually used in fitting the
model, as the resampling may incorrectly attempt to include cases with missing values. For lm, glm
and nls, missing values cause the return of an error message.
An attempt to fit using a bootstrap sample may fail. In a lm or glm fit, the bootstrap sample could
have a different rank from the original fit. In an nls fit, convergence may not be obtained for some
bootstraps. In either case, NA are returned for the value of the function f. The summary methods
handle the NAs appropriately.
Fox and Weisberg (2017) cited below discusses this function and provides more examples.
Value
See boot for the returned value of the structure returned by this function.
Author(s)
Sanford Weisberg, <[email protected]>. Achim Zeileis added multicore support, and also fixed the
default method to work for many more regression models.
References
Davison, A, and Hinkley, D. (1997) Bootstrap Methods and their Applications. Oxford: Oxford
University Press.
Fox, J. and Weisberg, S. (2019) Companion to Applied Regression, Third Edition. Thousand Oaks:
Sage.
18 boxCox
See Also
Functions that work with boot objects from the boot package are boot.array, boot.ci, plot.boot
and empinf. Additional functions in the car package are summary.boot, confint.boot, and
hist.boot.
Examples
m1 <- lm(Fertility ~ ., swiss)
betahat.boot <- Boot(m1, R=199) # 199 bootstrap samples--too small to be useful
summary(betahat.boot) # default summary
confint(betahat.boot)
hist(betahat.boot)
# Bootstrap for the estimated residual standard deviation:
sigmahat.boot <- Boot(m1, R=199, f=sigmaHat, labels="sigmaHat")
summary(sigmahat.boot)
confint(sigmahat.boot)
Description
Computes and optionally plots profile log-likelihoods for the parameter of the Box-Cox power
family, the Yeo-Johnson power family, or for either of the parameters in a bcnPower family. This
is a slight generalization of the boxcox function in the MASS package that allows for families of
transformations other than the Box-Cox power family. the boxCox2d function produces a contour
plot of the two-dimensional likelihood profile for the bcnPower family.
Usage
boxCox(object, ...)
## Default S3 method:
boxCox(object,
lambda = seq(-2, 2, 1/10), plotit = TRUE,
interp = plotit, eps = 1/50,
xlab=NULL, ylab=NULL,
family="bcPower",
param=c("lambda", "gamma"), gamma=NULL,
grid=TRUE, ...)
Arguments
object a formula or fitted model object of class lm or aov.
lambda vector of values of λ, with default (-2, 2) in steps of 0.1, where the profile log-
likelihood will be evaluated.
plotit logical which controls whether the result should be plotted; default TRUE.
interp logical which controls whether spline interpolation is used. Default to TRUE if
plotting with lambda of length less than 100.
eps Tolerance for lambda = 0; defaults to 0.02.
xlab defaults to "lambda" or "gamma".
ylab defaults to "log-Likelihood" or for bcnPower family to the appropriate label.
family Defaults to "bcPower" for the Box-Cox power family of transformations. If
set to "yjPower" the Yeo-Johnson family, which permits negative responses, is
used. If set to bcnPower the function gives the profile log-likelihood for the
parameter selected via param.
param Relevant only to family="bcnPower", produces a profile log-likelihood for the
parameter selected, maximizing over the remaining parameter.
gamma For use when the family="bcnPower",param="gamma". If this is a vector of
positive values, then the profile log-likelihood for the location (or start) pa-
rameter in the bcnPower family is evaluated at these values of gamma. If
gamma is NULL, then evaulation is done at 100 equally spaced points between
min(.01,gmax -3*sd) and gmax + 3*sd, where gmax is the maximimum likeli-
hood estimate of gamma, and sd is the sd of the response. See bcnPower for the
definition of gamma.
grid If TRUE, the default, a light-gray background grid is put on the graph.
... additional arguments passed to the lm method with boxCox.formula or passed
to contour with boxCox2d.
x An object created by a call to powerTransform using family="bcnPower".
ksds Contour plotting of the log-likelihood surface will cover plus of minus ksds
standard deviations on each axis.
levels Contours will be drawn at the values of levels. For example, levels=c(.5,.99)
would display two contours, at the 50% level and at the 99% level.
main Title for the contour plot
20 boxCox
Details
The boxCox function is an elaboration of the boxcox function in the MASS package. The first
7 arguments are the same as in boxcox, and if the argument family="bcPower" is used, the re-
sult is essentially identical to the function in MASS. Two additional families are the yjPower and
bcnPower families that allow a few values of the response to be non-positive. The bcnPower family
has two parameters: a power λ and a start or location parameter γ, and the boxCox function can be
used to obtain a profile log-likelihood for either parameter with λ as the default. Alternatively, the
boxCox2d function can be used to get a contour plot of the profile log-likelihood.
Value
Both functions ae designed for their side effects of drawing a graph. The boxCox function returns
a list of the lambda (or possibly, gamma) vector and the computed profile log-likelihood vector,
invisibly if the result is plotted. If plotit=TRUE plots log-likelihood vs lambda and indicates a
95% confidence interval about the maximum observed value of lambda. If interp=TRUE, spline
interpolation is used to give a smoother plot.
Author(s)
Sanford Weisberg, <[email protected]>
References
Box, G. E. P. and Cox, D. R. (1964) An analysis of transformations. Journal of the Royal Statisisti-
cal Society, Series B. 26 211-46.
Cook, R. D. and Weisberg, S. (1999) Applied Regression Including Computing and Graphics. Wi-
ley.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Hawkins, D. and Weisberg, S. (2017) Combining the Box-Cox Power and Generalized Log Trans-
formations to Accomodate Nonpositive Responses In Linear and Mixed-Effects Linear Models
South African Statistics Journal, 51, 317-328.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley.
Yeo, I. and Johnson, R. (2000) A new family of power transformations to improve normality or
symmetry. Biometrika, 87, 954-959.
See Also
boxcox, yjPower, bcPower, bcnPower, powerTransform, contour
Examples
with(trees, boxCox(Volume ~ log(Height) + log(Girth), data = trees,
lambda = seq(-0.25, 0.25, length = 10)))
Description
Computes a constructed variable for the Box-Cox transformation of the response variable in a linear
model.
Usage
boxCoxVariable(y)
Arguments
y response variable.
Details
y ) − 1], where ye is the geometric mean of y.
The constructed variable is defined as y[log(y/e
The constructed variable is meant to be added to the right-hand-side of the linear model. The t-test
for the coefficient of the constructed variable is an approximate score test for whether a transforma-
tion is required.
If b is the coefficient of the constructed variable, then an estimate of the normalizing power trans-
formation based on the score statistic is 1 − b. An added-variable plot for the constructed variable
shows leverage and influence on the decision to transform y.
Value
a numeric vector of the same length as y.
Author(s)
John Fox <[email protected]>
References
Atkinson, A. C. (1985) Plots, Transformations, and Regression. Oxford.
Box, G. E. P. and Cox, D. R. (1964) An analysis of transformations. JRSS B 26 211–246.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
boxcox, powerTransform, bcPower
22 Boxplot
Examples
mod <- lm(interlocks + 1 ~ assets, data=Ornstein)
mod.aux <- update(mod, . ~ . + boxCoxVariable(interlocks + 1))
summary(mod.aux)
# avPlots(mod.aux, "boxCoxVariable(interlocks + 1)")
Description
Boxplot is a wrapper for the standard R boxplot function, providing point identification, axis
labels, and a formula interface for boxplots without a grouping variable.
Usage
Boxplot(y, ...)
## Default S3 method:
Boxplot(y, g, id=TRUE, xlab, ylab, ...)
Arguments
y a numeric variable for which the boxplot is to be constructed; a list of numeric
variables, each element of which will be treated as a group; a numeric data frame
or a numeric matrix, each of whose columns will be treated as a group.
g a grouping variable, usually a factor, for constructing parallel boxplots.
id a list of named elements giving one or more specifications for labels of indi-
vidual points ("outliers"): n, the maximum number of points to label (default
10); location, "lr" (left or right) of points or "avoid" to try to avoid over-
plotting; method, one of "y" (automatic, the default), "identify" (interactive),
or "none"; col for labels (default is the first color in carPalette() ); and cex
size of labels (default is 1). Can be FALSE to suppress point identification or
boxTidwell 23
TRUE (the default) to use all defaults. This is similar to how showLabels han-
dles point labels for other functions in the car package, except that the usual
default is id=FALSE.
xlab, ylab text labels for the horizontal and vertical axes; if missing, Boxplot will use the
variable names, or, in the case of a list, data frame, or matrix, empty labels.
formula a ‘model’ formula, of the form ~ y to produce a boxplot for the variable y, or of
the form y ~ g, y ~ g1*g2*..., or y ~ g1 + g2 + ... to produce parallel boxplots
for y within levels of the grouping variable(s) g, etc., usually factors.
data, subset, na.action
as for statistical modeling functions (see, e.g., lm).
... further arguments, such as at, to be passed to boxplot.
Author(s)
John Fox <[email protected]>, with a contribution from Steve Ellison to handle at argument (see
boxplot).
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
boxplot
Examples
Boxplot(~income, data=Prestige, id=list(n=Inf)) # identify all outliers
Boxplot(income ~ type, data=Prestige)
Boxplot(income ~ type, data=Prestige, at=c(1, 3, 2))
Boxplot(k5 + k618 ~ lfp*wc, data=Mroz)
with(Prestige, Boxplot(income, id=list(labels=rownames(Prestige))))
with(Prestige, Boxplot(income, type, id=list(labels=rownames(Prestige))))
Boxplot(scale(Prestige[, 1:4]))
Description
Usage
boxTidwell(y, ...)
## Default S3 method:
boxTidwell(y, x1, x2=NULL, max.iter=25, tol=0.001,
verbose=FALSE, ...)
Arguments
formula two-sided formula, the right-hand-side of which gives the predictors to be trans-
formed.
other.x one-sided formula giving the predictors that are not candidates for transforma-
tion, including (e.g.) factors.
data an optional data frame containing the variables in the model. By default the
variables are taken from the environment from which boxTidwell is called.
subset an optional vector specifying a subset of observations to be used.
na.action a function that indicates what should happen when the data contain NAs. The
default is set by the na.action setting of options.
verbose if TRUE a record of iterations is printed; default is FALSE.
tol if the maximum relative change in coefficients is less than tol then convergence
is declared.
max.iter maximum number of iterations.
y response variable.
x1 matrix of predictors to transform.
x2 matrix of predictors that are not candidates for transformation.
... not for the user.
x boxTidwell object.
digits number of digits for rounding.
Details
The maximum-likelihood estimates of the transformation parameters are computed by Box and Tid-
well’s (1962) method, which is usually more efficient than using a general nonlinear least-squares
routine for this problem. Score tests for the transformations are also reported.
Value
an object of class boxTidwell, which is normally just printed.
brief 25
Author(s)
John Fox <[email protected]>
References
Box, G. E. P. and Tidwell, P. W. (1962) Transformation of the independent variables. Technometrics
4, 531-550.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Examples
boxTidwell(prestige ~ income + education, ~ type + poly(women, 2), data=Prestige)
Description
Print data objects and statistical model summaries in abbreviated form.
Usage
brief(object, ...)
## Default S3 method:
brief(object, terms = ~ .,
intercept=missing(terms), pvalues=FALSE,
digits=3, horizontal=TRUE, ...)
Arguments
object a data or model object to abbreviate.
rows for a matrix or data frame, a 2-element integer vector with the number of rows to
print at the beginning and end of the display; for a vector or factor, the number
of lines of output to show at the beginning and end; for a list, the number of
elements to show at the beginning and end; for a function, the number of lines
to show at the beginning and end.
cols for a matrix or data frame, a 2-element integer vector with the number of columns
to print at the beginning (i.e., left) and end (right) of the display.
head, tail alternatives to the rows argument; if TRUE, print the first or last 6 rows; can also
be the number of the first or last few rows to print; only one of heads and tails
should be specified; ignored if FALSE (the default).
elided controls whether to report the number of elided elements, rows, or columns;
default is TRUE.
classes show the class of each column of a data frame at the top of the column; the
classes are shown in single-character abbreviated form—e.g., [f] for a factor,
[i] for an integer variable, [n] for a numeric variable, [c] for a character vari-
able.
terms a one-sided formula giving the terms to summarize; the default is ~ .—i.e., to
summarize all terms in the model.
brief 27
intercept whether or not to include the intercept; the default is TRUE unless the terms
argument is given, in which case the default is FALSE; ignored for polr models.
pvalues include the p-value for each coefficient in the table; default is FALSE.
exponentiate for a "glm" or "glmerMod" model using the log or logit link, or a "polr"
or "multinom" model, show exponentiated coefficient estimates and confidence
bounds.
digits significant digits for printing.
horizontal if TRUE (the default), orient the summary produced by brief horizontally, which
typically saves space.
dispersion see summary.glm
vcov. either a matrix giving the estimated covariance matrix of the estimates, or a func-
tion that when called with object as an argument returns an estimated covari-
ance matrix of the estimates. The default vcov. = vcov(object,complete=FALSE)
uses the usual estimated covariance matrix with NA for any row and column
with aliased regressors. Other choices include the functions documented at
hccm, and a bootstrap estimate vcov.=vcov(Boot(object)); see the documen-
tation for Boot. For the glm method, if the vcov. or dispersion argument is
specified, then Wald-based confidence limits are computed; otherwise the re-
ported confidence limits are computed by profiling the likelihood. NOTES: (1)
The dispersion and vcov. arguments may not both be specified. (2) Setting
vcov.=vcov returns an error if the model includes aliased terms; use vcov.=vcov(object,complete=FAL
(3) The hccm method will generally return a matrix of full rank even if the model
has aliased terms. Similarly vcov.=vcov(Boot(object)) may return a full
rank matrix, or it will a matrix with NA corresponding to aliased regressors if
same regressors are aliased in every bootstrap sample.
... arguments to pass down.
Value
Invisibly returns object for a data object, or summary for a model object.
Note
The method brief.matrix calls brief.data.frame.
Author(s)
John Fox <[email protected]>
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
S
28 car-defunct
Examples
brief(rnorm(100))
brief(Duncan)
brief(OBrienKaiser, elided=TRUE)
brief(matrix(1:500, 10, 50))
brief(lm)
Description
These functions are were deprecated in 2009 and are now defunct.
Usage
av.plot(...)
av.plots(...)
box.cox(...)
bc(...)
box.cox.powers(...)
box.cox.var(...)
box.tidwell(...)
cookd(...)
confidence.ellipse(...)
ceres.plot(...)
ceres.plots(...)
cr.plot(...)
cr.plots(...)
data.ellipse(...)
durbin.watson(...)
levene.test(...)
leverage.plot(...)
leverage.plots(...)
linear.hypothesis(...)
ncv.test(...)
outlier.test(...)
qq.plot(...)
skewPower(...)
spread.level.plot(...)
car-deprecated 29
Arguments
... pass arguments down.
Details
av.plot and av.plots are replaced by avPlot and avPlots functions.
box.cox and bc are now replaced by bcPower.
box.cox.powers is replaced by powerTransform.
box.cox.var is replaced by boxCoxVariable.
box.tidwell is replaced by boxTidwell.
cookd is replaced by cooks.distance in the stats package.
confidence.ellipse is replaced by confidenceEllipse.
ceres.plot and ceres.plots are now replaced by the ceresPlot and ceresPlots functions.
cr.plot and cr.plots are now replaced by the crPlot and crPlots functions.
data.ellipse is replaced by dataEllipse.
durbin.watson is replaced by durbinWatsonTest.
levene.test is replaced by leveneTest function.
leverage.plot and leverage.plots are now replaced by the leveragePlot and leveragePlots
functions.
linear.hypothesis is replaced by the linearHypothesis function.
ncv.test is replaced by ncvTest.
outlier.test is replaced by outlierTest.
qq.plot is replaced by qqPlot.
skewPower is replaced by bcnPower.
spread.level.plot is replaced by spreadLevelPlot.
Description
These functions are provided for compatibility with older versions of the car package only, and
may be removed eventually. Commands that worked in versions of the car package prior to version
3.0-0 will not necessarily work in version 3.0-0 and beyond, or may not work in the same manner.
Usage
bootCase(...)
nextBoot(...)
30 carHexsticker
Arguments
... arguments to pass to methods.
Details
These functions are replaced by Boot.
See Also
See Also Boot
Description
These objects (currently only the .carEnv environment) are exported for technical reasons and are
not for direct use.
Author(s)
John Fox <[email protected]>
carHexsticker View the Official Hex Sticker for the car Package
Description
Open the official hex sticker for the car package in your browser
Usage
carHexsticker()
Value
Used for its side effect of openning the hex sticker for the car package in your browser.
Author(s)
John Fox <[email protected]>
Examples
## Not run:
carHexsticker()
## End(Not run)
carPalette 31
Description
This function is used to set or retrieve colors to be used in car package graphics functions.
Usage
carPalette(palette)
Arguments
palette if missing, returns the colors that will be used in car graphics; if present, the
colors to be used in graphics will be set. The palette argument may also be
one of "car" or "default" to use the default car palette (defined below), "R" to
use the default R palette, or "colorblind" to use a colorblind-friendly palette
(from https://jfly.uni-koeln.de/color/).
Details
This function sets or returns the value of options(carPalette=pallete) that will be use in car
graphics functions to determine colors. The default is c("black","blue","magenta","cyan","orange","gray","green3
which is nearly a permutation of the colors returned by the standard palette function that mini-
mizes the use of red and green in the same graph, and that substitutes orange for the often hard to
see yellow.
Value
Author(s)
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
palette, colors
32 carWeb
Examples
# Standard color palette
palette()
# car standard color palette
carPalette()
# set colors to all black
carPalette(rep("black", 8))
# Use a custom color palette with 12 distinct colors
carPalette(sample(colors(distinct=TRUE), 12))
# restore default
carPalette("default")
Description
This function will access the website for An R Companion to Applied Regression, or setup files or
data.
Usage
carWeb(page = c("webpage", "errata", "taskviews"), script, data, setup)
Arguments
page A character string indicating what page to open. The default "webpage" will
open the main web page, "errata" displays the errata sheet for the book, "taskviews"
fetches and displays a list of available task views from CRAN.
script The quoted name of a chapter in An R Companion to Applied Regression, like
"chap-1", "chap-2", up to "chap-10". All the R commands used in that chap-
ter will be displayed in your browser, where you can save them as a text file.
data The quoted name of a data file in An R Companion to Applied Regression,
like "Duncan.txt" or "Prestige.txt". The file will be opened in your web
browser. You do not need to specify the extension .txt
setup If TRUE this command will download a number of files to your computer that are
discussed in Fox and Weisberg (2019), beginning in Chapter 1.
Value
Either displays a web page or a PDF document or downloads files to your working directory.
Author(s)
Sanford Weisberg, based on the function UsingR in the UsingR package by John Verzani
ceresPlots 33
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Examples
## Not run:
carWeb()
carWeb(setup=TRUE)
## End(Not run)
Description
These functions draw Ceres plots for linear and generalized linear models.
Usage
ceresPlots(model, ...)
## Default S3 method:
ceresPlots(model, terms = ~., layout = NULL, ask, main,
...)
ceresPlot(model, ...)
Arguments
model model object produced by lm or glm.
terms A one-sided formula that specifies a subset of the regressors. One component-
plus-residual plot is drawn for each term. The default ~. is to plot against all
numeric predictors. For example, the specification terms = ~ . -X3 would plot
against all predictors except for X3. Factors and nonstandard predictors such as
B-splines are skipped. If this argument is a quoted name of one of the regressors,
the component-plus-residual plot is drawn for that predictor only.
34 ceresPlots
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 the user before drawing the next plot; if FALSE, the default, don’t
ask. This is relevant only if not all the graphs can be drawn in one window.
main Overall title for any array of cerers plots; if missing a default is provided.
... ceresPlots passes these arguments to ceresPlot. ceresPlot passes them to
plot.
variable A quoted string giving the name of a variable for the horizontal axis
id controls point identification; if FALSE (the default), no points are identified; can
be a list of named arguments to the showLabels function; TRUE is equivalent to
list(method=list(abs(residuals(model,type="pearson")),"x"),n=2,cex=1,col=carPalette(
which identifies the 2 points with the largest residuals and the 2 points with the
most extreme horizontal (X) values.
line TRUE to plot least-squares line.
smooth specifies the smoother to be used along with its arguments; if FALSE, no smoother
is shown; can be a list giving the smoother function and its named arguments;
TRUE, the default, is equivalent to list(smoother=loessLine). See ScatterplotSmoothers
for the smoothers supplied by the car package and their arguments. Ceres plots
do not support variance smooths.
col color for points; the default is the first entry in the current car palette (see
carPalette and par).
col.lines a list of at least two colors. The first color is used for the ls line and the second
color is used for the fitted lowess line. To use the same color for both, use, for
example, col.lines=c("red","red")
xlab,ylab labels for the x and y axes, respectively. If not set appropriate labels are created
by the function.
pch plotting character for points; default is 1 (a circle, see par).
lwd line width; default is 2 (see par).
grid If TRUE, the default, a light-gray background grid is put on the graph
Details
Ceres plots are a generalization of component+residual (partial residual) plots that are less prone to
leakage of nonlinearity among the predictors.
The function intended for direct use is ceresPlots.
The model cannot contain interactions, but can contain factors. Factors may be present in the model,
but Ceres plots cannot be drawn for them.
Value
NULL. These functions are used for their side effect: producing plots.
compareCoefs 35
Author(s)
John Fox <[email protected]>
References
Cook, R. D. and Weisberg, S. (1999) Applied Regression, Including Computing and Graphics.
Wiley.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
crPlots, avPlots, showLabels
Examples
ceresPlots(lm(prestige~income+education+type, data=Prestige), terms= ~ . - type)
compareCoefs Print estimated coefficients and their standard errors in a table for
several regression models.
Description
This function extracts estimates of regression parameters and their standard errors from one or more
models and prints them in a table.
Usage
compareCoefs(..., se = TRUE, zvals = FALSE, pvals = FALSE, vcov.,
print = TRUE, digits = 3)
Arguments
... One or more regression-model objects. These may be of class lm, glm, nlm,
or any other regression method for which the functions coef and vcov return
appropriate values, or if the object inherits from the mer class created by the
lme4 package or lme in the nlme package.
se If TRUE, the default, show standard errors as well as estimates.
zvals If TRUE (the default is FALSE), print Wald statistics, the ratio of each coefficient
to its standard error.
pvals If codeTRUE (the default is FALSE), print two-sided p-values from the standard
normal distribution corresponding to the Wald statistics.
36 Contrasts
Value
This function is mainly used for its side-effect of printing the result. It also invisibly returns a matrix
of estimates, standard errors, Wald statistics, and p-values.
Author(s)
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Examples
mod1 <- lm(prestige ~ income + education, data=Duncan)
mod2 <- update(mod1, subset=-c(6,16))
mod3 <- update(mod1, . ~ . + type)
mod4 <- update(mod1, . ~ . + I(income + education)) # aliased coef.
compareCoefs(mod1)
compareCoefs(mod1, mod2, mod4)
compareCoefs(mod1, mod2, mod3, zvals=TRUE, pvals=TRUE)
compareCoefs(mod1, mod2, se=FALSE)
compareCoefs(mod1, mod1, vcov.=list(vcov, hccm))
Description
These are substitutes for similarly named functions in the stats package (note the uppercase letter
starting the second word in each function name). The only difference is that the contrast functions
from the car package produce easier-to-read names for the contrasts when they are used in statistical
models.
The functions and this documentation are adapted from the stats package.
Contrasts 37
Usage
contr.Treatment(n, base = 1, contrasts = TRUE)
Arguments
n a vector of levels for a factor, or the number of levels.
base an integer specifying which level is considered the baseline level. Ignored if
contrasts is FALSE.
contrasts a logical indicating whether contrasts should be computed.
Details
These functions are used for creating contrast matrices for use in fitting analysis of variance and
regression models. The columns of the resulting matrices contain contrasts which can be used for
coding a factor with n levels. The returned value contains the computed contrasts. If the argument
contrasts is FALSE then a square matrix is returned.
Several aspects of these contrast functions are controlled by options set via the options command:
decorate.contrasts This option should be set to a 2-element character vector containing the pre-
fix and suffix characters to surround contrast names. If the option is not set, then c("[","]")
is used. For example, setting options(decorate.contrasts=c(".","")) produces contrast
names that are separated from factor names by a period. Setting options( decorate.contrasts=c("",""))
reproduces the behaviour of the R base contrast functions.
decorate.contr.Treatment A character string to be appended to contrast names to signify treat-
ment contrasts; if the option is unset, then "T." is used.
decorate.contr.Sum Similar to the above, with default "S.".
decorate.contr.Helmert Similar to the above, with default "H.".
contr.Sum.show.levels Logical value: if TRUE (the default if unset), then level names are used
for contrasts; if FALSE, then numbers are used, as in contr.sum in the base package.
Note that there is no replacement for contr.poly in the base package (which produces orthogonal-
polynomial contrasts) since this function already constructs easy-to-read contrast names.
Value
A matrix with n rows and k columns, with k = n -1 if contrasts is TRUE and k = n if contrasts is
FALSE.
Author(s)
John Fox <[email protected]>
38 crPlots
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
contr.treatment, contr.sum, contr.helmert, contr.poly
Examples
# contr.Treatment vs. contr.treatment in the base package:
## Call:
## lm(formula = prestige ~ (income + education) * type, data = Prestige,
## contrasts = list(type = "contr.Treatment"))
##
## Coefficients:
## (Intercept) income education
## 2.275753 0.003522 1.713275
## type[T.prof] type[T.wc] income:type[T.prof]
## 15.351896 -33.536652 -0.002903
## income:type[T.wc] education:type[T.prof] education:type[T.wc]
## -0.002072 1.387809 4.290875
## Call:
## lm(formula = prestige ~ (income + education) * type, data = Prestige,
## contrasts = list(type = "contr.treatment"))
##
## Coefficients:
## (Intercept) income education
## 2.275753 0.003522 1.713275
## typeprof typewc income:typeprof
## 15.351896 -33.536652 -0.002903
## income:typewc education:typeprof education:typewc
## -0.002072 1.387809 4.290875
Description
These functions construct component+residual plots, also called partial-residual plots, for linear
and generalized linear models.
crPlots 39
Usage
crPlots(model, ...)
## Default S3 method:
crPlots(model, terms = ~., layout = NULL, ask, main,
...)
crp(...)
crPlot(model, ...)
Arguments
model model object produced by lm or glm.
terms A one-sided formula that specifies a subset of the regressors. One component-
plus-residual plot is drawn for each regressor. The default ~. is to plot against
all numeric regressors. For example, the specification terms = ~ . -X3 would
plot against all regressors except for X3, while terms = ~ log(X4) would give
the plot for the predictor X4 that is represented in the model by log(X4). If this
argument is a quoted name of one of the predictors, the component-plus-residual
plot is drawn for that predictor only.
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 the user before drawing the next plot; if FALSE, the default, don’t
ask. This is relevant only if not all the graphs can be drawn in one window.
main The title of the plot; if missing, one will be supplied.
... crPlots passes these arguments to crPlot. crPlot passes them to plot.
variable A quoted string giving the name of a variable for the horizontal axis.
id controls point identification; if FALSE (the default), no points are identified; can
be a list of named arguments to the showLabels function; TRUE is equivalent to
list(method=list(abs(residuals(model,type="pearson")),"x"),n=2,cex=1,col=carPalette(
which identifies the 2 points with the largest residuals and the 2 points with the
most extreme horizontal (X) values.
order order of polynomial regression performed for predictor to be plotted; default 1.
line TRUE to plot least-squares line.
40 crPlots
smooth specifies the smoother to be used along with its arguments; if FALSE, no smoother
is shown; can be a list giving the smoother function and its named arguments;
TRUE, the default, is equivalent to list(smoother=loessLine). See ScatterplotSmoothers
for the smoothers supplied by the car package and their arguments.
col color for points; the default is the first entry in the current car palette (see
carPalette and par).
col.lines a list of at least two colors. The first color is used for the ls line and the second
color is used for the fitted lowess line. To use the same color for both, use, for
example, col.lines=c("red","red")
xlab,ylab labels for the x and y axes, respectively. If not set appropriate labels are created
by the function.
pch plotting character for points; default is 1 (a circle, see par).
lwd line width; default is 2 (see par).
grid If TRUE, the default, a light-gray background grid is put on the graph.
Details
The function intended for direct use is crPlots, for which crp is an abbreviation.
The model cannot contain interactions, but can contain factors. Parallel boxplots of the partial
residuals are drawn for the levels of a factor.
Value
NULL. These functions are used for their side effect of producing plots.
Author(s)
John Fox <[email protected]>
References
Cook, R. D. and Weisberg, S. (1999) Applied Regression, Including Computing and Graphics.
Wiley.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
ceresPlots, avPlots
Examples
crPlots(m<-lm(prestige ~ income + education, data=Prestige))
Description
deltaMethod is a generic function that uses the delta method to get a first-order approximate stan-
dard error for a nonlinear function of a vector of random variables with known or estimated covari-
ance matrix.
Usage
deltaMethod(object, ...)
## Default S3 method:
deltaMethod(object, g., vcov., func=g., constants, level=0.95,
rhs, ..., envir=parent.frame())
## S3 method for class 'lm'
deltaMethod(object, g., vcov.=vcov(object, complete=FALSE),
parameterNames=names(coef(object)), ..., envir=parent.frame())
## S3 method for class 'nls'
deltaMethod(object, g., vcov.=vcov(object, complete=FALSE), ..., envir=parent.frame())
## S3 method for class 'multinom'
deltaMethod(object, g., vcov. = vcov(object, complete=FALSE),
parameterNames = if (is.matrix(coef(object)))
colnames(coef(object)) else names(coef(object)), ..., envir=parent.frame())
## S3 method for class 'polr'
deltaMethod(object, g., vcov.=vcov(object, complete=FALSE), ..., envir=parent.frame())
## S3 method for class 'survreg'
deltaMethod(object, g., vcov. = vcov(object, complete=FALSE),
parameterNames = names(coef(object)), ..., envir=parent.frame())
## S3 method for class 'coxph'
deltaMethod(object, g., vcov. = vcov(object, complete=FALSE),
parameterNames = names(coef(object)), ..., envir=parent.frame())
## S3 method for class 'mer'
deltaMethod(object, g., vcov. = vcov(object, complete=FALSE),
parameterNames = names(fixef(object)), ..., envir=parent.frame())
## S3 method for class 'merMod'
deltaMethod(object, g., vcov. = vcov(object, complete=FALSE),
parameterNames = names(fixef(object)), ..., envir=parent.frame())
## S3 method for class 'lme'
deltaMethod(object, g., vcov. = vcov(object, complete=FALSE),
parameterNames = names(fixef(object)), ..., envir=parent.frame())
42 deltaMethod
Arguments
object For the default method, object is either (1) a vector of p named elements, so
names(object) returns a list of p character strings that are the names of the ele-
ments of object; or (2) a model object for which there are coef and vcov meth-
ods, and for which the named coefficient vector returned by coef is asymptoti-
cally normally distributed with asymptotic covariance matrix returned by vcov.
For the other methods, object is a regression object for which coef(object)
or fixef(object) returns a vector of parameter estimates.
g. A quoted string that is the function of the parameter estimates to be evaluated;
see the details below.
vcov. The (estimated) covariance matrix of the coefficient estimates. For the default
method, this argument is required. For all other methods, this argument must
either provide the estimated covariance matrix or a function that when applied
to object returns a covariance matrix. The default is to use the function vcov.
func A quoted string used to annotate output. The default of func = g. is usually
appropriate.
parameterNames A character vector of length p that gives the names of the parameters in the same
order as they appear in the vector of estimates. This argument will be useful if
some of the names in the vector of estimates include special characters, like
I(x2^2), or x1:x2 that will confuse the numerical differentiation function. See
details below.
constants This argument is a named vector whose elements are constants that are used in
the f argument. It isn’t generally necessary to specify this argument but it may
be convenient to do so.
level level for confidence interval, default 0.95.
rhs hypothesized value for the specified function of parameters; if absent no hypoth-
esis test is performed.
... Used to pass arguments to the generic method.
envir Environment in which g. is evaluated; not normally specified by the user.
Details
Suppose x is a random vector of length p that is at least approximately normally distributed with
mean β and estimated covariance matrix C. Then any function g(β) of β, is estimated by g(x),
which is in large samples normally distributed with mean g(β) and estimated variance h0 Ch, where
h is the first derivative of g(β) with respect to β evaluated at x. This function returns both g(x) and
its standard error, the square root of the estimated variance.
The default method requires that you provide x in the argument object, C in the argument vcov.,
and a text expression in argument g. that when evaluated gives the function g. The call names(object)
must return the names of the elements of x that are used in the expression g..
deltaMethod 43
Since the delta method is often applied to functions of regression parameter estimates, the argu-
ment object may be the name of a regression object from which the estimates and their esti-
mated variance matrix can be extracted. In most regression models, estimates are returned by the
coef(object) and the variance matrix from vcov(object). You can provide an alternative func-
tion for computing the sample variance matrix, for example to use a sandwich estimator.
For mixed models using lme4 or nlme, the coefficient estimates are returned by the fixef function,
while for multinom, lmList and nlsList coefficient estimates are returned by coef as a matrix.
Methods for these models are provided to get the correct estimates and variance matrix.
The argument g. must be a quoted character string that gives the function of interest. For example, if
you set m2 <-lm(Y ~ X1 + X2 + X1:X2), then deltaMethod(m2,"X1/X2") applies the delta method
to the ratio of the coefficient estimates for X1 and X2. The argument g. can consist of constants and
names associated with the elements of the vector of coefficient estimates.
In some cases the names may include characters such as the colon : used in interactions, or mathe-
matical symbols like + or - signs that would confuse the function that computes numerical deriva-
tives, and for this case you can replace the names of the estimates with the parameterNames argu-
ment. For example, the ratio of the X2 main effect to the interaction term could be computed using
deltaMethod(m2,"b1/b3",parameterNames=c("b0","b1","b2","b3")). The name “(Intercept)”
used for the intercept in linear and generalized linear models is an exception, and it will be correctly
interpreted by deltaMethod. Another option is to use back-ticks to quote nonstandard R names, as
in deltaMethod(m2,"X1/`X1:X2`").
For multinom objects, the coef function returns a matrix of coefficients, with each row giving the
estimates for comparisons of one category to the baseline. The deltaMethod function applies the
delta method to each row of this matrix. Similarly, for lmList and nlsList objects, the delta
method is computed for each element of the list of models fit.
For nonlinear regression objects produced by the nls function, the call coef(object) returns
the estimated coefficient vectors with names corresponding to parameter names. For example, m2
<-nls(y ~ theta/(1 + gamma * x),start = list(theta=2,gamma=3)) will have parameters named
c("theta","gamma"). In many other familiar regression models, such as those produced by lm and
glm, the names of the coefficient estimates are the corresponding regressor names, not parameter
names.
For mixed-effects models fit with lmer and glmer from the lme4 package or lme and nlme from the
nlme package, only fixed-effect coefficients are considered.
For regression models for which methods are not provided, you can extract the named vector of co-
efficient estimates and and estimate of its covariance matrix and then apply the default deltaMethod
function.
Note: Earlier versions of deltaMethod included an argument parameterPrefix that implemented
the same functionality as the parameterNames argument, but which caused several problems that
were not easily fixed without the change in syntax.
Value
An object of class "deltaMethod", inheriting from "data.frame", for which a print method is
provided. The object contains columns named Estimate for the estimate, SE for its standard error,
and columns for confidence limits and possibly a hypothesis test. The value of g. is given as a row
label.
44 densityPlot
Author(s)
Sanford Weisberg, <[email protected]>, John Fox <[email protected]>, and Pavel Krivitsky.
References
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley, Section 6.1.2.
See Also
First derivatives of g. are computed using symbolic differentiation by the function D.
Examples
m1 <- lm(time ~ t1 + t2, data = Transact)
deltaMethod(m1, "b1/b2", parameterNames= paste("b", 0:2, sep=""))
deltaMethod(m1, "t1/t2", rhs=1) # use names of preds. rather than coefs.
deltaMethod(m1, "t1/t2", vcov=hccm) # use hccm function to est. vars.
deltaMethod(m1, "1/(Intercept)")
# The next example invokes the default method by extracting the
# vector of estimates and covariance matrix explicitly
deltaMethod(coef(m1), "t1/t2", vcov.=vcov(m1))
Description
densityPlot contructs and graphs nonparametric density estimates, possibly conditioned on a fac-
tor, using the standard R density function or by default adaptiveKernel, which computes an
adaptive kernel density estimate. depan provides the Epanechnikov kernel and dbiwt provides the
biweight kernel.
Usage
densityPlot(x, ...)
## Default S3 method:
densityPlot(x, g, method=c("adaptive", "kernel"),
bw=if (method == "adaptive") bw.nrd0 else "SJ", adjust=1,
kernel, xlim, ylim,
normalize=FALSE, xlab=deparse(substitute(x)), ylab="Density", main="",
col=carPalette(), lty=seq_along(col), lwd=2, grid=TRUE,
legend=TRUE, show.bw=FALSE, rug=TRUE, ...)
depan(x)
dbiwt(x)
Arguments
x a numeric variable, the density of which is estimated; for depan and dbiwt, the
argument of the kernel function.
g an optional factor to divide the data.
formula an R model formula, of the form ~ variable to estimate the unconditional den-
sity of variable, or variable ~ factor to estimate the density of variable
within each level of factor.
data an optional data frame containing the data.
subset an optional vector defining a subset of the data.
na.action a function to handle missing values; defaults to the value of the R na.action
option, initially set to na.omit.
method either "adaptive" (the default) for an adaptive-kernel estimate or "kernel" for
a fixed-bandwidth kernel estimate.
bw the geometric mean bandwidth for the adaptive-kernel or bandwidth of the ker-
nel density estimate(s). Must be a numerical value or a function to compute
the bandwidth (default bw.nrd0) for the adaptive kernel estimate; for the kernel
estimate, may either the quoted name of a rule to compute the bandwidth, or a
numeric value. If plotting by groups, bw may be a vector of values, one for each
group. See density and bw.SJ for details of the kernel estimator.
adjust a multiplicative adjustment factor for the bandwidth; the default, 1, indicates
no adjustment; if plotting by groups, adjust may be a vector of adjustment
factors, one for each group. The default bandwidth-selection rule tends to give
a value that’s too large if the distribution is asymmetric or has multiple modes;
try setting adjust < 1, particularly for the adaptive-kernel estimator.
kernel for densityPlot this is the name of the kernel function for the kernel esti-
mator (the default is "gaussian", see density); or a kernel function for the
adaptive-kernel estimator (the default is dnorm, producing the Gaussian kernel).
For adaptivekernel this is a kernel function, defaulting to dnorm, which is the
Gaussian kernel (standard-normal density).
xlim, ylim axis limits; if missing, determined from the range of x-values at which the den-
sities are estimated and the estimated densities.
normalize if TRUE (the default is FALSE), the estimated densities are rescaled to integrate ap-
proximately to 1; particularly useful if the density is estimated over a restricted
domain, as when from or to are specified.
xlab label for the horizontal-axis; defaults to the name of the variable x.
46 densityPlot
Details
If you use a different kernel function than the default dnorm that has a standard deviation different
from 1 along with an automatic rule like the default function bw.nrd0, you can attach an attribute
to the kernel function named "scale" that gives its standard deviation. This is true for the two
supplied kernels, depan and dbiwt
Value
densityPlot invisibly returns the "density" object computed (or list of "density" objects) and
draws a graph. adaptiveKernel returns an object of class "density" (see density).
Author(s)
John Fox <[email protected]>
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
W. N. Venables and B. D. Ripley (2002) Modern Applied Statistics with S. New York: Springer.
B.W. Silverman (1986) Density Estimation for Statistics and Data Analysis. London: Chapman and
Hall.
dfbetaPlots 47
See Also
density, bw.SJ, plot.density
Examples
densityPlot(~ income, show.bw=TRUE, method="kernel", data=Prestige)
densityPlot(~ income, show.bw=TRUE, data=Prestige)
densityPlot(~ income, from=0, normalize=TRUE, show.bw=TRUE, data=Prestige)
Description
These functions display index plots of dfbeta (effect on coefficients of deleting each observation
in turn) and dfbetas (effect on coefficients of deleting each observation in turn, standardized by a
deleted estimate of the coefficient standard error). In the plot of dfbeta, horizontal lines are drawn
at 0 and +/- one standard error; in the plot of dfbetas, horizontal lines are drawn and 0 and +/- 1.
Usage
dfbetaPlots(model, ...)
dfbetasPlots(model, ...)
Arguments
model model object produced by lm or glm.
terms A one-sided formula that specifies a subset of the terms in the model. One
dfbeta or dfbetas plot is drawn for each regressor. The default ~. is to plot
against all terms in the model with the exception of an intercept. For example,
the specification terms = ~.-X3 would plot against all terms except for X3. If
this argument is a quoted name of one of the terms, the index plot is drawn for
that term only.
intercept Include the intercept in the plots; default is FALSE.
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.
main The title of the graph; if missing, one will be supplied.
xlab Horizontal axis label; defaults to "Index".
ylab Vertical axis label; defaults to coefficient name.
ask If TRUE, ask the user before drawing the next plot; if FALSE, the default, don’t
ask.
... optional additional arguments to be passed to plot, points, and showLabels.
id.method, labels, id.n, id.cex, id.col, id.location
Arguments for the labelling of points. The default is id.n=0 for labeling no
points. See showLabels for details of these arguments.
col color for points; defaults to the first entry in the color carPalette.
grid If TRUE, the default, a light-gray background grid is put on the graph
Value
NULL. These functions are used for their side effect: producing plots.
Author(s)
John Fox <[email protected]>
durbinWatsonTest 49
References
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
dfbeta ,dfbetas
Examples
dfbetaPlots(lm(prestige ~ income + education + type, data=Duncan))
Description
Computes residual autocorrelations and generalized Durbin-Watson statistics and their bootstrapped
p-values. dwt is an abbreviation for durbinWatsonTest.
Usage
durbinWatsonTest(model, ...)
dwt(...)
## Default S3 method:
durbinWatsonTest(model, max.lag=1, ...)
Arguments
model a linear-model object, or a vector of residuals from a linear model.
max.lag maximum lag to which to compute residual autocorrelations and Durbin-Watson
statistics.
simulate if TRUE p-values will be estimated by bootstrapping.
50 Ellipses
Value
Returns an object of type "durbinWatsonTest".
Note
p-values are available only from the lm method.
Author(s)
John Fox <[email protected]>
References
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Examples
durbinWatsonTest(lm(fconvict ~ tfr + partic + degrees + mconvict, data=Hartnagel))
Description
These functions draw ellipses, including data ellipses, and confidence ellipses for linear, generalized
linear, and possibly other models.
Usage
ellipse(center, shape, radius, log="", center.pch=19, center.cex=1.5,
segments=51, draw=TRUE, add=draw, xlab="", ylab="",
col=carPalette()[2], lwd=2, fill=FALSE, fill.alpha=0.3, grid=TRUE, ...)
confidenceEllipse(model, ...)
## Default S3 method:
confidenceEllipse(model, which.coef, vcov.=vcov,
L, levels=0.95, Scheffe=FALSE, dfn,
center.pch=19, center.cex=1.5, segments=51, xlab, ylab,
col=carPalette()[2], lwd=2, fill=FALSE, fill.alpha=0.3, draw=TRUE, add=!draw, ...)
Arguments
center 2-element vector with coordinates of center of ellipse.
shape 2 × 2 shape (or covariance) matrix.
radius radius of circle generating the ellipse.
log when an ellipse is to be added to an existing plot, indicates whether computa-
tions were on logged values and to be plotted on logged axes; "x" if the x-axis
is logged, "y" if the y-axis is logged, and "xy" or "yx" if both axes are logged.
The default is "", indicating that neither axis is logged.
center.pch character for plotting ellipse center; if FALSE or NULL the center point isn’t plot-
ted.
center.cex relative size of character for plotting ellipse center.
segments number of line-segments used to draw ellipse.
draw if TRUE produce graphical output; if FALSE, only invisibly return coordinates of
ellipse(s).
add if TRUE add ellipse(s) to current plot.
xlab label for horizontal axis.
ylab label for vertical axis.
x a numeric vector, or (if y is missing) a 2-column numeric matrix.
y a numeric vector, of the same length as x.
groups optional: a factor to divide the data into groups; a separate ellipse will be plotted
for each group (level of the factor).
group.labels labels to be plotted for the groups; by default, the levels of the groups factor.
52 Ellipses
ellipse.label a label for the ellipse(s) or a vector of labels; if several ellipses are drawn and
just one label is given, then that label will be repeated. The default is not to label
the ellipses.
weights a numeric vector of weights, of the same length as x and y to be used by cov.wt
or cov.trob in computing a weighted covariance matrix; if absent, weights of
1 are used.
plot.points if FALSE data ellipses are drawn, but points are not plotted.
levels draw elliptical contours at these (normal) probability or confidence levels.
robust if TRUE use the cov.trob function in the MASS package to calculate the center
and covariance matrix for the data ellipse.
model a model object produced by lm or glm.
which.coef 2-element vector giving indices of coefficients to plot; if missing, the first two
coefficients (disregarding the regression constant) will be selected.
vcov. a coefficient-covariance matrix or a function (such as hccm) to compute the
coefficent-covariance matrix from model; the default is the vcov function.
L As an alternative to selecting coefficients to plot, a transformation matrix can be
specified to compute two linear combinations of the coefficients; if the L matrix
is given, it takes precedence over the which.coef argument. L should have two
rows and as many columns as there are coefficients. It can be given directly
as a numeric matrix, or specified by a pair of character-valued expressions, in
the same manner as for the link{linearHypothesis} function, but with no
right-hand side.
Scheffe if TRUE scale the ellipse so that its projections onto the axes give Scheffe confi-
dence intervals for the coefficients.
dfn “numerator” degrees of freedom (or just degrees of freedom for a GLM) for
drawing the confidence ellipse. Defaults to the number of coefficients in the
model (disregarding the constant) if Scheffe is TRUE, or to 2 otherwise; select-
ing dfn = 1 will draw the “confidence-interval generating” ellipse, with projec-
tions on the axes corresponding to individual confidence intervals with the stated
level of coverage.
chisq if TRUE, the confidence ellipse for the coefficients in a generalized linear model
are based on the chisquare statistic, if FALSE on the $F$-statistic. This corre-
sponds to using the default and linear-model methods respectively.
col color for lines and ellipse center; the default is the second entry in the current
car palette (see carPalette and par). For dataEllipse, two colors can be
given, in which case the first is for plotted points and the second for lines and
the ellipse center; if ellipses are plotted for groups, then this is a vector of colors
for the groups.
pch for dataEllipse this is the plotting character (default, symbol 1, a hollow cir-
cle) to use for the points; if ellipses are plotted by groups, then this a vector of
plotting characters, with consecutive symbols starting with 1 as the default.
lwd line width; default is 2 (see par).
fill fill the ellipse with translucent color col (default, FALSE)?
fill.alpha transparency of fill (default = 0.3).
Ellipses 53
Details
The ellipse is computed by suitably transforming a unit circle.
dataEllipse superimposes the normal-probability contours over a scatterplot of the data.
Value
These functions are mainly used for their side effect of producing plots. For greater flexibility
(e.g., adding plot annotations), however, ellipse returns invisibly the (x, y) coordinates of the
calculated ellipse. dataEllipse and confidenceEllipse return invisibly the coordinates of one
or more ellipses, in the latter instance a list named by levels.
Author(s)
Georges Monette, John Fox <[email protected]>, and Michael Friendly.
References
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Monette, G. (1990) Geometry of multiple regression and 3D graphics. In Fox, J. and Long, J. S.
(Eds.) Modern Methods of Data Analysis. Sage.
See Also
cov.trob, cov.wt, linearHypothesis.
Examples
dataEllipse(Duncan$income, Duncan$education, levels=0.1*1:9,
ellipse.label=0.1*1:9, lty=2, fill=TRUE, fill.alpha=0.1)
confidenceEllipse(lm(prestige~income+education, data=Duncan),
L=c("income + education", "income - education"))
Description
Uses the export function in the rio package to export a file to disk. This function adds an argument
for converting row.names to a column in the resulting file.
Usage
Export(x, file, format, ..., keep.row.names)
Arguments
x A data frame or matrix to be written to a file.
file A character string naming a file. If the file name has an extension, such as .xlsx,
the extention is used to infer the type of file to be exported. See export for the
file types supported.
format see export.
... Additional arguments; see export.
keep.row.names If set to TRUE, then the data frame’s row.names are appended to the left of the
data frame with the name "id". If set to quoted character string, the row.names
are added using the character string as its name. If set to FALSE row.names are
lost.
Details
This is a convenience function in the car package for exporting (writing) a data frame to a file
in a wide variety of formats including csv, Microsoft Excel. It optionally allows converting the
row.names for the data frame to a column before writing. It then calls export in the rio package.
That function in turn uses many other packages and functions for writing the function to a file.
hccm 55
Value
Author(s)
References
Chung-hong Chan, Geoffrey CH Chan, Thomas J. Leeper, and Jason Becker (2017). rio: A Swiss-
army knife for data file I/O. R package version 0.5.0.
See Also
export, Import
Examples
Export(Duncan, "Duncan.csv", keep.row.names="occupation")
Duncan2 <- Import("Duncan.csv") # Automatically restores row.names
identical(Duncan, Duncan2)
# cleanup
unlink("Duncan.csv")
Description
Usage
hccm(model, ...)
## Default S3 method:
hccm(model, ...)
56 hccm
Arguments
model a unweighted or weighted linear model, produced by lm.
type one of "hc0", "hc1", "hc2", "hc3", or "hc4"; the first of these gives the classic
White correction. The "hc1", "hc2", and "hc3" corrections are described in
Long and Ervin (2000); "hc4" is described in Cribari-Neto (2004).
singular.ok if FALSE (the default is TRUE), a model with aliased coefficients produces an
error; otherwise, the aliased coefficients are ignored in the coefficient covariance
matrix that’s returned.
... arguments to pass to hccm.lm.
Details
The original White-corrected coefficient covariance matrix ("hc0") for an unweighted model is
where e2i are the squared residuals, and X is the model matrix. The other methods represent ad-
justments to this formula. If there are weights, these are incorporated in the corrected covariance
matrix.
The function hccm.default simply catches non-lm objects.
Value
The heteroscedasticity-corrected covariance matrix for the model.
Author(s)
John Fox <[email protected]>
References
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Cribari-Neto, F. (2004) Asymptotic inference under heteroskedasticity of unknown form. Compu-
tational Statistics and Data Analysis 45, 215–233.
Long, J. S. and Ervin, L. H. (2000) Using heteroscedasity consistent standard errors in the linear
regression model. The American Statistician 54, 217–224.
White, H. (1980) A heteroskedastic consistent covariance matrix estimator and a direct test of het-
eroskedasticity. Econometrica 48, 817–838.
Examples
options(digits=4)
mod<-lm(interlocks~assets+nation, data=Ornstein)
vcov(mod)
## (Intercept) assets nationOTH nationUK nationUS
## (Intercept) 1.079e+00 -1.588e-05 -1.037e+00 -1.057e+00 -1.032e+00
hist.boot 57
Description
The Boot function in the car package uses the boot function from the boot package to do a straight-
forward case or residual bootstrap for many regression objects. These are method functions for stan-
dard generics to summarize the results of the bootstrap. Other tools for this purpose are available in
the boot package.
Usage
Arguments
x, object An object created by a call to boot in the boot package, or to Boot in the car
package of class "boot".
parm A vector of numbers or coefficient names giving the coefficients for which a
histogram or confidence interval is desired. If numbers are used, 1 corresponds
to the intercept, if any. The default is all coefficients.
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 the user before drawing the next plot; if FALSE, don’t ask.
main Main title for the graphs. The default is main="" for no title.
freq The default for the generic hist function is freq=TRUE to give a frequency his-
togram. The default for hist.boot is freq=FALSE to give a density histogram.
A density estimate and/or a fitted normal density can be added to the graph if
freq=FALSE but not if freq=TRUE.
estPoint, point.col, point.lty, point.lwd
If estPoint=TRUE, the default, a vertical line is drawn on the histgram at the
value of the point estimate computed from the complete data. The remaining
three optional arguments set the color, line type and line width of the line that is
drawn.
estDensity, den.col, den.lty, den.lwd
If estDensity=TRUE andfreq=FALSE, the default, a kernel density estimate is
drawn on the plot with a call to the density function with no additional argu-
ments. The remaining three optional arguments set the color, line type and line
width of the lines that are drawn.
estNormal, nor.col, nor.lty, nor.lwd
If estNormal=TRUE andfreq=FALSE, the default, a normal density with mean
and sd computed from the data is drawn on the plot. The remaining three op-
tional arguments set the color, line type and line width of the lines that are drawn.
ci A confidence interval based on the bootstrap will be added to the histogram
using the BCa method if ci="bca" the percentile method if ci="perc", or the
normal method if ci="norm". No interval is drawn if ci="none". The default
is "bca". The interval is indicated by a thick horizontal line at y=0. For some
bootstraps the BCa method is unavailable, in which case a warning is issued and
ci="perc" is substituted. If you wish to see all the options at once, see boot.ci.
The normal method is computed as the (estimate from the original data) minus
the bootstrap bias plus or minus the standard deviation of the bootstrap replicates
times the appropriate quantile of the standard normal distribution.
legend A legend can be added to the (array of) histograms. The value “"top"” puts at
the top-left of the plots. The value “"separate"” puts the legend in its own graph
following all the histograms. The value “"none"” suppresses the legend.
box Add a box around each histogram.
hist.boot 59
... Additional arguments passed to hist; for other methods this is included for
compatibility with the generic method. For example, the argument border=par()$bg
in hist will draw the histogram transparently, leaving only the density esti-
mates. With the vcov function, the additional arguments are passed to cov. See
the Value section, below.
high.moments Should the skewness and kurtosis be included in the summary? Default is
FALSE.
extremes Should the minimum, maximum and range be included in the summary? Default
is FALSE.
level Confidence level, a number between 0 and 1. In confint, level can be a vec-
tor; for example level=c(.50,.90,.95) will return the following estimated
quantiles: c(.025,.05,.25,.75,.95,.975).
type Selects the confidence interval type. The types implemented are the "percentile"
method, which uses the function quantile to return the appropriate quantiles
for the confidence limit specified, the default bca which uses the bias-corrected
and accelerated method presented by Efron and Tibshirani (1993, Chapter 14).
For the other types, see the documentation for boot.
Value
hist is used for the side-effect of drawing an array of historgams of each column of the first ar-
gument. summary returns a matrix of summary statistics for each of the columns in the bootstrap
object. The confint method returns confidence intervals. Confint appends the estimates based on
the original fitted model to the left of the confidence intervals.
The vcov returns the sample covariance of the bootstrap sample estimates. If any of the bootstrap
replications returned an NA value, then vcov will return a matrix of NAs. You can compute the
covariance of the complete cases by adding the argument use="complete.obs" to the call to vcov;
see cov.
Author(s)
Sanford Weisberg, <[email protected]>
References
Efron, B. and Tibsharini, R. (1993) An Introduction to the Bootstrap. New York: Chapman and
Hall.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition. Thousand
Oaks: Sage.
Fox, J. and Weisberg, S. (2018) Bootstrapping Regression Models in R, https://socialsciences.
mcmaster.ca/jfox/Books/Companion/appendices/Appendix-Bootstrapping.pdf.
Weisberg, S. (2013) Applied Linear Regression, Fourth Edition, Wiley
See Also
See Also Boot, hist, density, Fox and Weisberg (2017), cited above
60 Import
Examples
m1 <- lm(Fertility ~ ., swiss)
betahat.boot <- Boot(m1, R=99) # 99 bootstrap samples--too small to be useful
summary(betahat.boot) # default summary
confint(betahat.boot)
hist(betahat.boot)
Description
Uses the import function from the rio package to read a data.frame from a variety of file types. The
Import function includes 2 additional arguments adding row names and for converting character
and logical variables to factors for some file types.
Usage
Import(file, format, ..., row.names=TRUE,
stringsAsFactors = FALSE)
Arguments
file A character string naming a file, URL, or .zip or .tar archive. See the details
below. If the file name has an extension like .xlsx or .csv then the type of file
is inferred from the extension.
format If an extension is not present in the file name or it is wrong, the file format can
be set with this argument; see import.
... Additional arguments passed to import.
row.names If TRUE, the default, the left-most character variable that has all unique elements
is removed from the data frame and set to be row.names. To match import, set
row.names=FALSE.
stringsAsFactors
If TRUE, then character variables that do not have all unique elements are con-
verted to factors. The default is FALSE. Prior to May 2020 the default was de-
termined by getOption("stringsAsFactors"), which then defaulted to TRUE.
This option is FALSE in R 4.0.0 and has been deprecated.
Details
This function calls the import function to read a data frame from a file. Many file types are sup-
ported. For files of type "txt","csv","xlsx","xls" or "ods" the arguments row.names and
stringsAsFactors can be used to add row names and convert character variables to factors, re-
spectively. Many more details are given on the man page for import.
infIndexPlot 61
Value
A data frame. See import for more details
Author(s)
Sanford Weisberg <[email protected]>
See Also
import, Export, strings2factors
Examples
head(Duncan, 3) # first three rows
Export(Duncan, "Duncan.csv", keep.row.names="occupation")
Duncan2 <- Import("Duncan.csv") # Automatically restores row.names and factors
brief(Duncan2)
identical(Duncan, Duncan2) # FALSE because type is of a different class
Duncan3 <- Import("Duncan.csv", stringsAsFactors=TRUE)
brief(Duncan3)
identical(Duncan, Duncan3) # TRUE type is of same class
# cleanup
unlink("Duncan.csv")
Description
Provides index plots of influence and related diagnostics for a regression model.
Usage
infIndexPlot(model, ...)
influenceIndexPlot(model, ...)
Arguments
model A regression object of class lm, glm, or lmerMod, or an influence object for a
lmer, glmer, or lme object (see influence.mixed.models). The "lmerMod"
method calls the "lm" method and can take the same arguments.
vars All the quantities listed in this argument are plotted. Use "Cook" for Cook’s
distances, "Studentized" for Studentized residuals, "Bonf" for Bonferroni p-
values for an outlier test, and and "hat" for hat-values (or leverages) for a linear
or generalized linear model, or "dfbeta", "dfbetas", "var.cov.comps", and
"cookd" for an influence object derived from a mixed model. Capitalization is
optional. All but "dfbeta" and "dfbetas" may be abbreviated by the first one
or more letters.
main main title for graph
id a list of named values controlling point labelling. The default, TRUE, is equiva-
lent to id=list(method="y",n=2,cex=1,col=carPalette()[1],location="lr");
FALSE suppresses point labelling. See showLabels for details.
grid If TRUE, the default, a light-gray background grid is put on the graph.
... Arguments passed to plot
Value
Used for its side effect of producing a graph. Produces index plots of diagnostic quantities.
Author(s)
References
Cook, R. D. and Weisberg, S. (1999) Applied Regression, Including Computing and Graphics.
Wiley.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley.
See Also
Examples
influenceIndexPlot(lm(prestige ~ income + education + type, Duncan))
influence.mixed.models 63
influence.mixed.models
Influence Diagnostics for Mixed-Effects Models
Description
These functions compute deletion influence diagnostics for linear mixed-effects models fit by lmer
in the lme4 package and lme in the nlme package and for generalized linear mixed-effects models fit
by glmer in the lme4 package. The main functions are methods for the influence generic function.
Other functions are provided for computing dfbeta, dfbetas, cooks.distance, and influence
on variance-covariance components based on the objects computed by influence.merMod and
influence.lme.
Usage
## S3 method for class 'merMod'
influence(model, groups, data, maxfun=1000, ncores=1, ...)
## S3 method for class 'lme'
influence(model, groups, data, ncores=1, ...)
Arguments
model in the case of influence.merMod or influence.lme, a model of class "merMod"
or "lme"; in the case of cooks.distance, dfbeta, or dfbetas, an object re-
turned by influence.merMod or influence.lme.
groups a character vector containing the name of a grouping factor or names of group-
ing factors; if more than one name is supplied, then groups are defined by all
combinations of levels of the grouping factors that appear in the data. If omit-
ted, then each individual row of the data matrix is treated as a "group" to be
deleted in turn.
data an optional data frame with the data to which model was fit; influence.merMod
can usually reconstruct the data, and influence.lme can access the data unless
keep.data=FALSE was specified in the call to lme, so it’s usually unnecessary
to supply the data argument.
64 influence.mixed.models
Details
influence.merMod and influence.lme start with the estimated variance-covariance components
from model and then refit the model omitting each group in turn, not necessarily iterating to com-
pletion. For example, maxfun=20 takes up to 20 function evaluations step away from the ML or
REML solution for the full data, which usually provides decent approximations to the fully iterated
estimates.
The other functions are methods for the dfbeta, dfbetas, and cooks.distance generics, to be ap-
plied to the "influence.merMod" or "influence.lme" object produced by the influence func-
tion; the dfbeta methods can also return influence on the variance-covariance components.
Value
influence.merMod and influence.lme return objects of class "influence.merMod" and "influence.lme"
respectively, each of which contains the following elements:
"fixed.effects" the estimated fixed effects for the model.
"fixed.effects[-groups]" a matrix with columns corresponding to the fixed-effects coefficients
and rows corresponding to groups, giving the estimated fixed effects with each group deleted
in turn; groups is formed from the name(s) of the grouping factor(s).
"var.cov.comps" the estimated variance-covariance parameters for the model.
"var.cov.comps[-groups]" a matrix with the estimated covariance parameters (in columns) with
each group deleted in turn.
"vcov" The estimated covariance matrix of the fixed-effects coefficients.
"vcov[-groups]" a list each of whose elements is the estimated covariance matrix of the fixed-
effects coefficients with one group deleted.
"groups" a character vector giving the names of the grouping factors.
"deleted" the possibly composite grouping factor, each of whose elements is deleted in turn.
"converged" for influence.merMod, a logical vector indicating whether the computation con-
verged for each group.
"function.evals" for influence.merMod, a vector of the number of function evaluations per-
formed for each group.
For plotting "influence.merMod" and "influence.lme" objects, see infIndexPlot.
influencePlot 65
Author(s)
J. Fox <[email protected]>
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
lmer, glmer, lme, infIndexPlot.
Examples
if (require("lme4")){
print(fm1 <- lmer(Reaction ~ Days + (Days | Subject),
sleepstudy)) # from ?lmer
infIndexPlot(influence(fm1, "Subject"))
}
if (require("lme4")){
gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
data = cbpp, family = binomial)
infIndexPlot(influence(gm1, "herd", maxfun=100))
gm1.11 <- update(gm1, subset = herd != 11) # check deleting herd 11
compareCoefs(gm1, gm1.11)
}
Description
This function creates a “bubble” plot of Studentized residuals versus hat values, with the areas of the
circles representing the observations proportional to the value Cook’s distance. Vertical reference
lines are drawn at twice and three times the average hat value, horizontal reference lines at -2, 0,
and 2 on the Studentized-residual scale.
Usage
influencePlot(model, ...)
Arguments
model a linear, generalized-linear, or linear mixed model; the "lmerMod" method calls
the "lm" method and can take the same arguments.
scale a factor to adjust the size of the circles.
xlab, ylab axis labels.
id settings for labelling points; see link{showLabels} for details. To omit point
labelling, set id=FALSE; the default, id=TRUE is equivalent to id=list(method="noteworthy",n=2,cex=
The default method="noteworthy" is used only in this function and indicates
setting labels for points with large Studentized residuals, hat-values or Cook’s
distances. Set id=list(method="identify") for interactive point identifica-
tion.
... arguments to pass to the plot and points functions.
Value
If points are identified, returns a data frame with the hat values, Studentized residuals and Cook’s
distance of the identified points. If no points are identified, nothing is returned. This function is
primarily used for its side-effect of drawing a plot.
Author(s)
References
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
Examples
## End(Not run)
invResPlot 67
Description
For a lm model, draws an inverse.response plot with the response Y on the vertical axis and the
fitted values Ŷ on the horizontal axis. Uses nls to estimate λ in the function Ŷ = b0 + b1 Y λ . Adds
the fitted curve to the plot. invResPlot is an alias for inverseResponsePlot.
Usage
inverseResponsePlot(model, lambda=c(-1,0,1), robust=FALSE, xlab=NULL, ...)
invResPlot(model, ...)
Arguments
model A "lm" regression object.
lambda A vector of values for lambda. A plot will be produced with curves correspond-
ing to these lambdas and to the nonlinear least squares estimate of lambda.
robust If TRUE, then estimation uses Huber M-estimates with the median absolute de-
viation to estimate scale and k= 1.345. The default is FALSE.
xlab The horizontal axis label. If NULL, it is constructed by the function.
id controls point identification; if FALSE (the default), no points are identified; can
be a list of named arguments to the showLabels function; TRUE is equivalent to
list(method=list(method="x",n=2,cex=1,col=carPalette()[1],location="lr"),
which identifies the 2 points with the most extreme horizontal (X) values.
... Other arguments passed to invTranPlot and then to plot.
Value
As a side effect, a plot is produced with the response on the horizontal axis and fitted values on the
vertical axis. Several lines are added to be plot as the ols estimates of the regression of Ŷ on Y λ ,
interpreting λ = 0 to be natural logarithms.
Numeric output is a list with elements
Author(s)
Sanford Weisberg, [email protected]
References
Fox, J. and Weisberg, S. (2011) An R Companion to Applied Regression, Second Edition, Sage.
Prendergast, L. A., & Sheather, S. J. (2013) On sensitivity of inverse response plot estimation and
the benefits of a robust estimation approach. Scandinavian Journal of Statistics, 40(2), 219-237.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley, Chapter 7.
See Also
invTranPlot, powerTransform, showLabels
Examples
m2 <- lm(rate ~ log(len) + log(adt) + slim + shld + log(sigs1), Highway1)
invResPlot(m2)
Description
invTranPlot draws a two-dimensional scatterplot of Y versus X, along with the OLS fit from the
regression of Y on (X λ − 1)/λ. invTranEstimate finds the nonlinear least squares estimate of λ
and its standard error.
Usage
invTranPlot(x, ...)
## Default S3 method:
invTranPlot(x, y, lambda=c(-1, 0, 1), robust=FALSE,
lty.lines=rep(c("solid", "dashed", "dotdash", "longdash", "twodash"),
length=1 + length(lambda)), lwd.lines=2,
col=carPalette()[1], col.lines=carPalette(),
xlab=deparse(substitute(x)), ylab=deparse(substitute(y)),
family="bcPower", optimal=TRUE, key="auto", id=FALSE,
grid=TRUE, ...)
Arguments
x The predictor variable, or a formula with a single response and a single predictor
y The response variable
data An optional data frame to get the data for the formula
subset Optional, as in lm, select a subset of the cases
na.action Optional, as in lm, the action for missing data
lambda The powers used in the plot. The optimal power than minimizes the residual
sum of squares is always added unless optimal is FALSE.
robust If TRUE, then the estimated transformation is computed using Huber M-estimation
with the MAD used to estimate scale and k=1.345. The default is FALSE.
family The transformation family to use, "bcPower", "yjPower", or a user-defined
family.
confidence returns a profile likelihood confidence interval for the optimal transformation
with this confidence level. If FALSE, or if robust=TRUE, no interval is returned.
optimal Include the optimal value of lambda?
lty.lines line types corresponding to the powers
lwd.lines the width of the plotted lines, defaults to 2 times the standard
col color(s) of the points in the plot. If you wish to distinguish points according
to the levels of a factor, we recommend using symbols, specified with the pch
argument, rather than colors.
col.lines color of the fitted lines corresponding to the powers. The default is to use the
colors returned by carPalette
key The default is "auto", in which case a legend is added to the plot, either above
the top marign or in the bottom right or top right corner. Set to NULL to suppress
the legend.
xlab Label for the horizontal axis.
ylab Label for the vertical axis.
id controls point identification; if FALSE (the default), no points are identified; can
be a list of named arguments to the showLabels function; TRUE is equivalent to
list(method=list(method="x",n=2,cex=1,col=carPalette()[1],location="lr"),
which identifies the 2 points with the most extreme horizontal values — i.e., the
response variable in the model.
... Additional arguments passed to the plot method, such as pch.
grid If TRUE, the default, a light-gray background grid is put on the graph
Value
invTranPlot plots a graph and returns a data frame with λ in the first column, and the residual sum
of squares from the regression for that λ in the second column.
invTranEstimate returns a list with elements lambda for the estimate, se for its standard error,
and RSS, the minimum value of the residual sum of squares.
70 leveneTest
Author(s)
Sanford Weisberg, <[email protected]>
References
Fox, J. and Weisberg, S. (2011) An R Companion to Applied Regression, Second Edition, Sage.
Prendergast, L. A., & Sheather, S. J. (2013) On sensitivity of inverse response plot estimation and
the benefits of a robust estimation approach. Scandinavian Journal of Statistics, 40(2), 219-237.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley, Chapter 7.
See Also
inverseResponsePlot,optimize
Examples
with(UN, invTranPlot(ppgdp, infantMortality))
with(UN, invTranEstimate(ppgdp, infantMortality))
Description
Computes Levene’s test for homogeneity of variance across groups.
Usage
leveneTest(y, ...)
## S3 method for class 'formula'
leveneTest(y, data, ...)
## S3 method for class 'lm'
leveneTest(y, ...)
## Default S3 method:
leveneTest(y, group, center=median, ...)
Arguments
y response variable for the default method, or a lm or formula object. If y is
a linear-model object or a formula, the variables on the right-hand-side of the
model must all be factors and must be completely crossed.
group factor defining groups.
center The name of a function to compute the center of each group; mean gives the
original Levene’s test; the default, median, provides a more robust test.
data a data frame for evaluating the formula.
... arguments to be passed down, e.g., data for the formula and lm methods; can
also be used to pass arguments to the function given by center (e.g., center=mean
and trim=0.1 specify the 10% trimmed mean).
leveragePlots 71
Value
returns an object meant to be printed showing the results of the test.
Note
adapted from a response posted by Brian Ripley to the r-help email list.
Author(s)
John Fox <[email protected]>; original generic version contributed by Derek Ogle
References
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Examples
with(Moore, leveneTest(conformity, fcategory))
with(Moore, leveneTest(conformity, interaction(fcategory, partner.status)))
leveneTest(conformity ~ fcategory*partner.status, data=Moore)
leveneTest(lm(conformity ~ fcategory*partner.status, data=Moore))
leveneTest(conformity ~ fcategory*partner.status, data=Moore, center=mean)
leveneTest(conformity ~ fcategory*partner.status, data=Moore, center=mean, trim=0.1)
Description
These functions display a generalization, due to Sall (1990) and Cook and Weisberg (1991), of
added-variable plots to multiple-df terms in a linear model. When a term has just 1 df, the leverage
plot is a rescaled version of the usual added-variable (partial-regression) plot.
Usage
leveragePlots(model, terms = ~., layout = NULL, ask,
main, ...)
leveragePlot(model, ...)
Arguments
model model object produced by lm
terms A one-sided formula that specifies a subset of the numeric regressors, factors
and interactions. One added-variable plot is drawn for each term, either a main
effect or an interactions. The default ~. is to plot against all terms in the model.
For example, the specification terms = ~ . -X3 would plot against all predictors
except for X3. If this argument is a quoted name of one of the predictors, the
added-variable plot is drawn for that predictor only. The plots for main effects
with interactions present violate the marginality principle and may not be easily
interpreted.
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, a menu is provided in the R Console for the user to select the term(s) to
plot.
xlab, ylab axis labels; if missing, labels will be supplied.
main title for plot; if missing, a title will be supplied.
... arguments passed down to method functions.
term.name Quoted name of term in the model to be plotted; this argument is omitted for
leveragePlots.
id controls point identification; if FALSE, no points are identified; can be a list of
named arguments to the showLabels function; TRUE, the default, is equivalent to
list(method=list(abs(residuals(model,type="pearson")),"x"),n=2,cex=1,col=carPalette(
which identifies the 2 points with the largest residuals and the 2 points with the
greatest partial leverage.
col color(s) of points
col.lines color of the fitted line
lwd line width; default is 2 (see par).
grid If TRUE, the default, a light-gray background grid is put on the graph
Details
The function intended for direct use is leveragePlots.
The model can contain factors and interactions. A leverage plot can be drawn for each term in the
model, including the constant.
leveragePlot.glm is a dummy function, which generates an error message.
Value
NULL. These functions are used for their side effect: producing plots.
linearHypothesis 73
Author(s)
John Fox <[email protected]>
References
Cook, R. D. and Weisberg, S. (1991). Added Variable Plots in Linear Regression. In Stahel, W. and
Weisberg, S. (eds.), Directions in Robust Statistics and Diagnostics. Springer, 47-60.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Sall, J. (1990) Leverage plots for general linear hypotheses. American Statistician 44, 308–315.
See Also
avPlots
Examples
leveragePlots(lm(prestige~(income+education)*type, data=Duncan))
Description
Generic function for testing a linear hypothesis, and methods for linear models, generalized linear
models, multivariate linear models, linear and generalized linear mixed-effects models, generalized
linear models fit with svyglm in the survey package, robust linear models fit with rlm in the MASS
package, and other models that have methods for coef and vcov. For mixed-effects models, the
tests are Wald chi-square tests for the fixed effects.
Usage
linearHypothesis(model, ...)
lht(model, ...)
## Default S3 method:
linearHypothesis(model, hypothesis.matrix, rhs=NULL,
test=c("Chisq", "F"), vcov.=NULL, singular.ok=FALSE, verbose=FALSE,
coef. = coef(model), ...)
## Default S3 method:
matchCoefs(model, pattern, coef.=coef, ...)
linearHypothesis 75
Arguments
model fitted model object. The default method of linearHypothesis works for mod-
els for which the estimated parameters can be retrieved by coef and the corre-
sponding estimated covariance matrix by vcov. See the Details for more infor-
mation.
hypothesis.matrix
matrix (or vector) giving linear combinations of coefficients by rows, or a char-
acter vector giving the hypothesis in symbolic form (see Details).
rhs right-hand-side vector for hypothesis, with as many entries as rows in the hy-
pothesis matrix; can be omitted, in which case it defaults to a vector of zeroes.
For a multivariate linear model, rhs is a matrix, defaulting to 0. This argument
isn’t available for F-tests for linear mixed models.
singular.ok if FALSE (the default), a model with aliased coefficients produces an error; if
TRUE, the aliased coefficients are ignored, and the hypothesis matrix should not
have columns for them. For a multivariate linear model: will return the hypoth-
esis and error SSP matrices even if the latter is singular; useful for computing
univariate repeated-measures ANOVAs where there are fewer subjects than df
for within-subject effects.
idata an optional data frame giving a factor or factors defining the intra-subject model
for multivariate repeated-measures data. See Details for an explanation of the
intra-subject design and for further explanation of the other arguments relating
to intra-subject factors.
icontrasts names of contrast-generating functions to be applied by default to factors and
ordered factors, respectively, in the within-subject “data”; the contrasts must
produce an intra-subject model matrix in which different terms are orthogonal.
idesign a one-sided model formula using the “data” in idata and specifying the intra-
subject design.
iterms the quoted name of a term, or a vector of quoted names of terms, in the intra-
subject design to be tested.
check.imatrix check that columns of the intra-subject model matrix for different terms are mu-
tually orthogonal (default, TRUE). Set to FALSE only if you have already checked
that the intra-subject model matrix is block-orthogonal.
76 linearHypothesis
Details
linearHypothesis computes either a finite-sample F statistic or asymptotic Chi-squared statistic
for carrying out a Wald-test-based comparison between a model and a linearly restricted model. The
default method will work with any model object for which the coefficient vector can be retrieved
by coef and the coefficient-covariance matrix by vcov (otherwise the argument vcov. has to be set
explicitly). For computing the F statistic (but not the Chi-squared statistic) a df.residual method
needs to be available. If a formula method exists, it is used for pretty printing.
The method for "lm" objects calls the default method, but it changes the default test to "F", supports
the convenience argument white.adjust (for backwards compatibility), and enhances the output
linearHypothesis 77
by the residual sums of squares. For "glm" objects just the default method is called (bypassing the
"lm" method). The svyglm method also calls the default method.
The function lht also dispatches to linearHypothesis.
The hypothesis matrix can be supplied as a numeric matrix (or vector), the rows of which specify
linear combinations of the model coefficients, which are tested equal to the corresponding entries
in the right-hand-side vector, which defaults to a vector of zeroes.
Alternatively, the hypothesis can be specified symbolically as a character vector with one or more
elements, each of which gives either a linear combination of coefficients, or a linear equation in the
coefficients (i.e., with both a left and right side separated by an equals sign). Components of a linear
expression or linear equation can consist of numeric constants, or numeric constants multiplying
coefficient names (in which case the number precedes the coefficient, and may be separated from
it by spaces or an asterisk); constants of 1 or -1 may be omitted. Spaces are always optional.
Components are separated by plus or minus signs. Newlines or tabs in hypotheses will be treated
as spaces. See the examples below.
If the user sets the arguments coef. and vcov., then the computations are done without reference
to the model argument. This is like assuming that coef. is normally distibuted with estimated vari-
ance vcov. and the linearHypothesis will compute tests on the mean vector for coef., without
actually using the model argument.
A linear hypothesis for a multivariate linear model (i.e., an object of class "mlm") can optionally
include an intra-subject transformation matrix for a repeated-measures design. If the intra-subject
transformation is absent (the default), the multivariate test concerns all of the corresponding coef-
ficients for the response variables. There are two ways to specify the transformation matrix for the
repeated measures:
1. The transformation matrix can be specified directly via the P argument.
2. A data frame can be provided defining the repeated-measures factor or factors via idata,
with default contrasts given by the icontrasts argument. An intra-subject model-matrix is
generated from the one-sided formula specified by the idesign argument; columns of the
model matrix corresponding to different terms in the intra-subject model must be orthogonal
(as is insured by the default contrasts). Note that the contrasts given in icontrasts can
be overridden by assigning specific contrasts to the factors in idata. The repeated-measures
transformation matrix consists of the columns of the intra-subject model matrix corresponding
to the term or terms in iterms. In most instances, this will be the simpler approach, and
indeed, most tests of interests can be generated automatically via the Anova function.
matchCoefs is a convenience function that can sometimes help in formulating hypotheses; for
example matchCoefs(mod,":") will return the names of all interaction coefficients in the model
mod.
Value
For a univariate model, an object of class "anova" which contains the residual degrees of freedom
in the model, the difference in degrees of freedom, Wald statistic (either "F" or "Chisq"), and
corresponding p value. The value of the linear hypothesis and its covariance matrix are returned
respectively as "value" and "vcov" attributes of the object (but not printed).
For a multivariate linear model, an object of class "linearHypothesis.mlm", which contains sums-
of-squares-and-product matrices for the hypothesis and for error, degrees of freedom for the hypoth-
esis and error, and some other information.
78 linearHypothesis
Author(s)
Achim Zeileis and John Fox <[email protected]>
References
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Hand, D. J., and Taylor, C. C. (1987) Multivariate Analysis of Variance and Repeated Measures: A
Practical Approach for Behavioural Scientists. Chapman and Hall.
O’Brien, R. G., and Kaiser, M. K. (1985) MANOVA method for analyzing repeated measures de-
signs: An extensive primer. Psychological Bulletin 97, 316–333.
See Also
anova, Anova, waldtest, hccm, vcovHC, vcovHAC, coef, vcov
Examples
mod.davis <- lm(weight ~ repwt, data=Davis)
linearHypothesis(mod.duncan, "income-education=0")
linearHypothesis(mod.duncan, "1*income - 1*education + 1 = 1")
linearHypothesis(mod.duncan, "2income = 2*education")
## test against the null model (i.e., only the intercept is not set to 0)
linearHypothesis(mod.duncan.2, coefs[-1])
## Not run:
library(nlme)
example(lme)
linearHypothesis(fm2, "age = 0")
## End(Not run)
## Not run:
library(lme4)
example(glmer)
80 logit
## End(Not run)
Description
Compute the logit transformation of proportions or percentages.
Usage
logit(p, percents=range.p[2] > 1, adjust)
Arguments
p numeric vector or array of proportions or percentages.
percents TRUE for percentages.
adjust adjustment factor to avoid proportions of 0 or 1; defaults to 0 if there are no such
proportions in the data, and to .025 if there are.
Details
Computes the logit transformation logit = log[p/(1 − p)] for the proportion p.
If p = 0 or 1, then the logit is undefined. logit can remap the proportions to the interval (adjust,1
-adjust) prior to the transformation. If it adjusts the data automatically, logit will print a warning
message.
Value
a numeric vector or array of the same shape and size as p.
Author(s)
John Fox <[email protected]>
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
probabilityAxis
mcPlots 81
Examples
options(digits=4)
logit(.1*0:10)
## [1] -3.6636 -1.9924 -1.2950 -0.8001 -0.3847 0.0000 0.3847
## [8] 0.8001 1.2950 1.9924 3.6636
## Warning message:
## proportions remapped to (0.025, 0.975) in: logit(0.1 * 0:10)
logit(.1*0:10, adjust=0)
## [1] -Inf -2.1972 -1.3863 -0.8473 -0.4055 0.0000 0.4055
## [8] 0.8473 1.3863 2.1972 Inf
Description
the mcPlot function draws two graphs or overlays the two graphs. For a response Y and a regressor
X, the first plot is the marginal 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 conditional 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.
Usage
mcPlots(model, ...)
## Default S3 method:
mcPlots(model, terms=~., layout=NULL, ask, overlaid=TRUE, ...)
mcPlot(model, ...)
Arguments
terms A one-sided formula that specifies a subset of the predictors. One added-variable
plot is drawn for each regressor and for each basis vector used to define a factor.
For example, the specification terms = ~ . -X3 would plot against all terms ex-
cept for X3. If this argument is a quoted name of one of the regressors or factors,
the added-variable plot is drawn for that regressor or factor only. Unlike other
car functions, the formula should include the names of regressors, not predic-
tors. That is, if log(X4) is used to represent a predictor X4, the formula should
specify terms = ~ log(X4).
variable A quoted string giving the name of a numeric predictor in the model matrix for
the horizontal 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 type with levels A, B and C, using the usual drop-first level parameteriza-
tion of the factor, the regressors for type would be typeB or typeC. Similarly,
to plot against the regressor log(X4), you must specify "log((X4)", not "X4".
layout If set to a value like c(1,2) or c(6,2), the layout of the graph will have this
many rows and columns. If not set, behavior depends on the value of the
overlaid argument; see the details
ask If TRUE, ask the user before drawing the next plot; if FALSE don’t ask.
... mcPlots passes these arguments to mcPlot. mcPlot passes arguments to plot.
id controls point identification; if FALSE (the default), no points are identified; can
be a list of named arguments to the showLabels function; TRUE is equivalent to
list(method=list(abs(residuals(model,type="pearson")),"x"),n=2,cex=1,col=carPalette(
which identifies the 2 points with the largest residuals and the 2 points with the
most extreme horizontal (X) values.
overlaid If TRUE, the default, overlay the marginal and conditional plots on the same
graph; otherwise plot them side-by-side. See the details below
col.marginal, col.conditional
colors for points, lines, ellipses in the marginal and conditional plots, respec-
tively. The defaults are determined by the carPalette function.
col.arrows color for the arrows with overlaid=TRUE
pch Plotting character for marginal and conditional plots, respectively.
lwd line width; default is 2 (see par).
grid If TRUE, the default, a light-gray background grid is put on the graph.
ellipse Arguments to pass to the dataEllipse function, in the form of a list with named
elements; e.g., ellipse.args=list(robust=TRUE)) will cause the ellipse to
be plotted using a robust covariance-matrix. if FALSE, the default, no ellipse
is plotted. TRUE is equivalent to ellipse=list(levels=0.5), which plots a
bivariate-normal 50 percent concentration ellipse.
new if TRUE, the default, the plot window is reset when overlaid=FALSE using
par{mfrow=c(1,2)}. If FALSE, the layout of the plot window is not reset. Users
will ordinarily ignore this argument.
title If TRUE, the default, the standard main argument in plot is used to add a stan-
dard title to each plot. If FALSE no title is used.
mcPlots 83
Details
With an 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 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 overlaid=FALSE, then the two graphs are shown in side-by-side plots as long as the second
argument to layout is equal to 2, or layout is set by the function. The arrows are omitted if the
graphs are not overlaid.
These 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.
This 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.
These graphs are closely related to the ARES plots proposed by Cook and Weisberg (1989). This
plot would benefit from animation.
Value
These functions are used for their side effect of producing plots.
Author(s)
References
Cook, R. D. and Weisberg, S. (1989) Regression diagnostics with dynamic graphics, Technometrics,
31, 277.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley.
See Also
Examples
m1 <- lm(partic ~ tfr + menwage + womwage + debt + parttime, data = Bfox)
mcPlot(m1, "womwage")
mcPlot(m1, "womwage", overlaid=FALSE, ellipse=TRUE)
Description
For a regression object, draw a plot of the response on the vertical axis versus a linear combina-
tion 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(...)
marginalModelPlot(...)
mmp(model, ...)
## Default S3 method:
mmp(model, variable, sd = FALSE,
xlab = deparse(substitute(variable)), ylab, smooth=TRUE,
key=TRUE, pch, groups=NULL,
col.line = carPalette()[c(2, 8)], col=carPalette()[1],
id=FALSE, 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 TRUE, the default, then a marginal model plot in the direction of the fitted
values for a linear model or the 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. If this argument is missing,
the horizontal variable is the linear predictor, returned by predict(object)
for models of class lm, with default label "Fitted values", or returned by
predict(object,type="link") for models of class glm, with default label
"Linear predictor". It can be any other vector of length equal to the num-
ber 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.
ylab label for vertical axis, defaults to name of response.
smooth specifies the smoother to be used along with its arguments; if FALSE, no smoother
is shown; can be a list giving the smoother function and its named arguments;
TRUE, the default, is equivalent to list(smoother=loessLine,span=2/3) for
linear models and list(smoother=gamLine,k=3) for generalized linear mod-
els. See ScatterplotSmoothers for the smoothers supplied by the car package
and their arguments; the spread argument is not supported for marginal model
plots.
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.
86 mmps
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 controls point identification; if FALSE (the default), no points are identified; can
be a list of named arguments to the showLabels function; TRUE is equivalent to
list(method="y",n=2,cex=1,col=carPalette()[1],location="lr"), which
identifies the 2 points with the most unusual response (Y) values.
pch plotting character to use if no grouping is present.
col.line colors for data and model smooth, respectively. The default is to use carPalette,
carPalette()[c(2,8)], 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
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.
For linear models, the default smoother is loess. 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.
Value
Used for its side effect of producing plots.
Author(s)
Sanford Weisberg, <[email protected]>
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. (2019) An R Companion to Applied Regression, Third Edition. Sage.
Weisberg, S. (2005) Applied Linear Regression, Third Edition, Wiley, Section 8.4.
ncvTest 87
See Also
ScatterplotSmoothers, plot
Examples
c1 <- lm(infantMortality ~ ppgdp, UN)
mmps(c1)
c2 <- update(c1, ~ log(ppgdp))
mmps(c2)
# 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)
Description
Computes a score test of the hypothesis of constant error variance against the alternative that the
error variance changes with the level of the response (fitted values), or with a linear combination of
predictors.
Usage
ncvTest(model, ...)
Arguments
model a weighted or unweighted linear model, produced by lm.
var.formula a one-sided formula for the error variance; if omitted, the error variance depends
on the fitted values.
... arguments passed down to methods functions; not currently used.
88 outlierTest
Details
This test is often called the Breusch-Pagan test; it was independently suggested with some extension
by Cook and Weisberg (1983).
ncvTest.glm is a dummy function to generate an error when a glm model is used.
Value
The function returns a chisqTest object, which is usually just printed.
Author(s)
John Fox <[email protected]>, Sandy Weisberg <[email protected]>
References
Breusch, T. S. and Pagan, A. R. (1979) A simple test for heteroscedasticity and random coefficient
variation. Econometrica 47, 1287–1294.
Cook, R. D. and Weisberg, S. (1983) Diagnostics for heteroscedasticity in regression. Biometrika
70, 1–10.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley.
See Also
hccm, spreadLevelPlot
Examples
ncvTest(lm(interlocks ~ assets + sector + nation, data=Ornstein))
Description
Reports the Bonferroni p-values for testing each observation in turn to be a mean-shift outlier, based
Studentized residuals in linear (t-tests), generalized linear models (normal tests), and linear mixed
models.
outlierTest 89
Usage
outlierTest(model, ...)
Arguments
model an lm, glm, or lmerMod model object; the "lmerMod" method calls the "lm"
method and can take the same arguments.
cutoff observations with Bonferroni p-values exceeding cutoff are not reported, un-
less no observations are nominated, in which case the one with the largest Stu-
dentized residual is reported.
n.max maximum number of observations to report (default, 10).
order report Studenized residuals in descending order of magnitude? (default, TRUE).
labels an optional vector of observation names.
... arguments passed down to methods functions.
x outlierTest object.
digits number of digits for reported p-values.
Details
For a linear model, p-values reported use the t distribution with degrees of freedom one less than the
residual df for the model. For a generalized linear model, p-values are based on the standard-normal
distribution. The Bonferroni adjustment multiplies the usual two-sided p-value by the number of
observations. The lm method works for glm objects. To show all of the observations set cutoff=Inf
and n.max=Inf.
Value
Author(s)
References
Cook, R. D. and Weisberg, S. (1982) Residuals and Influence in Regression. Chapman and Hall.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley.
Williams, D. A. (1987) Generalized linear model diagnostics using the deviance and single case
deletions. Applied Statistics 36, 181–191.
Examples
outlierTest(lm(prestige ~ income + education, data=Duncan))
Description
a panel function for use with coplot that plots points, a lowess line, and a regression line.
Usage
panel.car(x, y, col, pch, cex=1, span=0.5, lwd=2,
reg.line=lm, lowess.line=TRUE, ...)
Arguments
x vector giving horizontal coordinates.
y vector giving vertical coordinates.
col point color.
pch plotting character for points.
cex character expansion factor for points.
span span for lowess smoother.
lwd line width, default is 2.
reg.line function to compute coefficients of regression line, or FALSE for no line.
lowess.line if TRUE plot lowess smooth.
... other arguments to pass to functions lines and regLine.
Value
NULL. This function is used for its side effect: producing a panel in a coplot.
Author(s)
John Fox <[email protected]>
poTest 91
See Also
coplot, regLine
Examples
coplot(prestige ~ income|education, panel=panel.car,
col="red", data=Prestige)
Description
The poTest function implements tests proposed by Brant (1990) for proportional odds for logistic
models fit by the polr function in the MASS package.
Usage
poTest(model, ...)
## S3 method for class 'polr'
poTest(model, ...)
## S3 method for class 'poTest'
print(x, digits=3, ...)
Arguments
model a proptional-odds logit model fit by polr.
x an object produced by poTest.
digits number of significant digits to print.
... ignored.
Value
poTest returns an object meant to be printed showing the results of the tests.
Author(s)
John Fox <[email protected]>
References
R. Brant, "Assessing proportionality in the proportional odds model for ordinal logistic regression."
Biometrics 46: 1171–1178, 1990.
92 powerTransform
Examples
if (require("MASS")){
.W <- Womenlf
.W$partic <- factor(.W$partic, levels=c("not.work", "parttime", "fulltime"))
poTest(polr(partic ~ hincome + children + region, data=.W))
}
Description
powerTransform uses the maximum likelihood-like approach of Box and Cox (1964) to select
a transformatiion of a univariate or multivariate response for normality, linearity and/or constant
variance. Available families of transformations are the default Box-Cox power family and two
additioal families that are modifications of the Box-Cox family that allow for (a few) negative
responses. The summary method automatically computes two or three likelihood ratio type tests
concerning the transformation powers.
Usage
powerTransform(object, ...)
## Default S3 method:
powerTransform(object, family="bcPower", ...)
Arguments
object This can either be an object of class lm or lmerMod, a formula, or a matrix or
vector; see below.
family The quoted name of a family of transformations. The available options are
"bcPower" for the default for the Box-Cox power family; "bcnPower" for a
two-parameter modification of the Box-Cox family that allows negative responses
(Hawkins and Weisberg (2017)), and the "yjPower" family (Yeo and John-
son(2000)), another modifiation of the Box-Cox family that allows a few nega-
tive values. All three families are documented at bcPower.
powerTransform 93
Details
This function implements the Box and Cox (1964) method of selecting a power transformation of a
variable toward normality, and its generalization by Velilla (1993) to a multivariate response. Cook
and Weisberg (1999) and Weisberg (2014) suggest the usefulness of transforming a set of predictors
z1,z2,z3 for multivariate normality. It also includes two additional families that allow for negative
values.
If the object argument is of class ‘lm’ or ‘lmerMod’, the Box-Cox procedure is applied to the
conditional distribution of the response given the predictors. For ‘lm’ objects, the respose may be
multivariate, and each column will have its own transformation. With ‘lmerMod’ the response must
be univariate.
The object argument may also be a formula. For example, z ~ x1 + x2 + x3 will estimate a trans-
formation for the response z from a family after fitting a linear model with the given formula.
cbind(y1,y2,y3) ~ 1 specifies transformations to multivariate normality with no predictors. A
vector value for object, for example powerTransform(ais$LBM), is equivalent topowerTransform(LBM
~ 1,ais). Similarly, powerTransform(cbind(ais$LBM,ais$SSF)), where the first argument is a
matrix rather than a formula is equivalent to specification of a mulitvariate linear model powerTransform(cbind(LBM,SSF)
~ 1,ais).
Three families of power transformations are available. The default Box-Cox power family (family="bcPower")
of power transformations effectively replaces a vector by that vector raised to a power, generally
in the range from -3 to 3. For powers close to zero, the log-transformtion is suggested. In prac-
tical situations, after estimating a power using the powerTransform function, a variable would be
replaced by a simple power transformation of it, for example, if λ ≈ 0.5, then the correspoding
variable would be replaced by its square root; if λ is close enough to zero, the the variable would be
replaced by its natural logarithm. The Box-Cox family requires the responses to be strictly positive.
The family="bcnPower", or Box-Cox with negatives, family proposed by Hawkins and Weisberg
(2017) allows for (a few) non-positive values, while allowing for the transformed data to be inter-
preted similarly to the interpretation of Box-Cox transformed values. This family is the Box-Cox
transformation of z = .5∗(y +(y 2 +γ 2 )1/2 ) that depends on a location parameter γ. The quantity z
is positive for all values of y. If γ = 0 and y is strictly positive, then the Box-Cox and the bcnPower
transformations are identical. When fitting the Box-Cox with negatives family, lambda is restricted
to the range [-3, 3], and gamma is restricted to the range from gamma.min=.1 to the largest positive
value of the variable, since values outside these ranges are unreasonable in practice. The value of
gamma.min can be changed with an argument to powerTransform.
The final family family="yjPower" uses the Yeo-Johnson transformation, which is the Box-Cox
transformation of U + 1 for nonnegative values, and of |U | + 1 with parameter 2 − λ for U negative
and thus it provides a family for fitting when (a few) observations are negative. Because of the
94 powerTransform
unusual constraints on the powers for positive and negative data, this transformation is not used
very often, as results are difficult to interpret. In practical problems, a variable would be replaced
by its Yeo-Johnson transformation computed using the yjPower function.
The function testTransform is used to obtain likelihood ratio tests for any specified value for the
transformation parameter(s).
Computations maximize the likelihood-like functions described by Box and Cox (1964) and by
Velilla (1993). For univariate responses, the computations are very stable and problems are unlikely,
although for ‘lmer’ models computations may be very slow because the model is refit many times.
For multivariate responses with the bcnPower family, the computing algorithm may fail. In this case
we recommend adding the argument itmax = 1 to the call to powerTransform. This will return the
starting value estimates of the transformation parameters, fitting a d-dimensional response as if all
the d responses were independent.
Value
An object of class powerTransform or class bcnPowerTransform if family="bcnPower" that in-
herits from powerTransform is returned, including the components listed below.
A summary method presents estimated values for the transformation power lambda and for the
‘bcnPower’ family the location parameter gamma as well. Standard errors and Wald 95% confidence
intervals based on the standard errors are computed from the inverse of the sample Hessian matrix
evaluted at the estimates. The interval estimates for the gamma parameters will generally be very
wide, reflecting little information available about the location parameter. Likelihood ratio type tests
are also provided. For the ‘bcnPower’ family these are based on the profile loglikelihood for lambda
alone; that is, we treat gamma as a nusiance parameter and average over it.
The components of the returned object includes
The summary method for powerTransform returns an array with columns labeled "Est Power" for
the value of lambda that maximizes the likelihood; "Rounded Pwr" for roundlam, and columns
"Wald Lwr Bnd" and "Wald Ur Bnd" for a 95 percent Wald normal theory confidence interval for
lambda computed as the estimate plus or minus 1.96 times the standard error.
Author(s)
Sanford Weisberg, <[email protected]>
References
Box, G. E. P. and Cox, D. R. (1964) An analysis of transformations. Journal of the Royal Statisisti-
cal Society, Series B. 26 211-46.
powerTransform 95
Cook, R. D. and Weisberg, S. (1999) Applied Regression Including Computing and Graphics. Wi-
ley.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Hawkins, D. and Weisberg, S. (2017) Combining the Box-Cox Power and Generalized Log Trans-
formations to Accomodate Nonpositive Responses In Linear and Mixed-Effects Linear Models
South African Statistics Journal, 51, 317-328.
Velilla, S. (1993) A note on the multivariate Box-Cox transformation to normality. Statistics and
Probability Letters, 17, 259-263.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley.
Yeo, I. and Johnson, R. (2000) A new family of power transformations to improve normality or
symmetry. Biometrika, 87, 954-959.
See Also
testTransform, bcPower, bcnPower, transform, optim, boxCox.
Examples
# Box Cox Method, univariate
summary(p1 <- powerTransform(cycles ~ len + amp + load, Wool))
# fit linear model with transformed response:
coef(p1, round=TRUE)
summary(m1 <- lm(bcPower(cycles, p1$roundlam) ~ len + amp + load, Wool))
## Not run:
# takes a few seconds:
# multivariate bcnPower, with 8 responses
summary(powerTransform(update(m2, as.matrix(LoBD[, -1]) ~ .), family="bcnPower"))
# multivariate bcnPower, fit with one iteration using starting values as estimates
96 Predict
## End(Not run)
## End(Not run)
Description
Predict is a generic function with, at present, a single method for "lm" objects, Predict.lm, which
is a modification of the standard predict.lm method in the stats package, but with an additional
vcov. argument for a user-specified covariance matrix for intreval estimation.
Usage
Predict(object, ...)
Arguments
Details
If there is no appropriate method for Predict, then a predict method is invoked. If there is a
specific predict method for the primary class of object but only an inherited Predict method,
then the predict method is invoked. Thus an object of class c("glm","lm") will invoke method
predict.glm rather than Predict.lm, but an object of class c("aov","lm") will invoke Predict.lm
rather than predict.lm.
Value
Author(s)
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
predict, predict.lm
Examples
mod <- lm(interlocks ~ log(assets), data=Ornstein)
newd <- data.frame(assets=exp(4:12))
(p1 <- predict(mod, newd, interval="prediction"))
p2 <- Predict(mod, newd, interval="prediction", vcov.=vcov)
all.equal(p1, p2) # the same
Description
Plots empirical quantiles of a variable, or of studentized residuals from a linear model, against
theoretical quantiles of a comparison distribution. Includes options not available in the qqnorm
function.
98 qqPlot
Usage
qqPlot(x, ...)
qqp(...)
## Default S3 method:
qqPlot(x, distribution="norm", groups, layout,
ylim=range(x, na.rm=TRUE), ylab=deparse(substitute(x)),
xlab=paste(distribution, "quantiles"), glab=deparse(substitute(groups)),
main=NULL, las=par("las"),
envelope=.95, col=carPalette()[1], col.lines=carPalette()[2],
lwd=2, pch=1, cex=par("cex"),
line=c("quartiles", "robust", "none"), id=TRUE, grid=TRUE, ...)
Arguments
x vector of numeric values or lm object.
distribution root name of comparison distribution – e.g., "norm" for the normal distribution;
t for the t-distribution.
groups an optional factor; if specified, a QQ plot will be drawn for x within each level
of groups.
layout a 2-vector with the number of rows and columns for plotting by groups – for
example c(1,3) for 1 row and 3 columns; if omitted, the number of rows
and columns will be selected automatically; the specified number of rows and
columns must be sufficient to accomodate the number of groups; ignored if there
is no grouping factor.
formula one-sided formula specifying a single variable to be plotted or a two-sided for-
mula of the form variable ~ factor, where a QQ plot will be drawn for variable
within each level of factor.
data optional data frame within which to evaluage the formula.
subset optional subset expression to select cases to plot.
ylim limits for vertical axis; defaults to the range of x. If plotting by groups, a com-
mon y-axis is used for all groups.
qqPlot 99
Details
Draws theoretical quantile-comparison plots for variables and for studentized residuals from a linear
model. A comparison line is drawn on the plot either through the quartiles of the two distributions,
or by robust regression.
Any distribution for which quantile and density functions exist in R (with prefixes q and d, respec-
tively) may be used. When plotting a vector, the confidence envelope is based on the SEs of the
order statistics of an independent random sample from the comparison distribution (see Fox, 2016).
Studentized residuals from linear models are plotted against the appropriate t-distribution with a
point-wise confidence envelope computed by default by a parametric bootstrap, as described by
Atkinson (1985). The function qqp is an abbreviation for qqPlot.
Value
These functions return the labels of identified points, unless a grouping factor is employed, in which
case NULL is returned invisibly.
100 recode
Author(s)
John Fox <[email protected]>
References
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Atkinson, A. C. (1985) Plots, Transformations, and Regression. Oxford.
See Also
qqplot, qqnorm, qqline, showLabels
Examples
x<-rchisq(100, df=2)
qqPlot(x)
qqPlot(x, dist="chisq", df=2)
Description
Recodes a numeric vector, character vector, or factor according to simple recode specifications.
Recode is an alias for recode that avoids name clashes with packages, such as Hmisc, that have a
recode function.
Usage
recode(var, recodes, as.factor, as.numeric=TRUE, levels)
Recode(...)
Arguments
var numeric vector, character vector, or factor.
recodes character string of recode specifications: see below.
as.factor return a factor; default is TRUE if var is a factor, FALSE otherwise.
as.numeric if TRUE (the default), and as.factor is FALSE, then the result will be coerced to
numeric if all values in the result are numerals—i.e., represent numbers.
recode 101
levels an optional argument specifying the order of the levels in the returned factor; the
default is to use the sort order of the level names.
... arguments to be passed to recode.
Details
Recode specifications appear in a character string, separated by semicolons (see the examples be-
low), of the form input=output. If an input value satisfies more than one specification, then the
first (from left to right) applies. If no specification is satisfied, then the input value is carried over
to the result. NA is allowed on input and output. Several recode specifications are supported:
Character data and factor levels on the left-hand side of a recode specification must be quoted.
Thus, e.g., c(a,b,c) = 'low' is not allowed, and should be c('a','b','c') = 'low'. Similarly,
the colon is reserved for numeric data, and, e.g., c('a':'c') = 'low' is not allowed. If the var
argument is a character variable with (some) values that are composed of numerals, or a factor
with (some) levels that are numerals (e.g., '12' or '-2'), then these too must be quoted and can-
not be used with colons (e.g., '15':'19' = '15 to 19' is not allowed, and could be specified as
c('15','16','17','18','19') = '15 to 19', assuming that all values are the character repre-
sentation of whole numbers).
If all of the output values are numeric, and if as.factor is FALSE, then a numeric result is returned;
if var is a factor, then by default so is the result.
Value
a recoded vector of the same length as var.
Author(s)
John Fox <[email protected]>
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
cut, factor
102 regLine
Examples
x<-rep(1:3,3)
x
## [1] 1 2 3 1 2 3 1 2 3
recode(x, "c(1,2)='A';
else='B'")
## [1] "A" "A" "B" "A" "A" "B" "A" "A" "B"
Recode(x, "1:2='A'; 3='B'")
## [1] "A" "A" "B" "A" "A" "B" "A" "A" "B"
Description
Plots a regression line on a scatterplot; the line is plotted between the minimum and maximum
x-values.
Usage
regLine(mod, col=carPalette()[2], lwd=2, lty=1,...)
Arguments
mod a model, such as produced by lm, that responds to the coef function by returning
a 2-element vector, whose elements are interpreted respectively as the intercept
and slope of a regresison line.
col color for points and lines; the default is the second entry in the current car
palette (see carPalette and par).
lwd line width; default is 2 (see par).
lty line type; default is 1, a solid line (see par).
... optional arguments to be passed to the lines plotting function.
Details
In contrast to abline, this function plots only over the range of the observed x-values. The x-values
are extracted from mod as the second column of the model matrix.
Value
NULL. This function is used for its side effect: adding a line to the plot.
Author(s)
John Fox <[email protected]>
residualPlots 103
See Also
abline, lines
Examples
plot(repwt ~ weight, pch=c(1,2)[sex], data=Davis)
regLine(lm(repwt~weight, subset=sex=="M", data=Davis))
regLine(lm(repwt~weight, subset=sex=="F", data=Davis), lty=2)
Description
Plots the residuals versus each term in a mean function and versus fitted values. Also computes a
curvature test for each of the plots by adding a quadratic term and testing the quadratic to be zero.
For linear models, this is Tukey’s test for nonadditivity when plotting against fitted values.
Usage
### This is a generic function with only one required argument:
## Default S3 method:
residualPlots(model, terms = ~., layout = NULL, ask,
main = "", fitted = TRUE, AsIs=TRUE, plot = TRUE,
tests = TRUE, groups, ...)
residualPlot(model, ...)
## Default S3 method:
residualPlot(model, variable = "fitted", type = "pearson",
groups, plot = TRUE, linear = TRUE,
quadratic = if(missing(groups)) TRUE else FALSE,
smooth=FALSE, id=FALSE,
col = carPalette()[1], col.quad = carPalette()[2], pch=1,
xlab, ylab, lwd = 1, lty = 1,
104 residualPlots
Arguments
type Type of residuals to be used. Pearson residuals are appropriate for lm objects
since these are equivalent to ordinary residuals with ols and correctly weighted
residuals with wls. Any quoted string that is an appropriate value of the type
argument to residuals.lm or "rstudent" or "rstandard" for Studentized or
standardized residuals.
groups A grouping indicator. Points in different groups will be plotted with different
colors and symbols. If missing, no grouping is used. In residualPlots, the
grouping variable can also be set in the terms argument, as described above.
The default is no grouping. If used, the groups argument shoud be a vector
of values of the same length as the vector of residuals, for example groups =
subject where subject indicates the grouping.
linear If TRUE, adds a horizontal line at zero if no groups. With groups, display the
within level of groups ols regression of the residuals as response and the hori-
zontal axis as the regressor.
quadratic if TRUE, fits the quadratic regression of the vertical axis on the horizontal axis
and displays a lack of fit test. Default is TRUE for lm and FALSE for glm or if
groups not missing.
smooth specifies the smoother to be used along with its arguments; if FALSE, which is
the default except for generalized linear models, no smoother is shown; can be a
list giving the smoother function and its named arguments; TRUE is equivalent to
list(smoother=loessLine,span=2/3,col=carPalette()[3]), which is the
default for a GLM. See ScatterplotSmoothers for the smoothers supplied by
the car package and their arguments.
id controls point identification; if FALSE (the default), no points are identified; can
be a list of named arguments to the showLabels function; TRUE is equivalent to
list(method="r",n=2,cex=1,col=carPalette()[1],location="lr"), which
identifies the 2 points with the largest absolute residuals.
col default color for points. If groups is set, col can be a list at least as long as the
number of levels for groups giving the colors for each groups.
col.quad default color for quadratic fit if groups is missing. Ignored if groups are used.
pch plotting character. The default is pch=1. If groups are used, pch can be set to a
vector at least as long as the number of groups.
xlab X-axis label. If not specified, a useful label is constructed by the function.
ylab Y-axis label. If not specified, a useful label is constructed by the function.
lwd line width for lines.
lty line type for quadratic.
grid If TRUE, the default, a light-gray background grid is put on the graph
key Should a key be added to the plot? Default is !is.null(groups).
Details
residualPlots draws one or more residuals plots depending on the value of the terms and fitted
arguments. If terms = ~ ., the default, then a plot is produced of residuals versus each first-order
term in the formula used to create the model. Interaction terms, spline terms, and polynomial terms
106 residualPlots
of more than one predictor are skipped. In addition terms that use the “as-is” function, e.g., I(X^2),
will also be skipped unless you set the argument AsIs=TRUE. A plot of residuals versus fitted values
is also included unless fitted=FALSE.
In addition to plots, a table of curvature tests is displayed. For plots against a term in the model
formula, say X1, the test displayed is the t-test for for I(X1^2) in the fit of update,model,~. +
I(X1^2)). Econometricians call this a specification test. For factors, the displayed plot is a boxplot,
no curvature test is computed, and grouping is ignored. For fitted values in a linear model, the test is
Tukey’s one-degree-of-freedom test for nonadditivity. You can suppress the tests with the argument
tests=FALSE. If grouping is used curvature tests are not displayed.
residualPlot, which is called by residualPlots, should be viewed as an internal function, and
is included here to display its arguments, which can be used with residualPlots as well. The
residualPlot function returns the curvature test as an invisible result.
residCurvTest computes the curvature test only. For any factors a boxplot will be drawn. For
any polynomials, plots are against the linear term. Other non-standard predictors like B-splines are
skipped.
Value
For lm objects, returns a data.frame with one row for each plot drawn, one column for the curvature
test statistic, and a second column for the corresponding p-value. This function is used primarily
for its side effect of drawing residual plots.
Author(s)
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition. Sage.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley, Chapter 8
See Also
Examples
Description
car package replacements for the summary (S) and confint (Confint) functions for lm, glm,
multinom, and polr objects, with additional arguments but the same defaults as the original func-
tions. The Confint method for "polr" objects profiles the likelihood to get confidence intervals
for the regression parameters but uses Wald intervals for the thresholds. Default methods that call
the standard R summary and confint functions are provided for the S and Confint generics, so the
car functions should be safe to use in general. The default method for Confint also assumes that
there is an appropriate coef method. For briefer model summaries, see brief.
Usage
## Default S3 method:
S(object, brief, ...)
Confint(object, ...)
## Default S3 method:
Confint(object, estimate=TRUE, level=0.95, vcov., ...)
Arguments
object a model or other object, e.g., of class "lm" as produced by a call to lm.
exponentiate for a "glm" or "glmerMod" model using the log or logit link, or a "polr"
or "multinom" model, show exponentiated coefficient estimates and confidence
bounds.
correlation, symbolic.cor
see summary.lm
x, digits, signif.stars
see summary.lm
dispersion see summary.glm
vcov. either a matrix giving the estimated covariance matrix of the estimates, or a
function that when called with object as an argument returns an estimated co-
variance matrix of the estimates. The default of vcov. = vcov uses the usual
estimated covariance matrix. Other choices include the functions documented
at hccm. See example below for using a bootstrap to estimate the covariance
matrix. For the glm methods of Confint and S, if the vcov. or dispersion ar-
gument is specified, then Wald-based confidence limits are computed; otherwise
the reported confidence limits are computed by profiling the likelihood. NOTE:
The dispersion and vcov. arguments may not both be specified.
header if TRUE, print the header for the summary output, default is TRUE
resid.summary if TRUE, print the five-number summary of the residuals in the summary, defaults
to FALSE
adj.r2 if TRUE, print the adjusted r-squared in the summary, default is FALSE
brief if TRUE, set header, resid.summary and adj.r.squared to FALSE, and sup-
press exponeniated coefficients for GLMs with log or logit link. For a data
frame, equivalent to use of brief.
KR if TRUE (default is FALSE), compute Kenward-Roger standard errors and Sat-
terthwaite degrees of freedom for t-tests. Warning: This computation can be
very time-consuming.
110 S
Details
All these functions mimic functions in the stats and other standard R packages for summarizing
aspects of linear, generalized linear, and some other statistical models. The S function also provides
an alterntive to summary for data frames, treating character variables as if they were factors.
The S and Confint functions add support for the vcov. argument for linear models, which allows
specifying a covariance matrix for the regression coefficients other than the usual covariance matrix
returned by the function vcov. This argument may be either the name of a function, so that the call
to vcov.(object) returns a covariance matrix, or else vcov. is set equal to a covariance matrix. For
example, setting vcov.=hccm uses ’proposal 3’ described by Long and Ervin (2000) for a sandwich
coefficient-variance estimator that may be robust against nonconstant variance (see hccm). Setting
vcov. = hccm(object,type = "hc2") would use the matrix returned by the hccm function using
proposal 2. For use with a bootstrap, see the examples below. The overall F-test in the S.lm output
uses the supplied covariance matrix in a call to the linearHypothesis function.
The supplied print method for S.lm (and for other S methods) has additional arguments to cus-
tomize the standard summary.lm output. Standard output is obtained by setting resid.summary=TRUE,adj.r2=TRUE.
Using a heterscedasticy-corrected covariance matrix computed using hccm with GLMs other than
Gaussian is not justified; see the article by Freedman (2006).
The Summary.glm method for models fit with the log or logit link by default prints a table of expo-
nentiated coefficients and their confidence limits; Summary.multinom and Summary.polr option-
ally print tables of exponentiated coefficients.
Value
The S.lm and S.glm functions return a list with all the elements shown at summary.lm and summary.glm.
The S.multinom and S.polr functions return a list with all the elements shown at summary.multinom
and summary.polr plus potentially a table of exponentiated coefficients and confidence bounds.
The Confint.lm function returns either the output from confint.lm if vcov. = vcov or Wald-type
confidence intervals using the supplied covariance matrix for any other choice of vcov..
Finally, Confint applied to any object that does not inherit from "lm", "multinom", or "polr" sim-
ply calls confint, along with, by default, using coef to add a column of estimates to the confidence
limits.
Author(s)
Sanford Weisberg <[email protected]>
scatter3d 111
References
Freedman, David A. (2006). On the so-called Huber sandwich estimator and robust standard errors.
The American Statistician, 60, 299-302.
Long, J. S. and Ervin, L. H. (2000) Using heteroscedasity consistent standard errors in the linear
regression model. The American Statistician 54, 217–224.
White, H. (1980) A heteroskedastic consistent covariance matrix estimator and a direct test of het-
eroskedasticity. Econometrica 48, 817–838.
See Also
brief, summary, confint, coef, summary.lm, confint, vcov.lm, hccm, Boot, linearHypothesis
Examples
mod.prestige <- lm(prestige ~ education + income + type, Prestige)
S(mod.prestige, vcov.=hccm)
S(mod.prestige, brief=TRUE)
Confint(mod.prestige, vcov.=hccm)
# A logit model
mod.mroz <- glm(lfp ~ ., data=Mroz, family=binomial)
S(mod.mroz)
## Not run:
# Using the bootstrap for standard errors
b1 <- Boot(mod.prestige)
S(mod.prestige, vcov.= vcov(b1))
Confint(b1) # run with the boot object to get corrected confidence intervals
## End(Not run)
Description
The scatter3d function uses the rgl package to draw 3D scatterplots with various regression
surfaces. The function Identify3d allows you to label points interactively with the mouse: Press
the right mouse button (on a two-button mouse) or the centre button (on a three-button mouse), drag
a rectangle around the points to be identified, and release the button. Repeat this procedure for each
point or set of “nearby” points to be identified. To exit from point-identification mode, click the
right (or centre) button in an empty region of the plot.
112 scatter3d
Usage
scatter3d(x, ...)
## Default S3 method:
scatter3d(x, y, z,
xlab=deparse(substitute(x)), ylab=deparse(substitute(y)),
zlab=deparse(substitute(z)), axis.scales=TRUE, axis.ticks=FALSE,
revolutions=0,
bg.col=c("white", "black"),
axis.col=if (bg.col == "white") c("darkmagenta", "black", "darkcyan")
else c("darkmagenta", "white", "darkcyan"),
surface.col=carPalette()[-1], surface.alpha=0.5,
neg.res.col="magenta", pos.res.col="cyan",
square.col=if (bg.col == "white") "black" else "gray",
point.col="yellow", text.col=axis.col,
grid.col=if (bg.col == "white") "black" else "gray",
fogtype=c("exp2", "linear", "exp", "none"),
residuals=(length(fit) == 1),
surface=TRUE, fill=TRUE,
grid=TRUE, grid.lines=26, df.smooth=NULL, df.additive=NULL,
sphere.size=1, radius=1, threshold=0.01, speed=1, fov=60,
fit="linear", groups=NULL, parallel=TRUE,
ellipsoid=FALSE, level=0.5, ellipsoid.alpha=0.1, id=FALSE,
model.summary=FALSE, ...)
Arguments
formula “model” formula, of the form y ~ x + z or to plot by groups y ~ x + z | g, where
g evaluates to a factor or other variable dividing the data into groups.
data data frame within which to evaluate the formula.
subset expression defining a subset of observations.
x variable for horizontal axis.
y variable for vertical axis (response).
z variable for out-of-screen axis.
xlab, ylab, zlab
axis labels.
axis.scales if TRUE, label the values of the ends of the axes. Note: For Identify3d to work
properly, the value of this argument must be the same as in scatter3d.
axis.ticks if TRUE, print interior axis-“tick” labels; the default is FALSE. (The code for this
option was provided by David Winsemius.)
scatter3d 113
Details
The id argument to scatter3d can be FALSE, TRUE (in which case 2 points will be identified
according to their Mahalanobis distances from the center of the data), or a list containing any or all
of the following elements:
method if "mahal" (the default), relatively extreme points are identified automatically according
to their Mahalanobis distances from the centroid (point of means); if "identify", points
are identified interactively by right-clicking and dragging a box around them; right-click in
an empty area to exit from interactive-point-identification mode; if "xz", identify extreme
points in the predictor plane; if "y", identify unusual values of the response; if "xyz" identify
unusual values of an variable; if "none", no point identification. See showLabels for more
information.
n Number of relatively extreme points to identify automatically (default, 2, unless method="identify",
in which case identification continues until the user exits).
labels text labels for the points, one for each point; in the default method defaults to the observa-
tion indices, in the formula method to the row names of the data.
offset vertical displacement for point labels (to avoid overplotting the points).
scatter3d 115
Value
scatter3d does not return a useful value; it is used for its side-effect of creating a 3D scatterplot.
Identify3d returns the labels of the identified points.
Note
You have to install the rgl package to produce 3D plots. On a Macintosh (but not on Windows or
Linux), you may also need to install the X11 windowing system. Go to https://www.xquartz.
org/ and click on the link for XQuartz. Double-click on the downloaded disk-image file, and
then double-click on XQuartz.pkg to start the installer. You may take all of the defaults in the
installation. After XQuartz is installed, you should restart your Macintosh.
Author(s)
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
rgl-package, gam
Examples
if(interactive() && require(rgl) && require(mgcv)){
scatter3d(prestige ~ income + education, data=Duncan, id=list(n=3))
Sys.sleep(5) # wait 5 seconds
scatter3d(prestige ~ income + education | type, data=Duncan)
Sys.sleep(5)
scatter3d(prestige ~ income + education | type, surface=FALSE,
ellipsoid=TRUE, revolutions=3, data=Duncan)
scatter3d(prestige ~ income + education, fit=c("linear", "additive"),
data=Prestige)
Sys.sleep(5)
scatter3d(prestige ~ income + education | type,
radius=(1 + women)^(1/3), data=Prestige)
}
## Not run:
# drag right mouse button to identify points, click right button in open area to exit
scatter3d(prestige ~ income + education, data=Duncan, id=list(method="identify"))
scatter3d(prestige ~ income + education | type, data=Duncan, id=list(method="identify"))
## End(Not run)
116 scatterplot
Description
This function uses basic R graphics to draw a two-dimensional scatterplot, with options to allow for
plot enhancements that are often helpful with regression problems. Enhancements include adding
marginal boxplots, estimated mean and variance functions using either parametric or nonparametric
methods, point identification, jittering, setting characteristics of points and lines like color, size and
symbol, marking points and fitting lines conditional on a grouping variable, and other enhance-
ments. sp is an abbreviation for scatterplot.
Usage
scatterplot(x, ...)
## Default S3 method:
scatterplot(x, y, boxplots=if (by.groups) "" else "xy",
regLine=TRUE, legend=TRUE, id=FALSE, ellipse=FALSE, grid=TRUE,
smooth=TRUE,
groups, by.groups=!missing(groups),
xlab=deparse(substitute(x)), ylab=deparse(substitute(y)),
log="", jitter=list(), cex=par("cex"),
col=carPalette()[-1], pch=1:n.groups,
reset.par=TRUE, ...)
sp(x, ...)
Arguments
x vector of horizontal coordinates (or first argument of generic function).
y vector of vertical coordinates.
formula a model formula, of the form y ~ x or, if plotting by groups, y ~ x | z, where
z evaluates to a factor or other variable dividing the data into groups. If x is a
factor, then parallel boxplots are produced using the Boxplot function.
data data frame within which to evaluate the formula.
subset expression defining a subset of observations.
boxplots if "x" a marginal boxplot for the horizontal x-axis is drawn below the plot; if
"y" a marginal boxplot for vertical y-axis is drawn to the left of the plot; if "xy"
both marginal boxplots are drawn; set to "" or FALSE to suppress both boxplots.
scatterplot 117
regLine controls adding a fitted regression line to the plot. if regLine=FALSE, no line
is drawn. If TRUE, the default, an OLS line is fit. This argument can also be a list.
The default of TRUE is equivalent to regLine=list(method=lm,lty=1,lwd=2,col=col),
which specifies using the lm function to estimate the fitted line, to draw a solid
line (lty=1) of width 2 times the nominal width (lwd=2) in the color given by
the first element of the col argument described below.
legend when the plot is drawn by groups and legend=TRUE, controls placement and
properties of a legend; if FALSE, the legend is suppressed. Can be a list of named
arguments, as follows: title for the legend; inset, giving space as a proportion
of the axes to offset the legend from the axes; coords specifying the position of
the legend in any form acceptable to the legend function or, if not given, the leg-
end is placed above the plot in the upper margin; columns for the legend, deter-
mined automatically to prefer a horizontal layout if not given explicitly; cex giv-
ing the relative size of the legend symbols and text. TRUE (the default) is equiv-
alent to list(title=deparse(substitute(groups)),inset=0.02,cex=1).
id controls point identification; if FALSE (the default), no points are identified; can
be a list of named arguments to the showLabels function; TRUE is equivalent to
list(method="mahal",n=2,cex=1,col=carPalette()[-1],location="lr"),
which identifies the 2 points (in each group) with the largest Mahalanobis dis-
tances from the center of the data. See showLabels for a description of the other
arguments. The default behavior of id is not the same in all graphics functions
in car, as the method used depends on the type of plot.
ellipse controls plotting data-concentration ellipses. If FALSE (the default), no ellipses
are plotted. Can be a list of named values giving levels, a vector of one or more
bivariate-normal probability-contour levels at which to plot the ellipses; robust,
a logical value determing whether to use the cov.trob function in the MASS
package to calculate the center and covariance matrix for the data ellipses; and
fill and fill.alpha, which control whether the ellipse is filled and the trans-
parency of the fill. TRUE is equivalent to list(levels=c(.5,.95),robust=TRUE,fill=TRUE,fill.alph
grid If TRUE, the default, a light-gray background grid is put on the graph
smooth specifies a nonparametric estimate of the mean or median function of the vertical
axis variable given the horizontal axis variable and optionally a nonparametric
estimate of the conditional variance. If smooth=FALSE neither function is drawn.
If smooth=TRUE, then both the mean function and variance funtions are drawn
for ungrouped data, and the mean function only is drawn for grouped data. The
default smoother is loessLine, which uses the loess function from the stats
package. This smoother is fast and reliable. See the details below for changing
the smoother, line type, width and color, of the added lines, and adding argu-
ments for the smoother.
groups a factor or other variable dividing the data into groups; groups are plotted with
different colors, plotting characters, fits, and smooths. Using this argument is
equivalent to specifying the grouping variable in the formula.
by.groups if TRUE (the default when there are groups), regression lines are fit by groups.
xlab label for horizontal axis.
ylab label for vertical axis.
log same as the log argument to plot, to produce log axes.
118 scatterplot
jitter a list with elements x or y or both, specifying jitter factors for the horizontal and
vertical coordinates of the points in the scatterplot. The jitter function is used
to randomly perturb the points; specifying a factor of 1 produces the default
jitter. Fitted lines are unaffected by the jitter.
col with no grouping, this specifies a color for plotted points; with grouping, this
argument should be a vector of colors of length at least equal to the number of
groups. The default is value returned by carPalette[-1].
pch plotting characters for points; default is the plotting characters in order (see par).
cex sets the size of plotting characters, with cex=1 the standard size. You can also set
the sizes of other elements with the arguments cex.axis, cex.lab, cex.main,
and cex.sub. See par.
reset.par if TRUE (the default) then plotting parameters are reset to their previous values
when scatterplot exits; if FALSE then the mar and mfcol parameters are al-
tered for the current plotting device. Set to FALSE if you want to add graphical
elements (such as lines) to the plot.
... other arguments passed down and to plot. For example, the argument las sets
the style of the axis labels, and xlim and ylim set the limits on the horizontal
and vertical axes, respectively; see par.
Details
Many arguments to scatterplot were changed in version 3 of car to simplify use of this function.
The smooth argument is usually either set to TRUE or FALSE to draw, or omit, the smoother. Alter-
natively smooth can be set to a list of arguments. The default behavior of smooth=TRUE is equiva-
lent to smooth=list(smoother=loessLine,var=!by.groups,lty.var=2,lty.var=4), specify-
ing the smoother to be used, including the variance smooth, and the line widths and types for the
curves. You can also specify the colors you want to use for the mean and variance smooths with
the arguments col.smooth and col.var. Alternative smoothers are gamline which uses the gam
function from the mgcv package, and quantregLine which uses quantile regression to estimate
the median and quartile functions using rqss from the quantreg package. All of these smoothers
have one or more arguments described on their help pages, and these arguments can be added to
the smooth argument; for example, smooth = list(span=1/2) would use the default loessLine
smoother, include the variance smooth, and change the value of the smoothing parameter to 1/2.
For loessLine and gamLine the variance smooth is estimated by separately smoothing the squared
positive and negative residuals from the mean smooth, using the same type of smoother. The dis-
played curves are equal to the mean smooth plus the square root of the fit to the positive squared
residuals, and the mean fit minus the square root of the smooth of the negative squared residuals.
The lines therefore represent the comnditional variabiliity at each value on the horizontal axis. Be-
cause smoothing is done separately for positive and negative residuals, the variation shown will
generally not be symmetric about the fitted mean function. For the quantregLine method, the
center estimates the median for each value on the horizontal axis, and the variability estimates the
lower and upper quartiles of the estimated conditional distribution for each value of the horizontal
axis.
The sub-arguments spread, lty.spread and col.spread of the smooth argument are equivalent
to the newer var, col.var and lty.var, respectively, recognizing that the spread is a measuure of
conditional variability.
scatterplotMatrix 119
Value
If points are identified, their labels are returned; otherwise NULL is returned invisibly.
Author(s)
John Fox <[email protected]>
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
boxplot, jitter, legend, scatterplotMatrix, dataEllipse, Boxplot, cov.trob, showLabels,
ScatterplotSmoothers.
Examples
scatterplot(prestige ~ income, data=Prestige, ellipse=TRUE)
## Not run:
# remember to exit from point-identification mode
scatterplot(infantMortality ~ ppgdp, id=list(method="identify"), data=UN)
## End(Not run)
Description
This function provides a convenient interface to the pairs function to produce enhanced scatterplot
matrices, including univariate displays on the diagonal and a variety of fitted lines, smoothers,
variance functions, and concentration ellipsoids. spm is an abbreviation for scatterplotMatrix.
120 scatterplotMatrix
Usage
scatterplotMatrix(x, ...)
## Default S3 method:
scatterplotMatrix(x, smooth = TRUE,
id = FALSE, legend = TRUE, regLine = TRUE,
ellipse = FALSE, var.labels = colnames(x), diagonal = TRUE,
plot.points = TRUE, groups = NULL, by.groups = TRUE,
use = c("complete.obs", "pairwise.complete.obs"), col =
carPalette()[-1], pch = 1:n.groups, cex = par("cex"),
cex.axis = par("cex.axis"), cex.labels = NULL,
cex.main = par("cex.main"), row1attop = TRUE, ...)
spm(x, ...)
Arguments
x a data matrix or a numeric data frame.
formula a one-sided “model” formula, of the form ~ x1 + x2 + ... + xk or ~ x1 + x2 +
... + xk | z where z evaluates to a factor or other variable to divide the data
into groups.
data for scatterplotMatrix.formula, a data frame within which to evaluate the
formula.
subset expression defining a subset of observations.
smooth specifies a nonparametric estimate of the mean or median function of the vertical
axis variable given the horizontal axis variable and optionally a nonparametric
estimate of the spread or variance function. If smooth=FALSE neither function
is drawn. If smooth=TRUE, then both the mean function and variance funtions
are drawn for ungrouped data, and the mean function only is drawn for grouped
data. The default smoother is loessLine, which uses the loess function from
the stats package. This smoother is fast and reliable. See the details below
for changing the smoother, line type, width and color, of the added lines, and
adding arguments for the smoother.
id controls point identification; if FALSE (the default), no points are identified; can
be a list of named arguments to the showLabels function; TRUE is equivalent
to list(method="mahal",n=2,cex=1,location="lr"), which identifies the
2 points (in each group, if by.groups=TRUE) with the largest Mahalanobis dis-
tances from the center of the data; list(method="identify") for interactive
point identification is not allowed.
legend controls placement of a legend if the plot is drawn by groups; if FALSE, the
legend is suppressed. Can be a list with the named elementcoords specifying the
position of the legend in any form acceptable to the legend function; TRUE (the
scatterplotMatrix 121
Details
Many arguments to scatterplotMatrix were changed in version 3 of car, to simplify use of this
function.
122 scatterplotMatrix
The smooth argument is usually either set to TRUE or FALSE to draw, or omit, the smoother. Alterna-
tively smooth can be set to a list of arguments. The default behavior of smooth=TRUE is equivalent to
smooth=list(smoother=loessLine,spread=TRUE,lty.smooth=1,lwd.smooth=1.5,lty.spread=3,lwd.spread=1),
specifying the smoother to be used, including the spread or variance smooth, and the line widths
and types for the curves. You can also specify the colors you want to use for the mean and variance
smooths with the arguments col.smooth and col.spread. Alternative smoothers are gamline
which uses the gam function from the mgcv package, and quantregLine which uses quantile re-
gression to estimate the median and quartile functions using rqss from the quantreg package. All
of these smoothers have one or more arguments described on their help pages, and these argu-
ments can be added to the smooth argument; for example, smooth = list(span=1/2) would use
the default loessLine smoother, include the variance smooth, and change the value of the smooth-
ing parameter to 1/2. For loessLine and gamLine the variance smooth is estimated by separately
smoothing the squared positive and negative residuals from the mean smooth, using the same type
of smoother. The displayed curves are equal to the mean smooth plus the square root of the fit to
the positive squared residuals, and the mean fit minus the square root of the smooth of the negative
squared residuals. The lines therefore represent the comnditional variabiliity at each value on the
horizontal axis. Because smoothing is done separately for positive and negative residuals, the vari-
ation shown will generally not be symmetric about the fitted mean function. For the quantregLine
method, the center estimates the median for each value on the horizontal axis, and the spread esti-
mates the lower and upper quartiles of the estimated conditional distribution for each value of the
horizontal axis.
The sub-arguments spread, lty.spread and col.spread of the smooth argument are equivalent
to the newer var, col.var and lty.var, respectively, recognizing that the spread is a measuure of
conditional variability.
By default the diagonal argument is used to draw kernel density estimates of the variables by setting
diagonal=TRUE, which is equivalent to setting diagonal = list(method="adaptiveDensity",bw=bw.nrd0,adjust=1,ke
The additional arguments shown are descibed at adaptiveKernel. The other methods avaliable,
with their default arguments, are diagonal=list(method="density",bw="nrd0",adjust=1,kernel="gaussian",na.rm=
which uses density for nonadaptive kernel density estimation; diagonal=list(method ="histogram",breaks="FD")
which uses hist for drawing a histogram that ignores grouping, if present; diagonal=list(method="boxplot")
with no additional arguments which draws (parallel) boxplots; diagonal=list(method="qqplot")
with no additional arguments which draws a normal QQ plot; and diagonal=list(method="oned")
with no additional arguments which draws a rug plot tilted to the diagonal, as suggested by Richard
Heiberger.
Earlier versions of scatterplotMatrix included arguments transform and family to estimate
power transformations using the powerTransform function before drawing the plot. The same
functionality can be achieved by calling powerTransform directly to estimate a transformation,
saving the transformed variables, and then plotting.
Value
NULL, returned invisibly. This function is used for its side effect: producing a plot. If point identifi-
cation is used, a vector of identified points is returned.
Author(s)
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
pairs, scatterplot, dataEllipse, powerTransform, bcPower, yjPower, cov.trob, showLabels,
ScatterplotSmoothers.
Examples
scatterplotMatrix(~ income + education + prestige | type, data=Duncan)
scatterplotMatrix(~ income + education + prestige | type, data=Duncan,
regLine=FALSE, smooth=list(spread=FALSE))
scatterplotMatrix(~ income + education + prestige,
data=Duncan, id=TRUE, smooth=list(method=gamLine))
Description
These smoothers are used to draw nonparametric-regression lines on scatterplots produced by the
scatterplot, scatterplotMatrix, and several other car functions. The functions are not meant
to be called directly by the user, although the user can supply options via the smoother.args
argument, the contents of which vary by the smoother (see Details below). The gamLine smoother
uses the gam function in the mgcv package, the loessLine smoother uses the loess function in the
stats package, and the quantregLine smoother uses the rqss function in the quantreg package.
Usage
gamLine(x, y, col=carPalette()[1], log.x=FALSE, log.y=FALSE, var=FALSE, spread=var,
smoother.args=NULL, draw=TRUE, offset=0)
Arguments
x horizontal coordinates of points.
y vertical coordinates of points.
col line color.
log.x should be set to TRUE (default is FALSE) if the horizontal axis is logged.
log.y should be set to TRUE (default is FALSE) if the vertical axis is logged.
124 ScatterplotSmoothers
spread, var the default is to plot only an estimated mean or median function. If either of
these arguments is TRUE, then a measure of variability is also plotted.
smoother.args additional options accepted by the smoother, in the form of a list of named values
(see Details below).
draw if TRUE, the default, draw the smoother on the currently active graph. If FALSE,
return a list with coordinates x and y for the points that make up the smooth and
if requested x.pos,y.pos,x.neg,y.neg for the spread smooths.
offset For use when spread=TRUE, the vertical axis is sqrt(offset^2 + variance
smooth).
Details
The loessLine function is a re-implementation of the loess smoother that was used in car prior to
September 2012. The main enhancement is the ability to set more options through the smoother.args
argument.
The gamLine function is more general than loessLine because it supports fitting a generalized
spline regression model, with user-specified error distribution and link function.
The quantregLine function fits a model using splines with estimation based on L1 regression for
the median and quantile regression the (optional) spread. It is likely to be more robust than the other
smoothers.
The smoother.args argument is a list of named elements used to pass additional arguments to the
smoother. As of November, 2016, the smoother is evaluated by default at an equally spaced grid
of 50 points in the range of the horizontal variable. With any of the smoothers, you can change
to, say, 100 evaluation points via the argument smoother.args=list(evaluation=100). As of
version 3.0-1, the smoother.args elements col.var, lty.var, and lwd.var are equivalent to
col.spread, lty.spread, and lwd.spread, respectively.
For loessLine, the default is smoother.args=list(lty.smooth=1,lwd.smooth=2,lty.spread=4,lwd.spread=2,span=
(Prior to November 2016, the default span was 1/2.) The elements lty.smooth, lwd.smooth,
and col.spread are the line type, line width, and line color, respectively of the mean or median
smooth; lty.spread, lwd.spread, and col.spread are the line type, width, and color of the
spread smooths, if requested. The elements span, degree, and family are passed as arguments to
the loess function, along with iterations robustness iterations.
For gamLine, the default is smoother.args=list(lty.smooth=1,lwd.smooth=2,lty.spread=4,lwd.spread=2,k=-1,bs
The first four elements are as for loessLine. The next two elements are passed to the gam func-
tion to control smoothing: k=-1 allows gam to choose the number of splines in the basis function;
bs="tp" provides the type of spline basis to be used, with "tp" for the default thin-plate splines.
The last three arguments specify a distributional family, link function, and weights as in generalized
linear models. See the examples below. The spread element is ignored unless family="gaussian"
and link=NULL.
For quantregLine, the default is smoother.args=list(lty.smooth=1,lwd.smooth=2,lty.spread=4,lwd.spread=2,la
The first four elements are as for loessLine. The last element is passed to the qss function in
quantreg. It is a smoothing parameter, by default a robust estimate of the scale of the horizontal
axis variable. This is an arbitrary choice, and may not work well in all circumstances.
Author(s)
John Fox <[email protected]> and Sanford Weisberg <[email protected]>.
showLabels 125
See Also
Examples
set.seed(12345)
w <- 1 + rpois(100, 5)
x <- rnorm(100)
p <- 1/(1 + exp(-(x + 0.5*x^2)))
y <- rbinom(100, w, p)
scatterplot(y/w ~ x, smooth=list(smoother=gamLine, family="binomial",
weights=w))
scatterplot(y/w ~ x, smooth=list(smoother=gamLine, family=binomial,
link="probit", weights=w))
scatterplot(y/w ~ x, smooth=list(smoother=loessLine), reg=FALSE)
y <- rbinom(100, 1, p)
scatterplot(y ~ x, smooth=list(smoother=gamLine, family=binomial))
Description
This function is called by several graphical functions in the car package to mark extreme points
in a 2D plot. Although the user is unlikely to call this function directly, the documentation below
applies to all these other functions.
Usage
showLabels(x, y, labels=NULL, method="identify",
n = length(x), cex=1, col=carPalette()[1], location=c("lr", "ab", "avoid"), ...)
Arguments
x Plotted horizontal coordinates.
y Plotted vertical coordinates.
labels Plotting labels. When called from within a car plotting function, the labels are
automatically obtained from the row names in the data frame used to create the
modeling object. If labels=NULL, case numbers will be used. If labels are long,
the substr or abbreviate functions can be used to shorten them. Users may
supply their own labels as a character vector of length equal to the number of
plotted points. For use with car plotting functions, the number of plotted points
is equal to the number of rows of data that have neither missing values nor are
excluded using the ‘subset’ argument. When called directly, the length of labels
shoud equal the length of x.
method How points are to be identified. See Details below.
n Number of points to be identified. If set to 0, no points are identified.
cex Controls the size of the plotted labels. The default is 1.
col Controls the color of the plotted labels. The default is the first element returned
by carPalette().
location Where should the label be drawn? The default is "lr" to draw the label to the
left of the point for points in the right-half of the graph and to the right for points
in the left-half. The other option is "ab" for above the point for points below
the middle of the graph and above the point below the middle. Finally, "avoid"
tries to avoid over-plotting labels.
... not used.
Details
The argument method determine how the points to be identified are selected. For the default value
of method="identify", the identify function is used to identify points interactively using the
mouse. Up to n points can be identified, so if n=0, which is the default in many functions in the car
package, then no point identification is done.
Automatic point identification can be done depending on the value of the argument method.
• method = "x" select points according to their value of abs(x -mean(x))
• method = "y" select points according to their value of abs(y -mean(y))
showLabels 127
• method = "r" select points according to their value of abs(y), as may be appropriate in resid-
ual plots, or others with a meaningful origin at 0
• method = "mahal" Treat (x,y) as if it were a bivariate sample, and select cases according to
their Mahalanobis distance from (mean(x),mean(y))
• method can be a vector of the same length as x consisting of values to determine the points to
be labeled. For example, for a linear model m, setting method=cooks.distance(m) will label
the points corresponding to the largest values of Cook’s distance, or method = which(abs(residuals(m,type="pearso
> 2 would label all observations with Pearson residuals greater than 2 in absolute value. Warn-
ing: If missing data are present, points may be incorrectly labelled.
• method can be a vector of case numbers or case-labels, in which case those cases will be
labeled. Warning: If missing data are present, a list of case numbers may identify the wrong
points. A list of case labels, however, will work correctly with missing values.
• method = "none" causes no point labels to be shown.
With showLabels, the method argument can be a list, so, for example method=list("x","y")
would label according to the horizontal and vertical axes variables.
Finally, if the axes in the graph are logged, the function uses logged-variables where appropriate.
Value
A function primarily used for its side-effect of drawing point labels on a plot. Returns invisibly the
labels of the selected points, or NULL if no points are selected. Although intended for use with
other functions in the car package, this function can be used directly.
Author(s)
John Fox <[email protected]>, Sanford Weisberg <[email protected]>
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
avPlots, residualPlots, crPlots, leveragePlots
Examples
plot(income ~ education, Prestige)
with(Prestige, showLabels(education, income,
labels = rownames(Prestige), method=list("x", "y"), n=3))
m <- lm(income ~ education, Prestige)
plot(income ~ education, Prestige)
abline(m)
with(Prestige, showLabels(education, income,
labels=rownames(Prestige), method=abs(residuals(m)), n=4))
128 some
Description
This function provides a consistent method to return the estimated scale from a linear, generalized
linear, nonlinear, or other model.
Usage
sigmaHat(object)
Arguments
Details
For an lm or nls object, the returned quantity is the square root of the estimate of σ 2 . For a glm
object, the returned quantity is the square root of the estimated dispersion parameter.
Value
A nonnegative number
Author(s)
Examples
m1 <- lm(prestige ~ income + education, data=Duncan)
sigmaHat(m1)
Description
Randomly select a few elements of an object, typically a data frame, matrix, vector, or list. If the
object is a data frame or a matrix, then rows are sampled.
some 129
Usage
some(x, ...)
## Default S3 method:
some(x, n=10, ...)
Arguments
x the object to be sampled.
n number of elements to sample.
cols if NULL, use all columns, if a vector of column names or numbers, use only the
columns indicated
... arguments passed down.
Value
Sampled elements or rows.
Note
These functions are adapted from head and tail in the utils package.
Author(s)
John Fox <[email protected]>
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
head, tail, brief.
Examples
some(Duncan)
some(Duncan, cols=names(Duncan)[1:3])
130 spreadLevelPlot
Description
Creates plots for examining the possible dependence of spread on level, or an extension of these
plots to the studentized residuals from linear models.
Usage
spreadLevelPlot(x, ...)
slp(...)
## Default S3 method:
spreadLevelPlot(x, by, robust.line=TRUE,
start=0, xlab="Median", ylab="Hinge-Spread",
point.labels=TRUE, las=par("las"),
main=paste("Spread-Level Plot for", deparse(substitute(x)),
"by", deparse(substitute(by))),
col=carPalette()[1], col.lines=carPalette()[2],
pch=1, lwd=2, grid=TRUE, ...)
Arguments
x a formula of the form y ~ x, where y is a numeric vector and x is a factor, or an
lm object to be plotted; alternatively a numeric vector.
data an optional data frame containing the variables to be plotted. By default the vari-
ables are taken from the environment from which spreadLevelPlot is called.
subset an optional vector specifying a subset of observations to be used.
spreadLevelPlot 131
na.action a function that indicates what should happen when the data contain NAs. The
default is set by the na.action setting of options.
by a factor, numeric vector, or character vector defining groups.
robust.line if TRUE a robust line is fit using the rlm function in the MASS package; if FALSE
a line is fit using lm.
start add the constant start to each data value.
main title for the plot.
xlab label for horizontal axis.
ylab label for vertical axis.
point.labels if TRUE label the points in the plot with group names.
las if 0, ticks labels are drawn parallel to the axis; set to 1 for horizontal labels (see
par).
col color for points; the default is the first entry in the current car palette (see
carPalette and par).
col.lines for the default method, the line color, defaulting to the second entry in the car
color palette; for the "lm" method, a vector of two colors for, respectively, the
fitted straight line and a nonparametric regression smooth line, default to the
second and third entries in the car color palette.
pch plotting character for points; default is 1 (a circle, see par).
lwd line width; default is 2 (see par).
grid If TRUE, the default, a light-gray background grid is put on the graph
id controls point identification; if FALSE (the default), no points are identified; can
be a list of named arguments to the showLabels function; TRUE is equivalent to
list(method=list("x","y"),n=2,cex=1,col=carPalette()[1],location="lr"),
which identifies the 2 points the most extreme horizontal ("X", absolute studen-
tized residual) values and the 2 points with the most extreme horizontal ("Y",
fitted values) values.
smooth specifies the smoother to be used along with its arguments; if FALSE, no smoother
is shown; can be a list giving the smoother function and its named arguments;
TRUE, the default, is equivalent to list(smoother=loessLine). See ScatterplotSmoothers
for the smoothers supplied by the car package and their arguments.
... arguments passed to plotting functions.
Details
Except for linear models, computes the statistics for, and plots, a Tukey spread-level plot of log(hinge-
spread) vs. log(median) for the groups; fits a line to the plot; and calculates a spread-stabilizing
transformation from the slope of the line.
For linear models, plots log(abs(studentized residuals) vs. log(fitted values). Point labeling was
added in November, 2016.
The function slp is an abbreviation for spreadLevelPlot.
132 strings2factors
Value
An object of class spreadLevelPlot containing:
Statistics a matrix with the lower-hinge, median, upper-hinge, and hinge-spread for each
group. (Not for an lm object.)
PowerTransformation
spread-stabilizing power transformation, calculated as 1 − slope of the line fit
to the plot.
Author(s)
John Fox <[email protected]>
References
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Hoaglin, D. C., Mosteller, F. and Tukey, J. W. (Eds.) (1983) Understanding Robust and Exploratory
Data Analysis. Wiley.
See Also
hccm, ncvTest
Examples
spreadLevelPlot(interlocks + 1 ~ nation, data=Ornstein)
slp(lm(interlocks + 1 ~ assets + sector + nation, data=Ornstein))
Description
Converts the character variables (or a subset of these variables) in a data frame to factors, with
optional control of the order of the resulting factor levels.
Usage
strings2factors(object, which, not, exclude.unique, levels, verbose, ...)
## S3 method for class 'data.frame'
strings2factors(object, which, not,
exclude.unique=TRUE, levels=list(), verbose=TRUE, ...)
strings2factors 133
Arguments
object a data frame or an object inheriting from the "data.frame" class.
which an optional character vector of names or column numbers of the character vari-
ables to be converted to factors; if absent, all character variables will be con-
verted, except as excluded by the not and exclude.unique arguments (see be-
low).
not an optional character vector of names or column numbers of character variables
not to be converted to factors.
exclude.unique if TRUE (the default), character variables all of whose values are unique (i.e., all
different from each other) are not converted to factors. Such variables, which
would have as many levels as there are cases, are typically case identifiers and
not categorical variables. If FALSE, character variables all of whose values are
unique are converted to factors with a warning.
levels an optional named list, each element of which is a character vector of levels
of the corresponding factor. This argument allows you to control the order of
levels of the factor; if omitted, or if a particular factor is omitted from the list,
the levels will be in the default alphabetic order.
verbose if TRUE (the default), the names of the character variables that were converted to
factors are printed on the console.
... not used.
Value
a data frame with (some) character variables replaced by corresponding factors.
Author(s)
John Fox <[email protected]>
See Also
factor, data.frame
Examples
M <- Moore # from the carData package
M$partner <- as.character(Moore$partner.status)
M$fcat <- as.character(Moore$fcategory)
M$names <- rownames(M) # values are unique
str(M)
str(strings2factors(M))
str(strings2factors(M,
levels=list(partner=c("low", "high"), fcat=c("low", "medium", "high"))))
str(strings2factors(M, which="partner", levels=list(partner=c("low", "high"))))
str(strings2factors(M, not="partner", exclude.unique=FALSE))
134 subsets
Description
The regsubsets function in the leaps package finds optimal subsets of predictors based on some
criterion statistic. This function plots a measure of fit against subset size.
Usage
subsets(object, ...)
Arguments
object a regsubsets object produced by the regsubsets function in the leaps pack-
age.
names a vector of (short) names for the predictors, excluding the regression intercept, if
one is present; if missing, these are derived from the predictor names in object.
abbrev minimum number of characters to use in abbreviating predictor names.
min.size minimum size subset to plot; default is 1.
max.size maximum size subset to plot; default is number of predictors.
legend If not FALSE, in which case the legend is suppressed, the coordinates at which
to place a legend of the abbreviated predictor names on the plot, in a form rec-
ognized by the legend function. If "interactive", the legend is placed on the
plot interactively with the mouse. By expanding the left or right plot margin,
you can place the legend in the margin, if you wish (see par).
statistic statistic to plot for each predictor subset; one of: "bic", Bayes Information
Criterion; "cp", Mallows’s Cp ; "adjr2", R2 adjusted for degrees of freedom;
"rsq", unadjusted R2 ; "rss", residual sum of squares.
las if 0, ticks labels are drawn parallel to the axis; set to 1 for horizontal labels (see
par).
cex.subsets can be used to change the relative size of the characters used to plot the regres-
sion subsets; default is 1.
... arguments to be passed down to subsets.regsubsets and plot.
symbox 135
Value
NULL if the legend is TRUE; otherwise a data frame with the legend.
Author(s)
John Fox
References
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
See Also
regsubsets
Examples
if (require(leaps)){
subsets(regsubsets(undercount ~ ., data=Ericksen),
legend=c(3.5, -37))
}
Description
symbox first transforms x to each of a series of selected powers, with each transformation standard-
ized to mean 0 and standard deviation 1. The results are then displayed side-by-side in boxplots,
permiting a visual assessment of which power makes the distribution reasonably symmetric.
Usage
symbox(x, ...)
## S3 method for class 'formula'
symbox(formula, data=NULL, subset, na.action=NULL, ylab, ...)
## Default S3 method:
symbox(x, powers = c(-1, -0.5, 0, 0.5, 1), start,
trans=bcPower, xlab="Powers", ylab, ...)
136 symbox
Arguments
x a numeric vector.
formula a one-sided formula specifying a single numeric variable.
data, subset, na.action
as for statistical modeling functions (see, e.g., lm).
xlab, ylab axis labels; if ylab is missing, a label will be supplied.
powers a vector of selected powers to which x is to be raised. For meaningful compari-
son of powers, 1 should be included in the vector of powers.
start a constant to be added to x. If start is missing and trans is bcPower (the
default) or bcnPower, then a start will be automatically generated if there are
zero or negative values in x, and a warning will be printed; the auto-generated
start is the absolute value of the minimum x plus 1 percent of the range of x.
trans a transformation function whose first argument is a numeric vector and whose
second argument is a transformation parameter, given by the powers argument;
the default is bcPower, and another possibility is yjPower. bcnPower may also
be used, in which case the gamma parameter is set to the value of start.
... arguments to be passed down.
Value
as returned by boxplot.
Author(s)
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition. Sage.
See Also
Examples
Description
Applies a function, typically to compute a single statistic, like a mean, median, or standard devia-
tion, within levels of a factor or within combinations of levels of two or more factors to produce a
table of statistics. This function provides a formula interface to the standard R tapply function.
Usage
Tapply(formula, fun, data, na.action = na.pass, ..., targs = list())
Arguments
formula a two-sided formula of the form variable ~ factor.1 + factor.2 + ... + factor.n
or, equivalently, variable ~ factor.1*factor.2* ... *factor.n. The vari-
ables on the right-hand side of the formula are normally factors or are otherwise
coerced to factors.
fun a function, like mean, median, or sd, that takes a vector first argument and typi-
cally returns a single number as its value.
data an optional data frame within which to find the variable and factor(s).
na.action a function to handle missing values, as in statistical modeling functions like lm;
the default is na.pass.
... arguments to pass to the function given in the fun argument, such as (commonly)
na.rm=TRUE.
targs a list of optional arguments to pass to tapply.
Details
The function given by fun is applied to the values of the left-hand-side variable in formula within
(combination of) levels of the factor(s) given in the right-hand side of formula, producing a table
of statistics.
Value
The object returned by tapply, typically simply printed.
Author(s)
John Fox <[email protected]>
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition. Sage.
138 testTransform
See Also
tapply.
Examples
Tapply(conformity ~ partner.status + fcategory, mean, data=Moore)
Tapply(conformity ~ partner.status + fcategory, mean, data=Moore,
trim=0.2)
Moore[1, 2] <- NA
Tapply(conformity ~ partner.status + fcategory, mean, data=Moore)
Tapply(conformity ~ partner.status + fcategory, mean, data=Moore,
na.rm=TRUE)
Tapply(conformity ~ partner.status + fcategory, mean, data=Moore,
na.action=na.omit) # equivalent
remove("Moore")
Description
testTransform computes likelihood ratio tests for particular values of the power parameter based
on powerTransform objects.
Usage
testTransform(object, lambda)
Arguments
Details
The function powerTransform is used to estimate a power transformation for a univariate or multi-
variate sample or multiple linear regression problem, using the method of Box and Cox (1964). It
is usual to round the estimates to nearby convenient values, and this function is use to compulte a
likelihood ratio test for values of the transformation parameter other than the ml-type estimate.
For one-parameter families of transformations, namely the Box-Cox power family bcPower and the
Yeo-Johnson power family yjPower, this function computes a test based on twice the difference in
the log-likelihood between the maximum likelihood-like estimate and the log-likelihood evaluated
at the value of lambda specified.
For the bcnPower Box-Cox power with negatives allowed, the test is based on the profile loglike-
lihood maximizing over the location (or gamma) parameter(s). Thus, gamma is treated as a nusiance
parameter.
Value
A data frame with one row giving the value of the test statistic, its degrees of freedom, and a p-value.
The test is the likelihood ratio test, comparing the value of the log-likelihood at the hypothesized
value to the value of the log-likelihood at the maximum likelihood estimate.
Author(s)
Sanford Weisberg, <[email protected]>
References
Box, G. E. P. and Cox, D. R. (1964) An analysis of transformations. Journal of the Royal Statisisti-
cal Society, Series B. 26 211-46.
Cook, R. D. and Weisberg, S. (1999) Applied Regression Including Computing and Graphics. Wi-
ley.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley.
See Also
powerTransform and bcnPower for examples of the use of this function and other tests that might
be of interest in some circumstances.
Examples
summary(a3 <- powerTransform(cbind(len, adt, trks, sigs1) ~ htype, Highway1))
# test lambda = (0 0 0 -1)
testTransform(a3, c(0, 0, 0, -1))
summary(q1 <- powerTransform(lm(cbind(LoBD$I1L2, LoBD$I1L1) ~ pool, LoBD), family="bcnPower"))
testTransform(q1, c(.3, .8))
140 TransformationAxes
Description
These functions produce axes for the original scale of transformed variables. Typically these would
appear as additional axes to the right or at the top of the plot, but if the plot is produced with
axes=FALSE, then these functions could be used for axes below or to the left of the plot as well.
Usage
basicPowerAxis(power, base=exp(1),
side=c("right", "above", "left", "below"),
at, start=0, lead.digits=1, n.ticks, grid=FALSE, grid.col=gray(0.50),
grid.lty=2,
axis.title="Untransformed Data", cex=1, las=par("las"))
probabilityAxis(scale=c("logit", "probit"),
side=c("right", "above", "left", "below"),
at, lead.digits=1, grid=FALSE, grid.lty=2, grid.col=gray(0.50),
axis.title = "Probability", interval = 0.1, cex = 1, las=par("las"))
Arguments
power power for Box-Cox, Box-Cox with negatives, Yeo-Johnson, or simple power
transformation.
shift the shift (gamma) parameter for the Box-Cox with negatives family.
scale transformation used for probabilities, "logit" (the default) or "probit".
side side at which the axis is to be drawn; numeric codes are also permitted: side =
1 for the bottom of the plot, side=2 for the left side, side = 3 for the top, side
= 4 for the right side.
TransformationAxes 141
at numeric vector giving location of tick marks on original scale; if missing, the
function will try to pick nice locations for the ticks.
start if a start was added to a variable (e.g., to make all data values positive), it can
now be subtracted from the tick labels.
lead.digits number of leading digits for determining ‘nice’ numbers for tick labels (default
is 1.
n.ticks number of tick marks; if missing, same as corresponding transformed axis.
grid if TRUE grid lines for the axis will be drawn.
grid.col color of grid lines.
grid.lty line type for grid lines.
axis.title title for axis.
cex relative character expansion for axis label.
las if 0, ticks labels are drawn parallel to the axis; set to 1 for horizontal labels (see
par).
base base of log transformation for power.axis when power = 0.
interval desired interval between tick marks on the probability scale.
Details
The transformations corresponding to the three functions are as follows:
basicPowerAxis: Simple power transformation, x0 = xp for p 6= 0 and x0 = log x for p = 0.
bcPowerAxis: Box-Cox power transformation, x0 = (xλ − 1)/λ for λ 6= 0 and x0 = log x for
λ = 0.
bcnPowerAxis: Box-Cox with negatives power transformation, the Box-Cox power transformation
of z = .5 ∗ (y + (y 2 + γ 2 )1/2 ), where γ is strictly positive if y includes negative values and
non-negative otherwise. The value of z is always positive.
yjPowerAxis: Yeo-Johnson power transformation, for non-negative x, the Box-Cox transforma-
tion of x + 1; for negative x, the Box-Cox transformation of |x| + 1 with power 2 − p.
probabilityAxis: logit or probit transformation, logit = log[p/(1 − p)], or probit = Φ−1 (p),
where Φ−1 is the standard-normal quantile function.
These functions will try to place tick marks at reasonable locations, but producing a good-looking
graph sometimes requires some fiddling with the at argument.
Value
These functions are used for their side effects: to draw axes.
Author(s)
John Fox <[email protected]>
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
142 vif
See Also
basicPower, bcPower, yjPower, logit.
Examples
UN <- na.omit(UN)
par(mar=c(5, 4, 4, 4) + 0.1) # leave space on right
with(UN, qqPlot(logit(infantMortality/1000)))
probabilityAxis()
with(UN, qqPlot(qnorm(infantMortality/1000)))
probabilityAxis(at=c(.005, .01, .02, .04, .08, .16), scale="probit")
Description
Calculates variance-inflation and generalized variance-inflation factors for linear, generalized linear,
and other models.
Usage
vif(mod, ...)
## Default S3 method:
vif(mod, ...)
Arguments
mod for the default method, an object that responds to coef, vcov, and model.matrix,
such as an lm or glm object.
... not used.
Details
If all terms in an unweighted linear model have 1 df, then the usual variance-inflation factors are
calculated.
If any terms in an unweighted linear model have more than 1 df, then generalized variance-inflation
factors (Fox and Monette, 1992) are calculated. These are interpretable as the inflation in size of
the confidence ellipse or ellipsoid for the coefficients of the term in comparison with what would
be obtained for orthogonal data.
The generalized vifs are invariant with respect to the coding of the terms in the model (as long as
the subspace of the columns of the model matrix pertaining to each term is invariant). To adjust for
the dimension of the confidence ellipsoid, the function also prints GV IF 1/(2×df ) where df is the
degrees of freedom associated with the term.
Through a further generalization, the implementation here is applicable as well to other sorts of
models, in particular weighted linear models, generalized linear models, and mixed-effects models.
Value
A vector of vifs, or a matrix containing one row for each term in the model, and columns for the
GVIF, df, and GV IF 1/(2×df ) .
Author(s)
References
Fox, J. and Monette, G. (1992) Generalized collinearity diagnostics. JASA, 87, 178–183.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2018) An R Companion to Applied Regression, Third Edition, Sage.
Examples
Description
Given matrices x and y as arguments and an optional matrix or vector of weights, w, return a
weighted matrix cross-product, t(x) w y. If no weights are supplied, or the weights are constant,
the function uses crossprod for speed.
Usage
wcrossprod(x, y, w)
Arguments
x,y x, y numeric matrices; missing(y) is taken to be the same matrix as x. Vectors
are promoted to single-column or single-row matrices, depending on the context.
w A numeric vector or matrix of weights, conformable with x and y.
Value
A numeric matrix, with appropriate dimnames taken from x and y.
Author(s)
Michael Friendly, John Fox <[email protected]>
See Also
crossprod
Examples
set.seed(12345)
n <- 24
drop <- 4
sex <- sample(c("M", "F"), n, replace=TRUE)
x1 <- 1:n
x2 <- sample(1:n)
extra <- c( rep(0, n - drop), floor(15 + 10 * rnorm(drop)) )
y1 <- x1 + 3*x2 + 6*(sex=="M") + floor(10 * rnorm(n)) + extra
y2 <- x1 - 2*x2 - 8*(sex=="M") + floor(10 * rnorm(n)) + extra
# assign non-zero weights to 'dropped' obs
wt <- c(rep(1, n-drop), rep(.2,drop))
wcrossprod(X, Y)
wcrossprod(X, Y, w=wt)
wcrossprod(x1, y1)
wcrossprod(x1, y1, w=wt)
Description
These functions return the indices of the supplied row names of a data frame or names of another
object, such as a vector or list. whichNames is just an alias for which.names.
Usage
whichNames(names, object, ...)
## Default S3 method:
whichNames(names, object, ...)
Arguments
names a name or character vector of names.
object a data frame or an object with a names attribute.
... not used.
Value
Returns the index or indices of names in row names of the data frame or names of an object of
another class.
Author(s)
John Fox <[email protected]>
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
146 whichNames
Examples
whichNames(c('minister', 'conductor'), Duncan)
Index
∗ algebra Anova, 4
wcrossprod, 144 leveneTest, 70
∗ aplot linearHypothesis, 73
Ellipses, 50 ncvTest, 87
panel.car, 90 outlierTest, 88
regLine, 102 poTest, 91
TransformationAxes, 140 ∗ interface
∗ array carWeb, 32
wcrossprod, 144 ∗ manip
∗ color boxCoxVariable, 21
carPalette, 31 brief, 25
∗ connections logit, 80
Export, 54 recode, 100
Import, 60 strings2factors, 132
∗ distribution Tapply, 137
qqPlot, 97 ∗ misc
∗ hplot car-internal.Rd, 30
avPlots, 11 carHexsticker, 30
Boxplot, 22 Tapply, 137
ceresPlots, 33 ∗ models
crPlots, 38 Anova, 4
densityPlot, 44 Contrasts, 36
dfbetaPlots, 47 deltaMethod, 41
Ellipses, 50 influence.mixed.models, 63
infIndexPlot, 61 linearHypothesis, 73
invResPlot, 67 poTest, 91
invTranPlot, 68 Predict, 96
leveragePlots, 71 S, 107
mcPlots, 81 ∗ print
mmps, 84 compareCoefs, 35
residualPlots, 103 ∗ regression
scatter3d, 111 Anova, 4
scatterplot, 116 avPlots, 11
scatterplotMatrix, 119 bcPower, 13
ScatterplotSmoothers, 123 Boot, 15
spreadLevelPlot, 130 boxCox, 18
subsets, 134 boxCoxVariable, 21
symbox, 135 boxTidwell, 23
∗ htest car-deprecated, 29
147
148 INDEX
tail, 129