I want to complete the same task with my own data but with posterior predictive draws from cmdstanr$generate_quantities. I know the documentation suggests using add_draws from interfaces outside of brms and rstanarm, but i cannot seem to get the syntax correct - I think it has something to do with ypred being an array of length N and I am having troubling summarizing this? I want something like this but I cannot seem to get spread_draws() and other functions to cooperate upstream.

data{
int N;
vector[N] x;
}
parameters{
real HDL_constant; // mean function
real HDL_exponent; // mean function
real HDL_offset; // mean function
real HDL_noise_intercept; // sd function
real HDL_noise_slope; // sd function
}
generated quantities{
array[N] real ypred = normal_rng(HDL_constant*pow(x,HDL_exponent) + HDL_offset, HDL_noise_intercept*(1 + x*HDL_noise_slope));
}

If youāre using generated quantities to produce ypred values conditional on the x variable input to the model, then the input to add_draws() should be two things:

A data frame with your predictor (x) in the same order as the values of ypred. Assuming the x variable you input into your cmdstanr model is also called x in your R environment, this could be data.frame(x = x).

A matrix where rows index draws from the model and the number of columns is equal to length(x), where each column contains the draws from ypred for the corresponding value of x. If your cmdstanr model is stored in a variable called fit, this would be fit$draws("ypred", format = "matrix").

Putting that together, you want something like:

data.frame(x = x) |>
add_draws(fit$draws("ypred", format = "matrix")) |>
...

Thank you for this - I should have put this together. I get the following error below. Trying to deep dive purr::map2 and other, but cannot pinpoint yet the exact causeā¦

data.frame(agey = dat$agey) %>%
add_draws(fit_gq$draws("ypred", format = "matrix"))
Error in `map2()`:
ā¹ In index: 2.
ā¹ With name: .value.
Caused by error in `list_unchop()`:
! Can't combine `x[[1]]` <draws_matrix> and `x[[2]]` <draws_matrix>.
ā Some attributes are incompatible.
ā¹ The author of the class should implement vctrs methods.
ā¹ See <https://vctrs.r-lib.org/reference/faq-error-incompatible-attributes.html>.
Run `rlang::last_trace()` to see where the error occurred.

It seems add_draws does not like the attributes attached to the draws_matrix coming from cmdstanr / posterior. I had to ātransformā to a base matrix in r (although not really?), eliminate the dimnames, and then I can get everything to run. It is not the most elegant solution, but when I went into some of the examples in the references (e.g., add_draws(posterior_epred(m_mpg, newdata = .))), the posterior_epred function returns a base r matrix with no dimnames. So i tried to recreate this with the draws from cmdstanr.