The aim of this package is similar to the broom-package: transforming “untidy” input into a tidy data frame, especially for further use with ggplot. However, ggeffects does not return model-summaries; rather, this package computes marginal effects at the mean or average marginal effects from statistical models and returns the result as tidy data frame (as tibbles, to be more precisely).
Since the focus lies on plotting the data (the marginal effects), at least one model term needs to be specified for which the effects are computed. It is also possible to compute marginal effects for model terms, grouped by the levels of another model’s predictor. The package also allows plotting marginal effects for two- or three-way-interactions, or for specific values of a model term only. Examples are shown below.
The returned data frames always have the same, consistent structure and column names, so it’s easy to create ggplot-plots without the need to re-write the arguments to be mapped in each ggplot-call. x
and predicted
are the values for the x- and y-axis. conf.low
and conf.high
could be used as ymin
and ymax
aesthetics for ribbons to add confidence bands to the plot. group
can be used as grouping-aesthetics, or for faceting.
ggpredict()
computes predicted values for all possible levels and values from a model’s predictors. In the simplest case, a fitted model is passed as first argument, and the term in question as second argument:
library(ggeffects)
data(efc)
fit <- lm(barthtot ~ c12hour + neg_c_7 + c161sex + c172code, data = efc)
ggpredict(fit, terms = "c12hour")
#> # A tibble: 62 x 5
#> x predicted conf.low conf.high group
#> <dbl> <dbl> <dbl> <dbl> <fctr>
#> 1 4.00 74.4 72.3 76.5 1
#> 2 5.00 74.2 72.1 76.3 1
#> 3 6.00 73.9 71.9 76.0 1
#> 4 7.00 73.7 71.6 75.7 1
#> 5 8.00 73.4 71.4 75.4 1
#> 6 9.00 73.2 71.2 75.2 1
#> 7 10.0 72.9 70.9 74.9 1
#> 8 11.0 72.7 70.7 74.6 1
#> 9 12.0 72.4 70.5 74.3 1
#> 10 14.0 71.9 70.0 73.8 1
#> # ... with 52 more rows
The output shows the predicted values for the response at each value from the term c12hour. The data is already in shape for ggplot:
library(ggplot2)
theme_set(theme_bw())
mydf <- ggpredict(fit, terms = "c12hour")
ggplot(mydf, aes(x, predicted)) + geom_line()
The terms
-argument accepts up to three model terms, where the second and third term indicate grouping levels. This allows predictions for the term in question at different levels for other model terms:
ggpredict(fit, terms = c("c12hour", "c172code"))
#> # A tibble: 186 x 5
#> x predicted conf.low conf.high group
#> <dbl> <dbl> <dbl> <dbl> <fctr>
#> 1 4.00 73.7 70.3 77.1 low level of education
#> 2 4.00 74.5 72.4 76.5 intermediate level of education
#> 3 4.00 75.2 71.8 78.5 high level of education
#> 4 5.00 73.5 70.1 76.9 low level of education
#> 5 5.00 74.2 72.1 76.3 intermediate level of education
#> 6 5.00 74.9 71.6 78.2 high level of education
#> 7 6.00 73.2 69.8 76.6 low level of education
#> 8 6.00 73.9 71.9 76.0 intermediate level of education
#> 9 6.00 74.7 71.4 78.0 high level of education
#> 10 7.00 73.0 69.6 76.3 low level of education
#> # ... with 176 more rows
Creating a ggplot is pretty straightforward: the colour-aesthetics is mapped with the group
-column:
mydf <- ggpredict(fit, terms = c("c12hour", "c172code"))
ggplot(mydf, aes(x, predicted, colour = group)) + geom_line()
Finally, a second grouping structure can be defined, which will create another column named facet
, which - as the name implies - might be used to create a facted plot:
mydf <- ggpredict(fit, terms = c("c12hour", "c172code", "c161sex"))
mydf
#> # A tibble: 372 x 6
#> x predicted conf.low conf.high group facet
#> <dbl> <dbl> <dbl> <dbl> <fctr> <fctr>
#> 1 4.00 74.0 70.5 77.5 low level of education [2] Female
#> 2 4.00 72.9 68.4 77.5 low level of education [1] Male
#> 3 4.00 74.7 72.4 77.0 intermediate level of education [2] Female
#> 4 4.00 73.7 70.1 77.2 intermediate level of education [1] Male
#> 5 4.00 75.4 71.9 78.9 high level of education [2] Female
#> 6 4.00 74.4 70.1 78.7 high level of education [1] Male
#> 7 5.00 73.7 70.2 77.2 low level of education [2] Female
#> 8 5.00 72.7 68.1 77.2 low level of education [1] Male
#> 9 5.00 74.4 72.1 76.7 intermediate level of education [2] Female
#> 10 5.00 73.4 69.8 77.0 intermediate level of education [1] Male
#> # ... with 362 more rows
ggplot(mydf, aes(x, predicted, colour = group)) +
geom_line() +
facet_wrap(~facet)
ggaverage()
compute average marginal effects. While ggpredict()
creates a data-grid (using expand.grid()
) for all possible combinations of values (even if some combinations are not present in the data), ggaverage()
computes predicted values based on the given data. This means that different predicted values for the outcome may occure at the same value or level for the term in question. The predicted values are then averaged for each value of the term in question and the linear trend is smoothed accross the averaged predicted values. This means that the line representing the marginal effects may cross or diverge, and are not necessarily in paralell to each other.
mydf <- ggaverage(fit, terms = c("c12hour", "c172code"))
ggplot(mydf, aes(x, predicted, colour = group)) + geom_line()
The terms
-argument not only defines the model terms of interest, but each model term that defines the grouping structure can be limited to certain values. This allows to compute and plot marginal effects for terms at specific values only. To define these values, put them in square brackets directly after the term name: terms = c("c12hour [30, 50, 80]", "c172code [1,3]")
mydf <- ggpredict(fit, terms = c("c12hour [30, 50, 80]", "c172code [1,3]"))
mydf
#> # A tibble: 6 x 5
#> x predicted conf.low conf.high group
#> <dbl> <dbl> <dbl> <dbl> <fctr>
#> 1 30.0 67.1 64.0 70.3 low level of education
#> 2 30.0 68.6 65.4 71.8 high level of education
#> 3 50.0 62.1 59.0 65.1 low level of education
#> 4 50.0 63.5 60.3 66.7 high level of education
#> 5 80.0 54.5 51.3 57.7 low level of education
#> 6 80.0 55.9 52.4 59.5 high level of education
ggplot(mydf, aes(x, predicted, colour = group)) + geom_line()
To plot the marginal effects of interaction terms, simply specify these terms in the terms
-argument.
library(sjmisc)
data(efc)
# make categorical
efc$c161sex <- to_factor(efc$c161sex)
# fit model with interaction
fit <- lm(neg_c_7 ~ c12hour + barthtot * c161sex, data = efc)
# select only levels 30, 50 and 70 from continuous variable Barthel-Index
mydf <- ggpredict(fit, terms = c("barthtot [30,50,70]", "c161sex"))
ggplot(mydf, aes(x, predicted, colour = group)) + geom_line()
Since the terms
-argument accepts up to three model terms, you can also compute marginal effects for a 3-way-interaction.
To plot the marginal effects of interaction terms, simply specify these terms in the terms
-argument.
# fit model with 3-way-interaction
fit <- lm(neg_c_7 ~ c12hour * barthtot * c161sex, data = efc)
# select only levels 30, 50 and 70 from continuous variable Barthel-Index
mydf <- ggpredict(fit, terms = c("c12hour", "barthtot [30,50,70]", "c161sex"))
ggplot(mydf, aes(x, predicted, colour = group)) +
geom_line() +
facet_wrap(~facet)
ggpredict()
also works for models with polynomial terms or splines. Following code reproduces the plot from ?splines::bs
:
library(splines)
data(women)
fm1 <- lm(weight ~ bs(height, df = 5), data = women)
dat <- ggpredict(fm1, "height")
ggplot(dat, aes(x, predicted)) +
geom_line() +
geom_point()
ggeffects makes use of the sjlabelled-package and supports labelled data. If the data from the fitted models is labelled, the value and variable label attributes are usually copied to the model frame stored in the model object. ggeffects provides various getter-functions to access these labels, which are returned as character vector and can be used in ggplot’s lab()
- or scale_*()
-functions.
get_title()
- a generic title for the plot, based on the model family, like “predicted values” or “predicted probabilities”get_x_title()
- the variable label of the first model term in terms
.get_y_title()
- the variable label of the response.get_legend_title()
- the variable label of the second model term in terms
.get_x_labels()
- value labels of the first model term in terms
.get_legend_labels()
- value labels of the second model term in terms
.The data frame returned by ggpredict()
or ggaverage()
must be used as argument to one of the above function calls.
get_x_title(mydf)
#> [1] "average number of hours of care per week"
get_y_title(mydf)
#> [1] "Negative impact with 7 items"
ggplot(mydf, aes(x, predicted, colour = group)) +
geom_line() +
facet_wrap(~facet) +
labs(
x = get_x_title(mydf),
y = get_y_title(mydf),
colour = get_legend_title(mydf)
)
If you don’t want to write your own ggplot-code, ggeffects has a plot()
-method with some convenient defaults, which allows quickly creating ggplot-objects. plot()
has only a few arguments to keep this function small and simple. For instance, ci
allows you to show or hide confidence bands (or error bars, for discrete variables), facets
allows you to create facets even for just one grouping variable, or colors
allows you to quickly choose from some color-palettes, including black & white colored plots. Use rawdata
to add the raw data points to the plot.
data(efc)
efc$c172code <- to_label(efc$c172code)
fit <- lm(barthtot ~ c12hour + neg_c_7 + c161sex + c172code, data = efc)
# facet by group
dat <- ggpredict(fit, terms = c("c12hour", "c172code"))
plot(dat, facet = TRUE)
# don't use facets, b/w figure, w/o confidence bands
plot(dat, colors = "bw", ci = FALSE)
# plot raw data
dat <- ggpredict(fit, terms = c("c12hour", "c172code"))
plot(dat, rawdata = TRUE)
# for three variables, automatic facetting
dat <- ggpredict(fit, terms = c("c12hour", "c172code", "c161sex"))
plot(dat)
# categorical variables have errorbars
dat <- ggpredict(fit, terms = c("c172code", "c161sex"))
plot(dat)