Skip to content

Instantly share code, notes, and snippets.

@mjskay
Created April 1, 2022 21:32
Show Gist options
  • Select an option

  • Save mjskay/07d84bcd117c74bec41ee4ae85ea82ab to your computer and use it in GitHub Desktop.

Select an option

Save mjskay/07d84bcd117c74bec41ee4ae85ea82ab to your computer and use it in GitHub Desktop.
example of a continuous interaction plot with an ordinal model in brms
library(brms)
library(tidybayes)
library(dplyr)
library(ggplot2)
library(modelr)

mtcars_clean = mtcars %>%
  mutate(cyl = ordered(cyl))

m_cyl = brm(
  cyl ~ mpg * hp, 
  data = mtcars_clean, 
  family = cumulative,
  seed = 58393,
  prior = c(
    prior(normal(0, 35), class = b),
    prior(normal(0, 10), class = Intercept)
  )
)

preds = mtcars %>%
  data_grid(
    mpg = seq_range(mpg, n = 10),
    hp = seq_range(hp, n = 18)
  ) %>%
  add_epred_rvars(m_cyl, columns_to = "cyl")
  
preds %>%
  ggplot(aes(x = mpg, y = hp, size = mean(.epred), color = cyl)) +
  geom_point(position = position_dodge(width = 2.5), shape = 15) +
  theme_tidybayes()

Created on 2022-04-01 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment