Description Usage Arguments Details Value See Also Examples
Draw one or more conditional effects plots reflecting
model coefficients, or a function to perform the estimation with
model
as its only argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
object |
A statistical model object |
x |
The name of the variable to show on the x-axis |
dx |
The name of the variable whose effect should be plotted |
what |
The quantity to plot: 'prediction', 'effect', 'classprediction', or 'stackedprediction' |
type |
'response' or 'link' |
vcov |
the variance-covariance matrix used to calculate confidence intervals |
data |
data.frame over which to calculate individual marginal effects or predictions |
level |
The confidence level required (used to draw uncertainty bounds). |
draw |
A logical (default |
xvals |
A numeric vector of values at which to calculate predictions or
marginal effects, if |
z |
name of the third dimension variable over which quantities should be plotted (as facets). |
zvals |
discrete values of the z variable over which to plot |
n |
An integer specifying the number of points across |
rugplot |
logical include a rugplot at the bottom of the graph |
at |
Currently ignored. |
... |
Additional arguments such as |
Note that when what = "prediction"
, the plots show
predictions holding values of the data at their mean or mode, whereas when
what = "effect"
average marginal effects (i.e., at observed values)
are shown.
When examining generalized linear models (e.g., logistic regression models),
confidence intervals for predictions can fall outside of the response scale
(again, for logistic regression this means confidence intervals can exceed
the (0,1) bounds). This is consistent with the behavior of
predict
but may not be desired. The examples (below)
show ways of constraining confidence intervals to these bounds.
The overall aesthetic is somewhat similar to to the output produced by the
marginalModelPlot()
function in the
car package.
A ggplot2 object. Use draw = FALSE
to simply generate the
data structure for use elsewhere.
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 | ## Not run:
require('datasets')
# prediction from several angles
m <- lm(Sepal.Length ~ Sepal.Width, data = iris)
cplot(m)
# marginal effect of 'Petal.Width' across 'Petal.Width'
m <- lm(Sepal.Length ~ Sepal.Width * Petal.Width * I(Petal.Width ^ 2),
data = head(iris, 50))
cplot(m, x = "Petal.Width", what = "effect", n = 10)
# factor independent variables
mtcars[["am"]] <- factor(mtcars[["am"]])
m <- lm(mpg ~ am * wt, data = mtcars)
## predicted values for each factor level
cplot(m, x = "am")
## marginal effect of each factor level across numeric variable
cplot(m, x = "wt", dx = "am", what = "effect")
# non-linear model
m <- glm(am ~ wt*drat, data = mtcars, family = binomial)
cplot(m, x = "wt", type = 'response') # prediction (response scale)
cplot(m, x = "wt", type = 'link') # prediction (link scale)
# marginal effect of 'Petal.Width' across 'Sepal.Width'
## without drawing the plot
## this might be useful if you want even more control over the plots
tmp <- cplot(m, x = "Sepal.Width", dx = "Petal.Width",
what = "effect", n = 10, draw = FALSE)
# 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")
# ordinal outcome
if (require("MASS")) {
# x is a factor variable
house.plr <- polr(Sat ~ Infl + Type + Cont, weights = Freq,
data = housing)
## predicted probabilities
cplot(house.plr)
## cumulative predicted probabilities
cplot(house.plr, what = "stacked")
## ggplot2 example
if (require("ggplot2")) {
ggplot(cplot(house.plr), aes(x = xvals, y = yvals, group = level)) +
geom_line(aes(color = level))
}
# x is continuous
cyl.plr <- polr(factor(cyl) ~ wt, data = mtcars)
cplot(cyl.plr, col = c("red", "purple", "blue"), what = "stacked")
cplot(cyl.plr, what = "class")
}
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.