stan-dev / stan-dev/rstantools

An extention to the bayes_R2 function

Open
#128 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
48
Forks
24
Avg merge
1h 17m
Merged PRs (30d)
1

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading R/bayes_R2.R, the linked paper, and the attached BayesRsquared.pdf to compare the proposed extension with the current function. The issue does not define an agreed scope or acceptance criteria; done would require maintainers to decide whether the broader model support belongs in this package and specify the implementation and validation expectations.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.