Learn R Programming

margins (version 0.2.0)

persp.lm: Perspective plots for models

Description

Draw one or more perspectives plots reflecting predictions or marginal effects from a model. Currently methods exist for “lm” and “glm” models.

Usage

# S3 method for lm
persp(x, xvar = attributes(terms(x))[["term.labels"]][1],
  yvar = attributes(terms(x))[["term.labels"]][2], what = c("prediction",
  "effect"), type = c("response", "link"), 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 ", xvar), ticktype = c("detailed", "simple"),
  ...)

Arguments

x

A model object.

xvar

A character string specifying the name of variable to use as the x dimension in the plot. See persp for details.

yvar

A character string specifying the name of variable to use as the y dimension in the plot. See persp for details.

what

A character string specifying whether to draw “prediction” (fitted values from the model, calculated using predict) or “effect” (marginal effect of x, using margins).

type

A character string specifying whether to calculate predictions on the response scale (default) or link (only relevant for non-linear models).

nx

An integer specifying the number of points across x at which to calculate the predicted value or marginal effect.

ny

An integer specifying the number of points across y at which to calculate the predicted value or marginal effect.

theta

An integer vector specifying the value of theta in persp. If length greater than 1, multiple subplots are drawn with different rotations.

phi

An integer vector specifying the value of phi in persp. If length greater than 1, multiple subplots are drawn with different rotations.

shade

An integer vector specifying the value of shade in persp.

xlab

A character string specifying the value of xlab in persp.

ylab

A character string specifying the value of ylab in persp.

zlab

A character string specifying the value of zlab (vertical axis label) in persp.

ticktype

A character string specifying one of: “detailed” (the default) or “simple”. See persp.

Additional arguments passed to persp.

Details

Currently, this implements “marginal effects at means” of all covariates.

See Also

plot.margins, cplot

Examples

Run this code
# NOT RUN {
require('datasets')
# prediction from several angles
m <- lm(mpg ~ wt*drat, data = mtcars)
persp(m, theta = c(45, 135, 225, 315))

# 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

# 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")
# }

Run the code above in your browser using DataLab