Draw one or more perspectives plots reflecting predictions or marginal effects from a model, or the same using a flat heatmap or “filled contour” (image
) representation. Currently methods exist for “lm”, “glm”, and “loess” models.
# S3 method for lm
image(x, xvar = attributes(terms(x))[["term.labels"]][1],
yvar = attributes(terms(x))[["term.labels"]][2], dx = xvar,
what = c("prediction", "effect"), type = c("response", "link"),
vcov = stats::vcov(x), nx = 25L, ny = nx, nz = 20, xlab = xvar,
ylab = yvar, xaxs = "i", yaxs = xaxs, bty = "o",
col = gray(seq(0.05, 0.95, length.out = nz), alpha = 0.75),
contour = TRUE, contour.labels = NULL, contour.drawlabels = TRUE,
contour.cex = 0.6, contour.col = "black", contour.lty = 1,
contour.lwd = 1, ...)# S3 method for glm
image(x, xvar = attributes(terms(x))[["term.labels"]][1],
yvar = attributes(terms(x))[["term.labels"]][2], dx = xvar,
what = c("prediction", "effect"), type = c("response", "link"),
vcov = stats::vcov(x), nx = 25L, ny = nx, nz = 20, xlab = xvar,
ylab = yvar, xaxs = "i", yaxs = xaxs, bty = "o",
col = gray(seq(0.05, 0.95, length.out = nz), alpha = 0.75),
contour = TRUE, contour.labels = NULL, contour.drawlabels = TRUE,
contour.cex = 0.6, contour.col = "black", contour.lty = 1,
contour.lwd = 1, ...)
# S3 method for loess
image(x, xvar = attributes(terms(x))[["term.labels"]][1],
yvar = attributes(terms(x))[["term.labels"]][2], dx = xvar,
what = c("prediction", "effect"), type = c("response", "link"),
vcov = stats::vcov(x), nx = 25L, ny = nx, nz = 20, xlab = xvar,
ylab = yvar, xaxs = "i", yaxs = xaxs, bty = "o",
col = gray(seq(0.05, 0.95, length.out = nz), alpha = 0.75),
contour = TRUE, contour.labels = NULL, contour.drawlabels = TRUE,
contour.cex = 0.6, contour.col = "black", contour.lty = 1,
contour.lwd = 1, ...)
# S3 method for lm
persp(x, xvar = attributes(terms(x))[["term.labels"]][1],
yvar = attributes(terms(x))[["term.labels"]][2], dx = xvar,
what = c("prediction", "effect"), type = c("response", "link"),
vcov = stats::vcov(x), nx = 25L, ny = nx, theta = 45, phi = 10,
shade = 0.75, xlab = xvar, ylab = yvar, zlab = if (match.arg(what) ==
"prediction") "Predicted value" else paste0("Marginal effect of ", dx),
ticktype = c("detailed", "simple"), ...)
# S3 method for glm
persp(x, xvar = attributes(terms(x))[["term.labels"]][1],
yvar = attributes(terms(x))[["term.labels"]][2], dx = xvar,
what = c("prediction", "effect"), type = c("response", "link"),
vcov = stats::vcov(x), nx = 25L, ny = nx, theta = 45, phi = 10,
shade = 0.75, xlab = xvar, ylab = yvar, zlab = if (match.arg(what) ==
"prediction") "Predicted value" else paste0("Marginal effect of ", dx),
ticktype = c("detailed", "simple"), ...)
# S3 method for loess
persp(x, xvar = attributes(terms(x))[["term.labels"]][1],
yvar = attributes(terms(x))[["term.labels"]][2], dx = xvar,
what = c("prediction", "effect"), type = c("response", "link"),
vcov = stats::vcov(x), nx = 25L, ny = nx, theta = 45, phi = 10,
shade = 0.75, xlab = xvar, ylab = yvar, zlab = if (match.arg(what) ==
"prediction") "Predicted value" else paste0("Marginal effect of ", dx),
ticktype = c("detailed", "simple"), ...)
A model object.
A character string specifying the name of variable to use as the x dimension in the plot. See persp
for details.
A character string specifying the name of variable to use as the y dimension in the plot. See persp
for details.
A character string specifying the name of the variable for which the conditional average marginal effect is desired when what = "effect"
. By default this is xvar
.
A character string specifying whether to calculate predictions on the response scale (default) or link (only relevant for non-linear models).
A matrix containing the variance-covariance matrix for estimated model coefficients, or a function to perform the estimation with model
as its only argument.
An integer specifying the number of points across x
at which to calculate the predicted value or marginal effect.
An integer specifying the number of points across y
at which to calculate the predicted value or marginal effect.
An integer specifying, for image
, the number of breakpoints to use when coloring the plot.
A character string specifying the x-axis type (see par
).
A character string specifying the y-axis type (see par
).
A character string specifying the box type (see par
).
A character vector specifying colors to use when coloring the contour plot.
For image
, a logical specifying whether to overlay contour lines onto the heatmap using contour
.
For image
, if contour = TRUE
a logical specifying whether to overlay contour lines onto the heatmap.
For image
, if contour = TRUE
a logical specifying whether to overlay contour lines onto the heatmap.
For image
, if contour = TRUE
and contour.drawlabels = TRUE
a numeric specifying the label size for contour line labels (see par
).
For image
, if contour = TRUE
a character string specifying a color for contour lines.
For image
, if contour = TRUE
an integer specifying a line type for contour lines (see par
).
For image
, if contour = TRUE
an integer specifying a line width for contour lines (see par
).
For persp
, an integer vector specifying the value of theta
in persp
. If length greater than 1, multiple subplots are drawn with different rotations.
For persp
, an integer vector specifying the value of phi
in persp
. If length greater than 1, multiple subplots are drawn with different rotations.
For persp
, an integer vector specifying the value of shade
in persp
.
A character string specifying the value of zlab
(vertical axis label) in persp
.
A character string specifying one of: “detailed” (the default) or “simple”. See persp
.
# NOT RUN {
require('datasets')
# prediction from several angles
m <- lm(mpg ~ wt*drat, data = mtcars)
persp(m, theta = c(45, 135, 225, 315))
# flat/heatmap representation
image(m)
# marginal effect of 'drat' across drat and wt
m <- lm(mpg ~ wt*drat*I(drat^2), data = mtcars)
persp(m, xvar = "drat", yvar = "wt", what = "effect",
nx = 10, ny = 10, ticktype = "detailed")
# a non-linear model
m <- glm(am ~ wt*drat, data = mtcars, family = binomial)
persp(m, theta = c(30, 60)) # prediction
# flat/heatmap representation
image(m)
# effects on linear predictor and outcome
persp(m, xvar = "drat", yvar = "wt", what = "effect", type = "link")
persp(m, xvar = "drat", yvar = "wt", what = "effect", type = "response")
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab