Safely extract design matrix and corresponding coffieicent draws from GLM posterior

In a GLM, a function like posterior_linpred is ultimately returning draws of the form x_n^T \theta_m, where \theta_m is a posterior draw and x_n is a row of the design matrix. Is it possible to safely extract from a brmsfit object the matrices X and \Theta, with rows x_n and \theta_m, respectively, so that \Theta X^T is the output of posterior_linpred?


# Run a posterior sampler, get draws of the natural parameter:
post <-  brm(formula("y ~ g1 + (1 | g2) + 1"), data_df, family=bernoulli(link="logit"), ...)
eta_draws <- posterior_linpred(post, data_df) 

# Proposed function:
design_and_draws <- posterior_linpred_components(post, data_df) 

# Should be zero:
eta_draws - t(design_and_draws$theta) %*% design_and_draws$x 

It seems that the exported function standata may already return nearly what is needed, but I’m not sure if the columns are guaranteed to line up. For example, it appears that posterior_epred.brmsprep is doing a lot of extra logic. I’d like something that is guaranteed correct.

For context, this is for a project building model mis-specification diagnostics on top of brms output.

Please also provide the following information in addition to your question:

  • Operating System: Ubuntu 22.04.4 LTS
  • brms Version: 2.20.4