Skip to content

Commit

Permalink
Fix issues with glmmPQL (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke authored Dec 1, 2023
1 parent d0f7bd6 commit 7064912
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -153,3 +153,4 @@ Config/Needs/website:
r-lib/pkgdown,
easystats/easystatstemplate
Config/rcmdcheck/ignore-inconsequential-notes: true
Remotes: easystats/insight
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions R/performance_score.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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))
}
Expand Down Expand Up @@ -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(suppressWarnings(prob_fun(resp, mean = pr$pred, pis = pr$pred_zi, sum(resp))))

if (is.null(p_y) || all(is.na(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)

Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-glmmPQL.R
Original file line number Diff line number Diff line change
@@ -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")
})

0 comments on commit 7064912

Please sign in to comment.