Skip to content

Commit 4819311

Browse files
committed
check_normality.simres, for plotting feature
1 parent d725b35 commit 4819311

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ S3method(check_normality,htest)
7272
S3method(check_normality,lmerModLmerTest)
7373
S3method(check_normality,merMod)
7474
S3method(check_normality,numeric)
75+
S3method(check_normality,performance_simres)
7576
S3method(check_outliers,BFBayesFactor)
7677
S3method(check_outliers,DHARMa)
7778
S3method(check_outliers,character)

R/check_normality.R

+17
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ check_normality.glm <- function(x, ...) {
9292
invisible(out)
9393
}
9494

95+
# simulated residuals ----------
96+
97+
#' @export
98+
check_normality.performance_simres <- function(x, ...) {
99+
# check for normality of residuals
100+
res <- stats::residuals(x, quantile_function = stats::qnorm)
101+
p.val <- .check_normality(res[!is.infinite(res) & !is.na(res)], x)
102+
103+
attr(p.val, "data") <- x
104+
attr(p.val, "object_name") <- insight::safe_deparse_symbol(substitute(x))
105+
attr(p.val, "effects") <- "fixed"
106+
class(p.val) <- unique(c("check_normality", "see_check_normality", class(p.val)))
107+
108+
p.val
109+
}
110+
111+
95112
# numeric -------------------
96113

97114
#' @export

tests/testthat/test-check_normality.R

+31
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,34 @@ test_that("check_normality | t-test", {
8282
ignore_attr = TRUE
8383
)
8484
})
85+
86+
87+
test_that("check_normality | simulated residuals", {
88+
skip_if_not_installed("DHARMa")
89+
m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
90+
res <- simulate_residuals(m)
91+
out <- check_normality(res)
92+
expect_equal(
93+
as.numeric(out),
94+
0.2969038,
95+
tolerance = 1e-3,
96+
ignore_attr = TRUE
97+
)
98+
expect_identical(
99+
capture.output(print(out)),
100+
"OK: residuals appear as normally distributed (p = 0.297)."
101+
)
102+
103+
m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
104+
out <- check_normality(m)
105+
expect_equal(
106+
as.numeric(out),
107+
0.2303071,
108+
tolerance = 1e-3,
109+
ignore_attr = TRUE
110+
)
111+
expect_identical(
112+
capture.output(print(out)),
113+
"OK: residuals appear as normally distributed (p = 0.230)."
114+
)
115+
})

0 commit comments

Comments
 (0)