Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check_model in version 0.11.0 no longer produces qq plot residuals #709

Merged
merged 10 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.11.0.3
Version: 0.11.0.4
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
the usual style as for other models and no longer returns plots from
`bayesplot::pp_check()`.

## Bug fixes

* `check_model()` now falls back on normal Q-Q plots when a model is not supported
by the DHARMa package and simulated residuals cannot be calculated.

# performance 0.11.0

## New supported models
Expand Down
6 changes: 3 additions & 3 deletions R/check_homogeneity.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@
)

if (is.null(check)) {
insight::print_color("'check_homogeneity()' cannot perform check for normality. Please specify the 'method'-argument for the test of equal variances.\n", "red")

Check warning on line 73 in R/check_homogeneity.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_homogeneity.R,line=73,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 166 characters.

Check warning on line 73 in R/check_homogeneity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_homogeneity.R,line=73,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 166 characters.
return(NULL)
}

method <- ifelse(check < 0.05, "fligner", "bartlett")
}

if (method == "fligner") {

Check warning on line 80 in R/check_homogeneity.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_homogeneity.R,line=80,col=3,[if_switch_linter] Prefer switch() statements over repeated if/else equality tests, e.g., switch(x, a = 1, b = 2) over if (x == "a") 1 else if (x == "b") 2.

Check warning on line 80 in R/check_homogeneity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_homogeneity.R,line=80,col=3,[if_switch_linter] Prefer switch() statements over repeated if/else equality tests, e.g., switch(x, a = 1, b = 2) over if (x == "a") 1 else if (x == "b") 2.
r <- stats::fligner.test(f, data = insight::get_data(x, verbose = FALSE))
p.val <- r$p.value
} else if (method == "bartlett") {
Expand Down Expand Up @@ -117,7 +117,7 @@
} else if (x < 0.05) {
insight::print_color(sprintf("Warning: Variances differ between groups (%s, p = %.3f).\n", method.string, x), "red")
} else {
insight::print_color(sprintf("OK: There is not clear evidence for different variances across groups (%s, p = %.3f).\n", method.string, x), "green")
insight::print_color(sprintf("OK: There is not clear evidence for different variances across groups (%s, p = %.3f).\n", method.string, x), "green") # nolint

Check warning on line 120 in R/check_homogeneity.R

View check run for this annotation

Codecov / codecov/patch

R/check_homogeneity.R#L120

Added line #L120 was not covered by tests
}
invisible(x)
}
Expand Down Expand Up @@ -146,18 +146,18 @@
insight::format_error("Levene test is only aplicable to ANOVAs with between-subjects factors.")
}

data <- x$data$long # Use this to also get id column
long_data <- x$data$long # Use this to also get id column
dv <- attr(x, "dv")
id <- attr(x, "id")
between <- names(attr(x, "between"))
is_covar <- vapply(attr(x, "between"), is.null, logical(1))

ag_data <- stats::aggregate(data[, dv], data[, c(between, id)], mean)
ag_data <- stats::aggregate(long_data[, dv], long_data[, c(between, id)], mean)
colnames(ag_data)[length(c(between, id)) + 1] <- dv

