knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
knitr::opts_chunk$set(fig.height = 6, fig.width = 6) library(erikmisc)
The e_plot_model_contrasts()
function is a wrapper for emmeans::emmeans()
.
It automates plotting all of the model effects and annotating the plots with estimates and contrasts.
Now it is (almost) effortless to interpret a linear model output.
This has been tested for lm()
(fixed-effects model) and lme4::lmer
(mixed fixed- and random-effects model).
The data variables are labelled and those will be used for plots.
str(dat_mtcars_e)
This model is specifically chosen to have numeric and categorical main effects, and each type of two-way interaction (cat:cat, cat:num, num:num), as well as some categories that were not estimable. "Statistical significance" and "model fit" are not concerns in this demonstration.
# Set specific model with some interactions form_model <- mpg ~ cyl + disp + hp + wt + vs + am + cyl:vs + disp:hp + hp:vs fit <- lm( formula = form_model , data = dat_mtcars_e ) car::Anova( fit , type = 3 , singular.ok = TRUE )
summary(fit)
See the help ?e_plot_model_contrasts
for the method defaults.
I'm choosing to not print the output (default) while the function runs so that
we can examine the separate lists in the output.
fit_contrasts <- e_plot_model_contrasts( fit = fit , dat_cont = dat_mtcars_e , sw_print = FALSE )
The output contains lists for tables, plots, and text.
fit_contrasts |> names()
The tables
are give the estimates ($est
) and contrasts ($cont
) for each
effect in the model not involved with an interaction (argument
sw_marginal_even_if_interaction
can include all main effects).
fit_contrasts$tables # to print tables
The text
of the tables are written in a form that can be copy/pasted into a
report and worked into sentences.
fit_contrasts$text # to print caption text
The plots
illustrate the effects with labelled plots and the text
in the
caption for a single complete result.
From the help:
Plot interpretation: This EMM plot (Estimated Marginal Means, aka Least-Squares Means) is only available when conditioning on one variable. The blue bars are confidence intervals for the EMMs; don't ever use confidence intervals for EMMs to perform comparisons --- they can be very misleading. The red arrows are for the comparisons between means; the degree to which the "comparison arrows" overlap reflects as much as possible the significance of the comparison of the two estimates. If an arrow from one mean overlaps an arrow from another group, the difference is not significant, based on the adjust setting (which defaults to "tukey").
For two-way interactions, both directions of conditioning are plotted: var1|var2
and var2|var1
.
Furthermore, each plot is made separately and given in a side-by-side or above-below orientation.
Use ggsave()
for a specific plot object and choose a good width
and height
to make it look beautiful.
fit_contrasts$plots # to print plots
Below I use some options to specify one two-way interaction, stack them above-below ("tall"), and exclude the table in the plot.
fit_contrasts <- e_plot_model_contrasts( fit = fit , dat_cont = dat_mtcars_e , choose_contrasts = "cyl:vs" , sw_print = FALSE , sw_table_in_plot = FALSE , sw_TWI_plots_keep = c("singles", "both", "all")[2] , sw_TWI_both_orientation = c("wide", "tall")[2] ) fit_contrasts$plots # to print plots fit_contrasts$tables # to print tables fit_contrasts$text # to print caption text
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.