plot_model()
creates plots from regression models, either
estimates (as so-called forest or dot whisker plots) or marginal effects.
plot_model(model, type = c("est", "re", "eff", "pred", "int", "std", "std2", "slope", "resid", "diag"), transform, terms = NULL, sort.est = NULL, rm.terms = NULL, group.terms = NULL, order.terms = NULL, pred.type = c("fe", "re"), mdrt.values = c("minmax", "meansd", "zeromax", "quart", "all"), ri.nr = NULL, title = NULL, axis.title = NULL, axis.labels = NULL, wrap.title = 50, wrap.labels = 25, axis.lim = NULL, grid.breaks = NULL, ci.lvl = NULL, se = NULL, colors = "Set1", show.intercept = FALSE, show.values = FALSE, show.p = TRUE, show.data = FALSE, show.legend = TRUE, show.zeroinf = TRUE, value.offset = NULL, value.size, digits = 2, dot.size = NULL, line.size = NULL, vline.color = NULL, grid, case, auto.label = TRUE, prefix.labels = c("none", "varname", "label"), bpe = "median", bpe.style = "line", bpe.color = "white", ...) get_model_data(model, type = c("est", "re", "eff", "pred", "int", "std", "std2", "slope", "resid", "diag"), transform, terms = NULL, sort.est = NULL, rm.terms = NULL, group.terms = NULL, order.terms = NULL, pred.type = c("fe", "re"), ri.nr = NULL, ci.lvl = NULL, colors = "Set1", grid, case = "parsed", digits = 2, ...)
model | A regression model object. Depending on the |
---|---|
type | Type of plot. There are three groups of plot-types:
Marginal Effects (related vignette)
Model diagnostics
Note: For mixed models, the diagnostic plots like linear relationship or check for Homoscedasticity, do not take the uncertainty of random effects into account, but is only based on the fixed effects part of the model. |
transform | A character vector, naming a function that will be applied
on estimates and confidence intervals. By default, |
terms | Character vector with the names of those terms from
|
sort.est | Determines in which way estimates are sorted in the plot:
|
rm.terms | Character vector with names that indicate which terms should
be removed from the plot. Counterpart to |
group.terms | Numeric vector with group indices, to group coefficients. Each group of coefficients gets its own color (see 'Examples'). |
order.terms | Numeric vector, indicating in which order the coefficients should be plotted. See examples in this package-vignette. |
pred.type | Character, only applies for Marginal Effects plots
with mixed effects models. Indicates whether predicted values should be
conditioned on random effects ( |
mdrt.values | Indicates which values of the moderator variable should be
used when plotting interaction terms (i.e.
|
ri.nr | Numeric vector. If |
title | Character vector, used as plot title. By default,
|
axis.title | Character vector of length one or two (depending on the
plot function and type), used as title(s) for the x and y axis. If not
specified, a default labelling is chosen. Note: Some plot types
may not support this argument sufficiently. In such cases, use the returned
ggplot-object and add axis titles manually with
|
axis.labels | Character vector with labels for the model terms, used as
axis labels. By default, |
wrap.title | Numeric, determines how many chars of the plot title are displayed in one line and when a line break is inserted. |
wrap.labels | Numeric, determines how many chars of the value, variable or axis labels are displayed in one line and when a line break is inserted. |
axis.lim | Numeric vector of length 2, defining the range of the plot
axis. Depending on plot-type, may effect either x- or y-axis. For
Marginal Effects plots, |
grid.breaks | Numeric value or vector; if |
ci.lvl | Numeric, the level of the confidence intervals (error bars).
Use |
se | Either a logical, and if |
colors | May be a character vector of color values in hex-format, valid
color value names (see
|
show.intercept | Logical, if |
show.values | Logical, whether values should be plotted or not. |
show.p | Logical, adds asterisks that indicate the significance level of estimates to the value labels. |
show.data | Logical, for Marginal Effects plots, also plots the raw data points. |
show.legend | For Marginal Effects plots, shows or hides the legend. |
show.zeroinf | Logical, if |
value.offset | Numeric, offset for text labels to adjust their position relative to the dots or lines. |
value.size | Numeric, indicates the size of value labels. Can be used
for all plot types where the argument |
digits | Numeric, amount of digits after decimal point when rounding estimates or values. |
dot.size | Numeric, size of the dots that indicate the point estimates. |
line.size | Numeric, size of the lines that indicate the error bars. |
vline.color | Color of the vertical "zero effect" line. Default color is inherited from the current theme. |
grid | Logical, if |
case | Desired target case. Labels will automatically converted into the
specified character case. See |
auto.label | Logical, if |
prefix.labels | Indicates whether the value labels of categorical variables
should be prefixed, e.g. with the variable name or variable label. See
argument |
bpe | For Stan-models (fitted with the rstanarm- or
brms-package), the Bayesian point estimate is, by default, the median
of the posterior distribution. Use |
bpe.style | For Stan-models (fitted with the rstanarm- or
brms-package), the Bayesian point estimate is indicated as a small,
vertical line by default. Use |
bpe.color | Character vector, indicating the color of the Bayesian
point estimate. Setting |
... | Other arguments, passed down to various functions. Here is a list of supported arguments and their description in detail.
|
Depending on the plot-type, plot_model()
returns a
ggplot
-object or a list of such objects. get_model_data
returns the associated data with the plot-object as tidy data frame, or
(depending on the plot-type) a list of such data frames.
get_model_data
simply calls plot_model()
and returns
the data from the ggplot-object. Hence, it is rather inefficient and should
be used as alternative to brooms tidy()
-function only in
specific situations.
Some notes on the different plot-types:
type = "std2"
Plots standardized beta values,
however, standardization follows Gelman's (2008) suggestion, rescaling the
estimates by dividing them by two standard deviations instead of just one.
Resulting coefficients are then directly comparable for untransformed
binary predictors. This standardization uses the
standardize
-function from the arm-package.
type = "pred"
Plots marginal effects. Simply wraps
ggpredict
.
type = "eff"
Plots marginal effects. Simply wraps
ggeffect
.
type = "int"
A shortcut for marginal effects plots, where
interaction terms are automatically detected and used as
terms
-argument. Furthermore, if the moderator variable (the second
- and third - term in an interaction) is continuous, type = "int"
automatically chooses useful values based on the mdrt.values
-argument,
which are passed to terms
. Then, ggpredict
is called. type = "int"
plots the interaction term that appears
first in the formula along the x-axis, while the second (and possibly
third) variable in an interaction is used as grouping factor(s)
(moderating variable). Use type = "pred"
or type = "eff"
and specify a certain order in the terms
-argument to indicate
which variable(s) should be used as moderator.
plot_model()
replaces the functions sjp.lm
,
sjp.glm
, sjp.lmer
, sjp.glmer
and sjp.int
. These
are becoming softly deprecated and will be removed in a future update.
Gelman A (2008) "Scaling regression inputs by dividing by two
standard deviations." Statistics in Medicine 27: 2865–2873.
http://www.stat.columbia.edu/~gelman/research/published/standardizing7.pdf
Aiken and West (1991). Multiple Regression: Testing and Interpreting Interactions.
# prepare data library(sjmisc) data(efc) efc <- to_factor(efc, c161sex, e42dep, c172code) m <- lm(neg_c_7 ~ pos_v_4 + c12hour + e42dep + c172code, data = efc) # simple forest plot plot_model(m)# grouped coefficients plot_model(m, group.terms = c(1, 2, 3, 3, 3, 4, 4))# multiple plots, as returned from "diagnostic"-plot type, # can be arranged with 'plot_grid()'# NOT RUN { p <- plot_model(m, type = "diag") plot_grid(p) # }# plot random effects library(lme4) m <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy) plot_model(m, type = "re")# plot marginal effects plot_model(m, type = "eff", terms = "Days")# plot interactions# NOT RUN { m <- glm( tot_sc_e ~ c161sex + c172code * neg_c_7, data = efc, family = poisson() ) # type = "int" automatically selects groups for continuous moderator # variables - see argument 'mdrt.values'. The following function call is # identical to: # plot_model(m, type = "pred", terms = c("c172code", "neg_c_7 [7,28]")) plot_model(m, type = "int") # switch moderator plot_model(m, type = "pred", terms = c("neg_c_7", "c172code")) # same as # ggeffects::ggpredict(m, terms = c("neg_c_7", "c172code")) # }# plot Stan-model# NOT RUN { if (require("rstanarm")) { data(mtcars) m <- stan_glm(mpg ~ wt + am + cyl + gear, data = mtcars, chains = 1) plot_model(m, bpe.style = "dot") } # }