I want to fit a cumulative proportional odds model (ordinal logistic regression) with brms. In the example below, my dependent variable is a 7-level ordinal score (y) from 0 to 6. Y represents the observed outcome, and X the exposure group (x = placebo or epinephrine).
I am interested in a few outputs, such as:
- P(Y \geq y \mid X = x)
- P(Y \leq y \mid X= x)
- P(Y = y \mid X = x)
I believe that, in the code below, I am able to generate P(Y = y \mid X = x) for both exposure groups and all Y (0 to 6).
Thus, I would like to confirm that my code indeed yields the P(Y = y \mid X = x) and ask for help to generate P(Y \geq y \mid X = x) and P(Y \leq y \mid X= x).
Thanks!
library(brms)
library(tidybayes)
library(dplyr)
a <- c(rep(0,15), rep(1,10), rep(2,29), rep(3,20), rep(4,8), rep(5,8), rep(6,3903))
b <- c(rep(0,12), rep(1,17), rep(2,23), rep(3,35), rep(4,12), rep(5,27), rep(6,3881))
x <- c(rep('placebo', length(a)), rep('epinephrine', length(b)))
y <- c(a, b)
d = data.frame(x, y)
d$y <-factor(d$y, ordered = T)
d$x<-factor(d$x, levels = c("placebo", "epinephrine"))
# Reduce 90% of the sample size to aid in model fitting
set.seed(123)
dd = sample_frac(d, size = 0.1, weight = x)
m_crude <- brm(
family = cumulative("logit"),
formula = y ~ 1 + x,
data = dd,
backend = "cmdstanr",
cores = parallel::detectCores()
)
$ P(Y = y | X = x)?
m_crude |>
epred_draws(newdata = data.frame(x = c("placebo", "epinephrine"))) |>
group_by(x, .category) |>
median_hdi(.epred)
A tibble: 14 × 8
x .category .epred .lower .upper .width .point .interval
<chr> <fct> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 epinephrine 0 0.00416 0.000564 0.00924 0.95 median hdi
2 epinephrine 1 0.00165 0.0000280 0.00504 0.95 median hdi
3 epinephrine 2 0.00400 0.000765 0.00910 0.95 median hdi
4 epinephrine 3 0.00656 0.00186 0.0130 0.95 median hdi
5 epinephrine 4 0.00438 0.000614 0.00982 0.95 median hdi
6 epinephrine 5 0.00330 0.000408 0.00811 0.95 median hdi
7 epinephrine 6 0.974 0.960 0.987 0.95 median hdi
8 placebo 0 0.00371 0.000493 0.00938 0.95 median hdi
9 placebo 1 0.00145 0.0000417 0.00490 0.95 median hdi
10 placebo 2 0.00356 0.000540 0.00930 0.95 median hdi
11 placebo 3 0.00587 0.00138 0.0134 0.95 median hdi
12 placebo 4 0.00390 0.000357 0.00988 0.95 median hdi
13 placebo 5 0.00294 0.000226 0.00793 0.95 median hdi
14 placebo 6 0.976 0.957 0.991 0.95 median hdi