knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  dev = "png",
  fig.width = 7,
  fig.height = 3.5,
  message = FALSE, warning = FALSE
)
options(width = 800)
arrow_color <- "#FF00cc"

pkgs <- c(
  "ggplot2",
  "marginaleffects",
  "emmeans",
  "htmltools"
)

if (!all(vapply(pkgs, requireNamespace, quietly = TRUE, FUN.VALUE = logical(1L)))) {
  knitr::opts_chunk$set(eval = FALSE)
}
library(htmltools)
callout_tip <- function(header = NULL, ...) {
  div(
    class = "callout-tip",
    tags$h1(
      tags$img(src = "../man/figures/summary.png", width = "20", height = "17", style = "vertical-align:middle"), # nolint
      header
    ),
    ...
  )
}
includeCSS("../man/figures/callout.css")

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

  1. Significance Testing of Differences Between Predictions I: Contrasts and Pairwise Comparisons

  2. Significance Testing of Differences Between Predictions II: Comparisons of Slopes, Floodlight and Spotlight Analysis (Johnson-Neyman Intervals)

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

  4. Significance Testing of Differences Between Predictions IV: Contrasts and Comparisons for Zero-Inflation Models

Contrasts and comparisons for GLM - logistic regression example

We will now show an example for non-Gaussian models. For GLM's (generalized linear 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.

callout_tip(
  "Summary of most important points:",
  tags$ul(
    tags$li("Predictions (returned by ", tags$code("predict_response()"), ") are usually on the response scale. This is also true for other regression models than linear regression. E.g., predictions for logistic regression are presented as probailities, and for Poisson regression, the average count of event is returned."), # nolint
    tags$li(tags$code("test_predictions()"), " also returns contrasts and comparisons on the response scale by default. This is usually the most intuitive scale for people to understand. E.g., for a logistic regression model, contrasts are presented as difference between two probabilities (in percentage points)."), # nolint
    tags$li("It is possible to return contrasts or comparisons on other scales, too - but mostly, this is probably not necessary.") # nolint
  )
)

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),
  x3 = as.factor(sample(1:4, size = 100, TRUE, prob = c(0.1, 0.4, 0.2, 0.3)))
)

m <- glm(outcome ~ x1 + x2 + x3, data = dat, family = binomial())
predict_response(m, "x1")

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)
ht8 <- test_predictions(p)

The difference between the predicted probability of x1 = 1 (r sprintf("%.1f%%", 100 * p$predicted[1])) and x1 = 2 (r sprintf("%.1f%%", 100 * p$predicted[2])) is roughly r sprintf("%.1f%%", 100 * ht8$Contrast[1]) points. This difference is not statistically significant (p = r round(ht8$p.value[1], 3)).

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

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

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

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.

Contrasts and comparisons for different margin options

Like in predict_response(), the margin argument can be used in test_predictions() to define how to marginalize over the non-focal predictors, i.e. those variables that are not specified in terms. This can be important depending on the type of regression models in order to calculate accurate comparisons or contrasts, since these refer to the difference between predicted values.

For linear models, these differences are usually the same, regardless of the margin option. However, for non-Gaussian models, differences between predicted values may differ for the different margin options.

# predictions, using mean/mode for non-focal predictors
p1 <- predict_response(m, "x1")
# predictions, averaged across non-focal predictors
p2 <- predict_response(m, "x1", margin = "empirical")

p1

p2

# differences between predicted values
diff(p1$predicted)
diff(p2$predicted)

Consequently, test_predictions() either requires specifying the margin argument when a model and terms argument are provided, or the related ggeffects object returned by predict_response().

# contrast refers to predictions, using mean/mode for non-focal predictors
test_predictions(m, "x1")

# contrast refers to predictions, averaged across non-focal predictors
test_predictions(m, "x1", margin = "empirical")

# or
test_predictions(p2)

Go to next vignette: Significance Testing of Differences Between Predictions III: Contrasts and Comparisons for Zero-Inflation Models



strengejacke/ggeffects documentation built on Dec. 24, 2024, 3:27 a.m.