Skip to content

Commit

Permalink
Merge pull request #439 from florianhartig/testOutliersBugfix
Browse files Browse the repository at this point in the history
Update tests.R
  • Loading branch information
melina-leite authored Sep 30, 2024
2 parents 5b875f7 + 23001c7 commit b0cfceb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
22 changes: 11 additions & 11 deletions DHARMa/R/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ testQuantiles <- function(simulationOutput, predictor = NULL, quantiles = c(0.25
#' @param type either default, bootstrap or binomial. See details
#' @param nBoot number of boostrap replicates. Only used ot type = "bootstrap"
#' @param plot if TRUE, the function will create an additional plot
#' @param plotBoostrap if plot should be produced of outlier frequencies calculated under the bootstrap
#' @details DHARMa residuals are created by simulating from the fitted model, and comparing the simulated values to the observed data. It can occur that all simulated values are higher or smaller than the observed data, in which case they get the residual value of 0 and 1, respectively. I refer to these values as simulation outliers, or simply outliers.
#'
#' Because no data was simulated in the range of the observed value, we don't know "how strongly" these values deviate from the model expectation, so the term "outlier" should be used with a grain of salt. It is not a judgment about the magnitude of the residual deviation, but simply a dichotomous sign that we are outside the simulated range. Moreover, the number of outliers will decrease as we increase the number of simulations.
Expand All @@ -169,7 +170,7 @@ testQuantiles <- function(simulationOutput, predictor = NULL, quantiles = c(0.25
#' @author Florian Hartig
#' @seealso [testResiduals], [testUniformity], [testOutliers], [testDispersion], [testZeroInflation], [testGeneric], [testTemporalAutocorrelation], [testSpatialAutocorrelation], [testQuantiles], [testCategorical]
#' @export
testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater", "less"), margin = c("both", "upper", "lower"), type = c("default","bootstrap", "binomial"), nBoot = 100, plot = TRUE){
testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater", "less"), margin = c("both", "upper", "lower"), type = c("default","bootstrap", "binomial"), nBoot = 100, plot = TRUE, plotBoostrap = FALSE){

# check inputs
alternative = match.arg(alternative)
Expand Down Expand Up @@ -284,12 +285,17 @@ testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater
names(out$parameter) = "observations"
out$estimate = outliers
names(out$estimate) = paste("outlier frequency (expected:", mean(frequBoot),")")

if(plotBoostrap == T){
hist(frequBoot, xlim = range(frequBoot, outliers), col = "lightgrey", main = "Bootstrapped outlier frequency")
abline(v = mean(frequBoot), col = 1, lwd = 2)
abline(v = outliers, col = "red", lwd = 2)

# legend("center", c(paste("p=", round(out$p.value, digits = 5)), paste("Deviation ", ifelse(out$p.value < 0.05, "significant", "n.s."))), text.col = ifelse(out$p.value < 0.05, "red", "black" ))
}

if(plot == T) {

opar <- par(mfrow = c(1,2))
on.exit(par(opar))


hist(simulationOutput, main = "")

main = ifelse(out$p.value <= 0.05,
Expand All @@ -299,12 +305,6 @@ testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater
title(main = main, cex.main = 1,
col.main = ifelse(out$p.value <= 0.05, "red", "black"))

hist(frequBoot, xlim = range(frequBoot, outliers), col = "lightgrey")
abline(v = mean(frequBoot), col = 1, lwd = 2)
abline(v = outliers, col = "red", lwd = 2)

# legend("center", c(paste("p=", round(out$p.value, digits = 5)), paste("Deviation ", ifelse(out$p.value < 0.05, "significant", "n.s."))), text.col = ifelse(out$p.value < 0.05, "red", "black" ))

}
}
return(out)
Expand Down
4 changes: 2 additions & 2 deletions DHARMa/man/testCategorical.Rd

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

4 changes: 2 additions & 2 deletions DHARMa/man/testGeneric.Rd

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

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

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

0 comments on commit b0cfceb

Please sign in to comment.