From 1eb0551c8fe0f46df124c223c1495cd86bf682c3 Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Wed, 24 Jan 2024 10:13:30 -0500 Subject: [PATCH 01/10] Update R-CMD-check.yaml --- .github/workflows/R-CMD-check.yaml | 105 ++++++++--------------------- 1 file changed, 29 insertions(+), 76 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 3d025704..53cf991b 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -1,16 +1,10 @@ -# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag. -# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: - - main - - master - - GitAction - + branches: [main, master, GitAction] pull_request: - branches: - - main - - master + branches: [main, master] name: R-CMD-check @@ -24,85 +18,44 @@ jobs: fail-fast: false matrix: config: + - {os: macos-latest, r: 'release'} - {os: windows-latest, r: 'release'} - - {os: windows-latest, r: 'release'} - - {os: windows-latest, r: 'oldrel'} - - # - {os: macOS-latest, r: 'devel'} - - {os: macOS-latest, r: 'release'} - - - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} - - {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} + - {os: windows-latest, r: 'devel'} + - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} + - {os: ubuntu-latest, r: 'release'} + - {os: ubuntu-latest, r: 'oldrel-1'} env: - R_REMOTES_NO_ERRORS_FROM_WARNINGS: true - RSPM: ${{ matrix.config.rspm }} GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + R_KEEP_PKG_SOURCE: yes steps: - - uses: actions/checkout@v3 - - uses: r-lib/actions/setup-r@v2 - with: - r-version: ${{ matrix.config.r }} - - uses: r-lib/actions/setup-pandoc@v2 - + - uses: r-lib/actions/setup-tinytex@v2 - run: tlmgr --version - - name: Query dependencies - run: | - setwd("DHARMa") - install.packages('remotes') - saveRDS(remotes::dev_package_deps(dependencies = TRUE), "../.github/depends.Rds", version = 2) - writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), "../.github/R-version") - shell: Rscript {0} - - - name: Cache R packages - if: runner.os != 'Windows' - uses: actions/cache@v2 + - uses: r-lib/actions/setup-r@v2 with: - path: ${{ env.R_LIBS_USER }} - key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} - restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- - - - name: Install system dependencies - if: runner.os == 'Linux' - run: | - cd DHARMa - while read -r cmd - do - eval sudo $cmd - done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))') + r-version: ${{ matrix.config.r }} + http-user-agent: ${{ matrix.config.http-user-agent }} + use-public-rspm: true - - name: Install dependencies - run: | - setwd("DHARMa") - remotes::install_deps(dependencies = TRUE) - remotes::install_cran("rcmdcheck") - shell: Rscript {0} - - - name: Check_II - env: - _R_CHECK_CRAN_INCOMING_REMOTE_: false - CI: true - run: | - rcmdcheck::rcmdcheck("DHARMa", args = c("--no-multiarch", "--no-manual"), error_on = "warning", check_dir = "check") - shell: Rscript {0} - - - name: Check - env: - _R_CHECK_CRAN_INCOMING_REMOTE_: false - CI: true - run: | - rcmdcheck::rcmdcheck("DHARMa", args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check") - shell: Rscript {0} + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::rcmdcheck + needs: check + working-directory: DHARMa + # second check with different rcmdcheck arguments. + - uses: r-lib/actions/check-r-package@v2 + with: + upload-snapshots: true + working-directory: DHARMa + args: 'c("--no-multiarch", "--no-manual")' - - name: Upload check results - if: failure() - uses: actions/upload-artifact@main + - uses: r-lib/actions/check-r-package@v2 with: - name: ${{ runner.os }}-r${{ matrix.config.r }}-results - path: check + upload-snapshots: true + working-directory: DHARMa From 0fe091f292a33075e64748bf811769ae646ddd9e Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Wed, 24 Jan 2024 10:26:46 -0500 Subject: [PATCH 02/10] Update Matrix version to request a newer version on CI --- DHARMa/DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DHARMa/DESCRIPTION b/DHARMa/DESCRIPTION index 4356f4f9..c680b689 100644 --- a/DHARMa/DESCRIPTION +++ b/DHARMa/DESCRIPTION @@ -21,7 +21,7 @@ Imports: graphics, utils, grDevices, - Matrix, + Matrix (>= 1.6-2), parallel, gap, lmtest, From 04c8256926ac8eef724d403e28c660146c7ee403 Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Wed, 24 Jan 2024 11:04:54 -0500 Subject: [PATCH 03/10] Update plotResiduals.Rd --- DHARMa/man/plotResiduals.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DHARMa/man/plotResiduals.Rd b/DHARMa/man/plotResiduals.Rd index 4c6c14ac..b2ffa7f3 100644 --- a/DHARMa/man/plotResiduals.Rd +++ b/DHARMa/man/plotResiduals.Rd @@ -89,7 +89,7 @@ plotResiduals(simulationOutput, form = testData$group) # to diagnose overdispersion and heteroskedasticity it can be useful to # display residuals as absolute deviation from the expected mean 0.5 -plotResiduals(simulationOutput, absoluteDeviation = T, quantreg = FALSE) +plotResiduals(simulationOutput, absoluteDeviation = TRUE, quantreg = FALSE) # All these options can also be provided to the main plotting function From 00d15b3e869e717de927af91998ae093ff0fe7ea Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Tue, 6 Feb 2024 08:20:34 -0500 Subject: [PATCH 04/10] Update R-CMD-check.yaml --- .github/workflows/R-CMD-check.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 53cf991b..8a2855fb 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -30,7 +30,7 @@ jobs: R_KEEP_PKG_SOURCE: yes steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-pandoc@v2 From 24c23999ac58449b5202ec6b0341afeb9bdb7905 Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 7 Jun 2024 10:42:47 -0400 Subject: [PATCH 05/10] Commit doc chane --- DHARMa/man/hist.DHARMa.Rd | 2 +- DHARMa/man/plot.DHARMa.Rd | 2 +- DHARMa/man/plotQQunif.Rd | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/DHARMa/man/hist.DHARMa.Rd b/DHARMa/man/hist.DHARMa.Rd index ba521947..e20a6734 100644 --- a/DHARMa/man/hist.DHARMa.Rd +++ b/DHARMa/man/hist.DHARMa.Rd @@ -67,7 +67,7 @@ plotResiduals(simulationOutput, form = testData$group) # to diagnose overdispersion and heteroskedasticity it can be useful to # display residuals as absolute deviation from the expected mean 0.5 -plotResiduals(simulationOutput, absoluteDeviation = T, quantreg = FALSE) +plotResiduals(simulationOutput, absoluteDeviation = TRUE, quantreg = FALSE) # All these options can also be provided to the main plotting function diff --git a/DHARMa/man/plot.DHARMa.Rd b/DHARMa/man/plot.DHARMa.Rd index e070b036..065ae9d9 100644 --- a/DHARMa/man/plot.DHARMa.Rd +++ b/DHARMa/man/plot.DHARMa.Rd @@ -69,7 +69,7 @@ plotResiduals(simulationOutput, form = testData$group) # to diagnose overdispersion and heteroskedasticity it can be useful to # display residuals as absolute deviation from the expected mean 0.5 -plotResiduals(simulationOutput, absoluteDeviation = T, quantreg = FALSE) +plotResiduals(simulationOutput, absoluteDeviation = TRUE, quantreg = FALSE) # All these options can also be provided to the main plotting function diff --git a/DHARMa/man/plotQQunif.Rd b/DHARMa/man/plotQQunif.Rd index caa981f0..4f1205af 100644 --- a/DHARMa/man/plotQQunif.Rd +++ b/DHARMa/man/plotQQunif.Rd @@ -64,7 +64,7 @@ plotResiduals(simulationOutput, form = testData$group) # to diagnose overdispersion and heteroskedasticity it can be useful to # display residuals as absolute deviation from the expected mean 0.5 -plotResiduals(simulationOutput, absoluteDeviation = T, quantreg = FALSE) +plotResiduals(simulationOutput, absoluteDeviation = TRUE, quantreg = FALSE) # All these options can also be provided to the main plotting function From dc97eef9fb5fad24068015fb5590219495f7b979 Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 7 Jun 2024 11:09:20 -0400 Subject: [PATCH 06/10] Use `TRUE` and `FALSE` instead of `T` and `F` --- DHARMa/R/DHARMa.R | 8 +-- DHARMa/R/createData.R | 2 +- DHARMa/R/plots.R | 36 +++++------ DHARMa/R/random.R | 2 +- DHARMa/R/runBenchmarks.R | 22 +++---- DHARMa/R/simulateLRT.R | 8 +-- DHARMa/R/simulateResiduals.R | 14 ++--- DHARMa/R/tests.R | 76 +++++++++++------------ DHARMa/inst/examples/checkModelHelp.R | 2 +- DHARMa/inst/examples/plotsHelp.R | 2 +- DHARMa/inst/examples/runBenchmarksHelp.R | 4 +- DHARMa/inst/examples/simulateLRTHelp.R | 2 +- DHARMa/inst/examples/testDispersionHelp.R | 2 +- 13 files changed, 90 insertions(+), 90 deletions(-) diff --git a/DHARMa/R/DHARMa.R b/DHARMa/R/DHARMa.R index 2b5ec7d2..1471445a 100644 --- a/DHARMa/R/DHARMa.R +++ b/DHARMa/R/DHARMa.R @@ -96,7 +96,7 @@ outliers <- function(object, lowerQuantile = 0, upperQuantile = 1, return = c("i #' @note Either scaled residuals or (simulatedResponse AND observed response) have to be provided #' @example inst/examples/createDharmaHelp.R #' @export -createDHARMa <- function(simulatedResponse , observedResponse , fittedPredictedResponse = NULL, integerResponse = F, seed = 123, method = c("PIT", "traditional"), rotation = NULL){ +createDHARMa <- function(simulatedResponse , observedResponse , fittedPredictedResponse = NULL, integerResponse = FALSE, seed = 123, method = c("PIT", "traditional"), rotation = NULL){ randomState <-getRandomState(seed) on.exit({randomState$restoreCurrent()}) @@ -143,7 +143,7 @@ createDHARMa <- function(simulatedResponse , observedResponse , fittedPredictedR #' @return an object of class DHARMa #' @keywords internal ensureDHARMa <- function(simulationOutput, - convert = F){ + convert = FALSE){ if(inherits(simulationOutput, "DHARMa")){ return(simulationOutput) @@ -153,8 +153,8 @@ ensureDHARMa <- function(simulationOutput, else { if (class(simulationOutput)[1] %in% getPossibleModels()){ - if (convert == "Model" | convert == T) return(simulateResiduals(simulationOutput)) - } else if(is.vector(simulationOutput, mode = "numeric") & convert == T) { + if (convert == "Model" | convert == TRUE) return(simulateResiduals(simulationOutput)) + } else if(is.vector(simulationOutput, mode = "numeric") & convert == TRUE) { out = list() out$scaledResiduals = simulationOutput out$nObs = length(out$scaledResiduals) diff --git a/DHARMa/R/createData.R b/DHARMa/R/createData.R index ca01094b..85b7fb53 100644 --- a/DHARMa/R/createData.R +++ b/DHARMa/R/createData.R @@ -20,7 +20,7 @@ #' @param hasNA should an NA be added to the environmental predictor (for test purposes) #' @export #' @example /inst/examples/createDataHelp.R -createData <- function(sampleSize = 100, intercept = 0, fixedEffects = 1, quadraticFixedEffects = NULL, numGroups = 10, randomEffectVariance = 1, overdispersion = 0, family = poisson(), scale = 1, cor = 0, roundPoissonVariance = NULL, pZeroInflation = 0, binomialTrials = 1, temporalAutocorrelation = 0, spatialAutocorrelation =0, factorResponse = F, replicates=1, hasNA = F){ +createData <- function(sampleSize = 100, intercept = 0, fixedEffects = 1, quadraticFixedEffects = NULL, numGroups = 10, randomEffectVariance = 1, overdispersion = 0, family = poisson(), scale = 1, cor = 0, roundPoissonVariance = NULL, pZeroInflation = 0, binomialTrials = 1, temporalAutocorrelation = 0, spatialAutocorrelation =0, factorResponse = FALSE, replicates=1, hasNA = FALSE){ nPredictors = length(fixedEffects) diff --git a/DHARMa/R/plots.R b/DHARMa/R/plots.R index 0c77d533..64690888 100644 --- a/DHARMa/R/plots.R +++ b/DHARMa/R/plots.R @@ -4,7 +4,7 @@ #' #' @param x an object of class DHARMa with simulated residuals created by \code{\link{simulateResiduals}} #' @param ... further options for \code{\link{plotResiduals}}. Consider in particular parameters quantreg, rank and asFactor. xlab, ylab and main cannot be changed when using plot.DHARMa, but can be changed when using plotResiduals. -#' @param title The title for both panels (plotted via mtext, outer = T) +#' @param title The title for both panels (plotted via mtext, outer = TRUE) #' #' @details The function creates a plot with two panels. The left panel is a uniform qq plot (calling \code{\link{plotQQunif}}), and the right panel shows residuals against predicted values (calling \code{\link{plotResiduals}}), with outliers highlighted in red. #' @@ -31,7 +31,7 @@ plot.DHARMa <- function(x, title = "DHARMa residual", ...){ plotQQunif(x) plotResiduals(x, ...) - mtext(title, outer = T) + mtext(title, outer = TRUE) } @@ -57,7 +57,7 @@ hist.DHARMa <- function(x, cex.main = 1, ...){ - x = ensureDHARMa(x, convert = T) + x = ensureDHARMa(x, convert = TRUE) val = x$scaledResiduals val[val == 0] = -0.01 @@ -97,7 +97,7 @@ plotSimulatedResiduals <- function(simulationOutput, ...){ #' @seealso \code{\link{plotSimulatedResiduals}}, \code{\link{plotResiduals}} #' @example inst/examples/plotsHelp.R #' @export -plotQQunif <- function(simulationOutput, testUniformity = T, testOutliers = T, testDispersion = T, ...){ +plotQQunif <- function(simulationOutput, testUniformity = TRUE, testOutliers = TRUE, testDispersion = TRUE, ...){ a <- list(...) a$pch = checkDots("pch", 2, ...) @@ -114,7 +114,7 @@ plotQQunif <- function(simulationOutput, testUniformity = T, testOutliers = T, t do.call(gap::qqunif, append(list(simulationOutput$scaledResiduals), a)) if(testUniformity == TRUE){ - temp = testUniformity(simulationOutput, plot = F) + temp = testUniformity(simulationOutput, plot = FALSE) legend("topleft", c(paste("KS test: p=", round(temp$p.value, digits = 5)), paste("Deviation ", ifelse(temp$p.value < 0.05, "significant", "n.s."))), @@ -123,7 +123,7 @@ plotQQunif <- function(simulationOutput, testUniformity = T, testOutliers = T, t } if(testOutliers == TRUE){ - temp = testOutliers(simulationOutput, plot = F) + temp = testOutliers(simulationOutput, plot = FALSE) legend("bottomright", c(paste("Outlier test: p=", round(temp$p.value, digits = 5)), paste("Deviation ", ifelse(temp$p.value < 0.05, "significant", "n.s."))), @@ -132,7 +132,7 @@ plotQQunif <- function(simulationOutput, testUniformity = T, testOutliers = T, t } if(testDispersion == TRUE){ - temp = testDispersion(simulationOutput, plot = F) + temp = testDispersion(simulationOutput, plot = FALSE) legend("center", c(paste("Dispersion test: p=", round(temp$p.value, digits = 5)), paste("Deviation ", ifelse(temp$p.value < 0.05, "significant", "n.s."))), @@ -175,7 +175,7 @@ plotQQunif <- function(simulationOutput, testUniformity = T, testOutliers = T, t #' @seealso \code{\link{plotQQunif}}, \code{\link{testQuantiles}}, \code{\link{testOutliers}} #' @example inst/examples/plotsHelp.R #' @export -plotResiduals <- function(simulationOutput, form = NULL, quantreg = NULL, rank = T, asFactor = NULL, smoothScatter = NULL, quantiles = c(0.25, 0.5, 0.75), absoluteDeviation = FALSE, ...){ +plotResiduals <- function(simulationOutput, form = NULL, quantreg = NULL, rank = TRUE, asFactor = NULL, smoothScatter = NULL, quantiles = c(0.25, 0.5, 0.75), absoluteDeviation = FALSE, ...){ ##### Checks ##### @@ -185,12 +185,12 @@ plotResiduals <- function(simulationOutput, form = NULL, quantreg = NULL, rank = a$ylab = checkDots("ylab", yAxis , ...) a$xlab = checkDots("xlab", ifelse(is.null(form), "Model predictions", gsub(".*[$]","",deparse(substitute(form)))), ...) - if(rank == T) a$xlab = paste(a$xlab, "(rank transformed)") + if(rank == TRUE) a$xlab = paste(a$xlab, "(rank transformed)") - simulationOutput = ensureDHARMa(simulationOutput, convert = T) + simulationOutput = ensureDHARMa(simulationOutput, convert = TRUE) res = simulationOutput$scaledResiduals - if(absoluteDeviation == T){ + if(absoluteDeviation == TRUE){ res = 2 * abs(res - 0.5) } @@ -202,7 +202,7 @@ plotResiduals <- function(simulationOutput, form = NULL, quantreg = NULL, rank = if(!is.factor(pred)){ - if (rank == T){ + if (rank == TRUE){ pred = rank(pred, ties.method = "average") pred = pred / max(pred) a$xlim = checkDots("xlim", c(0,1), ...) @@ -256,16 +256,16 @@ plotResiduals <- function(simulationOutput, form = NULL, quantreg = NULL, rank = out = NULL if(is.numeric(pred)){ - if(quantreg == F){ + if(quantreg == FALSE){ title(main = main, cex.main = 1) abline(h = quantiles, col = "black", lwd = 0.5, lty = 2) try({ lines(smooth.spline(pred, res, df = 10), lty = 2, lwd = 2, col = "red") abline(h = 0.5, col = "red", lwd = 2) - }, silent = T) + }, silent = TRUE) }else{ - out = testQuantiles(res, pred, quantiles = quantiles, plot = F) + out = testQuantiles(res, pred, quantiles = quantiles, plot = FALSE) if(any(out$pvals < 0.05, na.rm = TRUE)){ @@ -293,7 +293,7 @@ plotResiduals <- function(simulationOutput, form = NULL, quantreg = NULL, rank = abline(h = quantiles[i], col = lineCol, lwd = 0.5, lty = 2) polygon(c(out$predictions$pred, rev(out$predictions$pred)), c(out$predictions[,2*i] - out$predictions[,2*i+1], rev(out$predictions[,2*i] + out$predictions[,2*i+1])), - col = "#00000020", border = F) + col = "#00000020", border = FALSE) lines(out$predictions$pred, out$predictions[,2*i], col = lineCol, lwd = 2) } @@ -358,7 +358,7 @@ plotConventionalResiduals <- function(fittedModel){ plot(predict(fittedModel), resid(fittedModel, type = "deviance"), main = "Deviance" , ylab = "Residual", xlab = "Predicted") plot(predict(fittedModel), resid(fittedModel, type = "pearson") , main = "Pearson", ylab = "Residual", xlab = "Predicted") plot(predict(fittedModel), resid(fittedModel, type = "response") , main = "Raw residuals" , ylab = "Residual", xlab = "Predicted") - mtext("Conventional residual plots", outer = T) + mtext("Conventional residual plots", outer = TRUE) } @@ -366,7 +366,7 @@ plotConventionalResiduals <- function(fittedModel){ # # -# if(quantreg == F){ +# if(quantreg == FALSE){ # # lines(smooth.spline(simulationOutput$fittedPredictedResponse, simulationOutput$scaledResiduals, df = 10), lty = 2, lwd = 2, col = "red") # diff --git a/DHARMa/R/random.R b/DHARMa/R/random.R index 014848f7..5c19f7b7 100644 --- a/DHARMa/R/random.R +++ b/DHARMa/R/random.R @@ -22,7 +22,7 @@ getRandomState <- function(seed = NULL){ current = mget(".Random.seed", envir = .GlobalEnv, ifnotfound = list(NULL))[[1]] - if(!is.null(seed) && is.logical(seed) && seed == F){ + if(!is.null(seed) && is.logical(seed) && seed == FALSE){ restoreCurrent <- function(){} }else{ restoreCurrent <- function(){ diff --git a/DHARMa/R/runBenchmarks.R b/DHARMa/R/runBenchmarks.R index d8343179..bbf31b8f 100644 --- a/DHARMa/R/runBenchmarks.R +++ b/DHARMa/R/runBenchmarks.R @@ -15,7 +15,7 @@ #' @author Florian Hartig #' @seealso \code{\link{plot.DHARMaBenchmark}} #' @example inst/examples/runBenchmarksHelp.R -runBenchmarks <- function(calculateStatistics, controlValues = NULL, nRep = 10, alpha = 0.05, parallel = FALSE, exportGlobal = F, ...){ +runBenchmarks <- function(calculateStatistics, controlValues = NULL, nRep = 10, alpha = 0.05, parallel = FALSE, exportGlobal = FALSE, ...){ start_time <- Sys.time() @@ -60,7 +60,7 @@ runBenchmarks <- function(calculateStatistics, controlValues = NULL, nRep = 10, # doesn't see to work properly loadedPackages = (.packages()) parExectuer = function(x = NULL, control = NULL) calculateStatistics(control) - if (exportGlobal == T) parallel::clusterExport(cl = cl, varlist = ls(envir = .GlobalEnv)) + if (exportGlobal == TRUE) parallel::clusterExport(cl = cl, varlist = ls(envir = .GlobalEnv)) parallel::clusterExport(cl = cl, c("parExectuer", "calculateStatistics", "loadedPackages"), envir = environment()) parallel::clusterEvalQ(cl, {for(p in loadedPackages) library(p, character.only=TRUE)}) @@ -141,11 +141,11 @@ runBenchmarks <- function(calculateStatistics, controlValues = NULL, nRep = 10, plot.DHARMaBenchmark <- function(x, ...){ if(length(x$controlValues)== 1){ - boxplot(x$simulations[,1:x$nSummaries], col = "grey", ylim = c(-0.3,1), horizontal = T, las = 2, xaxt='n', main = "p distribution", ...) + boxplot(x$simulations[,1:x$nSummaries], col = "grey", ylim = c(-0.3,1), horizontal = TRUE, las = 2, xaxt='n', main = "p distribution", ...) abline(v = 0) abline(v = c(0.25, 0.5, 0.75), lty = 2) text(-0.2, 1:x$nSummaries, labels = x$summaries$propSignificant[-1]) - # barplot(as.matrix(x$summaries$propSignificant[-1]), horiz = T, add = T, offset = -0.2, names.arg = "test", width = 0.5, space = 1.4) + # barplot(as.matrix(x$summaries$propSignificant[-1]), horiz = TRUE, add = TRUE, offset = -0.2, names.arg = "test", width = 0.5, space = 1.4) }else{ res = x$summaries$propSignificant @@ -157,7 +157,7 @@ plot.DHARMaBenchmark <- function(x, ...){ polygon(c(res$controlValues, rev(res$controlValues)), c(CIs[1,], rev(CIs[2,])), - col = "#00000020", border = F) + col = "#00000020", border = FALSE) lines(res$controlValues, res[,i+1], col = i, lty = i, lwd = 2) } legend("bottomright", colnames(res[,-1]), col = 1:x$nSummaries, lty = 1:x$nSummaries, lwd = 2) @@ -170,7 +170,7 @@ plot.DHARMaBenchmark <- function(x, ...){ plotMultipleHist <- function(x){ lin = ncol(x) - histList <- lapply(x, hist, breaks = seq(0,1,0.02), plot = F) + histList <- lapply(x, hist, breaks = seq(0,1,0.02), plot = FALSE) plot(NULL, xlim = c(0,1), ylim = c(0, lin), yaxt = 'n', ylab = NULL, xlab = "p-value") abline(h= 0) @@ -198,18 +198,18 @@ plotMultipleHist <- function(x){ #' @param main title for the plot #' @param ... additional arguments to hist #' @author Florian Hartig -testPDistribution <- function(x, plot = T, main = "p distribution \n expected is flat at 1", ...){ +testPDistribution <- function(x, plot = TRUE, main = "p distribution \n expected is flat at 1", ...){ out = suppressWarnings(ks.test(x, 'punif')) - hist(x, xlim = c(0,1), breaks = 20, freq = F, main = main, ...) + hist(x, xlim = c(0,1), breaks = 20, freq = FALSE, main = main, ...) abline(h=1, col = "red") return(out) } -# if(plot == T){ +# if(plot == TRUE){ # oldpar <- par(mfrow = c(4,4)) # hist(out, breaks = 50, col = "red", main = paste("mean of", nSim, "simulations")) -# for (i in 1:min(nSim, 15)) hist(out[i,], breaks = 50, freq = F, main = i) +# for (i in 1:min(nSim, 15)) hist(out[i,], breaks = 50, freq = FALSE, main = i) # par(oldpar) # } @@ -250,7 +250,7 @@ generateGenerator <- function(mod){ #' @export benchmarkRuntime<- function(createModel, evaluationFunctions, n){ m = length(evaluationFunctions) - models = replicate(n, createModel(), simplify = F) + models = replicate(n, createModel(), simplify = FALSE) runtimes = rep(NA, m) for (i in 1:m){ diff --git a/DHARMa/R/simulateLRT.R b/DHARMa/R/simulateLRT.R index 144182a1..6612513c 100644 --- a/DHARMa/R/simulateLRT.R +++ b/DHARMa/R/simulateLRT.R @@ -32,7 +32,7 @@ #' #' @example inst/examples/simulateLRTHelp.R #' @export -simulateLRT<-function(m0, m1, n = 250, seed = 123, plot = T, suppressWarnings = T, saveModels = F, ...){ +simulateLRT<-function(m0, m1, n = 250, seed = 123, plot = TRUE, suppressWarnings = TRUE, saveModels = FALSE, ...){ ######## general assertions and startup calculations ########## # identical to simulateResiduals @@ -64,7 +64,7 @@ simulateLRT<-function(m0, m1, n = 250, seed = 123, plot = T, suppressWarnings = out$simulatedLR = rep(NA, n) - if(saveModels == T) out$saveModels == list() + if(saveModels == TRUE) out$saveModels == list() for (i in 1:n){ @@ -76,7 +76,7 @@ simulateLRT<-function(m0, m1, n = 250, seed = 123, plot = T, suppressWarnings = # if (i==3) stop("x") # Note: also set silent = T for production - if(suppressWarnings == T){ + if(suppressWarnings == TRUE){ invisible(capture.output(suppressWarnings(suppressMessages({ refittedM0 = getRefit(m0, simObserved) refittedM1 = getRefit(m1, simObserved) @@ -87,7 +87,7 @@ simulateLRT<-function(m0, m1, n = 250, seed = 123, plot = T, suppressWarnings = refittedM1 = getRefit(m1, simObserved) } - if(saveModels == T) out$saveModels[[i]] = list(refittedM0 = refittedM0, refittedM1 = refittedM1) + if(saveModels == TRUE) out$saveModels[[i]] = list(refittedM0 = refittedM0, refittedM1 = refittedM1) out$simulatedLR[i] = logLik(refittedM1) - logLik(refittedM0) diff --git a/DHARMa/R/simulateResiduals.R b/DHARMa/R/simulateResiduals.R index ab3e21d8..c1dcf688 100644 --- a/DHARMa/R/simulateResiduals.R +++ b/DHARMa/R/simulateResiduals.R @@ -9,7 +9,7 @@ #' @param plot if TRUE, \code{\link{plotResiduals}} will be directly run after the residuals have been calculated #' @param ... parameters to pass on to the simulate function of the model object. An important use of this is to specify whether simulations should be conditional on the current random effect estimates, e.g. via re.form. Note that not all models support syntax to specify conditional or unconditional simulations. See also details and \code{\link{getSimulations}} #' @param seed the random seed to be used within DHARMa. The default setting, recommended for most users, is keep the random seed on a fixed value 123. This means that you will always get the same randomization and thus the same result when running the same code. NULL = no new seed is set, but previous random state will be restored after simulation. FALSE = no seed is set, and random state will not be restored. The latter two options are only recommended for simulation experiments. See vignette for details. -#' @param method for refit = F, the quantile randomization method used. The two options implemented at the moment are probability integral transform (PIT-) residuals (current default), and the "traditional" randomization procedure, that was used in DHARMa until version 0.3.0. Refit = T will always use "traditional", respectively of the value of method. For details, see \code{\link{getQuantile}} +#' @param method for refit = FALSE, the quantile randomization method used. The two options implemented at the moment are probability integral transform (PIT-) residuals (current default), and the "traditional" randomization procedure, that was used in DHARMa until version 0.3.0. Refit = T will always use "traditional", respectively of the value of method. For details, see \code{\link{getQuantile}} #' @param rotation optional rotation of the residual space prior to calculating the quantile residuals. The main purpose of this is to account for residual covariance as created by temporal or spatial residual autocorrelation. See details below, section *residual auto-correlation* ass well as the help of [getQuantile] and, for a practical example, [testTemporalAutocorrelation]. #' #' @details There are a number of important considerations when simulating from a more complex (hierarchical) model: @@ -41,7 +41,7 @@ #' @example inst/examples/simulateResidualsHelp.R #' @import stats #' @export -simulateResiduals <- function(fittedModel, n = 250, refit = F, integerResponse = NULL, plot = F, seed = 123, method = c("PIT", "traditional"), rotation = NULL, ...){ +simulateResiduals <- function(fittedModel, n = 250, refit = FALSE, integerResponse = NULL, plot = FALSE, seed = 123, method = c("PIT", "traditional"), rotation = NULL, ...){ ######## general assertions and startup calculations ########## @@ -174,14 +174,14 @@ simulateResiduals <- function(fittedModel, n = 250, refit = F, integerResponse = #' @keywords internal checkSimulations <- function(simulatedResponse, nObs, nSim){ - if(!inherits(simulatedResponse, "matrix")) securityAssertion("Simulation from the model produced wrong class", stop = T) + if(!inherits(simulatedResponse, "matrix")) securityAssertion("Simulation from the model produced wrong class", stop = TRUE) - if(any(dim(simulatedResponse) != c(nObs, nSim) )) securityAssertion("Simulation from the model do not have the same dimension as nObs.", stop = F) + if(any(dim(simulatedResponse) != c(nObs, nSim) )) securityAssertion("Simulation from the model do not have the same dimension as nObs.", stop = FALSE) if(any(!is.finite(simulatedResponse))) message("Simulations from your fitted model produce infinite values. Consider if this is sensible") - if(any(is.nan(simulatedResponse))) securityAssertion("Simulations from your fitted model produce NaN values. DHARMa cannot calculated residuals for this. This is nearly certainly an error of the regression package you are using", stop = T) - if(any(is.na(simulatedResponse))) securityAssertion("Simulations from your fitted model produce NA values. DHARMa cannot calculated residuals for this. This is nearly certainly an error of the regression package you are using", stop = T) + if(any(is.nan(simulatedResponse))) securityAssertion("Simulations from your fitted model produce NaN values. DHARMa cannot calculated residuals for this. This is nearly certainly an error of the regression package you are using", stop = TRUE) + if(any(is.na(simulatedResponse))) securityAssertion("Simulations from your fitted model produce NA values. DHARMa cannot calculated residuals for this. This is nearly certainly an error of the regression package you are using", stop = TRUE) } @@ -233,7 +233,7 @@ recalculateResiduals <- function(simulationOutput, group = NULL, aggregateBy = s out$observedResponse = aggregateByGroup(simulationOutput$observedResponse) out$fittedPredictedResponse = aggregateByGroup(simulationOutput$fittedPredictedResponse) - if (simulationOutput$refit == F){ + if (simulationOutput$refit == FALSE){ out$simulatedResponse = apply(simulationOutput$simulatedResponse, 2, aggregateByGroup) out$scaledResiduals = getQuantile(simulations = out$simulatedResponse , observed = out$observedResponse, integerResponse = simulationOutput$integerResponse, method = method, rotation = rotation) diff --git a/DHARMa/R/tests.R b/DHARMa/R/tests.R index e64b2200..67c639ef 100644 --- a/DHARMa/R/tests.R +++ b/DHARMa/R/tests.R @@ -10,7 +10,7 @@ #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @example inst/examples/testsHelp.R #' @export -testResiduals <- function(simulationOutput, plot = T){ +testResiduals <- function(simulationOutput, plot = TRUE){ opar = par(mfrow = c(1,3)) on.exit(par(opar)) @@ -48,23 +48,23 @@ testSimulatedResiduals <- function(simulationOutput){ #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @example inst/examples/testsHelp.R #' @export -testUniformity<- function(simulationOutput, alternative = c("two.sided", "less", "greater"), plot = T){ +testUniformity<- function(simulationOutput, alternative = c("two.sided", "less", "greater"), plot = TRUE){ - simulationOutput = ensureDHARMa(simulationOutput, convert = T) + simulationOutput = ensureDHARMa(simulationOutput, convert = TRUE) out <- suppressWarnings(ks.test(simulationOutput$scaledResiduals, 'punif', alternative = alternative)) - if(plot == T) plotQQunif(simulationOutput = simulationOutput) + if(plot == TRUE) plotQQunif(simulationOutput = simulationOutput) return(out) } # Experimental -testBivariateUniformity<- function(simulationOutput, alternative = c("two.sided", "less", "greater"), plot = T){ +testBivariateUniformity<- function(simulationOutput, alternative = c("two.sided", "less", "greater"), plot = TRUE){ - simulationOutput = ensureDHARMa(simulationOutput, convert = T) + simulationOutput = ensureDHARMa(simulationOutput, convert = TRUE) #out <- suppressWarnings(ks.test(simulationOutput$scaledResiduals, 'punif', alternative = alternative)) - #if(plot == T) plotQQunif(simulationOutput = simulationOutput) + #if(plot == TRUE) plotQQunif(simulationOutput = simulationOutput) out = NULL return(out) } @@ -89,14 +89,14 @@ testBivariateUniformity<- function(simulationOutput, alternative = c("two.sided" #' @example inst/examples/testQuantilesHelp.R #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @export -testQuantiles <- function(simulationOutput, predictor = NULL, quantiles = c(0.25,0.5,0.75), plot = T){ +testQuantiles <- function(simulationOutput, predictor = NULL, quantiles = c(0.25,0.5,0.75), plot = TRUE){ - if(plot == F){ + if(plot == FALSE){ out = list() out$data.name = deparse(substitute(simulationOutput)) - simulationOutput = ensureDHARMa(simulationOutput, convert = T) + simulationOutput = ensureDHARMa(simulationOutput, convert = TRUE) res = simulationOutput$scaledResiduals pred = ensurePredictor(simulationOutput, predictor) @@ -113,13 +113,13 @@ testQuantiles <- function(simulationOutput, predictor = NULL, quantiles = c(0.25 # settings for k = the dimension of the basis used to represent the smooth term. # see https://github.com/mfasiolo/qgam/issues/37 dimSmooth = min(length(unique(datTemp$pred)), 10) - quantResult = try(capture.output(quantileFits[[i]] <- qgam::qgam(res ~ s(pred, k = dimSmooth) , data =datTemp, qu = quantiles[i])), silent = T) + quantResult = try(capture.output(quantileFits[[i]] <- qgam::qgam(res ~ s(pred, k = dimSmooth) , data =datTemp, qu = quantiles[i])), silent = TRUE) if(inherits(quantResult, "try-error")){ message("Unable to calculate quantile regression for quantile ", quantiles[i], ". Possibly to few (unique) data points / predictions. Will be ommited in plots and significance calculations.") } else { x = summary(quantileFits[[i]]) pval[i] = min(p.adjust(c(x$p.table[1,4], x$s.table[1,4]), method = "BH")) # correction for test on slope and intercept - quantPre = predict(quantileFits[[i]], newdata = predictions, se = T) + quantPre = predict(quantileFits[[i]], newdata = predictions, se = TRUE) predictions[, 2*i] = quantPre$fit + quantiles[i] predictions[, 2*i + 1] = quantPre$se.fit } @@ -134,7 +134,7 @@ testQuantiles <- function(simulationOutput, predictor = NULL, quantiles = c(0.25 class(out) = "htest" - } else if(plot == T) { + } else if(plot == TRUE) { out <- plotResiduals(simulationOutput = simulationOutput, form = predictor, quantiles = quantiles, quantreg = TRUE) } return(out) @@ -169,7 +169,7 @@ testQuantiles <- function(simulationOutput, predictor = NULL, quantiles = c(0.25 #' @author Florian Hartig #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @export -testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater", "less"), margin = c("both", "upper", "lower"), type = c("default","bootstrap", "binomial"), nBoot = 100, plot = T){ +testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater", "less"), margin = c("both", "upper", "lower"), type = c("default","bootstrap", "binomial"), nBoot = 100, plot = TRUE){ # check inputs alternative = match.arg(alternative) @@ -210,7 +210,7 @@ testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater if (simulationOutput$integerResponse == T & out$p.value < 0.05) message("DHARMa:testOutliers with type = binomial may have inflated Type I error rates for integer-valued distributions. To get a more exact result, it is recommended to re-run testOutliers with type = 'bootstrap'. See ?testOutliers for details") - if(plot == T) { + if(plot == TRUE) { hist(simulationOutput, main = "") @@ -235,7 +235,7 @@ testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater simIndices = 1:simulationOutput$nSim nSim = simulationOutput$nSim - if(simulationOutput$refit == T){ + if(simulationOutput$refit == TRUE){ simResp = simulationOutput$refittedResiduals } else { simResp = simulationOutput$simulatedResponse @@ -254,7 +254,7 @@ testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater for (i in 1:nBoot){ #sel = -i - sel = sample(simIndices[-i], size = nSim, replace = T) + sel = sample(simIndices[-i], size = nSim, replace = TRUE) residuals <- getQuantile(simulations = simResp[,sel], observed = simResp[,i], @@ -285,7 +285,7 @@ testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater out$estimate = outliers names(out$estimate) = paste("outlier frequency (expected:", mean(frequBoot),")") - if(plot == T) { + if(plot == TRUE) { opar <- par(mfrow = c(1,2)) on.exit(par(opar)) @@ -330,9 +330,9 @@ testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @example inst/examples/testsHelp.R #' @export -testCategorical <- function(simulationOutput, catPred, quantiles = c(0.25, 0.5, 0.75), plot = T){ +testCategorical <- function(simulationOutput, catPred, quantiles = c(0.25, 0.5, 0.75), plot = TRUE){ - simulationOutput = ensureDHARMa(simulationOutput, convert = T) + simulationOutput = ensureDHARMa(simulationOutput, convert = TRUE) catPred = as.factor(catPred) out = list() @@ -344,7 +344,7 @@ testCategorical <- function(simulationOutput, catPred, quantiles = c(0.25, 0.5, if(nlevels(catPred) > 1) out$homogeneity = leveneTest(simulationOutput$scaledResiduals ~ catPred) - if(plot == T){ + if(plot == TRUE){ boxplot(simulationOutput$scaledResiduals ~ catPred, ylim = c(0,1), axes = FALSE, col = ifelse(out$uniformity$p.value.cor < 0.05, "red", "lightgrey")) axis(1, at = 1:nlevels(catPred), levels(catPred)) axis(2, at=c(0, quantiles, 1)) @@ -367,7 +367,7 @@ testCategorical <- function(simulationOutput, catPred, quantiles = c(0.25, 0.5, #' DHARMa dispersion tests #' -#' This function performs simulation-based tests for over/underdispersion. If type = "DHARMa" (default and recommended), simulation-based dispersion tests are performed. Their behavior differs depending on whether simulations are done with refit = F, or refit = T, and whether data is simulated conditional (e.g. re.form ~0 in lme4) (see below). If type = "PearsonChisq", a chi2 test on Pearson residuals is performed. +#' This function performs simulation-based tests for over/underdispersion. If type = "DHARMa" (default and recommended), simulation-based dispersion tests are performed. Their behavior differs depending on whether simulations are done with refit = FALSE, or refit = TRUE, and whether data is simulated conditional (e.g. re.form ~0 in lme4) (see below). If type = "PearsonChisq", a chi2 test on Pearson residuals is performed. #' #' @param simulationOutput an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case. #' @param alternative a character string specifying whether the test should test if observations are "greater", "less" or "two.sided" compared to the simulated null hypothesis. Greater corresponds to testing only for overdispersion. It is recommended to keep the default setting (testing for both over and underdispersion) @@ -381,13 +381,13 @@ testCategorical <- function(simulationOutput, catPred, quantiles = c(0.25, 0.5, #' #' **Simulation-based dispersion tests (type == "DHARMa")** #' -#' If type = "DHARMa" (default and recommended), simulation-based dispersion tests are performed. Their behavior differs depending on whether simulations are done with refit = F, or refit = T +#' If type = "DHARMa" (default and recommended), simulation-based dispersion tests are performed. Their behavior differs depending on whether simulations are done with refit = FALSE, or refit = T #' #' #' **Important:** for either refit = T or F, the results of type = "DHARMa" dispersion test will differ depending on whether simulations are done conditional (= conditional on fitted random effects) or unconditional (= REs are re-simulated). How to change between conditional or unconditional simulations is discussed in \code{\link{simulateResiduals}}. The general default in DHARMa is to use unconditional simulations, because this has advantages in other situations, but dispersion tests for models with strong REs specifically may increase substantially in power / sensitivity when switching to conditional simulations. I therefore recommend checking dispersion with conditional simulations if supported by the used regression package. #' -#' If refit = F, the function uses \code{\link{testGeneric}} to compare the variance of the observed raw residuals (i.e. var(observed - predicted), displayed as a red line) against the variance of the simulated residuals (i.e. var(simulated - predicted), histogram). The variances are scaled to the mean simulated variance. A significant ratio > 1 indicates overdispersion, a significant ratio < 1 underdispersion. +#' If refit = FALSE, the function uses \code{\link{testGeneric}} to compare the variance of the observed raw residuals (i.e. var(observed - predicted), displayed as a red line) against the variance of the simulated residuals (i.e. var(simulated - predicted), histogram). The variances are scaled to the mean simulated variance. A significant ratio > 1 indicates overdispersion, a significant ratio < 1 underdispersion. #' -#' If refit = T, the function compares the approximate deviance (via squared pearson residuals) with the same quantity from the models refitted with simulated data. Applying this is much slower than the previous alternative. Given the computational cost, I would suggest that most users will be satisfied with the standard dispersion test. +#' If refit = TRUE, the function compares the approximate deviance (via squared pearson residuals) with the same quantity from the models refitted with simulated data. Applying this is much slower than the previous alternative. Given the computational cost, I would suggest that most users will be satisfied with the standard dispersion test. #' #' ** Analytical dispersion tests (type == "PearsonChisq")** #' @@ -401,7 +401,7 @@ testCategorical <- function(simulationOutput, catPred, quantiles = c(0.25, 0.5, #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @example inst/examples/testDispersionHelp.R #' @export -testDispersion <- function(simulationOutput, alternative = c("two.sided", "greater", "less"), plot = T, type = c("DHARMa", "PearsonChisq"), ...){ +testDispersion <- function(simulationOutput, alternative = c("two.sided", "greater", "less"), plot = TRUE, type = c("DHARMa", "PearsonChisq"), ...){ alternative <- match.arg(alternative) type <- match.arg(type) @@ -417,7 +417,7 @@ testDispersion <- function(simulationOutput, alternative = c("two.sided", "great # if(!"re.form" %in% names(simulationOutput$additionalParameters) & is.null(simulationOutput$additionalParameters$re.form)) message("recommended to run conditional simulations for dispersion test, see help") #} - if(simulationOutput$refit == F){ + if(simulationOutput$refit == FALSE){ expectedVar = sd(simulationOutput$simulatedResponse)^2 spread <- function(x) var(x - simulationOutput$fittedPredictedResponse) / expectedVar @@ -441,7 +441,7 @@ testDispersion <- function(simulationOutput, alternative = c("two.sided", "great out$p.value = p class(out) = "htest" - if(plot == T) { + if(plot == TRUE) { #plotTitle = gsub('(.{1,50})(\\s|$)', '\\1\n', out$method) xLabel = paste("Simulated values, red line = fitted model. p-value (",out$alternative, ") = ", out$p.value, sep ="") @@ -561,7 +561,7 @@ testZeroInflation <- function(simulationOutput, ...){ #' @author Florian Hartig #' @example inst/examples/testsHelp.R #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} -testGeneric <- function(simulationOutput, summary, alternative = c("two.sided", "greater", "less"), plot = T, methodName = "DHARMa generic simulation test"){ +testGeneric <- function(simulationOutput, summary, alternative = c("two.sided", "greater", "less"), plot = TRUE, methodName = "DHARMa generic simulation test"){ out = list() out$data.name = deparse(substitute(simulationOutput)) @@ -584,7 +584,7 @@ testGeneric <- function(simulationOutput, summary, alternative = c("two.sided", class(out) = "htest" - if(plot == T) { + if(plot == TRUE) { plotTitle = gsub('(.{1,50})(\\s|$)', '\\1\n', methodName) xLabel = paste("Simulated values, red line = fitted model. p-value (",out$alternative, ") = ", out$p.value, sep ="") hist(simulated, xlim = range(simulated, observed, na.rm=T ), col = "lightgrey", main = plotTitle, xlab = xLabel, breaks = max(round(simulationOutput$nSim / 5), 20), cex.main = 0.8) @@ -624,9 +624,9 @@ testGeneric <- function(simulationOutput, summary, alternative = c("two.sided", #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @example inst/examples/testTemporalAutocorrelationHelp.R #' @export -testTemporalAutocorrelation <- function(simulationOutput, time, alternative = c("two.sided", "greater", "less"), plot = T){ +testTemporalAutocorrelation <- function(simulationOutput, time, alternative = c("two.sided", "greater", "less"), plot = TRUE){ - simulationOutput = ensureDHARMa(simulationOutput, convert = T) + simulationOutput = ensureDHARMa(simulationOutput, convert = TRUE) # actually not sure if this is neccessary for dwtest, but seems better to aggregate if(any(duplicated(time))) stop("testing for temporal autocorrelation requires unique time values - if you have several observations per time value, either use the recalculateResiduals function to aggregate residuals per time step, or extract the residuals from the fitted object, and plot / test each of them independently for temporally repeated subgroups (typical choices would be location / subject etc.). Note that the latter must be done by hand, outside testTemporalAutocorrelation.") @@ -643,7 +643,7 @@ testTemporalAutocorrelation <- function(simulationOutput, time, alternative = c( out = lmtest::dwtest(simulationOutput$scaledResiduals ~ 1, order.by = time, alternative = alternative) - if(plot == T) { + if(plot == TRUE) { oldpar <- par(mfrow = c(1,2)) on.exit(par(oldpar)) @@ -677,7 +677,7 @@ testTemporalAutocorrelation <- function(simulationOutput, time, alternative = c( #' @param plot whether to plot output #' @details The function performs Moran.I test from the package ape on the DHARMa residuals. If a distance matrix (distMat) is provided, calculations will be based on this distance matrix, and x,y coordinates will only used for the plotting (if provided). If distMat is not provided, the function will calculate the euclidean distances between x,y coordinates, and test Moran.I based on these distances. #' -#' If plot = T, a plot will be produced showing each residual with at its x,y position, colored according to the residual value. Residuals with 0.5 are colored white, everything below 0.5 is colored increasinly red, everything above 0.5 is colored increasingly blue. +#' If plot = TRUE, a plot will be produced showing each residual with at its x,y position, colored according to the residual value. Residuals with 0.5 are colored white, everything below 0.5 is colored increasinly red, everything above 0.5 is colored increasingly blue. #' #' Testing for spatial autocorrelation requires unique x,y values - if you have several observations per location, either use the recalculateResiduals function to aggregate residuals per location, or extract the residuals from the fitted object, and plot / test each of them independently for spatially repeated subgroups (a typical scenario would repeated spatial observation, in which case one could plot / test each time step separately for temporal autocorrelation). Note that the latter must be done by hand, outside testSpatialAutocorrelation. #' @@ -700,11 +700,11 @@ testTemporalAutocorrelation <- function(simulationOutput, time, alternative = c( #' @import grDevices #' @example inst/examples/testSpatialAutocorrelationHelp.R #' @export -testSpatialAutocorrelation <- function(simulationOutput, x = NULL, y = NULL, distMat = NULL, alternative = c("two.sided", "greater", "less"), plot = T){ +testSpatialAutocorrelation <- function(simulationOutput, x = NULL, y = NULL, distMat = NULL, alternative = c("two.sided", "greater", "less"), plot = TRUE){ alternative <- match.arg(alternative) data.name = deparse(substitute(simulationOutput)) # needs to be before ensureDHARMa - simulationOutput = ensureDHARMa(simulationOutput, convert = T) + simulationOutput = ensureDHARMa(simulationOutput, convert = TRUE) # Assertions @@ -750,13 +750,13 @@ testSpatialAutocorrelation <- function(simulationOutput, x = NULL, y = NULL, di } -getP <- function(simulated, observed, alternative, plot = F, ...){ +getP <- function(simulated, observed, alternative, plot = FALSE, ...){ if(alternative == "greater") p = mean(simulated >= observed) if(alternative == "less") p = mean(simulated <= observed) if(alternative == "two.sided") p = min(min(mean(simulated <= observed), mean(simulated >= observed) ) * 2,1) - if(plot == T){ + if(plot == TRUE){ hist(simulated, xlim = range(simulated, observed), col = "lightgrey", main = "Distribution of test statistic \n grey = simulated, red = observed", ...) abline(v = mean(simulated), col = 1, lwd = 2) abline(v = observed, col = "red", lwd = 2) diff --git a/DHARMa/inst/examples/checkModelHelp.R b/DHARMa/inst/examples/checkModelHelp.R index 2e720212..1dae3ac0 100644 --- a/DHARMa/inst/examples/checkModelHelp.R +++ b/DHARMa/inst/examples/checkModelHelp.R @@ -1,5 +1,5 @@ -testData = createData(sampleSize = 200, overdispersion = 0.5, randomEffectVariance = 0.5, family = gaussian(), hasNA = T) +testData = createData(sampleSize = 200, overdispersion = 0.5, randomEffectVariance = 0.5, family = gaussian(), hasNA = TRUE) fittedModel <- lm(observedResponse ~ Environment1 , data = testData) res <- simulateResiduals(fittedModel) # throws NA message diff --git a/DHARMa/inst/examples/plotsHelp.R b/DHARMa/inst/examples/plotsHelp.R index 0eb0043d..07facf80 100644 --- a/DHARMa/inst/examples/plotsHelp.R +++ b/DHARMa/inst/examples/plotsHelp.R @@ -32,7 +32,7 @@ plotResiduals(simulationOutput, rank = TRUE, quantreg = FALSE, smoothScatter = T plotResiduals(simulationOutput, form = testData$Environment1, quantreg = FALSE) -# if pred is a factor, or if asFactor = T, will produce a boxplot +# if pred is a factor, or if asFactor = TRUE, will produce a boxplot plotResiduals(simulationOutput, form = testData$group) # to diagnose overdispersion and heteroskedasticity it can be useful to diff --git a/DHARMa/inst/examples/runBenchmarksHelp.R b/DHARMa/inst/examples/runBenchmarksHelp.R index dd119f48..0e073bb0 100644 --- a/DHARMa/inst/examples/runBenchmarksHelp.R +++ b/DHARMa/inst/examples/runBenchmarksHelp.R @@ -32,8 +32,8 @@ out = runBenchmarks(returnStatistics, controlValues = c(0,0.5,1), nRep = 10, par plot.DHARMaBenchmark <- function(x, ...){ if(length(x$controlValues)== 1){ - vioplot::vioplot(x$simulations[,x$nSummaries:1], las = 2, horizontal = T, side = "right", - areaEqual = F, + vioplot::vioplot(x$simulations[,x$nSummaries:1], las = 2, horizontal = TRUE, side = "right", + areaEqual = FALSE, main = "p distribution under H0", ylim = c(-0.15,1), ...) abline(v = 1, lty = 2) diff --git a/DHARMa/inst/examples/simulateLRTHelp.R b/DHARMa/inst/examples/simulateLRTHelp.R index 045fbda6..a39fe6da 100644 --- a/DHARMa/inst/examples/simulateLRTHelp.R +++ b/DHARMa/inst/examples/simulateLRTHelp.R @@ -15,7 +15,7 @@ m0 = glm(observedResponse ~ Environment1 , data = dat, family = "poisson") out = simulateLRT(m0, m1, n = 10) # To inspect warnings thrown during the refits: -out = simulateLRT(m0, m1, saveModels = T, suppressWarnings = F, n = 10) +out = simulateLRT(m0, m1, saveModels = TRUE, suppressWarnings = FALSE, n = 10) summary(out$saveModels[[2]]$refittedM1) # RE SD = 0, no problem # If there are warnings that seem problematic, # could try changing the optimizer or iterations diff --git a/DHARMa/inst/examples/testDispersionHelp.R b/DHARMa/inst/examples/testDispersionHelp.R index 1232ea1b..0efd8e75 100644 --- a/DHARMa/inst/examples/testDispersionHelp.R +++ b/DHARMa/inst/examples/testDispersionHelp.R @@ -22,7 +22,7 @@ testDispersion(simulationOutput) # of RE groups. In doubt, only test for overdispersion testDispersion(simulationOutput, type = "PearsonChisq", alternative = "greater") -# if refit = T, a different test on simulated Pearson residuals will calculated (see help) +# if refit = TRUE, a different test on simulated Pearson residuals will calculated (see help) simulationOutput2 <- simulateResiduals(fittedModel = fittedModel, refit = TRUE, seed = 12, n = 20) testDispersion(simulationOutput2) From 977093e4d5b333bde460f069766cf91e5f8c4120 Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 7 Jun 2024 11:10:12 -0400 Subject: [PATCH 07/10] Update docs with roxygen2 --- DHARMa/man/createDHARMa.Rd | 2 +- DHARMa/man/createData.Rd | 2 +- DHARMa/man/ensureDHARMa.Rd | 2 +- DHARMa/man/hist.DHARMa.Rd | 2 +- DHARMa/man/plot.DHARMa.Rd | 4 +- DHARMa/man/plotQQunif.Rd | 6 +-- DHARMa/man/plotResiduals.Rd | 8 ++-- DHARMa/man/runBenchmarks.Rd | 6 +-- DHARMa/man/simulateLRT.Rd | 6 +-- DHARMa/man/simulateResiduals.Rd | 6 +-- DHARMa/man/testCategorical.Rd | 2 +- DHARMa/man/testDispersion.Rd | 12 ++--- DHARMa/man/testGeneric.Rd | 2 +- DHARMa/man/testOutliers.Rd | 2 +- DHARMa/man/testPDistribution.Rd | 2 +- DHARMa/man/testQuantiles.Rd | 2 +- DHARMa/man/testResiduals.Rd | 2 +- DHARMa/man/testSpatialAutocorrelation.Rd | 4 +- DHARMa/man/testTemporalAutocorrelation.Rd | 53 ++++++++++++++++++++--- DHARMa/man/testUniformity.Rd | 2 +- 20 files changed, 84 insertions(+), 43 deletions(-) diff --git a/DHARMa/man/createDHARMa.Rd b/DHARMa/man/createDHARMa.Rd index 0386599c..4a5cb998 100644 --- a/DHARMa/man/createDHARMa.Rd +++ b/DHARMa/man/createDHARMa.Rd @@ -5,7 +5,7 @@ \title{Create a DHARMa object from hand-coded simulations or Bayesian posterior predictive simulations} \usage{ createDHARMa(simulatedResponse, observedResponse, - fittedPredictedResponse = NULL, integerResponse = F, seed = 123, + fittedPredictedResponse = NULL, integerResponse = FALSE, seed = 123, method = c("PIT", "traditional"), rotation = NULL) } \arguments{ diff --git a/DHARMa/man/createData.Rd b/DHARMa/man/createData.Rd index 7c96e5b0..b963b165 100644 --- a/DHARMa/man/createData.Rd +++ b/DHARMa/man/createData.Rd @@ -9,7 +9,7 @@ createData(sampleSize = 100, intercept = 0, fixedEffects = 1, overdispersion = 0, family = poisson(), scale = 1, cor = 0, roundPoissonVariance = NULL, pZeroInflation = 0, binomialTrials = 1, temporalAutocorrelation = 0, spatialAutocorrelation = 0, - factorResponse = F, replicates = 1, hasNA = F) + factorResponse = FALSE, replicates = 1, hasNA = FALSE) } \arguments{ \item{sampleSize}{sample size of the dataset} diff --git a/DHARMa/man/ensureDHARMa.Rd b/DHARMa/man/ensureDHARMa.Rd index 9e134610..c3c9b105 100644 --- a/DHARMa/man/ensureDHARMa.Rd +++ b/DHARMa/man/ensureDHARMa.Rd @@ -4,7 +4,7 @@ \alias{ensureDHARMa} \title{Ensures that an object is of class DHARMa} \usage{ -ensureDHARMa(simulationOutput, convert = F) +ensureDHARMa(simulationOutput, convert = FALSE) } \arguments{ \item{simulationOutput}{a DHARMa simulation output or an object that can be converted into a DHARMa simulation output} diff --git a/DHARMa/man/hist.DHARMa.Rd b/DHARMa/man/hist.DHARMa.Rd index e20a6734..baf83604 100644 --- a/DHARMa/man/hist.DHARMa.Rd +++ b/DHARMa/man/hist.DHARMa.Rd @@ -62,7 +62,7 @@ plotResiduals(simulationOutput, rank = TRUE, quantreg = FALSE, smoothScatter = T plotResiduals(simulationOutput, form = testData$Environment1, quantreg = FALSE) -# if pred is a factor, or if asFactor = T, will produce a boxplot +# if pred is a factor, or if asFactor = TRUE, will produce a boxplot plotResiduals(simulationOutput, form = testData$group) # to diagnose overdispersion and heteroskedasticity it can be useful to diff --git a/DHARMa/man/plot.DHARMa.Rd b/DHARMa/man/plot.DHARMa.Rd index 065ae9d9..89adb215 100644 --- a/DHARMa/man/plot.DHARMa.Rd +++ b/DHARMa/man/plot.DHARMa.Rd @@ -9,7 +9,7 @@ \arguments{ \item{x}{an object of class DHARMa with simulated residuals created by \code{\link{simulateResiduals}}} -\item{title}{The title for both panels (plotted via mtext, outer = T)} +\item{title}{The title for both panels (plotted via mtext, outer = TRUE)} \item{...}{further options for \code{\link{plotResiduals}}. Consider in particular parameters quantreg, rank and asFactor. xlab, ylab and main cannot be changed when using plot.DHARMa, but can be changed when using plotResiduals.} } @@ -64,7 +64,7 @@ plotResiduals(simulationOutput, rank = TRUE, quantreg = FALSE, smoothScatter = T plotResiduals(simulationOutput, form = testData$Environment1, quantreg = FALSE) -# if pred is a factor, or if asFactor = T, will produce a boxplot +# if pred is a factor, or if asFactor = TRUE, will produce a boxplot plotResiduals(simulationOutput, form = testData$group) # to diagnose overdispersion and heteroskedasticity it can be useful to diff --git a/DHARMa/man/plotQQunif.Rd b/DHARMa/man/plotQQunif.Rd index 4f1205af..81483648 100644 --- a/DHARMa/man/plotQQunif.Rd +++ b/DHARMa/man/plotQQunif.Rd @@ -4,8 +4,8 @@ \alias{plotQQunif} \title{Quantile-quantile plot for a uniform distribution} \usage{ -plotQQunif(simulationOutput, testUniformity = T, testOutliers = T, - testDispersion = T, ...) +plotQQunif(simulationOutput, testUniformity = TRUE, testOutliers = TRUE, + testDispersion = TRUE, ...) } \arguments{ \item{simulationOutput}{a DHARMa simulation output (class DHARMa)} @@ -59,7 +59,7 @@ plotResiduals(simulationOutput, rank = TRUE, quantreg = FALSE, smoothScatter = T plotResiduals(simulationOutput, form = testData$Environment1, quantreg = FALSE) -# if pred is a factor, or if asFactor = T, will produce a boxplot +# if pred is a factor, or if asFactor = TRUE, will produce a boxplot plotResiduals(simulationOutput, form = testData$group) # to diagnose overdispersion and heteroskedasticity it can be useful to diff --git a/DHARMa/man/plotResiduals.Rd b/DHARMa/man/plotResiduals.Rd index b2ffa7f3..45e9cf7a 100644 --- a/DHARMa/man/plotResiduals.Rd +++ b/DHARMa/man/plotResiduals.Rd @@ -4,9 +4,9 @@ \alias{plotResiduals} \title{Generic res ~ pred scatter plot with spline or quantile regression on top} \usage{ -plotResiduals(simulationOutput, form = NULL, quantreg = NULL, rank = T, - asFactor = NULL, smoothScatter = NULL, quantiles = c(0.25, 0.5, 0.75), - absoluteDeviation = FALSE, ...) +plotResiduals(simulationOutput, form = NULL, quantreg = NULL, + rank = TRUE, asFactor = NULL, smoothScatter = NULL, + quantiles = c(0.25, 0.5, 0.75), absoluteDeviation = FALSE, ...) } \arguments{ \item{simulationOutput}{on object, usually a DHARMa object, from which residual values can be extracted. Alternatively, a vector with residuals or a fitted model can be provided, which will then be transformed into a DHARMa object.} @@ -84,7 +84,7 @@ plotResiduals(simulationOutput, rank = TRUE, quantreg = FALSE, smoothScatter = T plotResiduals(simulationOutput, form = testData$Environment1, quantreg = FALSE) -# if pred is a factor, or if asFactor = T, will produce a boxplot +# if pred is a factor, or if asFactor = TRUE, will produce a boxplot plotResiduals(simulationOutput, form = testData$group) # to diagnose overdispersion and heteroskedasticity it can be useful to diff --git a/DHARMa/man/runBenchmarks.Rd b/DHARMa/man/runBenchmarks.Rd index 1ac1aa00..9775a204 100644 --- a/DHARMa/man/runBenchmarks.Rd +++ b/DHARMa/man/runBenchmarks.Rd @@ -5,7 +5,7 @@ \title{Benchmark calculations} \usage{ runBenchmarks(calculateStatistics, controlValues = NULL, nRep = 10, - alpha = 0.05, parallel = FALSE, exportGlobal = F, ...) + alpha = 0.05, parallel = FALSE, exportGlobal = FALSE, ...) } \arguments{ \item{calculateStatistics}{the statistics to be benchmarked. Should return one value, or a vector of values. If controlValues are given, must accept a parameter control} @@ -66,8 +66,8 @@ out = runBenchmarks(returnStatistics, controlValues = c(0,0.5,1), nRep = 10, par plot.DHARMaBenchmark <- function(x, ...){ if(length(x$controlValues)== 1){ - vioplot::vioplot(x$simulations[,x$nSummaries:1], las = 2, horizontal = T, side = "right", - areaEqual = F, + vioplot::vioplot(x$simulations[,x$nSummaries:1], las = 2, horizontal = TRUE, side = "right", + areaEqual = FALSE, main = "p distribution under H0", ylim = c(-0.15,1), ...) abline(v = 1, lty = 2) diff --git a/DHARMa/man/simulateLRT.Rd b/DHARMa/man/simulateLRT.Rd index f72321a8..b989505d 100644 --- a/DHARMa/man/simulateLRT.Rd +++ b/DHARMa/man/simulateLRT.Rd @@ -4,8 +4,8 @@ \alias{simulateLRT} \title{Simulated likelihood ratio tests for (generalized) linear mixed models} \usage{ -simulateLRT(m0, m1, n = 250, seed = 123, plot = T, - suppressWarnings = T, saveModels = F, ...) +simulateLRT(m0, m1, n = 250, seed = 123, plot = TRUE, + suppressWarnings = TRUE, saveModels = FALSE, ...) } \arguments{ \item{m0}{Null Model} @@ -65,7 +65,7 @@ m0 = glm(observedResponse ~ Environment1 , data = dat, family = "poisson") out = simulateLRT(m0, m1, n = 10) # To inspect warnings thrown during the refits: -out = simulateLRT(m0, m1, saveModels = T, suppressWarnings = F, n = 10) +out = simulateLRT(m0, m1, saveModels = TRUE, suppressWarnings = FALSE, n = 10) summary(out$saveModels[[2]]$refittedM1) # RE SD = 0, no problem # If there are warnings that seem problematic, # could try changing the optimizer or iterations diff --git a/DHARMa/man/simulateResiduals.Rd b/DHARMa/man/simulateResiduals.Rd index 105202c5..b08a261d 100644 --- a/DHARMa/man/simulateResiduals.Rd +++ b/DHARMa/man/simulateResiduals.Rd @@ -4,8 +4,8 @@ \alias{simulateResiduals} \title{Create simulated residuals} \usage{ -simulateResiduals(fittedModel, n = 250, refit = F, - integerResponse = NULL, plot = F, seed = 123, method = c("PIT", +simulateResiduals(fittedModel, n = 250, refit = FALSE, + integerResponse = NULL, plot = FALSE, seed = 123, method = c("PIT", "traditional"), rotation = NULL, ...) } \arguments{ @@ -21,7 +21,7 @@ simulateResiduals(fittedModel, n = 250, refit = F, \item{seed}{the random seed to be used within DHARMa. The default setting, recommended for most users, is keep the random seed on a fixed value 123. This means that you will always get the same randomization and thus the same result when running the same code. NULL = no new seed is set, but previous random state will be restored after simulation. FALSE = no seed is set, and random state will not be restored. The latter two options are only recommended for simulation experiments. See vignette for details.} -\item{method}{for refit = F, the quantile randomization method used. The two options implemented at the moment are probability integral transform (PIT-) residuals (current default), and the "traditional" randomization procedure, that was used in DHARMa until version 0.3.0. Refit = T will always use "traditional", respectively of the value of method. For details, see \code{\link{getQuantile}}} +\item{method}{for refit = FALSE, the quantile randomization method used. The two options implemented at the moment are probability integral transform (PIT-) residuals (current default), and the "traditional" randomization procedure, that was used in DHARMa until version 0.3.0. Refit = T will always use "traditional", respectively of the value of method. For details, see \code{\link{getQuantile}}} \item{rotation}{optional rotation of the residual space prior to calculating the quantile residuals. The main purpose of this is to account for residual covariance as created by temporal or spatial residual autocorrelation. See details below, section \emph{residual auto-correlation} ass well as the help of \link{getQuantile} and, for a practical example, \link{testTemporalAutocorrelation}.} diff --git a/DHARMa/man/testCategorical.Rd b/DHARMa/man/testCategorical.Rd index 4ed2d09c..7c82a42d 100644 --- a/DHARMa/man/testCategorical.Rd +++ b/DHARMa/man/testCategorical.Rd @@ -5,7 +5,7 @@ \title{Test for categorical dependencies} \usage{ testCategorical(simulationOutput, catPred, quantiles = c(0.25, 0.5, 0.75), - plot = T) + plot = TRUE) } \arguments{ \item{simulationOutput}{an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case.} diff --git a/DHARMa/man/testDispersion.Rd b/DHARMa/man/testDispersion.Rd index cd136b5c..d5bf1413 100644 --- a/DHARMa/man/testDispersion.Rd +++ b/DHARMa/man/testDispersion.Rd @@ -5,7 +5,7 @@ \title{DHARMa dispersion tests} \usage{ testDispersion(simulationOutput, alternative = c("two.sided", "greater", - "less"), plot = T, type = c("DHARMa", "PearsonChisq"), ...) + "less"), plot = TRUE, type = c("DHARMa", "PearsonChisq"), ...) } \arguments{ \item{simulationOutput}{an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case.} @@ -19,7 +19,7 @@ testDispersion(simulationOutput, alternative = c("two.sided", "greater", \item{...}{arguments to pass on to \code{\link{testGeneric}}} } \description{ -This function performs simulation-based tests for over/underdispersion. If type = "DHARMa" (default and recommended), simulation-based dispersion tests are performed. Their behavior differs depending on whether simulations are done with refit = F, or refit = T, and whether data is simulated conditional (e.g. re.form ~0 in lme4) (see below). If type = "PearsonChisq", a chi2 test on Pearson residuals is performed. +This function performs simulation-based tests for over/underdispersion. If type = "DHARMa" (default and recommended), simulation-based dispersion tests are performed. Their behavior differs depending on whether simulations are done with refit = FALSE, or refit = TRUE, and whether data is simulated conditional (e.g. re.form ~0 in lme4) (see below). If type = "PearsonChisq", a chi2 test on Pearson residuals is performed. } \details{ Over / underdispersion means that the observed data is more / less dispersed than expected under the fitted model. There is no unique way to test for dispersion problems, and there are a number of different dispersion tests implemented in various R packages. @@ -28,13 +28,13 @@ The testDispersion function implements several dispersion tests: \strong{Simulation-based dispersion tests (type == "DHARMa")} -If type = "DHARMa" (default and recommended), simulation-based dispersion tests are performed. Their behavior differs depending on whether simulations are done with refit = F, or refit = T +If type = "DHARMa" (default and recommended), simulation-based dispersion tests are performed. Their behavior differs depending on whether simulations are done with refit = FALSE, or refit = T #' \strong{Important:} for either refit = T or F, the results of type = "DHARMa" dispersion test will differ depending on whether simulations are done conditional (= conditional on fitted random effects) or unconditional (= REs are re-simulated). How to change between conditional or unconditional simulations is discussed in \code{\link{simulateResiduals}}. The general default in DHARMa is to use unconditional simulations, because this has advantages in other situations, but dispersion tests for models with strong REs specifically may increase substantially in power / sensitivity when switching to conditional simulations. I therefore recommend checking dispersion with conditional simulations if supported by the used regression package. -If refit = F, the function uses \code{\link{testGeneric}} to compare the variance of the observed raw residuals (i.e. var(observed - predicted), displayed as a red line) against the variance of the simulated residuals (i.e. var(simulated - predicted), histogram). The variances are scaled to the mean simulated variance. A significant ratio > 1 indicates overdispersion, a significant ratio < 1 underdispersion. +If refit = FALSE, the function uses \code{\link{testGeneric}} to compare the variance of the observed raw residuals (i.e. var(observed - predicted), displayed as a red line) against the variance of the simulated residuals (i.e. var(simulated - predicted), histogram). The variances are scaled to the mean simulated variance. A significant ratio > 1 indicates overdispersion, a significant ratio < 1 underdispersion. -If refit = T, the function compares the approximate deviance (via squared pearson residuals) with the same quantity from the models refitted with simulated data. Applying this is much slower than the previous alternative. Given the computational cost, I would suggest that most users will be satisfied with the standard dispersion test. +If refit = TRUE, the function compares the approximate deviance (via squared pearson residuals) with the same quantity from the models refitted with simulated data. Applying this is much slower than the previous alternative. Given the computational cost, I would suggest that most users will be satisfied with the standard dispersion test. ** Analytical dispersion tests (type == "PearsonChisq")** @@ -70,7 +70,7 @@ testDispersion(simulationOutput) # of RE groups. In doubt, only test for overdispersion testDispersion(simulationOutput, type = "PearsonChisq", alternative = "greater") -# if refit = T, a different test on simulated Pearson residuals will calculated (see help) +# if refit = TRUE, a different test on simulated Pearson residuals will calculated (see help) simulationOutput2 <- simulateResiduals(fittedModel = fittedModel, refit = TRUE, seed = 12, n = 20) testDispersion(simulationOutput2) diff --git a/DHARMa/man/testGeneric.Rd b/DHARMa/man/testGeneric.Rd index eb1c5d5e..52a727e7 100644 --- a/DHARMa/man/testGeneric.Rd +++ b/DHARMa/man/testGeneric.Rd @@ -5,7 +5,7 @@ \title{Test for a generic summary statistic based on simulated data} \usage{ testGeneric(simulationOutput, summary, alternative = c("two.sided", - "greater", "less"), plot = T, + "greater", "less"), plot = TRUE, methodName = "DHARMa generic simulation test") } \arguments{ diff --git a/DHARMa/man/testOutliers.Rd b/DHARMa/man/testOutliers.Rd index 3a897585..3f5bfeb3 100644 --- a/DHARMa/man/testOutliers.Rd +++ b/DHARMa/man/testOutliers.Rd @@ -6,7 +6,7 @@ \usage{ testOutliers(simulationOutput, alternative = c("two.sided", "greater", "less"), margin = c("both", "upper", "lower"), type = c("default", - "bootstrap", "binomial"), nBoot = 100, plot = T) + "bootstrap", "binomial"), nBoot = 100, plot = TRUE) } \arguments{ \item{simulationOutput}{an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case.} diff --git a/DHARMa/man/testPDistribution.Rd b/DHARMa/man/testPDistribution.Rd index 74b5fb74..9725a9ea 100644 --- a/DHARMa/man/testPDistribution.Rd +++ b/DHARMa/man/testPDistribution.Rd @@ -4,7 +4,7 @@ \alias{testPDistribution} \title{Plot distribution of p-values} \usage{ -testPDistribution(x, plot = T, +testPDistribution(x, plot = TRUE, main = "p distribution \\n expected is flat at 1", ...) } \arguments{ diff --git a/DHARMa/man/testQuantiles.Rd b/DHARMa/man/testQuantiles.Rd index 980f35b7..2c3f5818 100644 --- a/DHARMa/man/testQuantiles.Rd +++ b/DHARMa/man/testQuantiles.Rd @@ -5,7 +5,7 @@ \title{Test for quantiles} \usage{ testQuantiles(simulationOutput, predictor = NULL, quantiles = c(0.25, 0.5, - 0.75), plot = T) + 0.75), plot = TRUE) } \arguments{ \item{simulationOutput}{an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case.} diff --git a/DHARMa/man/testResiduals.Rd b/DHARMa/man/testResiduals.Rd index 2b288aac..394bef7f 100644 --- a/DHARMa/man/testResiduals.Rd +++ b/DHARMa/man/testResiduals.Rd @@ -4,7 +4,7 @@ \alias{testResiduals} \title{DHARMa general residual test} \usage{ -testResiduals(simulationOutput, plot = T) +testResiduals(simulationOutput, plot = TRUE) } \arguments{ \item{simulationOutput}{an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case.} diff --git a/DHARMa/man/testSpatialAutocorrelation.Rd b/DHARMa/man/testSpatialAutocorrelation.Rd index 2730f7c6..453b999c 100644 --- a/DHARMa/man/testSpatialAutocorrelation.Rd +++ b/DHARMa/man/testSpatialAutocorrelation.Rd @@ -6,7 +6,7 @@ \usage{ testSpatialAutocorrelation(simulationOutput, x = NULL, y = NULL, distMat = NULL, alternative = c("two.sided", "greater", "less"), - plot = T) + plot = TRUE) } \arguments{ \item{simulationOutput}{an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case.} @@ -27,7 +27,7 @@ This function performs a Moran's I test for distance-based (spatial, phylogeneti \details{ The function performs Moran.I test from the package ape on the DHARMa residuals. If a distance matrix (distMat) is provided, calculations will be based on this distance matrix, and x,y coordinates will only used for the plotting (if provided). If distMat is not provided, the function will calculate the euclidean distances between x,y coordinates, and test Moran.I based on these distances. -If plot = T, a plot will be produced showing each residual with at its x,y position, colored according to the residual value. Residuals with 0.5 are colored white, everything below 0.5 is colored increasinly red, everything above 0.5 is colored increasingly blue. +If plot = TRUE, a plot will be produced showing each residual with at its x,y position, colored according to the residual value. Residuals with 0.5 are colored white, everything below 0.5 is colored increasinly red, everything above 0.5 is colored increasingly blue. Testing for spatial autocorrelation requires unique x,y values - if you have several observations per location, either use the recalculateResiduals function to aggregate residuals per location, or extract the residuals from the fitted object, and plot / test each of them independently for spatially repeated subgroups (a typical scenario would repeated spatial observation, in which case one could plot / test each time step separately for temporal autocorrelation). Note that the latter must be done by hand, outside testSpatialAutocorrelation. } diff --git a/DHARMa/man/testTemporalAutocorrelation.Rd b/DHARMa/man/testTemporalAutocorrelation.Rd index a26dd736..4c4869bd 100644 --- a/DHARMa/man/testTemporalAutocorrelation.Rd +++ b/DHARMa/man/testTemporalAutocorrelation.Rd @@ -5,7 +5,7 @@ \title{Test for temporal autocorrelation} \usage{ testTemporalAutocorrelation(simulationOutput, time, - alternative = c("two.sided", "greater", "less"), plot = T) + alternative = c("two.sided", "greater", "less"), plot = TRUE) } \arguments{ \item{simulationOutput}{an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case.} @@ -94,18 +94,32 @@ testTemporalAutocorrelation(res, time = unique(testData$time)) group <- factor(rep(1,n*5)) dat0 <- data.frame(y,times,group) - # fit model + # fit model / would be similar for nlme::gls and similar models model = glmmTMB(y ~ ar1(times + 0 | group), data=dat0) - # Standard residuals show spurious problems because of autocorrelation + # Note that standard residuals still show problems because of autocorrelation res <- simulateResiduals(model) plot(res) - - # grouped according to times, unrotated shows autocorrelation + + # The reason is that most (if not all) autoregressive models treat the + # autocorrelated error as random, i.e. the autocorrelated error structure + # is not used for making predictions. If you then make predictions based + # on the fixed effects and calculate residuals, the autocorrelation in the + # residuals remains. We can see this if we again calculate the auto- + # correlation test + res2 <- recalculateResiduals(res, group=dat0$times) testTemporalAutocorrelation(res2, time = 1:length(res2$scaledResiduals)) - # extract estimated AR1 covariance + # so, how can we check then if the current model correctly removed the + # autocorrelation? + + # Option 1: rotate the residuals in the direction of the autocorrelation + # to make the independent. Note that this only works perfectly for gls + # type models as nonlinear link function make the residuals covariance + # different from a multivariate normal distribution + + # this can be either done by extracting estimated AR1 covariance cov <- VarCorr(model) cov <- cov$cond$group # extract covariance matrix of REs @@ -113,6 +127,33 @@ testTemporalAutocorrelation(res, time = unique(testData$time)) res3 <- recalculateResiduals(res, group=dat0$times, rotation=cov) plot(res3) testTemporalAutocorrelation(res3, time = 1:length(res2$scaledResiduals)) + + # alternatively, you can let DHARMa estimate the covariance from the + # simulations + + res4 <- recalculateResiduals(res, group=dat0$times, rotation="estimated") + plot(res4) + testTemporalAutocorrelation(res3, time = 1:length(res2$scaledResiduals)) + + # Alternatively, in glmmTMB, we can condition on the estimated correlated + # residuals. Unfortunately, in this case, we will have to do simulations by + # hand as glmmTMB does not allow to simulate conditional on a fitted + # correlation structure + + # re.form = NULL creates predictions conditional on the fitted temporally + # autocorreated REs + pred = predict(model, re.form = NULL) + + # now we simulate data, conditional on the autocorrelation part, with the + # uncorrelated residual error + simulations = sapply(1:250, function(i) rnorm(length(pred), + pred, + summary(model)$sigma)) + res5 = createDHARMa(simulations, dat0$y, pred) + plot(res5) + + res5b <- recalculateResiduals(res5, group=dat0$times) + testTemporalAutocorrelation(res5b, time = 1:length(res5b$scaledResiduals)) # Poisson error # note that for GLMMs, covariances will be estimated at the scale of the diff --git a/DHARMa/man/testUniformity.Rd b/DHARMa/man/testUniformity.Rd index 36fe4754..be366fd4 100644 --- a/DHARMa/man/testUniformity.Rd +++ b/DHARMa/man/testUniformity.Rd @@ -5,7 +5,7 @@ \title{Test for overall uniformity} \usage{ testUniformity(simulationOutput, alternative = c("two.sided", "less", - "greater"), plot = T) + "greater"), plot = TRUE) } \arguments{ \item{simulationOutput}{an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case.} From 2dd8488f15ec5b6642123139b7ce394f7d4c4cb4 Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Fri, 7 Jun 2024 11:33:59 -0400 Subject: [PATCH 08/10] No need to set min Matrix version now? --- DHARMa/DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DHARMa/DESCRIPTION b/DHARMa/DESCRIPTION index c680b689..4356f4f9 100644 --- a/DHARMa/DESCRIPTION +++ b/DHARMa/DESCRIPTION @@ -21,7 +21,7 @@ Imports: graphics, utils, grDevices, - Matrix (>= 1.6-2), + Matrix, parallel, gap, lmtest, From bde49a06f0ba7e5c0b61ae694ceb36ebb4735206 Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Wed, 26 Jun 2024 09:54:01 -0400 Subject: [PATCH 09/10] Use alphanumeric version --- DHARMa/DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DHARMa/DESCRIPTION b/DHARMa/DESCRIPTION index bbd05125..99e7706d 100644 --- a/DHARMa/DESCRIPTION +++ b/DHARMa/DESCRIPTION @@ -1,6 +1,6 @@ Package: DHARMa Title: Residual Diagnostics for Hierarchical (Multi-Level / Mixed) Regression Models -Version: 0.4.6.x +Version: 0.4.6.9000 Date: 2022-09-08 Authors@R: c(person("Florian", "Hartig", email = "florian.hartig@biologie.uni-regensburg.de", role = c("aut", "cre"), comment=c(ORCID="0000-0002-6255-9059")), person("Lukas", "Lohse", role = "ctb")) Description: The 'DHARMa' package uses a simulation-based approach to create From 0c8c061c19cc39ae0b0376f6fab55bb24984575b Mon Sep 17 00:00:00 2001 From: Florian Hartig Date: Fri, 28 Jun 2024 11:08:30 +0200 Subject: [PATCH 10/10] T restored --- DHARMa/R/tests.R | 28 ++++++++++++------------ DHARMa/man/testCategorical.Rd | 2 +- DHARMa/man/testDispersion.Rd | 10 ++++----- DHARMa/man/testGeneric.Rd | 2 +- DHARMa/man/testOutliers.Rd | 2 +- DHARMa/man/testQuantiles.Rd | 2 +- DHARMa/man/testResiduals.Rd | 2 +- DHARMa/man/testSpatialAutocorrelation.Rd | 2 +- DHARMa/man/testUniformity.Rd | 2 +- 9 files changed, 26 insertions(+), 26 deletions(-) diff --git a/DHARMa/R/tests.R b/DHARMa/R/tests.R index 56704733..29af63b3 100644 --- a/DHARMa/R/tests.R +++ b/DHARMa/R/tests.R @@ -5,12 +5,12 @@ #' This function is a wrapper for the various test functions implemented in DHARMa. Currently, this function calls the \code{\link{testUniformity}} and the \code{\link{testDispersion}} functions. All other tests (see list below) have to be called by hand. #' #' @param simulationOutput an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case. -#' @param plot if T, plots functions of the tests are called +#' @param plot if TRUE, plots functions of the tests are called #' @author Florian Hartig #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @example inst/examples/testsHelp.R #' @export -testResiduals <- function(simulationOutput, plot = T){ +testResiduals <- function(simulationOutput, plot = TRUE){ opar = par(mfrow = c(1,3)) on.exit(par(opar)) @@ -42,13 +42,13 @@ testSimulatedResiduals <- function(simulationOutput){ #' #' @param simulationOutput an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case. #' @param alternative a character string specifying whether the test should test if observations are "greater", "less" or "two.sided" compared to the simulated null hypothesis. See \code{\link[stats]{ks.test}} for details -#' @param plot if T, plots calls \code{\link{plotQQunif}} as well +#' @param plot if TRUE, plots calls \code{\link{plotQQunif}} as well #' @details The function applies a \code{\link[stats]{ks.test}} for uniformity on the simulated residuals. #' @author Florian Hartig #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @example inst/examples/testsHelp.R #' @export -testUniformity<- function(simulationOutput, alternative = c("two.sided", "less", "greater"), plot = T){ +testUniformity<- function(simulationOutput, alternative = c("two.sided", "less", "greater"), plot = TRUE){ simulationOutput = ensureDHARMa(simulationOutput, convert = T) @@ -59,7 +59,7 @@ testUniformity<- function(simulationOutput, alternative = c("two.sided", "less", # Experimental -testBivariateUniformity<- function(simulationOutput, alternative = c("two.sided", "less", "greater"), plot = T){ +testBivariateUniformity<- function(simulationOutput, alternative = c("two.sided", "less", "greater"), plot = TRUE){ simulationOutput = ensureDHARMa(simulationOutput, convert = T) @@ -78,7 +78,7 @@ testBivariateUniformity<- function(simulationOutput, alternative = c("two.sided" #' @param simulationOutput an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case. #' @param predictor an optional predictor variable to be used, instead of the predicted response (default) #' @param quantiles the quantiles to be tested -#' @param plot if T, the function will create an additional plot +#' @param plot if TRUE, the function will create an additional plot #' @details The function fits quantile regressions (via package qgam) on the residuals, and compares their location to the expected location (because of the uniform distributionm, the expected location is 0.5 for the 0.5 quantile). #' #' A significant p-value for the splines means the fitted spline deviates from a flat line at the expected location (p-values of intercept and spline are combined via Benjamini & Hochberg adjustment to control the FDR) @@ -89,7 +89,7 @@ testBivariateUniformity<- function(simulationOutput, alternative = c("two.sided" #' @example inst/examples/testQuantilesHelp.R #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @export -testQuantiles <- function(simulationOutput, predictor = NULL, quantiles = c(0.25,0.5,0.75), plot = T){ +testQuantiles <- function(simulationOutput, predictor = NULL, quantiles = c(0.25,0.5,0.75), plot = TRUE){ if(plot == F){ @@ -152,7 +152,7 @@ testQuantiles <- function(simulationOutput, predictor = NULL, quantiles = c(0.25 #' @param margin whether to test for outliers only at the lower, only at the upper, or both sides (default) of the simulated data distribution #' @param type either default, bootstrap or binomial. See details #' @param nBoot number of boostrap replicates. Only used ot type = "bootstrap" -#' @param plot if T, the function will create an additional plot +#' @param plot if TRUE, the function will create an additional plot #' @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. @@ -169,7 +169,7 @@ testQuantiles <- function(simulationOutput, predictor = NULL, quantiles = c(0.25 #' @author Florian Hartig #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @export -testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater", "less"), margin = c("both", "upper", "lower"), type = c("default","bootstrap", "binomial"), nBoot = 100, plot = T){ +testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater", "less"), margin = c("both", "upper", "lower"), type = c("default","bootstrap", "binomial"), nBoot = 100, plot = TRUE){ # check inputs alternative = match.arg(alternative) @@ -319,7 +319,7 @@ testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater #' @param simulationOutput an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case. #' @param catPred a categorical predictor with the same dimensions as the residuals in simulationOutput #' @param quantiles whether to draw the quantile lines. -#' @param plot if T, the function will create an additional plot +#' @param plot if TRUE, the function will create an additional plot #' @details The function tests for two common problems: are residuals within each group distributed according to model assumptions, and is the variance between group heterogeneous. #' #' The test for within-group uniformity is performed via multipe KS-tests, with adjustment of p-values for multiple testing. If the plot is drawn, problematic groups are highlighted in red, and a corresponding message is displayed in the plot. @@ -330,7 +330,7 @@ testOutliers <- function(simulationOutput, alternative = c("two.sided", "greater #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @example inst/examples/testsHelp.R #' @export -testCategorical <- function(simulationOutput, catPred, quantiles = c(0.25, 0.5, 0.75), plot = T){ +testCategorical <- function(simulationOutput, catPred, quantiles = c(0.25, 0.5, 0.75), plot = TRUE){ simulationOutput = ensureDHARMa(simulationOutput, convert = T) @@ -624,7 +624,7 @@ testGeneric <- function(simulationOutput, summary, alternative = c("two.sided", #' @seealso \code{\link{testResiduals}}, \code{\link{testUniformity}}, \code{\link{testOutliers}}, \code{\link{testDispersion}}, \code{\link{testZeroInflation}}, \code{\link{testGeneric}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}, \code{\link{testQuantiles}}, \code{\link{testCategorical}} #' @example inst/examples/testTemporalAutocorrelationHelp.R #' @export -testTemporalAutocorrelation <- function(simulationOutput, time, alternative = c("two.sided", "greater", "less"), plot = T){ +testTemporalAutocorrelation <- function(simulationOutput, time, alternative = c("two.sided", "greater", "less"), plot = TRUE){ simulationOutput = ensureDHARMa(simulationOutput, convert = T) @@ -700,7 +700,7 @@ testTemporalAutocorrelation <- function(simulationOutput, time, alternative = c( #' @import grDevices #' @example inst/examples/testSpatialAutocorrelationHelp.R #' @export -testSpatialAutocorrelation <- function(simulationOutput, x = NULL, y = NULL, distMat = NULL, alternative = c("two.sided", "greater", "less"), plot = T){ +testSpatialAutocorrelation <- function(simulationOutput, x = NULL, y = NULL, distMat = NULL, alternative = c("two.sided", "greater", "less"), plot = TRUE){ alternative <- match.arg(alternative) data.name = deparse(substitute(simulationOutput)) # needs to be before ensureDHARMa @@ -805,7 +805,7 @@ testPhylogeneticAutocorrelation <- function(simulationOutput, -getP <- function(simulated, observed, alternative, plot = F, ...){ +getP <- function(simulated, observed, alternative, plot = FALSE, ...){ if(alternative == "greater") p = mean(simulated >= observed) if(alternative == "less") p = mean(simulated <= observed) diff --git a/DHARMa/man/testCategorical.Rd b/DHARMa/man/testCategorical.Rd index 7c82a42d..5e0f8e45 100644 --- a/DHARMa/man/testCategorical.Rd +++ b/DHARMa/man/testCategorical.Rd @@ -14,7 +14,7 @@ testCategorical(simulationOutput, catPred, quantiles = c(0.25, 0.5, 0.75), \item{quantiles}{whether to draw the quantile lines.} -\item{plot}{if T, the function will create an additional plot} +\item{plot}{if TRUE, the function will create an additional plot} } \description{ This function tests if there are probles in a res ~ group structure. It performs two tests: test for within-group uniformity, and test for between-group homogeneity of variances diff --git a/DHARMa/man/testDispersion.Rd b/DHARMa/man/testDispersion.Rd index d5bf1413..eed68114 100644 --- a/DHARMa/man/testDispersion.Rd +++ b/DHARMa/man/testDispersion.Rd @@ -5,7 +5,7 @@ \title{DHARMa dispersion tests} \usage{ testDispersion(simulationOutput, alternative = c("two.sided", "greater", - "less"), plot = TRUE, type = c("DHARMa", "PearsonChisq"), ...) + "less"), plot = T, type = c("DHARMa", "PearsonChisq"), ...) } \arguments{ \item{simulationOutput}{an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case.} @@ -19,7 +19,7 @@ testDispersion(simulationOutput, alternative = c("two.sided", "greater", \item{...}{arguments to pass on to \code{\link{testGeneric}}} } \description{ -This function performs simulation-based tests for over/underdispersion. If type = "DHARMa" (default and recommended), simulation-based dispersion tests are performed. Their behavior differs depending on whether simulations are done with refit = FALSE, or refit = TRUE, and whether data is simulated conditional (e.g. re.form ~0 in lme4) (see below). If type = "PearsonChisq", a chi2 test on Pearson residuals is performed. +This function performs simulation-based tests for over/underdispersion. If type = "DHARMa" (default and recommended), simulation-based dispersion tests are performed. Their behavior differs depending on whether simulations are done with refit = F, or refit = T, and whether data is simulated conditional (e.g. re.form ~0 in lme4) (see below). If type = "PearsonChisq", a chi2 test on Pearson residuals is performed. } \details{ Over / underdispersion means that the observed data is more / less dispersed than expected under the fitted model. There is no unique way to test for dispersion problems, and there are a number of different dispersion tests implemented in various R packages. @@ -28,13 +28,13 @@ The testDispersion function implements several dispersion tests: \strong{Simulation-based dispersion tests (type == "DHARMa")} -If type = "DHARMa" (default and recommended), simulation-based dispersion tests are performed. Their behavior differs depending on whether simulations are done with refit = FALSE, or refit = T +If type = "DHARMa" (default and recommended), simulation-based dispersion tests are performed. Their behavior differs depending on whether simulations are done with refit = F, or refit = T #' \strong{Important:} for either refit = T or F, the results of type = "DHARMa" dispersion test will differ depending on whether simulations are done conditional (= conditional on fitted random effects) or unconditional (= REs are re-simulated). How to change between conditional or unconditional simulations is discussed in \code{\link{simulateResiduals}}. The general default in DHARMa is to use unconditional simulations, because this has advantages in other situations, but dispersion tests for models with strong REs specifically may increase substantially in power / sensitivity when switching to conditional simulations. I therefore recommend checking dispersion with conditional simulations if supported by the used regression package. -If refit = FALSE, the function uses \code{\link{testGeneric}} to compare the variance of the observed raw residuals (i.e. var(observed - predicted), displayed as a red line) against the variance of the simulated residuals (i.e. var(simulated - predicted), histogram). The variances are scaled to the mean simulated variance. A significant ratio > 1 indicates overdispersion, a significant ratio < 1 underdispersion. +If refit = F, the function uses \code{\link{testGeneric}} to compare the variance of the observed raw residuals (i.e. var(observed - predicted), displayed as a red line) against the variance of the simulated residuals (i.e. var(simulated - predicted), histogram). The variances are scaled to the mean simulated variance. A significant ratio > 1 indicates overdispersion, a significant ratio < 1 underdispersion. -If refit = TRUE, the function compares the approximate deviance (via squared pearson residuals) with the same quantity from the models refitted with simulated data. Applying this is much slower than the previous alternative. Given the computational cost, I would suggest that most users will be satisfied with the standard dispersion test. +If refit = T, the function compares the approximate deviance (via squared pearson residuals) with the same quantity from the models refitted with simulated data. Applying this is much slower than the previous alternative. Given the computational cost, I would suggest that most users will be satisfied with the standard dispersion test. ** Analytical dispersion tests (type == "PearsonChisq")** diff --git a/DHARMa/man/testGeneric.Rd b/DHARMa/man/testGeneric.Rd index 52a727e7..eb1c5d5e 100644 --- a/DHARMa/man/testGeneric.Rd +++ b/DHARMa/man/testGeneric.Rd @@ -5,7 +5,7 @@ \title{Test for a generic summary statistic based on simulated data} \usage{ testGeneric(simulationOutput, summary, alternative = c("two.sided", - "greater", "less"), plot = TRUE, + "greater", "less"), plot = T, methodName = "DHARMa generic simulation test") } \arguments{ diff --git a/DHARMa/man/testOutliers.Rd b/DHARMa/man/testOutliers.Rd index 3f5bfeb3..0ae01825 100644 --- a/DHARMa/man/testOutliers.Rd +++ b/DHARMa/man/testOutliers.Rd @@ -19,7 +19,7 @@ testOutliers(simulationOutput, alternative = c("two.sided", "greater", \item{nBoot}{number of boostrap replicates. Only used ot type = "bootstrap"} -\item{plot}{if T, the function will create an additional plot} +\item{plot}{if TRUE, the function will create an additional plot} } \description{ This function tests if the number of observations outside the simulatio envelope are larger or smaller than expected diff --git a/DHARMa/man/testQuantiles.Rd b/DHARMa/man/testQuantiles.Rd index 2c3f5818..881ebbd1 100644 --- a/DHARMa/man/testQuantiles.Rd +++ b/DHARMa/man/testQuantiles.Rd @@ -14,7 +14,7 @@ testQuantiles(simulationOutput, predictor = NULL, quantiles = c(0.25, 0.5, \item{quantiles}{the quantiles to be tested} -\item{plot}{if T, the function will create an additional plot} +\item{plot}{if TRUE, the function will create an additional plot} } \description{ This function tests diff --git a/DHARMa/man/testResiduals.Rd b/DHARMa/man/testResiduals.Rd index 394bef7f..df7e31f1 100644 --- a/DHARMa/man/testResiduals.Rd +++ b/DHARMa/man/testResiduals.Rd @@ -9,7 +9,7 @@ testResiduals(simulationOutput, plot = TRUE) \arguments{ \item{simulationOutput}{an object of class DHARMa, either created via \code{\link{simulateResiduals}} for supported models or by \code{\link{createDHARMa}} for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case.} -\item{plot}{if T, plots functions of the tests are called} +\item{plot}{if TRUE, plots functions of the tests are called} } \description{ Calls both uniformity and dispersion test diff --git a/DHARMa/man/testSpatialAutocorrelation.Rd b/DHARMa/man/testSpatialAutocorrelation.Rd index 453b999c..d33067b2 100644 --- a/DHARMa/man/testSpatialAutocorrelation.Rd +++ b/DHARMa/man/testSpatialAutocorrelation.Rd @@ -27,7 +27,7 @@ This function performs a Moran's I test for distance-based (spatial, phylogeneti \details{ The function performs Moran.I test from the package ape on the DHARMa residuals. If a distance matrix (distMat) is provided, calculations will be based on this distance matrix, and x,y coordinates will only used for the plotting (if provided). If distMat is not provided, the function will calculate the euclidean distances between x,y coordinates, and test Moran.I based on these distances. -If plot = TRUE, a plot will be produced showing each residual with at its x,y position, colored according to the residual value. Residuals with 0.5 are colored white, everything below 0.5 is colored increasinly red, everything above 0.5 is colored increasingly blue. +If plot = T, a plot will be produced showing each residual with at its x,y position, colored according to the residual value. Residuals with 0.5 are colored white, everything below 0.5 is colored increasinly red, everything above 0.5 is colored increasingly blue. Testing for spatial autocorrelation requires unique x,y values - if you have several observations per location, either use the recalculateResiduals function to aggregate residuals per location, or extract the residuals from the fitted object, and plot / test each of them independently for spatially repeated subgroups (a typical scenario would repeated spatial observation, in which case one could plot / test each time step separately for temporal autocorrelation). Note that the latter must be done by hand, outside testSpatialAutocorrelation. } diff --git a/DHARMa/man/testUniformity.Rd b/DHARMa/man/testUniformity.Rd index be366fd4..9936e6bb 100644 --- a/DHARMa/man/testUniformity.Rd +++ b/DHARMa/man/testUniformity.Rd @@ -12,7 +12,7 @@ testUniformity(simulationOutput, alternative = c("two.sided", "less", \item{alternative}{a character string specifying whether the test should test if observations are "greater", "less" or "two.sided" compared to the simulated null hypothesis. See \code{\link[stats]{ks.test}} for details} -\item{plot}{if T, plots calls \code{\link{plotQQunif}} as well} +\item{plot}{if TRUE, plots calls \code{\link{plotQQunif}} as well} } \description{ This function tests the overall uniformity of the simulated residuals in a DHARMa object