diff --git a/R/runPairsBoot.R b/R/runPairsBoot.R index 1c11d57..b6faac6 100644 --- a/R/runPairsBoot.R +++ b/R/runPairsBoot.R @@ -14,8 +14,10 @@ #' @export #' @return eBoot, a named list with bootOut,wordsOut,xConc,xFlux values. bootOut is a data frame with the results #' of the bootstrapping tests. wordsOut is a character vector describing the results. -#' xConc, xFlux are vectors of length iBoot, of the change in flow normalized concentration or flux -#' computed by each bootstrap replicate (mg/L). pConc and pFlux are vectors of length iBoot, of the change +#' xConc, xFlux are vectors of length iBoot, of the change in flow normalized concentration or flux +#' computed by each bootstrap replicate (mg/L). xConcQTC, xFluxQTC, xConcCQTC, and xFluxCQTC are +#' the Discharge Trend Component (QTC) and Concentration vs Discharge Trend Component (CQTC) of +#' xConc and xFlux. pConc and pFlux are vectors of length iBoot, of the change #' in flow normalized concentration or flux computed from each bootstrap replicate expressed as % change. #' @seealso \code{\link{runGroupsBoot}}, \code{\link[EGRET]{runPairs}} #' @examples @@ -101,7 +103,13 @@ runPairsBoot <- function(eList, pairResults, xFlux <- rep(NA, nBoot) pConc <- rep(NA, nBoot) pFlux <- rep(NA, nBoot) - + + # Include GFN bootstrap results + xConcQTC <- rep(NA, nBoot) + xFluxQTC <- rep(NA, nBoot) + xConcCQTC <- rep(NA, nBoot) + xFluxCQTC <- rep(NA, nBoot) + # regDeltaConc <- pairResults$x22[1] - pairResults$x11[1] estC <- regDeltaConc @@ -117,6 +125,12 @@ runPairsBoot <- function(eList, pairResults, LFluxDiff <- log(pairResults$x22[2]) - log(pairResults$x11[2]) fcc <- format(regDeltaConc, digits = 3, width = 7) ffc <- format(regDeltaFlux, digits = 3, width = 8) + + QStartDate <- localDaily$Date[1] + numQDays <- length(localDaily$Date) + QEndDate <- localDaily$Date[numQDays] + Daily0 <- localDaily[localDaily$Date >= QStartDate & localDaily$Date <= QEndDate, ] + Daily1 <- localDaily[localDaily$Date >= as.Date(dateInfo$flowNormStart[1]) & localDaily$Date <= as.Date(dateInfo$flowNormEnd[1]), ] Daily2 <- localDaily[localDaily$Date >= as.Date(dateInfo$flowNormStart[2]) & localDaily$Date <= @@ -160,12 +174,39 @@ runPairsBoot <- function(eList, pairResults, xConc_here <- (2 * regDeltaConc) - (c22 - c11) xFlux_here <- (2 * regDeltaFlux) - (f22 - f11) + + DailyRS1FD0 <- estDailyFromSurfaces(eList, localsurfaces = surfaces1, + localDaily = Daily0) + annualFlex <- setupYears(DailyRS1FD0, paLong = paLong, paStart = paStart) + c10 <- mean(annualFlex$FNConc, na.rm = TRUE) + f10 <- mean(annualFlex$FNFlux, na.rm = TRUE) * 0.00036525 + DailyRS2FD0 <- estDailyFromSurfaces(eList, localsurfaces = surfaces2, + localDaily = Daily0) + annualFlex <- setupYears(DailyRS2FD0, paLong = paLong, paStart = paStart) + c20 <- mean(annualFlex$FNConc, na.rm = TRUE) + f20 <- mean(annualFlex$FNFlux, na.rm = TRUE) * 0.00036525 + + # CQTC bootstrap delta + regfRSpart <- (pairResults$x20[2] - pairResults$x10[2]) + regcRSpart <- (pairResults$x20[1] - pairResults$x10[1]) + xConcCQTC_here <- (2 * regcRSpart) - (c20 - c10) + xFluxCQTC_here <- (2 * regfRSpart) - (f20 - f10) + + # QTC bootstrap delta + regcFDpart <- regDeltaConc - regcRSpart + regfFDpart <- regDeltaFlux - regfRSpart + xConcQTC_here <- (2 * regcFDpart) - (c22 - c11 - c20 + c10) + xFluxQTC_here <- (2 * regfFDpart) - (f22 - f11 - f20 + f10) if(!is.na(xConc_here) & !is.na(xFlux_here)){ nBootGood <- nBootGood + 1 xConc[nBootGood] <- xConc_here xFlux[nBootGood] <- xFlux_here + xConcQTC[nBootGood] <- xConcQTC_here + xFluxQTC[nBootGood] <- xFluxQTC_here + xConcCQTC[nBootGood] <- xConcCQTC_here + xFluxCQTC[nBootGood] <- xFluxCQTC_here LConc <- (2 * LConcDiff) - (log(c22) - log(c11)) pConc[nBootGood] <- (100 * exp(LConc)) - 100 LFlux <- (2 * LFluxDiff) - (log(f22) - log(f11)) @@ -270,8 +311,15 @@ runPairsBoot <- function(eList, pairResults, width = 30)) pConc <- as.numeric(na.omit(pConc)) pFlux <- as.numeric(na.omit(pFlux)) + xConcQTC <- as.numeric(na.omit(xConcQTC)) + xFluxQTC <- as.numeric(na.omit(xFluxQTC)) + xConcCQTC <- as.numeric(na.omit(xConcCQTC)) + xFluxCQTC <- as.numeric(na.omit(xFluxCQTC)) + pairsBootOut <- list(bootOut = bootOut, wordsOut = wordsOut, xConc = xConc, xFlux = xFlux, pConc = pConc, pFlux = pFlux, + xConcQTC = xConcQTC, xFluxQTC = xFluxQTC, + xConcCQTC = xConcCQTC, xFluxCQTC = xFluxCQTC, startSeed = startSeed) attr(pairsBootOut, "year1") <- year1 attr(pairsBootOut, "year2") <- year2 diff --git a/tests/testthat/tests_boot_tests.R b/tests/testthat/tests_boot_tests.R index d643278..fdbca04 100644 --- a/tests/testthat/tests_boot_tests.R +++ b/tests/testthat/tests_boot_tests.R @@ -191,8 +191,9 @@ test_that("runPairsBoot", { boot_pair_out <- runPairsBoot(eList, pairOut_2, nBoot = 3) - expect_true(all(c("bootOut","wordsOut","xConc","xFlux", - "pConc","pFlux","startSeed") %in% names(boot_pair_out))) + expect_true(all(c("bootOut","wordsOut","xConc","xFlux", + "xConcQTC","xConcCQTC","xFluxQTC","xFluxCQTC", + "pConc","pFlux","startSeed") %in% names(boot_pair_out))) expect_true(boot_pair_out$bootOut$rejectC) expect_true(all(c("Upward trend in concentration is likely", @@ -204,6 +205,11 @@ test_that("runPairsBoot", { expect_equal(round(boot_pair_out$xFlux[1:2], digits = 2), c(0.05,0.06)) expect_equal(round(boot_pair_out$pConc[1:2], digits = 2), c(37.30,40.55)) expect_equal(round(boot_pair_out$pFlux[1:2], digits = 2), c(44.48,54.51)) + + expect_equal(round((boot_pair_out$xConcQTC + boot_pair_out$xConcCQTC)[1:2], digits=2), + c(0.39,0.41)) + expect_equal(round((boot_pair_out$xFluxQTC + boot_pair_out$xFluxCQTC)[1:2], digits=2), + c(0.05,0.06)) })