How to change selected values of conditional effects interaction plot in brms R

116 Views Asked by At

I've created a graph showing the interaction effect between "Disp" and "mpg" using the mtcars dataset. I want to change the default values of 106.78, 230.72, and 354.66 to show a line for other values, for example the minimum of 71.1, maximum of 472.0 and a middle value of maybe 258.0. I'd also like to manually select 2 or 4 values rather than the default of 3. All code is included below with some borrowed from other related questions about changing axes or legend labels but nothing about the graph itself.

data(mtcars)

interactionmodel <- brm(vs ~ 1 + mpg + disp + mpg:disp, data = mtcars, family = bernoulli())

conditional_effects(interactionmodel)

##Creating individual conditional effects plots
the_plotstest <- plot(conditional_effects(interactionmodel),
                             ask = FALSE)

#final plot
the_plotstest[['mpg:disp']] + ## if 'mpg:disp' is the conditional to show
  theme_bw(base_size = 14) + ## e. g. an existing theme of choice
  ylim(0, 1) +
  labs(x = 'MPG',
       y = 'VS')+
  scale_color_discrete(name = "Disp") +
  scale_fill_discrete(name = "Disp")

1

There are 1 best solutions below

2
Vincent On

You could use the plot_predictions() function from the marginaleffects package. The package website hosts over 25 vignettes with detailed tutorials and case studies. These two might be particularly useful:

data(mtcars)
library(brms)
library(marginaleffects)

interactionmodel <- brm(vs ~ 1 + mpg + disp + mpg:disp, data = mtcars, family = bernoulli(), backend = "cmdstanr")

plot_predictions(interactionmodel, condition = list(
    "mpg",
    "disp" = c(71, 258, 472)
    )
)

Disclaimer: I am the marginaleffects maintainer.