Learn R Programming

visreg (version 2.4-1)

visreg2d: 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", "effect"),
trans=I, scale=c("linear", "response"),
plot.type=c("image", "persp", "rgl"), nn=ifelse(plot.type=="persp", 49,
99), 
cond=list(), print.cond=FALSE, whitespace=0.2, ...)

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:

  • If 'conditional' is selected, the plot returned shows the value of the variable on the x-axis and the change in response on the y-axis, holding all other variables constant (by default, median for numeric variables and most common category for factors).

  • If 'contrast' is selected, the plot returned shows the effect on the expected value of the response by moving the x variable away from a reference point on the x-axis (for numeric variables, this is taken to be the mean).

For more details, see references.

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 scale='response' for a glm, the inverse link function will be applied so that the model is plotted on the scale of the original response.

plot.type

The style of plot to be produced. The following three options are supported:

  • 'image', a filled contour plot.

  • 'persp', a 3 dimensional perspective plot.

  • 'rgl', a version of the perspective plot that can be rotated. Note: requires the rgl package to use.

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 cond (see examples).

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 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.

Value

In addition to providing plots, the visreg2d function also invisibly returns the x, y, and Z values used in the creation of its plots.

References

See Also

http://pbreheny.github.io/visreg/surface.html visreg

Examples

Run this code
# 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")
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab