Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Last active April 13, 2023 19:08
Show Gist options
  • Save andrewheiss/3306dced3af45bb32987c44c4077ad50 to your computer and use it in GitHub Desktop.
Save andrewheiss/3306dced3af45bb32987c44c4077ad50 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(brms)

example_data <- tibble(outcome = c(rep(1, 200),
                                   sample(2:31, 200, replace = TRUE),
                                   rep(32, 150))) |> 
  mutate(outcome_0 = outcome - 1) |> 
  mutate(outcome_collapsed = case_when(
    outcome == 1 ~ "1",
    outcome >= 2 & outcome <= 31 ~ "2-31",
    outcome == 32 ~ "32"
  )) |> 
  mutate(outcome_collapsed = factor(outcome_collapsed, ordered = TRUE))

ggplot(example_data, aes(x = outcome)) +
  geom_histogram(binwidth = 1, boundary = 0, color = "white")

model_zero_poisson <- brm(
  bf(outcome_0 ~ 1),
  data = example_data,
  family = zero_inflated_poisson(),
  cores = 4, refresh = 0
)
pp_check(model_zero_poisson)

model_mixture_poisson <- brm(
  bf(outcome ~ 1),
  data = example_data,
  family = mixture(poisson, poisson),
  cores = 4, refresh = 0
)
pp_check(model_mixture_poisson)

ggplot(example_data, aes(x = outcome_collapsed)) +
  geom_bar()

model_collapsed <- brm(
  bf(outcome_collapsed ~ 1),
  data = example_data,
  family = cumulative,
  cores = 4, refresh = 0
)
pp_check(model_collapsed, type = "bars")

Created on 2023-03-30 with reprex v2.0.2

@andrewheiss
Copy link
Author

Whoa, this is awesome @dmi3kno—thanks!

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