diff --git a/NEWS.md b/NEWS.md index 9d08bf292..5e035fd08 100644 --- a/NEWS.md +++ b/NEWS.md @@ -33,6 +33,9 @@ ## Bug fixes +* `check_model()` falls back to model residuals for the default Q-Q plot if + it cannot simulate residuals, as with some ordered categorical GAMs (#794). + * `r2_coxsnell()` and `r2_nagelkerke()` now compute pseudo-$R^2$ values from model log-likelihoods (`stats::logLik()`) rather than unscaled deviances (falling back to deviances only for quasi-families), fixing incorrect results diff --git a/R/check_model.R b/R/check_model.R index 88d1b1e4c..ba7bd9bd7 100644 --- a/R/check_model.R +++ b/R/check_model.R @@ -42,7 +42,9 @@ #' randomized quantile residuals. For Gaussian models, the default is #' `"normal"`, which uses the default residuals from the model. Setting #' `residual_type = "normal"` for non-Gaussian models will use a half-normal Q-Q -#' plot of the absolute value of the standardized deviance residuals. +#' plot of the absolute value of the standardized deviance residuals. If +#' simulation fails when `residual_type` is omitted, `check_model()` uses the +#' model residuals for the Q-Q plot when possible. #' @param show_dots Logical, if `TRUE`, will show data points in the plot. Set #' to `FALSE` for models with many observations, if generating the plot is too #' time-consuming. By default, `show_dots = NULL`. In this case `check_model()` @@ -310,14 +312,11 @@ check_model.default <- function( minfo <- insight::model_info(model, verbose = FALSE) - # validate argument - if (!is.null(residual_type)) { - insight::validate_argument(residual_type, c("normal", "simulated")) - } - - # set default for residual_type - if (is.null(residual_type)) { + default_residual_type <- is.null(residual_type) + if (default_residual_type) { residual_type <- ifelse(minfo$is_linear && !minfo$is_gam, "normal", "simulated") + } else { + insight::validate_argument(residual_type, c("normal", "simulated")) } # catch models/families not supported by DHARMa - we need to add more @@ -334,7 +333,8 @@ check_model.default <- function( # set default for detrend; methods that call `check_model()` on another # model pass `detrend = NULL` when the user gave no `detrend` - if (missing(detrend) || is.null(detrend)) { + default_detrend <- missing(detrend) || is.null(detrend) + if (default_detrend) { detrend <- residual_type == "normal" } @@ -385,11 +385,15 @@ check_model.default <- function( is.null(assumptions_data$QQ) && residual_type == "simulated" ) { - insight::format_alert(paste0( - "Cannot simulate residuals for models of class `", - class(model)[1], - "`. Please try `check_model(..., residual_type = \"normal\")` instead." - )) + assumptions_data$QQ <- .check_model_qq_fallback( + model, + minfo, + default_residual_type, + verbose + ) + if (default_detrend && !is.null(assumptions_data$QQ)) { + detrend <- TRUE + } } # for binomial models with a binary outcome, the homogeneity plot is not @@ -436,6 +440,22 @@ check_model.default <- function( } +.check_model_qq_fallback <- function(model, model_info, default_residual_type, verbose) { + alert <- paste0("Cannot simulate residuals for models of class `", class(model)[1], "`.") + if (!default_residual_type) { + insight::format_alert(paste(alert, "Please try `check_model(..., residual_type = \"normal\")` instead.")) + return(NULL) + } + + qq <- .safe(suppressWarnings(.model_diagnostic_qq(model, model_info = model_info, verbose = verbose))) + if (!is.null(qq)) { + alert <- paste(alert, "Showing a Q-Q plot of the model residuals.") + } + insight::format_alert(alert) + qq +} + + # add plot settings as attributes, used by see::plot.see_check_model() ------ .add_check_model_attributes <- function( diff --git a/man/check_model.Rd b/man/check_model.Rd index b21b4e8ca..28a510099 100644 --- a/man/check_model.Rd +++ b/man/check_model.Rd @@ -77,7 +77,9 @@ is \code{"simulated"}, which uses simulated residuals. These are based on randomized quantile residuals. For Gaussian models, the default is \code{"normal"}, which uses the default residuals from the model. Setting \code{residual_type = "normal"} for non-Gaussian models will use a half-normal Q-Q -plot of the absolute value of the standardized deviance residuals.} +plot of the absolute value of the standardized deviance residuals. If +simulation fails when \code{residual_type} is omitted, \code{check_model()} uses the +model residuals for the Q-Q plot when possible.} \item{show_dots}{Logical, if \code{TRUE}, will show data points in the plot. Set to \code{FALSE} for models with many observations, if generating the plot is too diff --git a/tests/testthat/test-check_model.R b/tests/testthat/test-check_model.R index f5199d0a9..965ce7652 100644 --- a/tests/testthat/test-check_model.R +++ b/tests/testthat/test-check_model.R @@ -185,6 +185,35 @@ test_that("`check_model()` does not warn about simulated residuals without QQ pl expect_silent(check_model(m, check = "homogeneity", residual_type = "simulated")) }) +test_that("`check_model()` uses model residuals when the default simulation fails", { + skip_if_not_installed("mgcv") + skip_if_not_installed("DHARMa") + local_mocked_bindings(simulate_residuals = function(...) { + stop("Simulation unavailable", call. = FALSE) + }) + model <- mgcv::gam(carb ~ s(wt, k = 5), data = mtcars, family = mgcv::ocat(R = 8)) + normal_check <- check_model(model, check = "qq", residual_type = "normal") + + expect_message(check_model(model, check = "qq"), "Showing a Q-Q plot") + out <- suppressMessages(check_model(model, check = "qq")) + expect_identical(out$QQ, normal_check$QQ) + expect_true(attr(out, "detrend")) + grDevices::pdf(NULL) + on.exit(grDevices::dev.off(), add = TRUE) + expect_no_error(plot(out)) + + out <- suppressMessages(check_model(model, check = "qq", detrend = FALSE)) + expect_identical(out$QQ, normal_check$QQ) + expect_false(attr(out, "detrend")) + + expect_message( + check_model(model, check = "qq", residual_type = "simulated"), + "Please try" + ) + out <- suppressMessages(check_model(model, check = "qq", residual_type = "simulated")) + expect_null(out$QQ) +}) + test_that("`check_model()` forwards `type`, `detrend`, and `size_title` for `model_fit` objects", { skip_if_not_installed("DHARMa", minimum_version = "0.5.2") data(mtcars)