Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 30, 2023
1 parent 4d4b776 commit 25ff890
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 41 deletions.
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ 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,character)
S3method(check_outliers,data.frame)
Expand Down Expand Up @@ -106,6 +105,9 @@ S3method(check_overdispersion,poissonmfx)
S3method(check_predictions,BFBayesFactor)
S3method(check_predictions,default)
S3method(check_predictions,lme)
S3method(check_residuals,DHARMa)
S3method(check_residuals,default)
S3method(check_residuals,performance_simres)
S3method(check_singularity,MixMod)
S3method(check_singularity,clmm)
S3method(check_singularity,cpglmm)
Expand Down
19 changes: 0 additions & 19 deletions R/check_normality.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,6 @@ check_normality.glm <- function(x, ...) {
invisible(out)
}

# DHARMa -------------------

#' @export
check_normality.performance_simres <- function(x, ...) {
# check for normality of residuals
res <- stats::residuals(x, quantileFunction = stats::qnorm)
p.val <- suppressWarnings(stats::ks.test(stats::residuals(x), "punif"))$p.value

attr(p.val, "data") <- res
attr(p.val, "object_name") <- NULL
attr(p.val, "effects") <- "fixed"
attr(p.val, "type") <- "residuals"
attr(p.val, "model") <- x$fittedModel
attr(p.val, "model_info") <- insight::model_info(x$fittedModel)
class(p.val) <- unique(c("check_normality", "see_check_normality", "performance_simres", class(p.val)))

p.val
}

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

#' @export
Expand Down
46 changes: 35 additions & 11 deletions R/check_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,46 @@
#' misspecification problems, such as over/underdispersion, zero-inflation, and
#' residual spatial and temporal autocorrelation.
#'
#' @param x An object returned by [`simulated_residuals()`].
#' @param ... Passed down to [`DHARMa::testUniformity()`]
#' @param x An object returned by [`simulate_residuals()`] or
#' [`DHARMa::simulateResiduals()`].
#' @param alternative A character string specifying the alternative hypothesis.
#' See [`stats::ks.test()`] for details.
#' @param ... Passed down to [`stats::ks.test()`]
#'
#' @examplesIf require("DHARMa")
#' dat <- DHARMa::createData(sampleSize = 100, overdispersion = 0.5, family = poisson())
#' m <- glm(observedResponse ~ Environment1, family = poisson(), data = dat)
#' res <- simulate_residuals(m)
#' check_residuals(res)
#'
#' @export
check_residuals <- function(x, ...) {
insight::check_if_installed("DHARMa")
# TODO: This should be an S3 method instead of using ifelse
if (inherits(x, c("performance_simres", "DHARMa"))) {
# tests if the overall distribution conforms to expectations; equivalent to:
# ks.test(residuals(simulated_residuals), "punif")
DHARMa::testUniformity(x, plot = FALSE, ...)
} else {
insight::format_error("Unsupported input.")
}
UseMethod("check_residuals")
}

#' @export
check_residuals.default <- function(x, ...) {
insight::format_error("`check_residuals()` only works with objects returned by `simulate_residuals()` by `DHARMa::simulateResiduals()`.") # nolint
}

#' @rdname check_residuals
#' @export
check_residuals.performance_simres <- function(x,
alternative = c("two.sided", "less", "greater"),
...) {
alternative <- match.arg(alternative)
stats::ks.test(
stats::residuals(simulated_residuals),
"punif",
alternative = alternative,
...
)
}

#' @export
check_residuals.DHARMa <- check_residuals.performance_simres


# methods ------------------------------

# TODO: Add print method
17 changes: 11 additions & 6 deletions R/simulate_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' @description to do.
#'
#' @param x A model object.
#' @param iterations Number of simulations to run.
#' @param ... Arguments passed on to [`DHARMa::simulateResiduals()`].
#'
#' @return Simulated residuals.
Expand All @@ -19,12 +20,12 @@
#' - Dunn, P. K., & Smyth, G. K. (1996). Randomized Quantile Residuals. Journal
#' of Computational and Graphical Statistics, 5(3), 236. \doi{10.2307/1390802}
#'
#' @examples
#' @examplesIf require("DHARMa")
#' m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
#' simulate_residuals(m)
#'
#' @export
simulate_residuals <- function(x, ...) {
simulate_residuals <- function(x, iterations = 250, ...) {
insight::check_if_installed("DHARMa")
# TODO (low priority): Note that DHARMa::simulateResiduals(x, ...) does its own checks for whether
# or not the model passed to it is supported, do we want to use this or do our
Expand All @@ -34,7 +35,7 @@ simulate_residuals <- function(x, ...) {
# extracting the residuals from it because the object contains important stuff
# in it that we'll want to pass onto other functions later, such as passing
# the fitted model into check_model().
out <- DHARMa::simulateResiduals(x, ...)
out <- DHARMa::simulateResiduals(x, n = iterations, plot = FALSE, ...)
class(out) <- c("performance_simres", class(out))
out
}
Expand All @@ -46,8 +47,12 @@ print.performance_simres <- function(x, ...) {
# TODO (low priority): We can probably just base this off of the print method
# DHARMa uses, but with an easystats style. For now we can just stick with
# DHARMa's method.
cat(
"Simulated residuals from a model of class", class(x$fittedModel),
"based on", x$nSim, "simulations."
msg <- paste0(
"Simulated residuals from a model of class `", class(x$fittedModel),
"` based on ", x$nSim, " simulations. Use `check_residuals()` to check ",
"uniformity of residuals. It is recommended to refer to `?DHARMa::simulateReisudals`",
" and `vignette(\"DHARMa\")` for more information about different settings",
" in particular situations or for particular models."
)
cat(insight::format_message(msg))
}
19 changes: 17 additions & 2 deletions man/check_residuals.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions man/simulate_residuals.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 25ff890

Please sign in to comment.