Title: | Visualization of Regression Models |
---|---|
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.) |
Authors: | Patrick Breheny [aut, cre] , Woodrow Burchett [ctb] |
Maintainer: | Patrick Breheny <[email protected]> |
License: | GPL-3 |
Version: | 2.7.0.15 |
Built: | 2024-10-29 04:48:09 UTC |
Source: | https://github.com/pbreheny/visreg |
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.
## 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, ... )
## 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, ... )
x |
A 'visreg' or 'visregList' object; see [visreg()]. |
overlay |
By default, when 'by' is specified, separate panels are used to display each cross-section. If 'overlay=TRUE', these cross-sections are overlaid on top of each other in a single plot. |
print.cond |
If 'print.cond=TRUE', the explanatory variable values conditioned on in a conditional plot are printed to the console (default: 'FALSE'). If 'print.cond=TRUE' and 'type="contrast"', the conditions will still be printed, but they have no bearing on the plot unless interactions are present. |
whitespace |
When 'xvar' is a factor, 'whitespace' determines the amount of space in between factors on the x-axis. Default is 0.2, meaning that 20 percent of the horizontal axis is whitespace. |
partial |
If 'partial=TRUE' (the default), partial residuals are shown on the plot. |
band |
If 'band=TRUE' (the default), confidence bands are shown on the plot. |
rug |
By default, partial residuals are plotted. Alternatively, a
[rug()] may be plotted along the horizontal axis instead. Setting 'rug=TRUE'
turns off partial residuals by default; if one wants both to be plotted, both
'rug=TRUE' and 'partial=TRUE' need to be specified. Two types of rug plots
are available. If 'rug=1' or 'rug=TRUE', then a basic rug is drawn on the
bottom. If |
strip.names |
When 'by=TRUE', 'strip.names=TRUE' adds the name of the 'by' variable to the strip at the top of each panel. Default is 'FALSE' for factors and 'TRUE' for numeric 'by' variables. 'strip.names' can also be a character vector, in which case it replaces the strip names altogether with values chosen by the user. |
legend |
For overlay plots, ('overlay=TRUE'), should visreg create a legend? If 'legend=TRUE' (the default), a legend is placed in the top margin. |
top |
By default, the fitted line is plotted on top of the partial residuals; usually this is preferable, but it does run the risk of obscuring certain residuals. To change this behavior and plot the partial residuals on top, specify ‘top=’points''. |
gg |
By default ('gg=FALSE'), 'visreg' will use the **lattice** package to render the plot if multiple panels are required. If 'gg=TRUE', it will use the **ggplot2** package instead, provided that it is installed. |
line.par |
List of parameters (see [par()]) to pass to 'lines(...)' or [ggplot2::geom_line()] when lines are plotted. |
fill.par |
List of parameters (see [par()]) to pass to 'polygon(...)' or [ggplot2::geom_polygon()] when shaded confidence regions are plotted. |
points.par |
List of parameters ([par()]) to pass to 'points(...)' or [ggplot2::geom_point()] when partial residuals are plotted. |
... |
Graphical parameters can be passed to the function to customize the plots. If 'by=TRUE', lattice parameters can be passed, such as 'layout' (see examples below). |
Patrick Breheny and Woodrow Burchett
Breheny P and Burchett W. (2017) Visualization of regression models using visreg. *R Journal*, **9**: 56-71. doi:10.32614/RJ-2017-046
https://pbreheny.github.io/visreg/options.html, [visreg()], [visreg2d()]
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 https://pbreheny.github.io/visreg for more 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 https://pbreheny.github.io/visreg for more examples
Plot method for visualizing how two variables interact to affect the response in regression models.
## S3 method for class 'visreg2d' plot( x, plot.type = c("image", "persp", "rgl", "gg"), xlab, ylab, zlab, color, print.cond = FALSE, whitespace = 0.2, ... )
## S3 method for class 'visreg2d' plot( x, plot.type = c("image", "persp", "rgl", "gg"), xlab, ylab, zlab, color, print.cond = FALSE, whitespace = 0.2, ... )
x |
A [visreg2d()] object. |
plot.type |
The style of plot to be produced. The following options are supported: * 'image': a filled contour * 'gg': a filled contour plot using ggplot2 * 'persp': a 3 dimensional perspective plot * 'rgl': a version of the perspective plot that can be rotated (requires the rgl package to be installed) |
xlab |
Axis label for x variable |
ylab |
Axis label for y variable |
zlab |
Axis label for outcome |
color |
For ‘plot.type=’persp'‘ or 'plot.type=’rgl'', the color of the surface. For ‘plot.type=’image'‘ or 'plot.type=’gg'', a vector of colors used to establish a color palette. |
print.cond |
If 'print.cond==TRUE', the explanatory variable values
conditioned on in a conditional plot are printed to the console
(default: |
whitespace |
When 'xvar' or 'yvar' is a factor, 'whitespace' determines the amount of space in between the factors. Default is 0.2, meaning that 20 percent of the axis is whitespace. |
... |
Graphical parameters can be passed to the function to customize the plots. |
Patrick Breheny and Woodrow Burchett
Breheny P and Burchett W. (2017) Visualization of regression models using visreg. *R Journal*, **9**: 56-71. doi:10.32614/RJ-2017-046
https://pbreheny.github.io/visreg/surface.html, [visreg()]
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 visreg2d(fit,x="Wind",y="Temp",plot.type="rgl") ## Requires the ggplot2 package visreg2d(fit, x="Wind", y="Temp", plot.type="gg")
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 visreg2d(fit,x="Wind",y="Temp",plot.type="rgl") ## Requires the ggplot2 package visreg2d(fit, x="Wind", y="Temp", plot.type="gg")
Subset a visreg object so that only a portion of the full model is plotted.
## S3 method for class 'visreg' subset(x, sub, ...)
## S3 method for class 'visreg' subset(x, sub, ...)
x |
A 'visreg' object. |
sub |
Logical expression indicating elements to keep, as in [subset()] |
... |
Not used. |
# 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)
# 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)
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.
visreg( fit, xvar, by, breaks = 3, type = c("conditional", "contrast"), data = NULL, trans = I, scale = c("linear", "response"), xtrans, alpha = 0.05, nn = 101, cond = list(), jitter = FALSE, collapse = FALSE, plot = TRUE, ... )
visreg( fit, xvar, by, breaks = 3, type = c("conditional", "contrast"), data = NULL, trans = I, scale = c("linear", "response"), xtrans, alpha = 0.05, nn = 101, cond = list(), jitter = FALSE, collapse = FALSE, plot = TRUE, ... )
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., |
See plot.visreg
for plotting options, such as changing the
appearance of points, lines, confidence bands, etc.
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 |
Patrick Breheny and Woodrow Burchett
Breheny, P. and Burchett, W. (2017), Visualizing regression models using visreg. https://journal.r-project.org/archive/2017/RJ-2017-046/index.html
https://pbreheny.github.io/visreg plot.visreg
visreg2d
################### ## 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"))
################### ## 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"))
A function used to visualize how two variables interact to affect the response in regression models.
visreg2d( fit, xvar, yvar, type = c("conditional", "contrast"), data = NULL, trans = I, scale = c("linear", "response"), nn = 99, cond = list(), plot = TRUE, ... )
visreg2d( fit, xvar, yvar, type = c("conditional", "contrast"), data = NULL, trans = I, scale = c("linear", "response"), nn = 99, cond = list(), plot = TRUE, ... )
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., |
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. |
Patrick Breheny and Woodrow Burchett
Breheny, P. and Burchett, W. (2017), Visualizing regression models using visreg. https://journal.r-project.org/archive/2017/RJ-2017-046/index.html
https://pbreheny.github.io/visreg/surface.html
visreg
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)
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)
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.
visregList(..., labels, collapse = FALSE)
visregList(..., labels, collapse = FALSE)
... |
'visreg' objects, as produced by calls to [visreg()]. |
labels |
A character vector with length corresponding to the number of 'visreg' objects passed to the function that provides labels for the different objects in subsequent plots. Only has an effect if 'collapse=TRUE'. |
collapse |
If 'TRUE', the resulting object will be collapsed down into a single 'visreg' object. If 'FALSE', the resulting object will be a 'visregList'. |
A 'visreg' or 'visregList' object, depending on the value of 'collapse'.
Patrick Breheny
[visreg()], [plot.visreg()]
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)
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)