knitr::opts_chunk$set( collapse = TRUE, comment = "#>", dev = "png", fig.width = 7, fig.height = 4, message = TRUE, warning = FALSE) options(width = 800) if (!requireNamespace("datawizard", quietly = TRUE) || !requireNamespace("ggplot2", quietly = TRUE) || !requireNamespace("marginaleffects", quietly = TRUE) || !requireNamespace("parameters", quietly = TRUE)) { knitr::opts_chunk$set(eval = FALSE) }
This vignette demonstrates a typical workflow using the ggeffects
package, with a logistic regression model as an example. We will explore various aspects of the model, such as model coefficients, predicted probabilities, and pairwise comparisons. Let's get started!
First, we load the ggeffects
package and the coffee_data
data set, which is included in the package. The data set contains information on the effect of coffee consumption on alertness over time. The outcome variable is binary (alertness), and the predictor variables are coffee consumption (treatment) and time.
library(ggeffects) library(parameters) # for model summary library(datawizard) # for recodings data(coffee_data, package = "ggeffects") # dichotomize outcome variable coffee_data$alertness <- categorize(coffee_data$alertness, lowest = 0) # rename variable coffee_data$treatment <- coffee_data$coffee # model model <- glm(alertness ~ treatment * time, data = coffee_data, family = binomial())
Let's start by examining the model coefficients. We can use the model_parameters()
function to extract the coefficients from the model. By setting exponentiate = TRUE
, we can obtain the odds ratios for the coefficients.
# coefficients model_parameters(model, exponentiate = TRUE)
The model coefficients are difficult to interpret directly, in particular sinc we have an interaction effect. Instead, we should use the predict_response()
function to calculate predicted probabilities for the model. These refer to the adjusted probabilities of the outcome (higher alertness) depending on the predictor variables (treatment and time).
Thus, since we are interested in the interaction effect of coffee consumption (treatment) on alertness depending on different times of the day, we simply specify these two variables as focal terms in the predict_response()
function.
# predicted probabilities predictions <- predict_response(model, c("time", "treatment")) plot(predictions)
As we can see, the predicted probabilities of alertness are higher for participants who consumed coffee compared to those who did not, but only in the morning and in the afternoon. Furthermore, we see differences between the coffee and the control group at each time point - but are these differences statistically significant?
To check this, we finally use the test_predictions()
function to perform pairwise comparisons of the predicted probabilities. We simply pass our results from predict_response()
to the function.
# pairwise comparisons - quite long table test_predictions(predictions)
In the above output, we see all possible pairwise comparisons of the predicted probabilities. The table is quite long, but we can also group the comparisons, e.g. by the variable time.
# group comparisons by "time" test_predictions(predictions, by = "time")
The output shows that the differences between the coffee and the control group are statistically significant only in the noon time.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.