knitr::opts_chunk$set(collapse = TRUE, comment = "#>", dev = "png", fig.width = 7, fig.height = 3.5, message = FALSE, warning = FALSE) options(width = 800) if (!requireNamespace("magrittr", quietly = TRUE) || !requireNamespace("see", quietly = TRUE) || !requireNamespace("emmeans", quietly = TRUE) || !requireNamespace("sjmisc", quietly = TRUE)) { knitr::opts_chunk$set(eval = FALSE) }
ggpredict()
and ggemmeans()
compute predicted values for all possible levels or values from a model's predictor. Basically, ggpredict()
wraps the predict()
-method for the related model, while ggemmeans()
wraps the emmeans()
-method from the emmeans-package. Both ggpredict()
and ggemmeans()
do some data-preparation to bring the data in shape for the newdata
-argument (predict()
) resp. the at
-argument (emmeans()
). It is recommended to read the general introduction first, if you haven't done this yet.
Thus, effects returned by ggpredict()
conditional effects (i.e. these are conditioned on certain (reference) levels of factors), while ggemmeans()
returns marginal means, since the effects are "marginalized" (or "averaged") over the levels of factors.
For models without categorical predictors, the results from ggpredict()
and ggemmeans()
are identical (except some slight differences in the associated confidence intervals, which are, however, negligible).
library(magrittr) library(ggeffects) data(efc) fit <- lm(barthtot ~ c12hour + neg_c_7, data = efc) ggpredict(fit, terms = "c12hour") ggemmeans(fit, terms = "c12hour")
As can be seen, the continuous predictor neg_c_7
is held constant at its mean value, 11.83. For categorical predictors, ggpredict()
and ggemmeans()
behave differently. While ggpredict()
uses the reference level of each categorical predictor to hold it constant, ggemmeans()
- like ggeffect()
- averages over the proportions of the categories of factors.
library(sjmisc) data(efc) efc$e42dep <- to_label(efc$e42dep) fit <- lm(barthtot ~ c12hour + neg_c_7 + e42dep, data = efc) ggpredict(fit, terms = "c12hour") ggemmeans(fit, terms = "c12hour")
In this case, one would obtain the same results for ggpredict()
and ggemmeans()
again, if condition
is used to define specific levels at which variables, in our case the factor e42dep
, should be held constant.
ggpredict(fit, terms = "c12hour") ggemmeans(fit, terms = "c12hour", condition = c(e42dep = "independent"))
Creating plots is as simple as described in the vignette Plotting Marginal Effects.
ggemmeans(fit, terms = c("c12hour", "e42dep")) %>% plot()
But when should I use ggemmeans()
and when ggpredict()
?
When you are interested in the strength of association, it usually doesn't matter. as you can see in the plots below. The slope of our focal term, c12hour
, is the same for all three plots:
library(see) p1 <- plot(ggpredict(fit, terms = "c12hour"), ci = FALSE, show.title = FALSE, show.x.title = FALSE, show.y.title = FALSE) p2 <- plot(ggemmeans(fit, terms = "c12hour"), ci = FALSE, show.title = FALSE, show.x.title = FALSE, show.y.title = FALSE) p3 <- plot(ggemmeans(fit, terms = "c12hour", condition = c(e42dep = "independent")), ci = FALSE, show.title = FALSE, show.x.title = FALSE, show.y.title = FALSE) plots(p1, p2, p3, n_rows = 1)
However, the predicted outcome varies. This gives an impression when ggemmeans()
, i.e. marginal effects, matter: when you want to predict your outcome, marginalized over the different levels of factors, i.e. "generalized" to the population (of your sample). ggpredict()
would give a predicted outcome for a subgroup of your sample, i.e. conditioned on specific levels of factors. Hence, the predicted outcome from ggpredict()
does not necessarily generalize to the "population" (always keeping in mind that we assume having a "representative sample" of a "population" as data in our model).
But why should I use ggpredict()
anymore?
Some models are not yet supported by the emmeans package, thus, for certain models, only ggpredict()
works, not ggemmeans()
nor ggeffect()
. Sometimes, robust variance-covariance estimation is required for confidence intervals of predictions. In such cases, you have to rely on ggpredict()
. If you have no categorical predictors as non-focal terms (i.e. no factor needs to be held constant), then - as shown above - ggpredict()
and ggemmeans()
yield the same results.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.