Skip to content

Commit

Permalink
Error checking normality for t.test
Browse files Browse the repository at this point in the history
Fixes #689
  • Loading branch information
strengejacke committed Feb 23, 2024
1 parent e85e53a commit 20c8f86
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
2 changes: 1 addition & 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.9.1
Version: 0.10.9.2
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

* Rudimentary support for models of class `serp` from package *serp*.

## Bug fixes

* Fixed issue in `check_normality()` for t-tests.

# performance 0.10.9

## Changes
Expand Down
47 changes: 25 additions & 22 deletions R/check_htest.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @export
check_normality.htest <- function(x, ...) {
data <- insight::get_data(x)
if (is.null(data)) {
model_data <- insight::get_data(x)
if (is.null(model_data)) {
insight::format_error(
"Cannot check assumptions - Unable to retrieve data from `htest` object."
)
Expand All @@ -11,43 +11,43 @@ check_normality.htest <- function(x, ...) {

if (grepl("Welch", method, fixed = TRUE) ||
grepl("F test to compare two variances", method, fixed = TRUE)) {
m1 <- stats::lm(data[[1]] ~ 1)
m2 <- stats::lm(data[[2]] ~ 1)
m1 <- stats::lm(model_data[[1]] ~ 1)
m2 <- stats::lm(model_data[[2]] ~ 1)

out <- check_normality(m1)
out[2] <- check_normality(m2)[1]
attr(out, "units") <- c("Group1", "Group2")
} else if (grepl("Two Sample t-test", method, fixed = TRUE)) {
m <- stats::lm(
formula = Value ~ factor(Name),
data = datawizard::data_to_long(data)
formula = value ~ factor(name),
data = datawizard::data_to_long(model_data)
)

out <- check_normality(m)
} else if (grepl("One Sample t-test", method, fixed = TRUE)) {
m <- stats::lm(data[[1]] ~ 1)
m <- stats::lm(model_data[[1]] ~ 1)

out <- check_normality(m)
} else if (grepl("Paired t-test", method, fixed = TRUE)) {
d <- data[[1]] - data[[2]]
d <- model_data[[1]] - model_data[[2]]
m <- stats::lm(d ~ 1)

out <- check_normality(m)
} else if (grepl("One-way analysis of means (not assuming equal variances)", method, fixed = TRUE)) {
data <- split(data, data[[2]])
outs <- lapply(data, function(d) {
model_data <- split(model_data, model_data[[2]])
outs <- lapply(model_data, function(d) {
check_normality(stats::lm(d[[1]] ~ 1))
})

out <- unlist(outs)
attributes(out) <- attributes(outs[[1]])
attr(out, "units") <- paste0("Group", seq_along(outs))
} else if (grepl("One-way analysis of means", method, fixed = TRUE)) {
m <- stats::aov(data[[1]] ~ factor(data[[2]]))
m <- stats::aov(model_data[[1]] ~ factor(model_data[[2]]))

out <- check_normality(m)
} else if (grepl("Pearson's product-moment correlation", method, fixed = TRUE)) {
out <- .MVN_hz(data)[["p value"]]
out <- .MVN_hz(model_data)[["p value"]]
class(out) <- c("check_normality", "see_check_normality", "numeric")
attr(out, "type") <- "residuals"
} else if (grepl("Pearson's Chi-squared test", method, fixed = TRUE) ||
Expand All @@ -73,8 +73,8 @@ check_normality.htest <- function(x, ...) {

#' @export
check_homogeneity.htest <- function(x, ...) {
data <- insight::get_data(x)
if (is.null(data)) {
model_data <- insight::get_data(x)
if (is.null(model_data)) {
insight::format_error(
"Cannot check assumptions - Unable to retrieve data from `htest` object."
)
Expand All @@ -88,11 +88,14 @@ check_homogeneity.htest <- function(x, ...) {

if (grepl("Two Sample t-test", method, fixed = TRUE)) {
m <- stats::lm(
formula = Value ~ factor(Name),
data = datawizard::data_to_long(data)
formula = value ~ factor(name),
data = datawizard::data_to_long(model_data)
)
} else if (grepl("One-way analysis of means", method, fixed = TRUE)) {
m <- stats::aov(stats::reformulate(names(data)[2], response = names(data)[1]), data = data)
m <- stats::aov(
stats::reformulate(names(model_data)[2], response = names(model_data)[1]),
data = model_data
)
} else {
insight::format_error(
"This `htest` is not supported (or this assumption is not required for this test)."
Expand All @@ -109,19 +112,19 @@ check_homogeneity.htest <- function(x, ...) {

#' @export
check_symmetry.htest <- function(x, ...) {
data <- insight::get_data(x)
if (is.null(data)) {
model_data <- insight::get_data(x)
if (is.null(model_data)) {
insight::format_error(
"Cannot check assumptions - Unable to retrieve data from `htest` object."
)
}
method <- x[["method"]]

if (grepl("signed rank", method, fixed = TRUE)) {
if (ncol(data) > 1) {
out <- check_symmetry(data[[1]] - data[[2]])
if (ncol(model_data) > 1) {
out <- check_symmetry(model_data[[1]] - model_data[[2]])
} else {
out <- check_symmetry(data[[1]])
out <- check_symmetry(model_data[[1]])
}
} else {
insight::format_error(
Expand Down
38 changes: 33 additions & 5 deletions tests/testthat/test-check_normality.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ test_that("check_normality | afex", {
)
}))

msg <- capture.output(pM <- check_normality(aM))
msg <- capture.output(pW <- check_normality(aW))
msg <- capture.output(pB <- check_normality(aB))
msg <- capture.output({
pM <- check_normality(aM)
})
msg <- capture.output({
pW <- check_normality(aW)
})
msg <- capture.output({
pB <- check_normality(aB)
})

expect_equal(pM, 0.2054236, ignore_attr = TRUE, tolerance = 0.001)
expect_equal(pW, 0.5496325, ignore_attr = TRUE, tolerance = 0.001)
Expand All @@ -40,12 +46,34 @@ test_that("check_normality | glmmTMB", {
)

out <- check_normality(m, effects = "random")
expect_equal(attributes(out)$re_groups, "site: (Intercept)")
expect_identical(attributes(out)$re_groups, "site: (Intercept)")
expect_equal(as.vector(out), 0.698457693553405, tolerance = 1e-3)

expect_message(
out <- check_normality(m, effects = "fixed"),
{
out <- check_normality(m, effects = "fixed")
},
"for linear models"
)
expect_null(out)
})


test_that("check_normality | t-test", {
out <- t.test(mtcars$mpg, mtcars$hp, var.equal = TRUE)
expect_equal(
check_normality(out),
structure(
7.15789362314837e-12,
type = "residuals",
object_name = out,
effects = "fixed",
class = c(
"check_normality",
"see_check_normality", "numeric"
)
),
tolerance = 1e-3,
ignore_attr = TRUE
)
})

0 comments on commit 20c8f86

Please sign in to comment.