Define how to draw from posterior distribution
mc_distribution.Rdmodelcheck uses the R package tidybayes
to draw from posterior distributions. For posterior predictive distributions,
modelcheck uses tidybayes::predicted_draws(); for posterior distribution
of push-forward transformation, modelcheck uses tidybayes::linpred_draws().
Usage
mc_distribution(
  distribution = "predictive",
  newdata = NULL,
  draw_function = NULL,
  response_var = NULL,
  ndraws = 500,
  ...
)Arguments
- distribution
 Which distribution to draw from. The options include
"predictive"and and push-forward transformations (e.g.mu,sigma, andphi). For example, if the model is normal family anddistribution = "predictive", we draws from posterior predictive distribution; ifdistribution = "mu", we draws from the distribution of linear/link-level predictor (i.e. push-forward transformations).- newdata
 Data frame to generate predictions from, or
NULLto reuse the data frame used to fit model, i.e. replicated predictive distribution.- draw_function
 The function used to draw from model's posterior distributions. Default to be
NULL. Ifdraw_functionisNULL, thenmodelcheckwill usetidybayes::predicted_draws()fordistribution = "predictive"and usetidybayes::linpred_draws()for other distributions of linear/link-level predictors. Ifdraw_functionis notNULL, it should be a function that takesmodel,newdata, andndrawsas inputs and output a data frame that sampled frommodelonnewdatawith at least apredictioncolumn (the draws from the model's distribution), a.rowcolumn (a factor grouping rows from the input newdata), and a.drawcolumn (a unique index corresponding to each draw from the distribution).- response_var
 A string for the response variable in model. Default to be
NULL. IfNULL,modelcheckwill find the response variable inmodel$formula.- ndraws
 The number of draws to return, or
NULLto return all draws.- ...
 Augments passed to
draw_function. Ifdraw_functionisNULL, then...will be passed totidybayes::predicted_draws()ortidybayes::linpred_draws()
Examples
library(brms)
#> Loading required package: Rcpp
#> Loading 'brms' package (version 2.19.0). Useful instructions
#> can be found by typing help('brms'). A more detailed introduction
#> to the package is available through vignette('brms_overview').
#> 
#> Attaching package: ‘brms’
#> The following object is masked from ‘package:stats’:
#> 
#>     ar
library(modelr)
#> Error in library(modelr): there is no package called ‘modelr’
model = brm(
  bf(mpg ~ disp),
  init = "0",
  data = mtcars,
  iter = 6000,
  file = "models/example_model.rds" # cache model (can be removed)
)
#> Error: The directory 'models' does not exist. Please choose an existing directory where the model can be saved after fitting.
mcplot(model) +
  mc_distribution()
#> Error in mc_setting %>% e2() %>% e1(): could not find function "%>%"
mcplot(model) +
  mc_distribution("mu")
#> Error in mc_setting %>% e2() %>% e1(): could not find function "%>%"
mcplot(model) +
  mc_distribution("mu", data_grid(model$data, disp, carb, vs, am, mpg))
#> Error in mc_setting %>% e2() %>% e1(): could not find function "%>%"
library(tidybayes)
#> 
#> Attaching package: ‘tidybayes’
#> The following objects are masked from ‘package:brms’:
#> 
#>     dstudent_t, pstudent_t, qstudent_t, rstudent_t
draw_function = function(model, newdata, ...) {
  epred_draws(model, newdata, dpar = "mu", ...)
}
mcplot(model) +
  mc_distribution(draw_function = draw_function)
#> Error in mc_setting %>% e2() %>% e1(): could not find function "%>%"