if (any(is_covar)) {
insight::format_alert(
"Levene's test is not appropriate with quantitative explanatory variables. Testing assumption of homogeneity among factor groups only."

Check warning on line 160 in R/check_homogeneity.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_homogeneity.R,line=160,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 141 characters.

Check warning on line 160 in R/check_homogeneity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_homogeneity.R,line=160,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 141 characters.
)
# ## TODO maybe add as option?
# warning("Testing assumption of homogeneity on residualzied data among factor groups only.", call. = FALSE)
Expand Down
13 changes: 11 additions & 2 deletions R/check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#'
#' @details For Bayesian models from packages **rstanarm** or **brms**,
#' models will be "converted" to their frequentist counterpart, using
#' [`bayestestR::bayesian_as_frequentist`](https://easystats.github.io/bayestestR/reference/convert_bayesian_as_frequentist.html).

Check warning on line 63 in R/check_model.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_model.R,line=63,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 130 characters.

Check warning on line 63 in R/check_model.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_model.R,line=63,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 130 characters.
#' A more advanced model-check for Bayesian models will be implemented at a
#' later stage.
#'
Expand Down Expand Up @@ -88,7 +88,7 @@
#' plots are helpful to check model assumptions, they do not necessarily indicate
#' so-called "lack of fit", e.g. missed non-linear relationships or interactions.
#' Thus, it is always recommended to also look at
#' [effect plots, including partial residuals](https://strengejacke.github.io/ggeffects/articles/introduction_partial_residuals.html).

Check warning on line 91 in R/check_model.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_model.R,line=91,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 134 characters.
#'
#' @section Homogeneity of Variance:
#' This plot checks the assumption of equal variance (homoscedasticity). The
Expand Down Expand Up @@ -213,6 +213,15 @@
if (is.null(residual_type)) {
residual_type <- ifelse(minfo$is_linear && !minfo$is_gam, "normal", "simulated")
}

# catch models/families not supported by DHARMa - we need to add more
# exceptions here as they appear, but for now, `check_model()` also
# automatically falls back to normal Q-Q plot for all models not supported
# by DHARMa
if (minfo$family %in% c("quasipoisson", "quasibinomial")) {
residual_type <- "normal"
}

# set default for detrend
if (missing(detrend)) {
detrend <- residual_type == "normal"
Expand Down Expand Up @@ -242,8 +251,8 @@
}

# did Q-Q plot work with simulated residuals?
if (verbose && is.null(assumptions_data$QQ) && residual_type == "simulated") {
insight::format_warning(paste0(
if (is.null(assumptions_data$QQ) && residual_type == "simulated") {
insight::format_alert(paste0(
"Cannot simulate residuals for models of class `",
class(x)[1],
"`. Please try `check_model(..., residual_type = \"normal\")` instead."
Expand Down
2 changes: 1 addition & 1 deletion R/check_predictions.R
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@
# validation check, for mixed models, where re.form = NULL (default) might fail
if (is.null(out) && insight::is_mixed_model(object) && !isTRUE(is.na(re_formula))) {
if (verbose) {
insight::format_warning(
insight::format_alert(

Check warning on line 539 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L539

Added line #L539 was not covered by tests
paste0(
"Failed to compute posterior predictive checks with `re_formula=",
deparse(re_formula),
Expand Down
4 changes: 2 additions & 2 deletions R/cronbachs_alpha.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
# we need at least two columns for Cronach's Alpha
if (is.null(ncol(.data)) || ncol(.data) < 2) {
if (verbose) {
insight::format_warning("Too few columns in `x` to compute Cronbach's Alpha.")
insight::format_alert("Too few columns in `x` to compute Cronbach's Alpha.")
}
return(NULL)
}
Expand Down Expand Up @@ -68,7 +68,7 @@
pca_data <- attr(x, "data")
if (is.null(pca_data)) {
if (verbose) {
insight::format_warning("Could not find data frame that was used for the PCA.")
insight::format_alert("Could not find data frame that was used for the PCA.")

Check warning on line 71 in R/cronbachs_alpha.R

View check run for this annotation

Codecov / codecov/patch

R/cronbachs_alpha.R#L71

Added line #L71 was not covered by tests
}
return(NULL)
}
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Bollen's
Bortz
Breunig
Breusch
BRM
Bryk
Bundock
Burnham
Expand Down
30 changes: 29 additions & 1 deletion tests/testthat/test-check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,33 @@ test_that("`check_model()` warnings for zero-infl", {
art ~ fem + mar + kid5 + ment | kid5 + phd,
data = bioChemists
)
expect_message(expect_warning(check_model(model, verbose = TRUE), regex = "Cannot simulate"), regex = "Homogeneity")
expect_message(expect_message(check_model(model, verbose = TRUE), regex = "Cannot simulate"), regex = "Homogeneity")
})


test_that("`check_model()` no warnings for quasipoisson", {
skip_if_not_installed("datawizard")
set.seed(250419)
# Generate random x values
x <- rnorm(
n = 500,
mean = 5,
sd = 2
)
# Generate y values y = 5x + e
y <- 5 * x + rnorm(
n = 500,
mean = 5,
sd = 2
)
# Generate z as offset
z <- runif(500, min = 0, max = 6719)
mock_data <- data.frame(x, y, z) |>
# both should be whole numbers since they're counts
datawizard::data_modify(y = round(y), z = round(z)) |>
datawizard::data_filter(x >= 0, y >= 0)
# Run model
model1 <- glm(y ~ x + offset(log(z)), family = "quasipoisson", data = mock_data)
expect_message(check_model(model1, verbose = TRUE), regex = "Not enough")
expect_silent(check_model(model1))
})
7 changes: 3 additions & 4 deletions tests/testthat/test-cronbachs_alpha.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ test_that("cronbachs_alpha, data frame", {
x <- mtcars[, c("cyl", "gear", "carb", "hp")]
expect_equal(cronbachs_alpha(x), 0.09463206, tolerance = 1e-3)
})

test_that("cronbachs_alpha", {
expect_warning(expect_null(cronbachs_alpha(mtcars[1])))
expect_message(expect_null(cronbachs_alpha(mtcars[1])), regex = "Too few")
})


test_that("cronbachs_alpha, principal_components", {
skip_if_not_installed("parameters", minimum_version = "0.21.3")
pca <- parameters::principal_components(mtcars[, c("cyl", "gear", "carb", "hp")], n = 2)
expect_equal(cronbachs_alpha(pca, verbose = FALSE), c(PC1 = 0.1101384), tolerance = 1e-3)
expect_warning(cronbachs_alpha(pca))
expect_message(cronbachs_alpha(pca), regex = "Too few")

pca <- parameters::principal_components(mtcars[, c("cyl", "gear", "carb", "hp")], n = 1)
expect_equal(cronbachs_alpha(pca, verbose = FALSE), c(PC1 = 0.09463206), tolerance = 1e-3)
Expand All @@ -25,7 +25,6 @@ test_that("cronbachs_alpha, principal_components", {
expect_equal(cronbachs_alpha(pca), c(PC1 = 0.4396, PC2 = -1.44331), tolerance = 1e-3)
})


test_that("cronbachs_alpha, matrix", {
m <- as.matrix(mtcars[c("cyl", "gear", "carb", "hp")])
expect_equal(cronbachs_alpha(m), 0.09463206, tolerance = 1e-3)
Expand Down
Loading