View source: R/moplot_multpred.R
| moplot_multpred | R Documentation |
moplot_multpred() creates a graphical representation of model
interactions involving two or more categorical predictors.
The function decomposes each selected interaction coefficient into the elementary and higher-order contrasts implied by the contrast matrices used to fit the model. The upper panel displays the observed or model-predicted quantities involved in this decomposition. The optional lower panel displays the corresponding model coefficient, its confidence interval, and an approximate Cohen's d.
By default, the function identifies the interaction of highest order in the
fitted model and displays all coefficients associated with that interaction.
If more than one interaction of the highest order is present, the function
asks the user to select one explicitly through interaction.
moplot_multpred(
model,
data = NULL,
interaction = NULL,
coefficients = NULL,
interaction_info = TRUE,
split_rule = c("last_variable", "first_variable", "lowest_complexity",
"highest_complexity"),
top_split = NULL,
sig_level = 0.05,
conf_level = 0.95,
estimate_type = c("auto", "observed", "predicted"),
prediction_scale = c("response", "link"),
coefficient_scale = c("model", "interpreted"),
per_difference = FALSE,
bounds = NULL,
hypotheses = NULL,
show_coef = TRUE,
style = c("minimal", "tree"),
caption = TRUE,
digits = 2L,
base_size = 13,
panel_heights = c(2, 1)
)
model |
A fitted model supported by the package. Supported model
classes include objects returned by |
data |
Optional data frame containing the variables used to fit
|
interaction |
Optional character string identifying the interaction
term to display, for example If |
coefficients |
Optional character vector identifying specific model coefficients to display. If |
interaction_info |
Logical. Should the function print the available
interaction terms and their corresponding coefficient selections?
The default is The printed table shows syntax that can be copied directly into
|
split_rule |
Character string specifying how the top-level split variable is selected during the hierarchical decomposition. Available options are:
|
top_split |
Optional character string identifying the predictor to
force as the top-level split variable. When supplied, |
sig_level |
Numeric value between 0 and 1 defining the significance
threshold used for coefficient labels. The default is |
conf_level |
Numeric value between 0 and 1 specifying the confidence
level used for displayed confidence intervals. The default is |
estimate_type |
Character string specifying how quantities in the upper panel are computed. Available options are:
The default is |
prediction_scale |
Character string specifying the scale used for
model-based predictions when
The default is |
coefficient_scale |
Character string specifying the quantity displayed in the lower panel. Available options are:
The default is |
per_difference |
Logical. Should the lower-panel coefficients be
divided by their scale factor? The default is Each interaction coefficient equals the product of the scale factors of
its components multiplied by the displayed difference of differences
(for example |
bounds |
Optional numeric vector |
hypotheses |
Optional named list declaring, for one or more
categorical predictors, the comparisons that their contrast coefficients
are intended to estimate, one matrix per predictor. Each interaction
coefficient is checked through its components; the result is reported in
a message and in the caption. The figure always shows the comparisons
that the coefficients estimate. See |
show_coef |
Logical. Should the lower coefficient panel be displayed?
The default is |
style |
Character string specifying the graphical representation of the interaction decomposition. Available options are:
The default is |
caption |
Controls whether a descriptive caption is printed in the console. It can be:
The automatic caption describes the selected interaction, the graphical hierarchy, the contrast coding assigned to each interacting variable, the displayed estimate type, the selected graphical style, and the lower coefficient panel when present. The caption is printed in the console and is not added to the graphical object. |
digits |
Non-negative integer specifying the number of decimal places
used in lower-panel estimate and effect-size labels. The default is |
base_size |
Positive numeric value defining the base font size used in
both panels. The default is |
panel_heights |
Numeric vector of length two specifying the relative
heights of the upper and lower panels. The default is |
moplot_multpred() is a graphical interface to the hierarchical
decomposition performed by contrasts_multpred().
Each selected interaction coefficient is decomposed according to the contrast matrices assigned to the categorical predictors involved in the interaction. This allows treatment, sum, Helmert, reverse Helmert, sliding-difference, and interpretable custom contrasts to be represented directly in terms of the factor levels or level aggregates implied by the fitted model.
The graphical hierarchy is determined by split_rule or top_split.
The top-level split variable defines the outermost grouping structure,
whereas successive predictors define progressively lower levels of the
decomposition. The final predictor in the resulting hierarchy defines the
quantities represented on the x-axis.
When an interaction contains more than two predictors, the decomposition is recursive. Elementary contrasts are first constructed from pairs of displayed quantities and are then combined into progressively higher-order contrasts until the complete model interaction contrast is obtained.
For estimate_type = "predicted", predictions are population-level
predictions based on the fixed effects. For generalized models on the
response scale, uncertainty is propagated using the fixed-effect
variance-covariance matrix and the multivariate delta method where
appropriate. A methodological note describing only the settings relevant to
the current call is printed when the function is run.
When show_coef = FALSE, a ggplot object containing the upper interaction
decomposition panel.
When show_coef = TRUE, a patchwork object containing the aligned upper and
lower panels.
The arguments interaction and coefficients provide two complementary
levels of control.
To display all coefficients associated with one interaction, specify only
interaction, for example:
interaction = "A:B:C"
To display only selected coefficients from that interaction, specify both:
interaction = "A:B:C"
coefficients = c("A1:B2:C2", "A2:B2:C2")
If neither argument is supplied, the unique highest-order interaction is selected automatically.
When interaction_info = TRUE, the function prints the interactions found
in the model together with valid coefficient selections, providing
copy-ready syntax for subsequent calls.
The three graphical styles differ in how much of the hierarchical decomposition is shown.
"minimal" emphasizes the observed or predicted quantities and elementary
contrasts.
"tree" represents the same hierarchy through branching segments,
emphasizing the recursive structure of the decomposition.
The lower coefficient panel, when requested, remains horizontally aligned with the corresponding contrast block in the upper panel for all three styles.
set.seed(123)
dat <- expand.grid(
A = factor(c("A1", "A2", "A3")),
B = factor(c("B1", "B2")),
C = factor(c("C1", "C2")),
rep = seq_len(20)
)
contrasts(dat$A) <- reverse_helmert(3)
contrasts(dat$B) <- MASS::contr.sdif(2)
contrasts(dat$C) <- stats::contr.treatment(2)
dat$y <- with(
dat,
10 +
2 * (A == "A2") +
4 * (A == "A3") +
2 * (B == "B2") +
2 * (C == "C2") +
2 * (A == "A2" & B == "B2" & C == "C2") +
rnorm(nrow(dat))
)
mod <- stats::lm(
y ~ A * B * C,
data = dat
)
# Automatically display the unique highest-order interaction.
plot_auto <- moplot_multpred(
model = mod,
data = dat
)
# Display all coefficients from a specific interaction.
plot_tree <- moplot_multpred(
model = mod,
interaction = "A:B:C",
style = "tree"
)
# Display one specific coefficient using the minimal representation.
plot_minimal <- moplot_multpred(
model = mod,
interaction = "A:B:C",
coefficients = "A1:B2-1:C2",
style = "minimal"
)
# Use model-based predictions instead of observed means.
plot_predicted <- moplot_multpred(
model = mod,
interaction = "A:B:C",
estimate_type = "predicted",
prediction_scale = "response"
)
# The recommended size is stored in the plot and used by moplot_export().
attr(plot_tree, "moplot_export_size")
path <- moplot_export(
plot_tree,
filename = file.path(tempdir(), "moplot_multpred.png"),
dpi = 72
)
unlink(path)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.