---
title: "ggeffects: Marginal Effects of Regression Models"
author: "Daniel Lüdecke"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{ggeffects: Marginal Effects of Regression Models}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r set-options, echo = FALSE}
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("ggplot2", quietly = TRUE) ||
    !requireNamespace("splines", quietly = TRUE) ||
    !requireNamespace("sjmisc", quietly = TRUE)) {
  knitr::opts_chunk$set(eval = FALSE)
}
```

# Aim of the ggeffects-package

Results of regression models are typically presented as tables that are easy to understand. For more complex models that include interaction or quadratic / spline terms, tables with numbers are less helpful and more difficult to interpret. In such cases, the visualization of _marginal effects_ is far easier to understand and allows to intuitively get the idea of how predictors and outcome are associated, even for complex models. 

**ggeffects** computes marginal effects (or: _estimated marginal means_) at the mean (MEM) or at representative values (MER) from statistical models, i.e. predictions generated by a model when one holds the non-focal variables constant and varies the focal variable(s). The result is returned as data frame with consistent structure, especially for further use with [ggplot](https://cran.r-project.org/package=ggplot2). Definitions of "marginal effects" [can be found here](https://stats.stackexchange.com/tags/marginal-effect/info).

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-, three- or four-way-interactions, or for specific values of a model term only. Examples are shown below.

## Short technical note

`ggpredict()`, `ggemmeans()` and `ggeffect()` always return predicted values for the _response_ of a model (or _response distribution_ for Bayesian models).

Typically, `ggpredict()` returns confidence intervals based on the standard errors as returned by the `predict()`-function, assuming normal distribution (`+/- 1.96 * SE`). If `predict()` for a certain class does _not_ return standard errors (for example, *merMod*-objects), these are calculated manually, by following steps: matrix-multiply `X` by the parameter vector `B` to get the predictions, then extract the variance-covariance matrix `V` of the parameters and compute `XVX'` to get the variance-covariance matrix of the predictions. The square-root of the  diagonal of this matrix represent the standard errors of the predictions, which are then multiplied by 1.96 for the confidence intervals.

For mixed models, if `type = "re"` or `type = "re.zi"`, the uncertainty in the random effects is accounted for when calculating the standard errors. Hence, in such cases, the intervals may be considered as _prediction intervals_.

## Consistent and tidy structure

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.

The examples shown here mostly use **ggplot2**-code for the plots, however, there is also a `plot()`-method, which is described in the vignette [Plotting Marginal Effects](introduction_plotmethod.html).

# Marginal effects at the mean

`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. Use the raw name of the variable for the `terms`-argument only - you don't need to write things like `poly(term, 3)` or `I(term^2)` for the `terms`-argument.

```{r}
library(ggeffects)
data(efc)
fit <- lm(barthtot ~ c12hour + neg_c_7 + c161sex + c172code, data = efc)

ggpredict(fit, terms = "c12hour")
```

As you can see, `ggpredict()` (and `ggeffect()` or `ggemmeans()`) has a nice `print()`-method, which takes care of printing not too many rows (but always an equally spaced range of values, including minimum and maximum value of the term in question) and giving some extra information. This is especially useful when predicted values are shown depending on the levels of other terms (see below).

The output shows the predicted values for the response at each value from the term _c12hour_. The data is already in shape for ggplot:

```{r}
library(ggplot2)
theme_set(theme_bw())

mydf <- ggpredict(fit, terms = "c12hour")
ggplot(mydf, aes(x, predicted)) + geom_line()
```

# Marginal effects at the mean by other predictors' levels

The `terms`-argument accepts up to four model terms, where the second to fourth terms indicate grouping levels. This allows predictions for the term in question at different levels for other model terms:

```{r}
ggpredict(fit, terms = c("c12hour", "c172code"))
```

Creating a ggplot is pretty straightforward: the `colour`-aesthetics is mapped with the `group`-column:

```{r}
mydf <- ggpredict(fit, terms = c("c12hour", "c172code"))
ggplot(mydf, aes(x, predicted, colour = group)) + geom_line()
```

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:

```{r}
mydf <- ggpredict(fit, terms = c("c12hour", "c172code", "c161sex"))
mydf
ggplot(mydf, aes(x, predicted, colour = group)) + 
  geom_line() + 
  facet_wrap(~facet)
```

Finally, a third differentation can be defined, creating another column named `panel`. In such cases, you may create multiple plots (for each value in `panel`). **ggeffects** takes care of this when you use `plot()` and automatically creates an integrated plot with all panels in one figure.

```{r fig.height = 8}
mydf <- ggpredict(fit, terms = c("c12hour", "c172code", "c161sex", "neg_c_7"))
plot(mydf)
```

# Marginal effects for each model term

If the `term` argument is either missing or `NULL`, marginal effects for each model term are calculated. The result is returned as a list, which can be plotted manually (or using the `plot()` function).

```{r}
mydf <- ggpredict(fit)
mydf
```


# Two-Way, Three-Way- and Four-Way-Interactions

To plot the marginal effects of interaction terms, simply specify these terms in the `terms`-argument.

```{r}
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 four model terms, you can also compute marginal effects for a 3-way-interaction or 4-way-interaction. To plot the marginal effects of three interaction terms, just like before, specify all three terms in the `terms`-argument.

```{r}
# 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)
```

4-way-interactions are rather confusing to print and plot. When plotting, multiple plots (for each level of the fourth interaction term) are plotted for the remaining three interaction terms. This can easily be done using the [`plot()`-method](https://strengejacke.github.io/ggeffects/articles/introduction_plotmethod.html).

```{r fig.height = 8}
# fit model with 4-way-interaction
fit <- lm(neg_c_7 ~ c12hour * barthtot * c161sex * c172code, data = efc)

# marginal effects for all 4 interaction terms
pr <- ggpredict(fit, c("c12hour", "barthtot", "c161sex", "c172code"))

# use plot() method, easier than own ggplot-code from scratch
plot(pr)
```


# Polynomial terms and splines

`ggpredict()` also works for models with polynomial terms or splines. Following code reproduces the plot from `?splines::bs`:


```{r}
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()
```

# Survival models

`ggpredict()` also supports `coxph`-models from the **survival**-package and is able to either plot risk-scores (the default), probabilities of survival (`type = "surv"`) or cumulative hazards (`type = "cumhaz"`).

Since probabilities of survival and cumulative hazards are changing across time, the time-variable is automatically used as x-axis in such cases, so the `terms`-argument only needs up to **two** variables for `type = "surv"` or `type = "cumhaz"`.

```{r}
data("lung", package = "survival")
# remove category 3 (outlier)
lung <- subset(lung, subset = ph.ecog %in% 0:2)
lung$sex <- factor(lung$sex, labels = c("male", "female"))
lung$ph.ecog <- factor(lung$ph.ecog, labels = c("good", "ok", "limited"))

m <- survival::coxph(survival::Surv(time, status) ~ sex + age + ph.ecog, data = lung)

# predicted risk-scores
ggpredict(m, c("sex", "ph.ecog"))
```

```{r}
# probability of survival
ggpredict(m, c("sex", "ph.ecog"), type = "surv")
```

# Labelling the data

**ggeffects** makes use of the [sjlabelled-package](https://cran.r-project.org/package=sjlabelled) and supports [labelled data](https://cran.r-project.org/package=sjlabelled/vignettes/intro_sjlabelled.html). 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()`, `ggemmeans()` or `ggeffect()` must be used as argument to one of the above function calls.

```{r}
get_x_title(mydf)
get_y_title(mydf)

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)
  )
```
