Consider the following dataframe and glmer model:
Dataframe
hunger <- sample(x = 0:1, size = 50, replace = TRUE)
treat <- sample(x = c("A1","A2"), prob = c(.75, .25), size = 50, replace = TRUE)
owner <- sample(x = c("Alice","Ben"), prob = c(.5, .5), size = 50, replace = TRUE)
animal <- sample(x = c("d","cat","cow"), prob = c(.33, .33, .33), size = 50, replace = TRUE)
df <- as.data.frame(cbind(animal,treat,owner,hunger))
df$hunger <- as.numeric(df$hunger)
df$animal <- as.factor(df$animal)
Model
testM <- glmer(hunger ~ animal + treat + animal*treat +
(1 + animal + treat + animal*treat||owner), data = df, family="binomial", glmerControl(optimizer="bobyqa", optCtrl = list(maxfun=1e5)))
summary(testM)
and I visualize the model using ggcoef_model
as such:
ggcoef_model(testM, include = !contains(c("owner","Residual.sd")))
this produces the plot:
as we can see under "animal" one of the levels is named "d".
(1) How can I change this label to "dog"?
(2) In addition, I would also like to hide the visualization of the level "A1" under "treat".
I can see that here are ways to manipulate the variable labels (i.e. via set_variable_labels(treat = "Treat for the Animal")), but how do can I manipulate the labels of the levels within a variable?
This uses the answer from @Luciefromdafuture for Q2.
library(tidyverse)
library(lme4)
library(ggstats)
library(broom.mixed)
hunger <- sample(x = 0:1, size = 50, replace = TRUE)
treat <- sample(x = c("A1","A2"), prob = c(.75, .25), size = 50, replace = TRUE)
owner <- sample(x = c("Alice","Ben"), prob = c(.5, .5), size = 50, replace = TRUE)
animal <- sample(x = c("d","cat","cow"), prob = c(.33, .33, .33), size = 50, replace = TRUE)
df <- as.data.frame(cbind(animal,treat,owner,hunger))
df$hunger <- as.numeric(df$hunger)
df$animal <- as.factor(df$animal)
testM <- glmer(hunger ~ animal + treat + animal*treat +
(1 + animal + treat + animal*treat||owner), data = df, family="binomial", glmerControl(optimizer="bobyqa", optCtrl = list(maxfun=1e5)))
coef_data <- tidy(testM, effects = "fixed")
terms <- coef_data$term
terms
#> [1] "(Intercept)" "animalcow" "animald"
#> [4] "treatA2" "animalcow:treatA2" "animald:treatA2"
# Create a named vector for term_labels
term_labels <- c(
"(Intercept)" = "(Intercept)",
"animalcow" = "cow",
"animald" = "dog", # Rename 'animald' to 'animaldog'
"treatA2" = "A2",
"animalcow:treatA2" = "cow * A2",
"animald:treatA2" = "dog * A2" # Rename interaction term
)
# Plot the model with ggcoef_model and custom term labels
testM %>%
ggcoef_model(include = !contains(c("owner","Residual.sd")),no_reference_row = "treat",
term_labels = term_labels)
# Old plot
ggcoef_model(testM, include = !contains(c("owner","Residual.sd")))
Created on 2024-05-24 with reprex v2.1.0