# Significance Testing of Differences Between Predictions III: Contrasts and Comparisons for Generalized Linear Models

#### Daniel Lüdecke

#### 2024-02-27

Source:`vignettes/introduction_comparisons_3.Rmd`

`introduction_comparisons_3.Rmd`

This vignette is the third in a 3-part series:

**Significance Testing of Differences Between Predictions I: Contrasts and Pairwise Comparisons****Significance Testing of Differences Between Predictions III: Contrasts and Comparisons for Generalized Linear Models**

## Contrasts and comparisons for GLM - logistic regression example

Lastly, we show an example for non-Gaussian models. For GLM models
with (non-Gaussian) link-functions, `predict_response()`

always returns predcted values on the *response* scale. For
example, predicted values for logistic regression models are shown as
*probabilities*.

Let’s look at a simple example

```
library(ggeffects)
set.seed(1234)
dat <- data.frame(
outcome = rbinom(n = 100, size = 1, prob = 0.35),
x1 = as.factor(sample(1:3, size = 100, TRUE, prob = c(0.5, 0.2, 0.3))),
x2 = rnorm(n = 100, mean = 10, sd = 7)
)
m <- glm(outcome ~ x1 + x2, data = dat, family = binomial())
predict_response(m, "x1")
#> # Predicted probabilities of outcome
#>
#> x1 | Predicted | 95% CI
#> ---------------------------
#> 1 | 0.24 | 0.14, 0.39
#> 2 | 0.16 | 0.06, 0.37
#> 3 | 0.34 | 0.19, 0.54
#>
#> Adjusted for:
#> * x2 = 10.29
```

### Contrasts and comparisons for categorical focal terms

Contrasts or comparisons - like predictions (see above) - are by
default on the *response* scale, i.e. they’re represented as
difference between probabilities (in percentage points).

```
p <- predict_response(m, "x1")
test_predictions(p)
#> # Pairwise comparisons
#>
#> x1 | Contrast | 95% CI | p
#> ------------------------------------
#> 1-2 | 0.08 | -0.11, 0.27 | 0.397
#> 1-3 | -0.10 | -0.32, 0.11 | 0.359
#> 2-3 | -0.18 | -0.41, 0.05 | 0.119
#>
#> Contrasts are presented as probabilities.
```

The difference between the predicted probability of
`x1 = 1`

(24.4%) and `x1 = 2`

(16.1%) is roughly
8.3% points. This difference is not statistically significant (p =
0.397).

The `scale`

argument in `test_predictions()`

can be used to return contrasts or comparisons on a differen scale. For
example, to transform contrasts to *odds ratios*, we can use
`scale = "exp"`

.

```
test_predictions(p, scale = "exp")
#> # Pairwise comparisons
#>
#> x1 | Contrast | 95% CI | p
#> -----------------------------------
#> 1-2 | 1.09 | 0.90, 1.32 | 0.397
#> 1-3 | 0.90 | 0.73, 1.12 | 0.359
#> 2-3 | 0.83 | 0.66, 1.05 | 0.119
#>
#> Contrasts are presented on the exponentiated scale.
```

Contrasts or comparisons can also be represented on the link-scale,
in this case as *log-odds*. To do so, use
`scale = "link"`

.

```
test_predictions(p, scale = "link")
#> # Pairwise comparisons
#>
#> x1 | Contrast | 95% CI | p
#> ------------------------------------
#> 1-2 | 0.52 | -0.76, 1.80 | 0.427
#> 1-3 | -0.49 | -1.52, 0.54 | 0.350
#> 2-3 | -1.01 | -2.36, 0.34 | 0.142
#>
#> Contrasts are presented as log-odds.
```

### Contrasts and comparisons for numerical focal terms

For numeric focal variables, where the slopes (linear trends) are
estimated, transformed scales (like `scale = "exp"`

) are not
supported. However, `scale = "link"`

can be used to return
untransformed contrasts or comparisons on the link-scale.

```
test_predictions(m, "x2", scale = "link")
#> # Linear trend for x2
#>
#> Slope | 95% CI | p
#> ---------------------------
#> -0.06 | -0.12, 0.01 | 0.093
#>
#> Slopes are presented as log-odds.
```

Be aware whether and which back-transformation to use, as it affects the resulting p-values. A detailed overview of transformations can be found in this vignette.