Title: | Visualization of Regression Models |
Version: | 2.7.0 |
Date: | 2020-06-04 |
Author: | Patrick Breheny, Woodrow Burchett |
Maintainer: | Patrick Breheny <patrick-breheny@uiowa.edu> |
Imports: | lattice |
Suggests: | rgl, MASS, survival, knitr, ggplot2, Matrix |
Enhances: | nlme |
VignetteBuilder: | knitr |
Description: | Provides a convenient interface for constructing plots to visualize the fit of regression models arising from a wide variety of models in R ('lm', 'glm', 'coxph', 'rlm', 'gam', 'locfit', 'lmer', 'randomForest', etc.) |
License: | GPL-3 |
URL: | http://pbreheny.github.io/visreg |
Encoding: | UTF-8 |
NeedsCompilation: | no |
Packaged: | 2020-06-04 19:48:51 UTC; pbreheny |
Repository: | CRAN |
Date/Publication: | 2020-06-04 20:30:02 UTC |
Visualization of regression models
Description
visreg
provides a number of plotting functions for
visualizing fitted regression models: regression functions, confidence
bands, partial residuals, interactions, and more.
Details
This package allows the use of visreg and visreg2d, functions for visualizing regression models. See example below for the most basic use, and the help pages for each function for details. Also see the cited manuscript for additional details. If you have a question or feature request, please e-mail me at <patrick-breheny@uiowa.edu>.
Author(s)
Patrick Breheny and Woodrow Burchett
Maintainer: Patrick Breheny <patrick-breheny@uiowa.edu>
References
Breheny, P. and Burchett, W. (2017), Visualizing regression models using visreg. https://journal.r-project.org/archive/2017/RJ-2017-046/index.html
See Also
Examples
fit <- lm(Ozone ~ Solar.R + Wind + Temp, data=airquality)
visreg(fit, "Wind")
Visualization of regression functions
Description
A function for visualizing regression models quickly and easily.
Default plots contain a confidence band, prediction line, and partial
residuals. Factors, transformations, conditioning, interactions, and a
variety of other options are supported. The plot.visreg
function
accepts a visreg
or visregList
object as calculated by
visreg
and creates the plot.
Usage
## S3 method for class 'visreg'
plot(x, overlay=FALSE, print.cond=FALSE,
whitespace=0.2, partial=identical(x$meta$trans, I), band=TRUE,
rug=ifelse(partial, 0, 2), strip.names=is.numeric(x$fit[,x$meta$by]),
legend=TRUE, top=c('line', 'points'), gg=FALSE, line.par=NULL,
fill.par=NULL, points.par=NULL, ...)
Arguments
x |
A |
overlay |
When |
print.cond |
If |
whitespace |
When |
partial |
If |
band |
If |
rug |
By default, partial residuals are plotted. Alternatively, a
|
strip.names |
When |
legend |
For overlay plots, ( |
top |
By default, the model fits |
gg |
By default ( |
line.par |
List of parameters (see |
fill.par |
List of parameters (see |
points.par |
List of parameters (see |
... |
Graphical parameters can be passed to the function to customize the
plots. If |
Author(s)
Patrick Breheny and Woodrow Burchett
References
Breheny, P. and Burchett, W. (2017), Visualizing regression models using visreg. https://journal.r-project.org/archive/2017/RJ-2017-046/index.html
See Also
http://pbreheny.github.io/visreg/options.html
visreg
visreg2d
visreg-faq
Examples
fit <- lm(Ozone ~ Solar.R + Wind + Temp,data=airquality)
visreg(fit, "Wind", line=list(col="red"), points=list(cex=1, pch=1))
## Changing appearance
visreg(fit, "Wind", line=list(col="red"), points=list(cex=1, pch=1))
## See ?visreg and http://pbreheny.github.io/visreg for more examples
Visualization of regression functions for two variables
Description
Plot method for visualizing how two variables interact to affect the response in regression models.
Usage
## S3 method for class 'visreg2d'
plot(x, plot.type=c("image","persp","rgl", "gg"),
xlab, ylab, zlab, color, print.cond=FALSE, whitespace=0.2, ...)
Arguments
x |
A |
plot.type |
The style of plot to be produced. The following three options are supported:
|
xlab |
Axis label for x variable |
ylab |
Axis label for y variable |
zlab |
Axis label for outcome |
color |
For |
print.cond |
If |
whitespace |
When |
... |
Graphical parameters can be passed to the function to customize the plots. |
Author(s)
Patrick Breheny and Woodrow Burchett
References
Breheny, P. and Burchett, W. (2017), Visualizing regression models using visreg. https://journal.r-project.org/archive/2017/RJ-2017-046/index.html
See Also
http://pbreheny.github.io/visreg/surface.html
visreg
Examples
fit <- lm(Ozone ~ Solar.R + Wind + Temp + I(Wind^2) + I(Temp^2) +
I(Wind*Temp)+I(Wind*Temp^2) + I(Temp*Wind^2) + I(Temp^2*Wind^2),
data=airquality)
visreg2d(fit, x="Wind", y="Temp", plot.type="image")
visreg2d(fit, x="Wind", y="Temp", plot.type="image",
color=c("purple", "green", "red"))
visreg2d(fit, x="Wind", y="Temp", plot.type="persp")
## Requires the rgl package
## Not run:
visreg2d(fit,x="Wind",y="Temp",plot.type="rgl")
## End(Not run)
## Requires the ggplot2 package
## Not run:
visreg2d(fit, x="Wind", y="Temp", plot.type="gg")
## End(Not run)
Subset a visreg object
Description
Subset a visreg object so that only a portion of the full model is plotted.
Usage
## S3 method for class 'visreg'
subset(x, sub, ...)
Arguments
x |
A |
sub |
Logical expression indicating elements to keep, as in
|
... |
Not used. |
Examples
# Fit a model and construct a visreg object
airquality$Heat <- cut(airquality$Temp,3,labels=c("Cool","Mild","Hot"))
fit <- lm(Ozone~ Solar.R + Wind*Heat,data=airquality)
v <- visreg(fit, "Wind", by="Heat", plot=FALSE)
# Plot only certain levels
vv <- subset(v, Heat %in% c("Cool", "Hot"))
plot(vv)
# Plot only up to wind 15 mph
vv <- subset(v, Wind < 15)
plot(vv)
Visualization of regression functions
Description
A function for visualizing regression models quickly and easily.
Default plots contain a confidence band, prediction line, and partial
residuals. Factors, transformations, conditioning, interactions, and a
variety of other options are supported. The visreg
function
performs the calculations and, if plot=TRUE
(the default), these
calculations are passed to plot.visreg
for plotting.
Usage
visreg(fit, xvar, by, breaks=3, type=c("conditional", "contrast"),
data=NULL, trans=I, scale=c("linear","response"), xtrans, alpha=.05,
nn=101, cond=list(), jitter=FALSE, collapse=FALSE, plot=TRUE, ...)
Arguments
fit |
The fitted model object you wish to visualize. Any object with 'predict' and 'model.frame' methods are supported, including lm, glm, gam, rlm, coxph, and many more. |
xvar |
Character string specifying the variable to be put on the x-axis of your plot. Both continuous variables and factors are supported. |
by |
(Optional) A variable allowing you to divide your plot into
cross-sections based on levels of the |
breaks |
If a continuous variable is used for the |
type |
The type of plot to be produced. The following options are supported:
For more details, see references. |
data |
The data frame used to fit the model. Typically, visreg() can figure out where the data is, so it is not necessary to provide this. In some cases, however, the data set cannot be located and must be supplied explicitly. |
trans |
(Optional) A function specifying a transformation for the vertical axis. |
scale |
By default, the model is plotted on the scale of the linear
predictor. If |
xtrans |
(Optional) A function specifying a transformation for the horizontal
axis. Note that, for model terms such as |
alpha |
Alpha level (1-coverage) for the confidence band displayed in the plot (default: 0.05). |
nn |
Controls the smoothness of the line and confidence band. Increasing this number will add to the computational burden, but produce a smoother plot (default: 101). |
cond |
Named list specifying conditional values of other explanatory
variables. By default, conditional plots in visreg are constructed
by filling in other explanatory variables with the median (for
numeric variables) or most common category (for factors), but this
can be overridden by specifying their values using |
jitter |
Adds a small amount of noise to |
collapse |
If the |
plot |
Send the calculations to |
... |
Graphical parameters (e.g., |
Details
See plot.visreg
for plotting options, such as changing
the appearance of points, lines, confidence bands, etc.
Value
A visreg
or visregList
object (which is simply a list
of visreg
objects). A visreg
object has three
components:
fit |
A data frame with |
res |
A data frame with |
meta |
Contains meta-information needed to construct plots, such
as the name of the x and y variables, whether there were any
|
Author(s)
Patrick Breheny and Woodrow Burchett
References
Breheny, P. and Burchett, W. (2017), Visualizing regression models using visreg. https://journal.r-project.org/archive/2017/RJ-2017-046/index.html
See Also
http://pbreheny.github.io/visreg
plot.visreg
visreg2d
visreg-faq
Examples
###################
## Linear models ##
###################
## Basic
fit <- lm(Ozone ~ Solar.R + Wind + Temp, data=airquality)
visreg(fit)
visreg(fit, "Wind", type="contrast")
visreg(fit, "Wind", type="conditional")
## Factors
airquality$Heat <- cut(airquality$Temp, 3, labels=c("Cool","Mild","Hot"))
fit.heat <- lm(Ozone ~ Solar.R + Wind + Heat, data=airquality)
visreg(fit.heat, "Heat", type="contrast")
visreg(fit.heat, "Heat", type="conditional")
## Transformations
fit1 <- lm(Ozone ~ Solar.R + Wind + Temp + I(Wind^2), data=airquality)
fit2 <- lm(log(Ozone) ~ Solar.R + Wind + Temp, data=airquality)
fit3 <- lm(log(Ozone) ~ Solar.R + Wind + Temp + I(Wind^2), data=airquality)
visreg(fit1, "Wind")
visreg(fit2, "Wind", trans=exp, ylab="Ozone")
visreg(fit3, "Wind", trans=exp, ylab="Ozone")
## Conditioning
visreg(fit, "Wind", cond=list(Temp=50))
visreg(fit, "Wind", print.cond=TRUE)
visreg(fit, "Wind", cond=list(Temp=100))
## Interactions
fit.in1 <- lm(Ozone~ Solar.R + Wind*Heat, data=airquality)
visreg(fit.in1, "Wind", by="Heat")
visreg(fit.in1, "Heat", by="Wind")
visreg(fit.in1, "Wind", by="Heat", type="contrast")
visreg(fit.in1, "Heat", by="Wind", breaks=6)
visreg(fit.in1, "Heat", by="Wind", breaks=c(0,10,20))
## Overlay
visreg(fit.in1, "Wind", by="Heat", overlay=TRUE)
######################
## Nonlinear models ##
######################
## Logistic regression
data("birthwt", package="MASS")
birthwt$race <- factor(birthwt$race, labels=c("White","Black","Other"))
birthwt$smoke <- factor(birthwt$smoke, labels=c("Nonsmoker","Smoker"))
fit <- glm(low~age+race+smoke+lwt, data=birthwt, family="binomial")
visreg(fit, "lwt",
xlab="Mother's Weight", ylab="Log odds (low birthweight)")
visreg(fit, "lwt", scale="response", partial=FALSE,
xlab="Mother's Weight", ylab="P(low birthweight)")
visreg(fit, "lwt", scale="response", partial=FALSE,
xlab="Mother's Weight", ylab="P(low birthweight)", rug=2)
## Proportional hazards
require(survival)
data(ovarian)
ovarian$rx <- factor(ovarian$rx)
fit <- coxph(Surv(futime, fustat) ~ age + rx, data=ovarian)
visreg(fit, "age", ylab="log(Hazard ratio)")
## Robust regression
require(MASS)
fit <- rlm(Ozone ~ Solar.R + Wind*Heat, data=airquality)
visreg(fit, "Wind", cond=list(Heat="Mild"))
## And more...; anything with a 'predict' method should work
## Return raw components of plot
v <- visreg(fit, "Wind", cond=list(Heat="Mild"))
Frequently Asked Questions for visreg
Description
This page tries to answer some of the questions that I get asked most often about how to use the visreg package. If you have a question that does not appear below, I can be reached at <patrick-breheny@uiowa.edu>.
Frequent asked questions
-
What is the difference between 'conditional' and 'contrast' plots?
Suppose our data looked like:
SBP Sex Age 140 M 56 135 F 47 ... we fit a model with
fit <- lm(SBP~Sex+Age)
and we want to plot the relationship between Age and SBP. A 'conditional' plot illustrates the relationship between the two, conditional on the sex being, say, Male (the default in visreg is to choose the most common category).
The 'contrast' plot in visreg, on the other hand, illustrates the effect on SBP of a change in age – the default in visreg is to use the mean age as the reference point for this change. Since the above model does not have an interaction, this effect will be the same for men and women, and thus does not require you to specify a sex for the plot.
Both conditional and contrast plots answer subtly different questions, and both are useful in different situations.
-
Can visreg can be used for mixed models (i.e., from the 'nlme' or 'lme4' packages)?
Sort of. The underlying basis on which visreg operates is by using the predict method to plot predictions from the model. Predictions for mixed models are complicated. In particular, there is no
se.fit
option provided by thepredict
methods in thenlme
andlme4
packages, so you cannot obtain confidence bands for conditional plots. Nevertheless,visreg
will produce plots of estimated coefficients and partial residuals.In addition, there may be certain nesting structures among the covariates that
visreg
cannot be aware of; for example, if you are trying to plot the effect of age for various individuals, fixing sex atsex=Male
, this may involve setting the sex of female subjects toMale
for the sake of the plot. Whether such a plot has any meaning, you will have to judge for yourself. In general, contrast plots are more trustworthy than than conditional plots, given the intricacies of setting up conditions in a hierarchical model.Keep in mind that depending on what sort of predictions (BLUPs) you are interested in, you may need to manually control the inclusion of random effects in your predictions. By default, visreg includes no random effects (i.e.,
level=0
fornlme
models andre.form=NA
forlme4
models). If you are including a random effect as aby
variable invisreg
, you most likely want to add those effects back in, and you will have to do so manually, by directly specifying the appropriatelevel
orre.form
argument topredict
(see?predict.nlme
or?predict.merMod
). Handling this appropriately is the user's responsibility; I cannot hope to automatically decide this for all possible mixed models that could be passed to visreg.As mentioned above, you cannot obtain confidence bands for conditional plots. In the words of the authors of the
lme4
package, "There is no option for computing standard errors of predictions because it is difficult to define an efficient method that incorporates uncertainty in the variance parameters"; hence nose.fit
option. You can, however, get confidence bands for'contrast'
plots. In a contrast plot, the random effects cancel and the above issue is avoided.If you are running into difficulty using
visreg
with mixed models, feel free to e-mail me; mixed models have been less extensively tested withvisreg
than fixed-effect models, and there may still be bugs to work out. -
How do I use visreg for a model with offset terms?
By default, visreg is set up to provide conditional plots in which all other terms are set to their median value (or most common category). This includes offset terms. It is not uncommon, however, to want to see results with the offset included. To obtain these results, one needs to specify the offset among the arguments to cond. For example, using the Insurance data from the MASS package:
utils::data(Insurance, package="MASS")
fit <- glm(Claims ~ District + Group + Age + offset(log(Holders)), data = Insurance, family = poisson)
visreg(fit, "Group", scale="response")
This will provide the model's predictions for the expected number of claims given the median number of holders (here, 136). To obtain the expected number of claims per holder, we need to specify Holders=1 in cond:
visreg(fit, "Group", scale="response", cond=list(Holders=1))
Note also that to ensure proper functionality with all of visreg's options, the use of the
offset()
function, rather than theoffset=
argument, is recommended. -
Why doesn't visreg work with models I fit with package XXX?
visreg()
relies on being able to call certain generic functions to interface with the fitted model object that is passed to it. Specifically, iffit
is the fit of a model that is passed tovisreg
, the following have to work:model.frame(fit)
formula(fit)
If they do not, there is nothing I can really do on the
visreg
end to get it to work – the author of the package would need to add support for those generic functions to make it more portable. If the above lines of code do work andvisreg
still fails, please let me know – perhaps there is a bug somewhere that I can fix.
Author(s)
Patrick Breheny and Woodrow Burchett
Maintainer: Patrick Breheny <patrick-breheny@uiowa.edu>
References
Breheny, P. and Burchett, W. (2017), Visualizing regression models using visreg. https://journal.r-project.org/archive/2017/RJ-2017-046/index.html
Visualization of regression functions for two variables
Description
A function used to visualize how two variables interact to affect the response in regression models.
Usage
visreg2d(fit, xvar, yvar, type=c("conditional", "contrast"), data=NULL,
trans=I, scale=c("linear", "response"), nn=99, cond=list(), plot=TRUE,
...)
Arguments
fit |
The fitted model object you wish to visualize. Any object with 'predict' and 'model.frame' methods are supported, including lm, glm, gam, rlm, coxph, and many more. |
xvar |
Character string specifying the variable to be put on the x-axis of your plot. Both continuous variables and factors are supported. |
yvar |
Character string specifying the variable to be put on the y-axis of your plot. Both continuous variables and factors are supported. |
type |
The type of plot to be produced. The following options are supported:
For more details, see references. |
data |
The data frame used to fit the model. Typically, visreg() can figure out where the data is, so it is not necessary to provide this. In some cases, however, the data set cannot be located and must be supplied explicitly. |
trans |
(Optional) A function specifying a transformation for the vertical axis. |
scale |
By default, the model is plotted on the scale of the linear
predictor. If |
nn |
Resolution of the three dimensional plot. Higher values will results in a smoother looking plot. |
cond |
Named list specifying conditional values of other explanatory
variables. By default, conditional plots in visreg are constructed
by filling in other explanatory variables with the median (for
numeric variables) or most common category (for factors), but this
can be overridden by specifying their values using |
plot |
Send the calculations to |
... |
Graphical parameters (e.g., |
Value
A visreg2d
object consisting of:
x |
Values of |
y |
Values of |
z |
Values of outcome to be plotted |
meta |
Meta-information needed to construct plots, such as the name of the x and y variables. |
Author(s)
Patrick Breheny and Woodrow Burchett
References
Breheny, P. and Burchett, W. (2017), Visualizing regression models using visreg. https://journal.r-project.org/archive/2017/RJ-2017-046/index.html
See Also
http://pbreheny.github.io/visreg/surface.html
visreg
Examples
fit <- lm(Ozone ~ Solar.R + Wind + Temp + I(Wind^2) + I(Temp^2) +
I(Wind*Temp)+I(Wind*Temp^2) + I(Temp*Wind^2) + I(Temp^2*Wind^2),
data=airquality)
visreg2d(fit, x="Wind", y="Temp", plot.type="image")
visreg2d(fit, x="Wind", y="Temp", plot.type="persp")
## Requires the rgl package
## Not run:
visreg2d(fit, x="Wind", y="Temp", plot.type="rgl")
## End(Not run)
## Requires the ggplot2 package
## Not run:
visreg2d(fit, x="Wind", y="Temp", plot.type="gg")
## End(Not run)
Join multiple visreg objects together in a list
Description
This function takes multiple visreg objects, from separate calls to
visreg()
, and joins them together in a single object. The single object
will be of type visregList
unless collapse=TRUE
is specified, in
which case the list will be collapsed back down into a single visreg
object.
Usage
visregList(..., labels, collapse=FALSE)
Arguments
... |
|
labels |
A character vector with length corresponding to the number of |
collapse |
If |
Value
A visreg
or visregList
object, depending on the value of
collapse
.
Author(s)
Patrick Breheny
See Also
Examples
fit <- lm(Ozone ~ Solar.R + Wind + Temp, data=airquality)
v1 <- visreg(fit, "Wind", plot=FALSE, alpha=0.2)
v2 <- visreg(fit, "Wind", plot=FALSE, alpha=0.01)
vv1 <- visregList(v1, v2, collapse=FALSE)
vv2 <- visregList(v1, v2, collapse=TRUE,
labels=c("Confidence: 0.80", "Confidence: 0.99"))
op <- par(mfrow=c(1,2))
plot(vv1)
par(op)
plot(vv2)