Skip to contents

modelcheck 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, and phi). For example, if the model is normal family and distribution = "predictive", we draws from posterior predictive distribution; if distribution = "mu", we draws from the distribution of linear/link-level predictor (i.e. push-forward transformations).

newdata

Data frame to generate predictions from, or NULL to 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. If draw_function is NULL, then modelcheck will use tidybayes::predicted_draws() for distribution = "predictive" and use tidybayes::linpred_draws() for other distributions of linear/link-level predictors. If draw_function is not NULL, it should be a function that takes model, newdata, and ndraws as inputs and output a data frame that sampled from model on newdata with at least a prediction column (the draws from the model's distribution), a .row column (a factor grouping rows from the input newdata), and a .draw column (a unique index corresponding to each draw from the distribution).

response_var

A string for the response variable in model. Default to be NULL. If NULL, modelcheck will find the response variable in model$formula.

ndraws

The number of draws to return, or NULL to return all draws.

...

Augments passed to draw_function. If draw_function is NULL, then ... will be passed to tidybayes::predicted_draws() or tidybayes::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 "%>%"