Marginal effects plot with partial residuals from CmdStanFit

Could anyone please direct me to the method how to obtain marginal effects and associated partial residuals from CmdStanFit (fitted using the $sample method from the CmdStanR interface)? I would like to plot them to assess the potential presence of non-linear trends like here:

https://strengejacke.github.io/ggeffects/articles/introduction_partial_residuals.html

Would it be possible to use ggeffects (or any other similar package) with CmdStanFit object? Or is the only way to do it coding it within generated quantities block?

The doc is here:

By “marginal effects”, I assume you mean the estimated parameter values? For that, you wan to grab the summary statistics, which is covered in the getting started doc:

Residuals are tricky in Bayes land. Do you want residual vs. point prediction or a distribution of residuals in the posterior? The former has to be done on the outside, but the latter can calculate the residuals in the generated quantities block.

Many thanks for you response. My intention is to plot marginal effect of a predictor (i.e., to plot predicted values marginalized over the other predictors on the y-axis and predictor values on the x-axis) and to add corresponding partial residuals (as shown in the link in my previous post).

I should note that it is a mixed-effect model with multiple random effects. In addition, covariance structure of one of the random effects is modelled using Ornstein-Uhlenbeck Gaussian process, which is not yet implemented in brms.

I know predicted values can be calculated in generated quantities block but I am not sure how to marginalize the predictions over the random effects.

I tried to fit a simpler model using brms and used ggemmeans() function but even this approach does not provide partial residuals. However, the functions for predictions in the R package easystats provide residuals so it seems to me that calculating partial residuals should be doable. Unfortunately, I have not found any function in easystats or ggeffects, which would provide a user-friendly way for this task (i.e., marginal effect plot with partial residuals) using CmdStanFit models or at least brms models.

I hope @strengejacke and @DominiqueMakowski would not be angry if I tag them here as they could have some ideas. I would greatly appreciate any help. Thank you.

1 Like