From 6d7ee1cbcde6a2e625407119176f94a6c0bc432e Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 16 Mar 2024 10:35:14 +0100 Subject: [PATCH] lintr, styler, fix test --- R/check_model.R | 3 +-- R/check_normality.R | 8 ++++---- R/check_zeroinflation.R | 12 ++++++------ tests/testthat/test-check_residuals.R | 2 +- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/R/check_model.R b/R/check_model.R index 582d3c54a..8a3fa29ff 100644 --- a/R/check_model.R +++ b/R/check_model.R @@ -402,8 +402,7 @@ check_model.model_fit <- function(x, dat <- list() dat$VIF <- .diag_vif(model, verbose = verbose) - dat$QQ <- switch( - residual_type, + dat$QQ <- switch(residual_type, simulated = simulate_residuals(model, ...), .diag_qq(model, model_info = model_info, verbose = verbose) ) diff --git a/R/check_normality.R b/R/check_normality.R index 2d80ec89e..2b9f071d5 100644 --- a/R/check_normality.R +++ b/R/check_normality.R @@ -200,7 +200,7 @@ check_normality.merMod <- function(x, effects = c("fixed", "random"), ...) { } }, error = function(e) { - return(NULL) + NULL } ) @@ -262,7 +262,7 @@ check_normality.BFBayesFactor <- check_normality.afex_aov # helper --------------------- .check_normality <- function(x, model, type = "residuals") { - ts <- .safe({ + ts_result <- .safe({ if (length(x) >= 5000) { suppressWarnings(stats::ks.test(x, y = "pnorm", alternative = "two.sided")) } else { @@ -270,7 +270,7 @@ check_normality.BFBayesFactor <- check_normality.afex_aov } }) - if (is.null(ts)) { + if (is.null(ts_result)) { insight::print_color( sprintf("`check_normality()` does not support models of class `%s`.\n", class(model)[1]), "red" @@ -278,7 +278,7 @@ check_normality.BFBayesFactor <- check_normality.afex_aov return(NULL) } - out <- ts$p.value + out <- ts_result$p.value attr(out, "type") <- type out diff --git a/R/check_zeroinflation.R b/R/check_zeroinflation.R index e92d25bcd..d029e02c2 100644 --- a/R/check_zeroinflation.R +++ b/R/check_zeroinflation.R @@ -97,10 +97,10 @@ check_zeroinflation.default <- function(x, tolerance = 0.05, ...) { } # get predicted zero-counts - if (!is.null(theta)) { - pred.zero <- round(sum(stats::dnbinom(x = 0, size = theta, mu = mu))) - } else { + if (is.null(theta)) { pred.zero <- round(sum(stats::dpois(x = 0, lambda = mu))) + } else { + pred.zero <- round(sum(stats::dnbinom(x = 0, size = theta, mu = mu))) } # proportion @@ -156,10 +156,10 @@ print.check_zi <- function(x, ...) { lower <- 1 - x$tolerance upper <- 1 + x$tolerance - if (!is.null(x$p.value)) { - p_string <- paste0(" (", insight::format_p(x$p.value), ")") - } else { + if (is.null(x$p.value)) { p_string <- "" + } else { + p_string <- paste0(" (", insight::format_p(x$p.value), ")") } if (x$ratio < lower) { diff --git a/tests/testthat/test-check_residuals.R b/tests/testthat/test-check_residuals.R index 6c64159d8..33407abf9 100644 --- a/tests/testthat/test-check_residuals.R +++ b/tests/testthat/test-check_residuals.R @@ -22,5 +22,5 @@ test_that("check_singularity, lme4", { quine.nb1 <- MASS::glm.nb(x ~ mu) set.seed(123) result <- check_residuals(quine.nb1) - expect_equal(result, 0.000665414, tolerance = 1e-3) + expect_equal(result, 0.000665414, tolerance = 1e-3, ignore_attr = TRUE) })