mjskay / tidybayes

Bayesian analysis + tidy data + geoms (R package)
http://mjskay.github.io/tidybayes
GNU General Public License v3.0
726 stars 59 forks source link

tidy_residuals #133

Closed jebyrnes closed 3 years ago

jebyrnes commented 6 years ago

Have you thought about putting in some tidy output for residuals? Could be very convenient for, say, finite population standard deviation calculations. See some code below I developed with brms which I think could be easily extended.


tidy_residuals <- function(model){
  UseMethod("tidy_residuals")
}

tidy_residuals.brmsfit <- function(model){
  res <- residuals(model, summary=FALSE)
  props <- summary(model)
  nchains <- props$chains
  iter <- props$iter - props$warmup

  start <- seq(1, nrow(res), nrow(res)/nchains)

  chains <- map(start, ~as.mcmc(res[.x:(.x+iter-1),])) %>%
    coda::as.mcmc.list()

  tidy_draws(chains)

}

gather_residuals <- function(model){
  tidy_residuals(model) %>% 
    gather(.variable, .value, -.chain, -.iteration, -.draw)
}

spread_residuals <- function(model){
  tidy_residuals(model)
}
mjskay commented 6 years ago

Hm, good question. Originally I did not add this because in most cases this is just add_fitted_draws or add_predicted_draws (depending on type of residual) followed by a mutate. On the other hand, it could be nice as a convenience function, and standardizing it may reduce errors.

Probably I would pattern this after [add_][fitted|predicted]_draws, so there would be a residual_draws and add_residual_draws function. Output column name would then be .residual.

Looking at residuals.stanreg and predictive_error.stanreg, there will have to be some argument standardization... brms seems to merge these two into one function where method = "fitted" gives the former and method = "predict" gives the latter; rstanarm does not merge these into one function. I would probably standardize this usage and give one function (then document arg standardization in #70).

My preference would maybe be for method = "predicted" to be the default. Annoyingly type would also need standardization, as that argument in brms uses "ordinary" instead of "response" (which is what rstanarm uses to mean the same thing, seemingly patterned after residuals from base R).

Something that has been on my internal TODO list is to figure out where (if at all) doing randomized quantile residuals would fit into tidybayes. It occurs to me that with method = "predict" it might be possible to roll those in without much overhead, say as another value of type.

Thoughts on any of this?

paul-buerkner commented 6 years ago

At some point, we have to align the arguments names of the brms and rstanarm methods, but I don't think this will happen this year. So it's probably better to align in inside tidybayes for the time being.

jebyrnes commented 5 years ago

Cool - so, using add_predicted_draws and a little rlang (to grab the first column), do you think I could just go through the predicted_draws.R file and throw something back and you?

mjskay commented 5 years ago

Oh! I've already started down that path. Jussec, I'll commit something...

mjskay commented 5 years ago

Check out the dev branch now. I think it should work...

mjskay commented 5 years ago

still needs:

jebyrnes commented 5 years ago

Oh! Cool! Thanks! Will look!

mjskay commented 3 years ago

Closing this old issue (it is mostly implemented anyway).