Description Usage Arguments Details Value See Also Examples
Draw one or more conditional effects plots reflecting predictions or marginal effects from a model, conditional on a covariate. Currently methods exist for “lm”, “glm”, “loess” class models.
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | cplot(object, ...)
## Default S3 method:
cplot(
object,
x = attributes(terms(object))[["term.labels"]][1L],
dx = x,
what = c("prediction", "effect"),
data = prediction::find_data(object),
type = c("response", "link"),
vcov = stats::vcov(object),
at,
n = 25L,
xvals = prediction::seq_range(data[[x]], n = n),
level = 0.95,
draw = TRUE,
xlab = x,
ylab = if (match.arg(what) == "prediction") paste0("Predicted value") else
paste0("Marginal effect of ", dx),
xlim = NULL,
ylim = NULL,
lwd = 1L,
col = "black",
lty = 1L,
se.type = c("shade", "lines", "none"),
se.col = "black",
se.fill = grDevices::gray(0.5, 0.5),
se.lwd = lwd,
se.lty = if (match.arg(se.type) == "lines") 1L else 0L,
factor.lty = 0L,
factor.pch = 19L,
factor.col = se.col,
factor.fill = factor.col,
factor.cex = 1L,
xaxs = "i",
yaxs = xaxs,
las = 1L,
scatter = FALSE,
scatter.pch = 19L,
scatter.col = se.col,
scatter.bg = scatter.col,
scatter.cex = 0.5,
rug = TRUE,
rug.col = col,
rug.size = -0.02,
...
)
## S3 method for class 'clm'
cplot(
object,
x = attributes(terms(object))[["term.labels"]][1L],
dx = x,
what = c("prediction", "classprediction", "stackedprediction", "effect"),
data = prediction::find_data(object),
type = c("response", "link"),
vcov = stats::vcov(object),
at,
n = 25L,
xvals = seq_range(data[[x]], n = n),
level = 0.95,
draw = TRUE,
xlab = x,
ylab = if (match.arg(what) == "effect") paste0("Marginal effect of ", dx) else
paste0("Predicted value"),
xlim = NULL,
ylim = if (match.arg(what) %in% c("prediction", "stackedprediction")) c(0, 1.04)
else NULL,
lwd = 1L,
col = "black",
lty = 1L,
factor.lty = 1L,
factor.pch = 19L,
factor.col = col,
factor.fill = factor.col,
factor.cex = 1L,
xaxs = "i",
yaxs = xaxs,
las = 1L,
scatter = FALSE,
scatter.pch = 19L,
scatter.col = factor.col,
scatter.bg = scatter.col,
scatter.cex = 0.5,
rug = TRUE,
rug.col = col,
rug.size = -0.02,
...
)
## S3 method for class 'glm'
cplot(
object,
x = attributes(terms(object))[["term.labels"]][1L],
dx = x,
what = c("prediction", "effect"),
data = prediction::find_data(object),
type = c("response", "link"),
vcov = stats::vcov(object),
at,
n = 25L,
xvals = prediction::seq_range(data[[x]], n = n),
level = 0.95,
draw = TRUE,
xlab = x,
ylab = if (match.arg(what) == "prediction") paste0("Predicted value") else
paste0("Marginal effect of ", dx),
xlim = NULL,
ylim = NULL,
lwd = 1L,
col = "black",
lty = 1L,
se.type = c("shade", "lines", "none"),
se.col = "black",
se.fill = grDevices::gray(0.5, 0.5),
se.lwd = lwd,
se.lty = if (match.arg(se.type) == "lines") 1L else 0L,
factor.lty = 0L,
factor.pch = 19L,
factor.col = se.col,
factor.fill = factor.col,
factor.cex = 1L,
xaxs = "i",
yaxs = xaxs,
las = 1L,
scatter = FALSE,
scatter.pch = 19L,
scatter.col = se.col,
scatter.bg = scatter.col,
scatter.cex = 0.5,
rug = TRUE,
rug.col = col,
rug.size = -0.02,
...
)
## S3 method for class 'lm'
cplot(
object,
x = attributes(terms(object))[["term.labels"]][1L],
dx = x,
what = c("prediction", "effect"),
data = prediction::find_data(object),
type = c("response", "link"),
vcov = stats::vcov(object),
at,
n = 25L,
xvals = prediction::seq_range(data[[x]], n = n),
level = 0.95,
draw = TRUE,
xlab = x,
ylab = if (match.arg(what) == "prediction") paste0("Predicted value") else
paste0("Marginal effect of ", dx),
xlim = NULL,
ylim = NULL,
lwd = 1L,
col = "black",
lty = 1L,
se.type = c("shade", "lines", "none"),
se.col = "black",
se.fill = grDevices::gray(0.5, 0.5),
se.lwd = lwd,
se.lty = if (match.arg(se.type) == "lines") 1L else 0L,
factor.lty = 0L,
factor.pch = 19L,
factor.col = se.col,
factor.fill = factor.col,
factor.cex = 1L,
xaxs = "i",
yaxs = xaxs,
las = 1L,
scatter = FALSE,
scatter.pch = 19L,
scatter.col = se.col,
scatter.bg = scatter.col,
scatter.cex = 0.5,
rug = TRUE,
rug.col = col,
rug.size = -0.02,
...
)
## S3 method for class 'loess'
cplot(
object,
x = attributes(terms(object))[["term.labels"]][1L],
dx = x,
what = c("prediction", "effect"),
data = prediction::find_data(object),
type = c("response", "link"),
vcov = stats::vcov(object),
at,
n = 25L,
xvals = prediction::seq_range(data[[x]], n = n),
level = 0.95,
draw = TRUE,
xlab = x,
ylab = if (match.arg(what) == "prediction") paste0("Predicted value") else
paste0("Marginal effect of ", dx),
xlim = NULL,
ylim = NULL,
lwd = 1L,
col = "black",
lty = 1L,
se.type = c("shade", "lines", "none"),
se.col = "black",
se.fill = grDevices::gray(0.5, 0.5),
se.lwd = lwd,
se.lty = if (match.arg(se.type) == "lines") 1L else 0L,
factor.lty = 0L,
factor.pch = 19L,
factor.col = se.col,
factor.fill = factor.col,
factor.cex = 1L,
xaxs = "i",
yaxs = xaxs,
las = 1L,
scatter = FALSE,
scatter.pch = 19L,
scatter.col = se.col,
scatter.bg = scatter.col,
scatter.cex = 0.5,
rug = TRUE,
rug.col = col,
rug.size = -0.02,
...
)
## S3 method for class 'polr'
cplot(
object,
x = attributes(terms(object))[["term.labels"]][1L],
dx = x,
what = c("prediction", "classprediction", "stackedprediction", "effect"),
data = prediction::find_data(object),
type = c("response", "link"),
vcov = stats::vcov(object),
at,
n = 25L,
xvals = seq_range(data[[x]], n = n),
level = 0.95,
draw = TRUE,
xlab = x,
ylab = if (match.arg(what) == "effect") paste0("Marginal effect of ", dx) else
paste0("Predicted value"),
xlim = NULL,
ylim = if (match.arg(what) %in% c("prediction", "stackedprediction")) c(0, 1.04)
else NULL,
lwd = 1L,
col = "black",
lty = 1L,
factor.lty = 1L,
factor.pch = 19L,
factor.col = col,
factor.fill = factor.col,
factor.cex = 1L,
xaxs = "i",
yaxs = xaxs,
las = 1L,
scatter = FALSE,
scatter.pch = 19L,
scatter.col = factor.col,
scatter.bg = scatter.col,
scatter.cex = 0.5,
rug = TRUE,
rug.col = col,
rug.size = -0.02,
...
)
## S3 method for class 'multinom'
cplot(
object,
x = attributes(terms(object))[["term.labels"]][1L],
dx = x,
what = c("prediction", "classprediction", "stackedprediction", "effect"),
data = prediction::find_data(object),
type = c("response", "link"),
vcov = stats::vcov(object),
at,
n = 25L,
xvals = seq_range(data[[x]], n = n),
level = 0.95,
draw = TRUE,
xlab = x,
ylab = if (match.arg(what) == "effect") paste0("Marginal effect of ", dx) else
paste0("Predicted value"),
xlim = NULL,
ylim = if (match.arg(what) %in% c("prediction", "stackedprediction")) c(0, 1.04)
else NULL,
lwd = 1L,
col = "black",
lty = 1L,
factor.lty = 1L,
factor.pch = 19L,
factor.col = col,
factor.fill = factor.col,
factor.cex = 1L,
xaxs = "i",
yaxs = xaxs,
las = 1L,
scatter = FALSE,
scatter.pch = 19L,
scatter.col = factor.col,
scatter.bg = scatter.col,
scatter.cex = 0.5,
rug = TRUE,
rug.col = col,
rug.size = -0.02,
...
)
|
object |
A model object. |
... |
Additional arguments passed to |
x |
A character string specifying the name of variable to use as the x-axis dimension in the plot. |
dx |
If |
what |
A character string specifying whether to draw a “prediction” (fitted values from the model, calculated using |
data |
A data frame to override the default value offered in |
type |
A character string specifying whether to calculate predictions on the response scale (default) or link (only relevant for non-linear models). |
vcov |
A matrix containing the variance-covariance matrix for estimated model coefficients, or a function to perform the estimation with |
at |
Currently ignored. |
n |
An integer specifying the number of points across |
xvals |
A numeric vector of values at which to calculate predictions or marginal effects, if |
level |
The confidence level required (used to draw uncertainty bounds). |
draw |
A logical (default |
xlab |
A character string specifying the value of |
ylab |
A character string specifying the value of |
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 |
col |
A character string specifying the color of the prediction or marginal effect line. If |
lty |
An integer specifying the “line type” of the prediction or marginal effect line. See |
se.type |
A character string specifying whether to draw the confidence interval as “lines” (the default, using |
se.col |
If |
se.fill |
If |
se.lwd |
If |
se.lty |
If |
factor.lty |
If |
factor.pch |
If |
factor.col |
If |
factor.fill |
If |
factor.cex |
If |
xaxs |
A character string specifying |
yaxs |
A character string specifying |
las |
An integer string specifying |
scatter |
A logical indicating whether to plot the observed data in |
scatter.pch |
If |
scatter.col |
If |
scatter.bg |
If |
scatter.cex |
If |
rug |
A logical specifying whether to include an x-axis “rug” (see |
rug.col |
A character string specifying |
rug.size |
A numeric value specifying |
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 tidy data frame containing the data used to draw the plot. 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | ## 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 = head(iris, 50))
## marginal effect of 'Petal.Width' across 'Petal.Width'
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")
# marginal effect of 'Petal.Width' across 'Sepal.Width'
## without drawing the plot
## this might be useful for using, e.g., ggplot2 for plotting
tmp <- cplot(m, x = "Sepal.Width", dx = "Petal.Width",
what = "effect", n = 10, draw = FALSE)
if (require("ggplot2")) {
# use ggplot2 instead of base graphics
ggplot(tmp, aes(x = Petal.Width, y = "effect")) +
geom_line(lwd = 2) +
geom_line(aes(y = effect + 1.96*se.effect)) +
geom_line(aes(y = effect - 1.96*se.effect))
}
# a non-linear model
m <- glm(am ~ wt*drat, data = mtcars, family = binomial)
cplot(m, x = "wt") # prediction (response scale)
cplot(m, x = "wt") # prediction (link scale)
if (require("ggplot2")) {
# prediction (response scale, constrained to [0,1])
cplotdat <- cplot(m, x = "wt", type = "link", draw = FALSE)
ggplot(cplotdat, aes(x = xvals, y = plogis(yvals))) +
geom_line(lwd = 1.5) +
geom_line(aes(y = plogis(upper))) +
geom_line(aes(y = plotis(lower)))
}
# 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")
# plot conditional predictions across a third factor
local({
iris$long <- rbinom(nrow(iris), 1, 0.6)
x <- glm(long ~ Sepal.Width*Species, data = iris)
cplot(x, x = "Sepal.Width", data = iris[iris$Species == "setosa", ],
ylim = c(0,1), col = "red", se.fill = rgb(1,0,0,.5), xlim = c(2,4.5))
cplot(x, x = "Sepal.Width", data = iris[iris$Species == "versicolor", ],
draw = "add", col = "blue", se.fill = rgb(0,1,0,.5))
cplot(x, x = "Sepal.Width", data = iris[iris$Species == "virginica", ],
draw = "add", col = "green", se.fill = rgb(0,0,1,.5))
})
# 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.