Package 'DHARMa.helpers'

Title: Helper functions to check models not (yet) directly supported by DHARMa
Description: Helper functions to check models not (yet) directly supported by the DHARMa package, such as Bayesian models fitted with brms, or exponential random graph models (ERGMs).
Authors: Francisco Rodríguez-Sánchez [aut, cre]
Maintainer: Francisco Rodríguez-Sánchez <[email protected]>
License: MIT + file LICENSE
Version: 0.0.2
Built: 2024-11-13 05:30:53 UTC
Source: https://github.com/Pakillo/DHARMa.helpers

Help Index


Check Bayesian models fitted with brms

Description

Check Bayesian models fitted with brms

Usage

dh_check_brms(
  model,
  resp = NULL,
  integer = FALSE,
  plot = TRUE,
  nsamples = 1000,
  ntrys = 5,
  ...
)

Arguments

model

A fitted model brms::brmsfit-class(). Categorical and ordinal models not supported by now.

resp

Optional name of response variable (for multivariate models).

integer

Logical (TRUE/FALSE), indicating if response is an integer, as in Poisson and binomial models

plot

Logical. Plot residual checks? Default is TRUE.

nsamples

Integer. Number of samples to draw from the posterior.

ntrys

Integer. Number of trys to use for truncated distributions. See brms::posterior_predict().

...

Further arguments for DHARMa::plotResiduals()

Value

An object of type DHARMa. See DHARMa::createDHARMa() for more details.

See Also

https://frodriguezsanchez.net/post/using-dharma-to-check-bayesian-models-fitted-with-brms/

Examples

#' # Example models taken from brms::brm()

# Poisson regression for the number of seizures in epileptic patients
fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
           data = epilepsy, family = poisson())
simres <- dh_check_brms(fit1, integer = TRUE)
plot(simres, form = epilepsy$zAge)
testDispersion(simres)


# Probit regression using the binomial family
ntrials <- sample(1:10, 100, TRUE)
success <- rbinom(100, size = ntrials, prob = 0.4)
x <- rnorm(100)
data4 <- data.frame(ntrials, success, x)
fit4 <- brm(success | trials(ntrials) ~ x, data = data4,
            family = binomial("probit"))
summary(fit4)
simres <- dh_check_brms(fit4, integer = TRUE)
plot(simres, form = data4$x)


# Multivariate (multiresponse) model
data("BTdata", package = "MCMCglmm")
bf_tarsus <- bf(tarsus ~ sex + (1|p|fosternest) + (1|q|dam))
bf_back <- bf(back ~ hatchdate + (1|p|fosternest) + (1|q|dam))
fit <- brm(bf_tarsus + bf_back + set_rescor(TRUE),
            data = BTdata, chains = 2, cores = 2)
dh_check_brms(fit, resp = "tarsus")
brms::pp_check(fit, resp = "tarsus")
dh_check_brms(fit, resp = "back")
brms::pp_check(fit, resp = "back")