Learn R Programming

margins (version 0.2.0)

cplot: Conditional predicted value and marginal effect plots for models

Description

Draw one or more conditioanl effects plots reflecting predictions or marginal effects from a model, conditional on a covariate. Currently methods exist for “lm” and “glm” models.

Usage

cplot(object, ...)

# S3 method for lm cplot(object, x = attributes(terms(object))[["term.labels"]][1], dx = x, what = c("prediction", "effect"), type = c("response", "link"), n = 25L, level = 0.95, xlab = x, ylab = if (match.arg(what) == "prediction") paste0("Predicted value") else paste0("Marginal effect of ", dx), xlim, ylim, lwd = 2, col = "black", lty = 1, se.type = c("lines", "shade"), se.col = "black", se.fill = "gray", se.lwd = 1, se.lty = 2, xaxs = "i", yaxs = xaxs, las = 1, rug = TRUE, rug.col = "black", rug.size = -0.02, ...)

# S3 method for glm cplot(object, x = attributes(terms(object))[["term.labels"]][1], dx = x, what = c("prediction", "effect"), type = c("response", "link"), n = 25L, level = 0.95, xlab = x, ylab = if (match.arg(what) == "prediction") paste0("Predicted value") else paste0("Marginal effect of ", dx), xlim, ylim, lwd = 2, col = "black", lty = 1, se.type = c("lines", "shade"), se.col = "black", se.fill = "gray", se.lwd = 1, se.lty = 2, xaxs = "i", yaxs = xaxs, las = 1, rug = TRUE, rug.col = "black", rug.size = -0.02, ...)

Arguments

object

A model object.

x

A character string specifying the name of variable to use as the x-axis dimension in the plot.

dx

If what = "effect", the variable whose conditional marginal effect should be displayed. By default it is x (so the plot displays the marginal effect of x across values of x); ignored otherwise.

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

n

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

level

The confidence level required (used to draw uncertainty bounds).

xlab

A character string specifying the value of xlab in plot.

ylab

A character string specifying the value of ylab in plot.

xlim

A two-element numeric vector specifying the x-axis limits. Set automatically if missing.

ylim

A two-element numeric vector specifying the y-axis limits. Set automatically if missing.

lwd

An integer specifying the width of the prediction or marginal effect line. See lines.

col

A character string specifying the color of the prediction or marginal effect line.

lty

An integer specifying the “line type” of the prediction or marginal effect line. See par.

se.type

A character string specifying whether to draw the confidence interval as “lines” (the default, using lines) or a “shade” (using polygon).

se.col

If se.type = "lines", a character string specifying the color of the confidence interval lines. If se.type = "shade", the color of the shaded region border.

se.fill

If se.type = "shade", the color of the shaded region. Ignored otherwise.

se.lwd

If se.type = "lines", the width of the confidence interval lines. See lines.

se.lty

If se.type = "lines", an integer specifying the “line type” of the confidence interval lines; if se.type = "shade", the line type of the shaded polygon border. See par.

xaxs

A character string specifying xaxs. See par.

yaxs

A character string specifying xaxs. See par.

las

An integer string specifying las. See par.

rug

A logical specifying whether to include an x-axis “rug” (see rug).

rug.col

A character string specifying col to rug.

rug.size

A numeric value specifying ticksize to rug.

Additional arguments passed to plot.

Details

Currently, this implements “marginal effects at means” of all covariates. And confidence intervals are not drawn for marginal effects plots.

See Also

plot.margins, persp.lm

Examples

Run this code
# NOT RUN {
require('datasets')
# prediction from several angles
m <- lm(Sepal.Length ~ Sepal.Width, data = iris)
cplot(m)

# more complex model
m <- lm(Sepal.Length ~ Sepal.Width * Petal.Width * I(Petal.Width ^ 2), data = iris)
## marginal effect of 'Petal.Width' across 'Petal.Width'
cplot(m, x = "Petal.Width", what = "effect", n = 10)
## marginal effect of 'Petal.Width' across 'Sepal.Width'
cplot(m, x = "Sepal.Width", dx = "Petal.Width", what = "effect", n = 10)

# a non-linear model
m <- glm(am ~ wt*drat, data = mtcars, family = binomial)
cplot(m, x = "wt") # prediction

# effects on linear predictor and outcome
cplot(m, x = "drat", dx = "wt", what = "effect", type = "link")
cplot(m, x = "drat", dx = "wt", what = "effect", type = "response")
# }

Run the code above in your browser using DataLab