Hi,
I am trying to plot the conditional effects of a gamm fit with brms, the categorical predictor (ECO_GROUP2) has 7 levels so the resulting plot is very busy. Is there a way to only plot a subset of the levels (i.e. Lodges & Marshes)?
Welcome to the forum! I’m hoping that someone provides a more elegant solution to this issue, but I did find a workaround that should give you what you want.
Here I make a simple example that mimics your model (though not exactly):
cat_var <- rep(LETTERS[1:4], each = 100) # Four-level factor
num_var <- runif(400, min = 0, max = 10) # uniform predictor variable
outcome <- rnorm(400) # gaussian outcome variable
# Creating the dataframe
df <- data.frame(cat_var, num_var, outcome)
# Loading brms and fitting model
library(brms)
fit <- brm(outcome ~ s(num_var, by = cat_var),
data = df)
The code below saves the output of the plotting function, takes only the plot we wanted, then strips out the rows of the underlying dataframe that are cluttering up our plot. You should be able to modify this for your own purposes easily enough, but let me know if there are any issues!
p <- plot(conditional_effects(fit, ask=FALSE))
# Take only the interaction plot
p2 <- p$`num_var:cat_var`
# Subset the data to only the factor levels we want
p2$data <- p2$data[p2$data$cat_var %in% c("A", "B"),]
# Print out the new modified plot
p2
This isn’t answering your question, but have you tried just faceting on those levels rather than trying to overlay them all on a single plot? I think that’d be useful even if you only have two variables to compare.