cplot: Conditional predicted value and average marginal effect plots...

Description Usage Arguments Details Value See Also Examples

View source: R/cplot.R

Description

Draw one or more conditional effects plots reflecting model coefficients, or a function to perform the estimation with model as its only argument.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
cplot(
  object,
  x = NULL,
  dx = NULL,
  what = c("prediction", "effect", "classprediction", "stackedprediction"),
  type = c("response", "link"),
  vcov = stats::vcov(object),
  data = NULL,
  level = 0.95,
  draw = TRUE,
  xvals = NULL,
  z = NULL,
  zvals = NULL,
  n = 25,
  rugplot = TRUE,
  at = NULL,
  ...
)

Arguments

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 TRUE), specifying whether to draw the plot. If FALSE, the data used in drawing are returned as a list of data.frames. This might be useful if you want to plot using an alternative plotting package (e.g., ggplot2). Also, if set to value “add”, then the resulting data is added to the existing plot.

xvals

A numeric vector of values at which to calculate predictions or marginal effects, if x is numeric. By default, it is calculated from the data using seq_range. If x is a factor, this is ignored, as is n.

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 x at which to calculate the predicted value or marginal effect, when x is numeric. Ignored otherwise.

rugplot

logical include a rugplot at the bottom of the graph

at

Currently ignored.

...

Additional arguments such as colour, linetype, size, shape, fill, alpha. These will be passed to ggplot2 geom functions to alter the style of the plot. If 'x' is a factor, these arguments will be passed to geom_pointrange. If 'x' is numeric, these arguments will be passed to geom_line and geom_ribbon. The alpha and fill arguments are not passed to geom_line. The colour argument is not passed to geom_ribbon.

Details

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.

Value

A ggplot2 object. Use draw = FALSE to simply generate the data structure for use elsewhere.

See Also

plot.margins, persp.lm

Examples

 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)

vincentarelbundock/marginsplot documentation built on May 30, 2020, 12:07 a.m.