diff --git a/NEWS.md b/NEWS.md index 93005579e..54198c3f1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,6 +9,12 @@ * `check_outliers()` for `"ICS"` method is now more stable and less likely to fail. +## Bug fixes + +* `check_collinearity()` did not work for hurdle- or zero-inflated models of + package *pscl* when model had no explicitly defined formula for the + zero-inflation model. + # performance 0.10.4 ## Changes to functions diff --git a/R/check_collinearity.R b/R/check_collinearity.R index d1c5e0f22..075ecf5b1 100644 --- a/R/check_collinearity.R +++ b/R/check_collinearity.R @@ -440,6 +440,14 @@ check_collinearity.zerocount <- function(x, f <- insight::find_formula(x) + # hurdle or zeroinfl model can have no zero-inflation formula, in which case + # we have the same formula as for conditional formula part + if (inherits(x, c("hurdle", "zeroinfl", "zerocount")) && + component == "zero_inflated" && + is.null(f[["zero_inflated"]])) { + f$zero_inflated <- f$conditional + } + if (inherits(x, "mixor")) { terms <- labels(x$terms) } else { diff --git a/tests/testthat/test-check_collinearity.R b/tests/testthat/test-check_collinearity.R index 6fb97c41e..3d68b87ac 100644 --- a/tests/testthat/test-check_collinearity.R +++ b/tests/testthat/test-check_collinearity.R @@ -190,3 +190,24 @@ test_that("check_collinearity, ci are NA", { ) ) }) + +test_that("check_collinearity, hurdle/zi models w/o zi-formula", { + skip_if_not_installed("pscl") + data("bioChemists", package = "pscl") + m <- pscl::hurdle( + art ~ fem + mar, + data = bioChemists, + dist = "poisson", + zero.dist = "binomial", + link = "logit" + ) + out <- check_collinearity(m) + expect_identical( + colnames(out), + c( + "Term", "VIF", "VIF_CI_low", "VIF_CI_high", "SE_factor", "Tolerance", + "Tolerance_CI_low", "Tolerance_CI_high", "Component" + ) + ) + expect_equal(out$VIF, c(1.05772, 1.05772, 1.06587, 1.06587), tolerance = 1e-4) +})