Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An extention to the bayes_R2 function #128

Open
jalilian opened this issue Oct 21, 2024 · 1 comment
Open

An extention to the bayes_R2 function #128

jalilian opened this issue Oct 21, 2024 · 1 comment

Comments

@jalilian
Copy link

jalilian commented Oct 21, 2024

Hi,

This work shows that we can extend the use of the Bayesian R-square in the bayes_R2 function to a wider range of models. I've suggested some changes to the bayes_R2 function, which you can find below and in the attached file. It is not an issue, but I wanted to know whether to create an issue or submit a direct pull request.
BayesRsquared.pdf

Thank you for considering this.

Best regards,
Abdollah

bayes_R2_new <- function(fit)
{
  fam <- family(fit) # family dist. of the response
  eta <- posterior_linpred(fit) # linear predictor: eta
  mu <- fam$linkinv(eta) # conditional mean
  varfit <- apply(mu, 1, var)
  varres <- switch(fam$family, gaussian={
     as.matrix(fit, pars="sigma")^2
  }, binomial={
    v <- fam$variance(mu)
    apply(v, 1, mean)
  }, poisson={
    v <- fam$variance(mu)
    apply(v, 1, mean)
  }, Gamma={
    v <- fam$variance(mu)
    apply(v, 1, mean) / as.matrix(fit, pars="shape")
  }, beta={
    v <- fam$variance(mu)
    apply(v, 1, mean)
  }, neg_binomial_2={
    size <- as.matrix(fit, pars="reciprocal_dispersion")
    v <- fam$variance(mu, theta=c(size))
    apply(v, 1, mean)
  }, inverse.gaussian={
    v <- family(fit)$variance(mu)
    apply(v, 1, mean) / as.matrix(fit, pars="lambda")
  }, stop("the speciefied family is not implemented"))
  R2 <- varfit / (varres + varfit) # Bayesian R-squared
  attributes(R2) <- list(varfit=varfit, varres=varres)
  return(R2)
}

BayesRsquared.pdf

@jgabry
Copy link
Member

jgabry commented Oct 21, 2024

Thanks! In the rstantools package we typically implement generic and default methods that don't take fitted model objects as input. So I think if this uses fitted model objects it would need to go into the rstanarm and/or brms bayes_R2 methods, which do take fitted model objects as input. Also tagging @avehtari.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants