Tidybayes spread_draws unexpected output

Hi,

I’m having an issue with using the R tidybayes::spread_draws function to extract draws of a random slope coefficient in brms model.

It is giving me twice the number of draws I expect, and the second half seem not correspond to the correct coefficient.

I’ve attached my code output here: https://bignardi.co.uk/MiscellaneousRCode/99_spreaddraws_confusion.html

I may have made a mistake somewhere, but I’m trying to understand what I’ve done wrong so I can avoid making mistakes with this in future.

My current hypothesis is that maybe it is extracting some transformed version of the parameter that brms creates in stan?

In your sampling call you are specifying 2 chains of 10,000 iterations. The estimated posterior will thus contain 20,000 samples of random effect parameters.

You can reduce the number of samples by specifying an argument to ndraws

I don’t think so because i have 5,000 warm up samples. My output says that: total post-warmup draws = 10000.

spread_draws should not include warmup iterations by deafult, and i set brms to not save warm up samples.

It seems you have parameters in the form of x[i, j], so your use of the function,

tidybayes::spread_draws(r_pps[pps,condition_contrast])

I think creates a variable condition_contrast in the resulting data frame/tibble and puts all variations of what shows up in x[,__] into that variable. I see two types that show up (intercept, and condition_contrast), so assuming the function behaves the same with brms objects as with cmdstanr/Stan objects that would explain what you think are double and some of them not being what you expect.

2 Likes

I think @ssp3nc3r has identified the likely explanation.

@Giacomo to get a better sense of the structure of the output of spread_draws(), you can pipe the output of spread_draws() into the median_qi() function. This will show you what the unique values of the indices of the array were as well as medians and quantile intervals for them.

You could also try spread_rvars() instead of spread_draws(), which outputs posterior::rvar()s instead of many rows of draws, and therefore makes it easier to see the structure of the output data frame.

2 Likes

Thank you both! Of course! I missed that when checking the output, but now i remember in the past filter()'ing the draws. I will check out spread_rvars() as I will probably make this same mistake again in future.

1 Like