Description Usage Arguments Details Value Author(s) References See Also Examples
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.
1 2 3 |
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
http://pbreheny.github.io/visreg
plot.visreg
visreg2d
visreg-faq
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | ###################
## 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"))
|
Conditions used in construction of plot
Solar.R: 207
Temp: 79
Loading required package: survival
Loading required package: MASS
Attaching package: 'MASS'
The following object is masked _by_ '.GlobalEnv':
birthwt
Conditions used in construction of plot
Solar.R: 207
Heat: Mild
Warning message:
Note that you are attempting to plot a 'main effect' in a model that contains an
interaction. This is potentially misleading; you may wish to consider using the 'by'
argument.
Conditions used in construction of plot
Solar.R: 207
Heat: Mild
Warning message:
Note that you are attempting to plot a 'main effect' in a model that contains an
interaction. This is potentially misleading; you may wish to consider using the 'by'
argument.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.