mjskay / tidybayes

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

Feature request: for a `brms` model, when `draw_ids` are specified, `[add_]epred_draws` returns `draw_ids` in `.draw`. #304

Open Fumeng-Yang opened 1 year ago

Fumeng-Yang commented 1 year ago

When draw_ids are specified to get a subset of the draws, [add_]epred_draws still returns 1, 2, ..., row number in .draw. It would be useful to have draw_ids in .draw. Because we may want to split the draws and later combine them.

A minimal reproducible example

set.seed(1234)

# need a minimal model
m <- brm(
       x ~ 1, 
       data = tibble(x = rnorm(100, 0, 1)),  # generate some data
       backend = 'cmdstanr',
       refresh = 2000, # don't want lots of messages
       family = gaussian()
       )
add_epred_draws(
    m,
    newdata = tibble(x = 0),
    draw_ids = 100:102 # a subset of draws
  )

The output is

x .row .chain .iteration .draw .epred
0 1 NA NA 1 0.1354060
0 1 NA NA 2 0.0083186
0 1 NA NA 3 -0.0979545

Notice that .draw is still 1,2,3 while we specified 100:102.

Fumeng-Yang commented 1 year ago

FYI: It seems that brms's posterior_epred returns row names for an ordinal model. Not sure whether this is used in tidybayes.

A minimal reproducible example

set.seed(1234)

c <- brm(x ~ 1, 
       data = tibble(x = sample(1:3, 100, replace = TRUE)),  # need some data
       backend = 'cmdstanr',
       refresh = 2000, # don't want lots of messages
       family = cumulative('logit')
       )
df <- brms::posterior_epred(c,
                newdata = tibble(x = 1),
                draw_ids = 100:105 # a subset of draws 
                # but don't want to be confused with the categories 
          )
dimnames(df)

The output is

[[1]]
[1] "1" "2" "3" "4" "5" "6"

[[2]]
NULL

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

where we see the first dimension has row numbers as row names.

Fumeng-Yang commented 1 year ago

BTW: these two features should be simple to add. I can probably figure out something after I understand the structure of tidybayes....