Changing the order of categories shown in conditional effects plots brms

Short summary of the problem

I am running a simple logistic regression with an interaction between between two categorical variables, one with five categories and the other binary. The conditional_effects() command displays the bars for the reference category first, but I want to change the order that the bars appear. Is there a way to do that?

code_to_run_your_model(if_applicable)

fit <- brms(y ~ x1*x2, family=‘bernoulli’, data=dat)
conditional_effects(fit,“x1:x2”)

x1 has five levels, (0, 1, 2, 3, 4), and the reference category is 2. Currently, the bars are plotted in the order of 2 0 1 3 4. I would like to have them shown from 0 to 4. Is this possible without having to change the reference category to 0?

Thank you

Please also provide the following information in addition to your question:

  • Operating System: Windows 10
  • brms Version: 2.12.0

Looking forward to your topic!

One relatively easy way to do this is to save the plot as an object and change the order of the levels directly in the plot object:

# save plot as p
p <- conditional_effects(fit,“x1:x2”)

#change order of factor levels in plot
p$`x1:x2`$effect1__ <- factor(p$`x1:x2`$effect1__, levels(p$`x1:x2`$effect1__)[c(2,3,1,4,5)])

#plot again
p

should do what you want. I hope this answers your question.

If you wanted to do the same thing for the main-effect plot of x1 the 2nd line would become

p$x1$effect1__ <- factor(p$x1$effect1__, levels(p$x1$effect1__)[c(2,3,1,4,5)])

That worked! Thanks a lot!

Hi everyone,

I wanted to reopen this question. I am running an ordinal regression with categorical predictors:

fit <- brm(
  formula = y ~ x1 + x2,
  data = data,
  family = cumulative("probit"))

x1 has three levels (Disagree, Neutral, Agree) and the reference category is Neutral. Currently the plot displays Neutral, Disagree, Agree, but I would like a plot with Disagree, Neutral, Agree.

I tried the solution proposed by @julianquandt but unfortunately it does not seem to change the output if categorical = TRUE when using conditional_effects:

p <- conditional_effects(fit, 
                             effects = "x1", 
                             categorical = TRUE)

I have tried the following options:

p$x1$effect1__ <- factor(p$x1$effect1__, levels(p$x1$effect1__)[c(2,1,3)])
p$x1$effect1__ <- factor(p$x1$effect1__, levels(p$x1$effect1__)[c("Disagree", "Neutral", "Agree")])

Any help on this topic would be much appreciated! Thank you so much

I think the code should be as follows:

p$`x1:cats__`$effect1__ <- factor(p$`x1:cats__`$effect1__, levels(p$`x1:cats__`$effect1__)[c(2,1,3)])

The conditional_effects function returns a list of data frames, one for each effect. You can see this for your case by running str(p) in the console. In this case, there will be only one data frame, since you provided a single variable to the effects argument. Note in the output of str(p) that the data frame is named x1:cats__ (you could also run names(p) to see this).

In julianquant’s example, the data frame was named x1:x2. But when the response variable is categorical, conditional_effects tacks on :cats__ to the names of the data frames in its output.

Thank you so much for your help!

This worked perfectly.