From 7afa2ccc3bddb78cfa501c02663bbbdc9aae9d15 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 30 Nov 2023 15:16:50 +0100 Subject: [PATCH] Fix issues with glmmPQL --- DESCRIPTION | 2 +- NEWS.md | 3 +++ R/performance_score.R | 16 ++++++++++------ tests/testthat/test-glmmPQL.R | 15 +++++++++++++++ 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 tests/testthat/test-glmmPQL.R diff --git a/DESCRIPTION b/DESCRIPTION index ac7741ecd..35a58af93 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: performance Title: Assessment of Regression Models Performance -Version: 0.10.8.6 +Version: 0.10.8.7 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/NEWS.md b/NEWS.md index 5984b7b9d..f3bb5fdd3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,9 @@ * Fixed issue in `binned_residuals()` for models with binary outcome, where in rare occasions empty bins could occur. +* `performance_score()` should no longer fail for models where scoring rules + can't be calculated. Instead, an informative message is returned. + # performance 0.10.8 ## Changes diff --git a/R/performance_score.R b/R/performance_score.R index eb5ee9b31..cd5b7efd6 100644 --- a/R/performance_score.R +++ b/R/performance_score.R @@ -65,7 +65,7 @@ performance_score <- function(model, verbose = TRUE, ...) { if (minfo$is_ordinal || minfo$is_multinomial) { if (verbose) { - insight::print_color("Can't calculate proper scoring rules for ordinal, multinomial or cumulative link models.\n", "red") + insight::format_alert("Can't calculate proper scoring rules for ordinal, multinomial or cumulative link models.") } return(list(logarithmic = NA, quadratic = NA, spherical = NA)) } @@ -74,10 +74,7 @@ performance_score <- function(model, verbose = TRUE, ...) { if (!is.null(ncol(resp)) && ncol(resp) > 1) { if (verbose) { - insight::print_color( - "Can't calculate proper scoring rules for models without integer response values.\n", - "red" - ) + insight::format_alert("Can't calculate proper scoring rules for models without integer response values.") } return(list(logarithmic = NA, quadratic = NA, spherical = NA)) } @@ -127,7 +124,14 @@ performance_score <- function(model, verbose = TRUE, ...) { } else { datawizard::to_numeric(resp, dummy_factors = FALSE, preserve_levels = TRUE) } - p_y <- prob_fun(resp, mean = pr$pred, pis = pr$pred_zi, sum(resp)) + p_y <- .safe(prob_fun(resp, mean = pr$pred, pis = pr$pred_zi, sum(resp))) + + if (is.null(p_y)) { + if (verbose) { + insight::format_alert("Can't calculate proper scoring rules for this model.") + } + return(list(logarithmic = NA, quadratic = NA, spherical = NA)) + } quadrat_p <- sum(p_y^2) diff --git a/tests/testthat/test-glmmPQL.R b/tests/testthat/test-glmmPQL.R new file mode 100644 index 000000000..64caaccea --- /dev/null +++ b/tests/testthat/test-glmmPQL.R @@ -0,0 +1,15 @@ +skip_if_not_installed("MASS") +test_that("r2", { + example_dat <- data.frame( + prop = c(0.2, 0.2, 0.5, 0.7, 0.1, 1, 1, 1, 0.1), + size = c("small", "small", "small", "large", "large", "large", "large", "small", "small"), + x = c(0.1, 0.1, 0.8, 0.7, 0.6, 0.5, 0.5, 0.1, 0.1), + species = c("sp1", "sp1", "sp2", "sp2", "sp3", "sp3", "sp4", "sp4", "sp4"), + stringsAsFactors = FALSE + ) + mn <- MASS::glmmPQL(prop ~ x + size, + random = ~ 1 | species, + family = "quasibinomial", data = example_dat + ) + expect_message(performance_score(mn), regex = "Cant calculate") +})