Here it is. Apologies for probably clumsy R code but it should do the trick …
# Bayesian McKelvey-Zavoina R2 ------------------------------------------------
# Bayesian version of McKelvey and Zavoina's pseudo-R2 for binary and ordinal
# brms models (McKelvey and Zavoina, 1975). See also Gelman et al. (2018). This
# pseudo-R2 closely approximates the R2 that would have been obtained if a
# linear model had have been run on observations of the continuous latent
# variable underlying the discrete responses (Veall and Zimmermann, 1992; Hagle
# and Mitchell, 1992; Veall and Zimmermann, 1994).
Bayes_R2_MZ <- function(fit, ...) {
y_pred <- fitted(fit, scale = "linear", summary = FALSE, ...)
var_fit <- apply(y_pred, 1, var)
if (fit$formula$family$family == "cumulative" ||
fit$formula$family$family == "bernoulli") {
if (fit$formula$family$link == "probit" ||
fit$formula$family$link == "probit_approx") {
var_res <- 1
}
else if (fit$formula$family$link == "logit") {
var_res <- pi^2 / 3
}
}
else {
sum_fit <- summary(fit)
sig_res <- sum_fit$spec_pars["sigma", "Estimate"]
var_res <- sig_res^2
}
R2_MZ <- var_fit / (var_fit + var_res)
print(
data.frame(
Estimate = mean(R2_MZ),
Est.Error = sd(R2_MZ),
"l-95% CI" = quantile(R2_MZ, 0.025),
"u-95% CI" = quantile(R2_MZ, 0.975),
row.names = "Bayes_R2_MZ",
check.names = FALSE),
digits = 2)
}