From 764dcdf5bb067c6db22a9d2a74774a7df7a67efb Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 15 Mar 2024 15:30:28 +0100 Subject: [PATCH] allow to directly pass models --- R/check_residuals.R | 14 ++++++++------ man/check_residuals.Rd | 4 ++-- tests/testthat/test-check_residuals.R | 10 ++++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/R/check_residuals.R b/R/check_residuals.R index 3bb202e21..628c5cfd0 100644 --- a/R/check_residuals.R +++ b/R/check_residuals.R @@ -29,16 +29,18 @@ check_residuals <- function(x, ...) { UseMethod("check_residuals") } +#' @rdname check_residuals #' @export -check_residuals.default <- function(x, ...) { - insight::format_error("`check_residuals()` only works with objects returned by `simulate_residuals()` or `DHARMa::simulateResiduals()`.") # nolint +check_residuals.default <- function(x, alternative = c("two.sided", "less", "greater"), ...) { + if (insight::is_model(x)) { + check_residuals(simulate_residuals(x, ...), alternative = alternative) + } else { + insight::format_error("`check_residuals()` only works with objects supported by `simulate_residuals()` or `DHARMa::simulateResiduals()`.") # nolint + } } -#' @rdname check_residuals #' @export -check_residuals.performance_simres <- function(x, - alternative = c("two.sided", "less", "greater"), - ...) { +check_residuals.performance_simres <- function(x, alternative = c("two.sided", "less", "greater"), ...) { alternative <- match.arg(alternative) ts <- suppressWarnings( stats::ks.test( diff --git a/man/check_residuals.Rd b/man/check_residuals.Rd index d8c0b1ca2..b91615953 100644 --- a/man/check_residuals.Rd +++ b/man/check_residuals.Rd @@ -2,12 +2,12 @@ % Please edit documentation in R/check_residuals.R \name{check_residuals} \alias{check_residuals} -\alias{check_residuals.performance_simres} +\alias{check_residuals.default} \title{Check uniformity of simulated residuals} \usage{ check_residuals(x, ...) -\method{check_residuals}{performance_simres}(x, alternative = c("two.sided", "less", "greater"), ...) +\method{check_residuals}{default}(x, alternative = c("two.sided", "less", "greater"), ...) } \arguments{ \item{x}{An object returned by \code{\link[=simulate_residuals]{simulate_residuals()}} or diff --git a/tests/testthat/test-check_residuals.R b/tests/testthat/test-check_residuals.R index df92e3505..b057174d0 100644 --- a/tests/testthat/test-check_residuals.R +++ b/tests/testthat/test-check_residuals.R @@ -11,4 +11,14 @@ test_that("check_singularity, lme4", { capture.output(print(out)), "Warning: Non-uniformity of simulated residuals detected (p = 0.019)." ) + skip_if_not_installed("MASS") + set.seed(3) + mu <- rpois(500, lambda = 3) + x <- rnorm(500, mu, mu * 3) + x <- ceiling(x) + x <- pmax(x, 0) + quine.nb1 <- MASS::glm.nb(x ~ mu) + set.seed(123) + result <- check_residuals(quine.nb1) + expect_equal(result, 0.000665414, tolerance = 1e-3) })