mjskay / tidybayes

Bayesian analysis + tidy data + geoms (R package)
http://mjskay.github.io/tidybayes
GNU General Public License v3.0
725 stars 59 forks source link

Feature request: for a multivariate ordinal model, `[add]_epred_draws` returns response variables somewhere #305

Open Fumeng-Yang opened 1 year ago

Fumeng-Yang commented 1 year ago

For a multivariate ordinal model, brms still uses .category for ordinal responses; therefore, it seems that response variables are not included in [add]_epred_draws (and [add]_epred_rvars?)

It would be nice to have the response variables somewhere, at least when resp = ... is specified.

A minimal reproducible example

set.seed(1234)
simulated_data <- 
  tibble(
    likert1 = sample(1:5, 100, replace = TRUE),
    likert2 = sample(1:5, 100, replace = TRUE)
  ) 
mvo <- brm(
       bf(mvbind(
          likert1, 
          likert2
           ) ~ 1) 
       + set_rescor(FALSE), # set correlation in residuals to FALSE
       data = simulated_data, 
       refresh = 2000, # don't want lots of messages
       backend = 'cmdstanr',
       family = cumulative('logit')
       )
add_epred_draws(object = mvo,
                                   # need some placeholders
                newdata = tibble(likert1 = -1, likert2 = -1),
                resp = c('likert1', 'likert2'),
                draw_ids = 100:102
                )

The output is

likert1 likert2 .row .chain .iteration .draw .category .epred
-1 -1 1 NA NA 1 1 0.1159525
-1 -1 1 NA NA 2 1 0.1254349
-1 -1 1 NA NA 3 1 0.2474628
-1 -1 1 NA NA 1 2 0.1834422
-1 -1 1 NA NA 2 2 0.2435328
-1 -1 1 NA NA 3 2 0.1387139
-1 -1 1 NA NA 1 3 0.2417583
-1 -1 1 NA NA 2 3 0.2418615
-1 -1 1 NA NA 3 3 0.1800300
-1 -1 1 NA NA 1 4 0.3103983
-1 -1 1 NA NA 2 4 0.1766087
-1 -1 1 NA NA 3 4 0.2802388
-1 -1 1 NA NA 1 5 0.1484487
-1 -1 1 NA NA 2 5 0.2125622
-1 -1 1 NA NA 3 5 0.1535545

Verify the output of brms:posterior_epred

dimnames(brms::posterior_epred(
    mvo,
    newdata = simulated_data[1,],
    draw_ids = 100:102
  ))

The output is

[[1]]
[1] "1" "2" "3"

[[2]]
NULL

[[3]]
 [1] "1" "2" "3" "4" "5" "1" "2" "3" "4" "5"
Fumeng-Yang commented 1 year ago

[add_][predicted|linpred]_draws appears to return response variables.

add_predicted_draws(object = mvo,
                # need some placeholders
                newdata = tibble(likert1 = -1, likert2 = -1),
                resp = c('likert1', 'likert2'),
                draw_ids = 100:102
                )

The output is

likert1 likert2 .row .chain .iteration .draw .category .prediction
-1 -1 1 NA NA 1 likert1 3
-1 -1 1 NA NA 2 likert1 5
-1 -1 1 NA NA 3 likert1 4
-1 -1 1 NA NA 1 likert2 3
-1 -1 1 NA NA 2 likert2 1
-1 -1 1 NA NA 3 likert2 5