Skip to content

Commit

Permalink
check_normality.simres, for plotting feature
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Mar 20, 2024
1 parent d725b35 commit 4819311
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ S3method(check_normality,htest)
S3method(check_normality,lmerModLmerTest)
S3method(check_normality,merMod)
S3method(check_normality,numeric)
S3method(check_normality,performance_simres)
S3method(check_outliers,BFBayesFactor)
S3method(check_outliers,DHARMa)
S3method(check_outliers,character)
Expand Down
17 changes: 17 additions & 0 deletions R/check_normality.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ check_normality.glm <- function(x, ...) {
invisible(out)
}

# simulated residuals ----------

#' @export
check_normality.performance_simres <- function(x, ...) {
# check for normality of residuals
res <- stats::residuals(x, quantile_function = stats::qnorm)
p.val <- .check_normality(res[!is.infinite(res) & !is.na(res)], x)

attr(p.val, "data") <- x
attr(p.val, "object_name") <- insight::safe_deparse_symbol(substitute(x))
attr(p.val, "effects") <- "fixed"
class(p.val) <- unique(c("check_normality", "see_check_normality", class(p.val)))

p.val
}


# numeric -------------------

#' @export
Expand Down
31 changes: 31 additions & 0 deletions tests/testthat/test-check_normality.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,34 @@ test_that("check_normality | t-test", {
ignore_attr = TRUE
)
})


test_that("check_normality | simulated residuals", {
skip_if_not_installed("DHARMa")
m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
res <- simulate_residuals(m)
out <- check_normality(res)
expect_equal(
as.numeric(out),
0.2969038,
tolerance = 1e-3,
ignore_attr = TRUE
)
expect_identical(
capture.output(print(out)),
"OK: residuals appear as normally distributed (p = 0.297)."
)

m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
out <- check_normality(m)
expect_equal(
as.numeric(out),
0.2303071,
tolerance = 1e-3,
ignore_attr = TRUE
)
expect_identical(
capture.output(print(out)),
"OK: residuals appear as normally distributed (p = 0.230)."
)
})

0 comments on commit 4819311

Please sign in to comment.