Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

GFN bootstrap results #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions R/runPairsBoot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 <=
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions tests/testthat/tests_boot_tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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))

})

Expand Down