This method adds support for plot_predictions
,
interact_plot
, cat_plot
, and effect_plot
for models fit with
rstanarm
.
# S3 method for stanreg
make_predictions(model, pred, pred.values = NULL,
modx = NULL, modx.values = NULL, mod2 = NULL, mod2.values = NULL,
centered = "all", data = NULL, plot.points = FALSE,
interval = TRUE, int.width = 0.95, estimate = "mean",
linearity.check = FALSE, set.offset = 1, pred.labels = NULL,
modx.labels = NULL, mod2.labels = NULL, preds.per.level = 100,
force.cat = FALSE, facet.modx = linearity.check, ...)
A stanreg
model.
The name of the predictor variable involved in the interaction. This must be a string.
Which values of the predictor should be included in the plot? By default, all levels are included.
The name of the moderator variable involved in the interaction. This must be a string.
For which values of the moderator should lines be plotted?
Default is NULL
. If NULL
, then the customary +/- 1 standard
deviation from the mean as well as the mean itself are used for continuous
moderators. If "plus-minus"
, plots lines when the moderator is at
+/- 1 standard deviation without the mean. You may also choose "terciles"
to split the data into equally-sized groups and choose the point at the
mean of each of those groups.
If the moderator is a factor variable and modx.values
is
NULL
, each level of the factor is included. You may specify
any subset of the factor levels (e.g., c("Level 1", "Level 3")
) as long
as there is more than 1. The levels will be plotted in the order you
provide them, so this can be used to reorder levels as well.
Optional. The name of the second moderator variable involved in the interaction. This can be a bare name or string.
For which values of the second moderator should the plot
be
facetted by? That is, there will be a separate plot for each level of this
moderator. Defaults are the same as modx.values
.
A vector of quoted variable names that are to be
mean-centered. If "all"
, all non-focal predictors are centered. You
may instead pass a character vector of variables to center. User can
also use "none" to base all predictions on variables set at 0.
The response variable, pred
, modx
, and mod2
variables are never
centered.
Optional, default is NULL. You may provide the data used to
fit the model. This can be a better way to get mean values for centering
and can be crucial for models with variable transformations in the formula
(e.g., log(x)
) or polynomial terms (e.g., poly(x, 2)
). You will
see a warning if the function detects problems that would likely be
solved by providing the data with this argument and the function will
attempt to retrieve the original data from the global environment.
Logical. If TRUE
, plots the actual data points as
a scatterplot on top of the interaction lines. The color of the dots will
be based on their moderator value.
Logical. If TRUE
, plots confidence/prediction
intervals around the line using geom_ribbon
.
How large should the interval be, relative to the standard error? The default, .95, corresponds to roughly 1.96 standard errors and a .05 alpha level for values outside the range. In other words, for a confidence interval, .95 is analogous to a 95% confidence interval.
Should estimates be based on mean or median simulation? Default is "mean".
For two-way interactions only. If TRUE
, plots a
pane for each level of the moderator and superimposes a loess smoothed
line (in gray) over the plot. This enables you to see if the effect is
linear through the span of the moderator. See Hainmueller et al. (2016) in
the references for more details on the intuition behind this. It is
recommended that you also set plot.points = TRUE
and use
modx.values = "terciles"
with this option.
For models with an offset (e.g., Poisson models), sets an offset for the predicted values. All predicted values will have the same offset. By default, this is set to 1, which makes the predicted values a proportion. See details for more about offset support.
A character vector of 2 labels for the predictor if it is
a 2-level factor or a continuous variable with only 2 values. If
NULL
, the default, the factor labels are used.
A character vector of labels for each level of the
moderator values, provided in the same order as the modx.values
argument. If NULL
, the values themselves are used as labels unless
modx,values
is also NULL
. In that case, "+1 SD" and "-1 SD"
are used.
A character vector of labels for each level of the 2nd
moderator values, provided in the same order as the mod2.values
argument. If NULL
, the values themselves are used as labels unless
mod2.values
is also NULL
. In that case, "+1 SD" and "-1 SD"
are used.
For continuous predictors, a series of equally spaced points across the observed range of the predictor are used to create the lines for each level of the moderator. Use this to choose how many points are used for that. Default is 100, but for complicated models larger numbers may better capture the curvature.
If TRUE
, treats numeric predictor as categorical. This
can be helpful when you have 0/1 dummy variables that you don't want to
plot as if intermediate values are possible.
Create separate panels for each level of the moderator?
Default is FALSE, except when linearity.check
is TRUE.
Ignored.