This report is automatically generated with the R package knitr (version 1.40) .

##!! WARNING !!##
## Running the randomSampleLinkage() function in this script performs a computationally intensive procedure to randomly sample data used in the linkage model.
## 10,000 iterations were chosen in an attempt to capture a representative distribution of possible outcomes and reduce potential variation of the results
## Running the randomSampleLinkage() function with 10,000 iterations may require 12+ hours to complete (depending on processing hardware)
## Because the randomSampleLinkage() function relies on random selection, the randomSampled_AqSafeConc output may vary from Board staff's output and result in a slightly different Margin of Safety


source("R Functions/functions_estimate NDDNQ values.R") # Load first so select() isn't masked from dplyr
source("R Functions/functions_QA data.R")
source("R Functions/function_randomSampleLinkage.R")


# Load New Data and Filter for Aq 2012-2019 & BB 2016-2019
load('Reeval_Impl_Goals_Linkage_Analysis/eval1_TLG Eval & BB Impl Goal/Output/blackBass_2000.RData') # loads "blackBass_2000" ~ Final Black Bass raw data
load('Reeval_Impl_Goals_Linkage_Analysis/eval1_TLG Eval & BB Impl Goal/Output/BlkBassStd350_FinalModels_2000.RData') # loads "BlkBassStd350_FinalModels_2000" ~ Final Black Bass Std 350 [Hg] Models for each Year

# Filter New Data for BB 2016-2019
blackBass_2016_2019 <- blackBass_2000 %>%
  semi_join(., BlkBassStd350_FinalModels_2000, by=c('Subarea', 'Year')) %>%   # Only keep data that we handpicked for BB Imp Goal Evaluation
  filter(Year >= 2016,  # not using grepl to find DRMP data because 2016 DRMP BB data was input into CEDEN and not provided in separate excel file like other years 
         !grepl('yolo', Subarea, ignore.case=T))  # Remove Yolo Bypass


load('Reeval_Impl_Goals_Linkage_Analysis/eval1_TLG Eval & BB Impl Goal/Output/BBImpGoal.RData') # loads "BBImpGoal" loads final Black Bass Implementation Goal (calculated using Black Bass data weighted average by year for each subarea)

# Load Aqueous Data
aqdf <- loadData("Reeval_Impl_Goals_Linkage_Analysis/Data/Aqueous/6a_AqMaster_noRepeats.xlsx")
## Data set has 11845 rows. Added Shape column based on SourceID for use in result_time_Plotly().
# Convert columns that are character but should be numeric or date
aqdf <- chara_to_NumDate(aqdf)

aqData1 <- aqdf %>%
  filter(grepl('DRMP', SourceID, ignore.case=T), # filter for DRMP Aq data - DRMP data only in 2016 - 2019
         !grepl('yolo', Subarea, ignore.case=T)) # Remove Yolo Bypass

aqData2 <- aqData1 %>%
  mutate(Subarea = recode(Subarea, "X2" = "West Delta",  # Change X2 to West Delta, consistent with the 2010 TMDL Staff Report
                          "Cache Creek Settling Basin" = "Yolo Bypass North")) # Change CCSB to Yolo Bypass North (there are no corresponding BB data in Yolo Bypass North)

aqData3 <- aqData2 %>%
  filter(Analyte     == 'Methylmercury, Total',
         Unit        == 'ng/L',
         MatrixName  == 'Aqueous')

aqNDDNQs <- robinNDDNQ(aqData3, ResultQualCode)
## Tested llogis, weibull, gamma, and burr 
## Results for tested distributions are returned as a list. 
##  Order of distributions defaults to llogis, then weibull, then gamma, then best to worst AIC value.
##  To retrieve dataframe with estimated ND/DNQ values use "<varname>$<distname>$fitted"    or      "<varname>[[1]][[1]]"
##  To view goodness of fit graphs                use "plot(<varname>$<distname>$fitmodel)" or "plot(<varname>[[1]][[2]])"
##  Unable to test lnorm and pareto distributions due to fitdistcen() errors.
aqData4 <- aqNDDNQs$llogis$fitted # Selected distribution - primarily based on CDF & P-P plot showing best fit to data

aqData <- aqData4


# Allowed Extrapolation in BB 350mm standardization
extrapAllowed <- BlkBassStd350_FinalModels_2000 %>%
  filter(PredictionType == "Extrapolated",
         ExtrapLength == max(ExtrapLength)) %>%
  pull(ExtrapLength)
if(length(extrapAllowed) == 0) {extrapAllowed <- 0}


# RANDOMLY SAMPLE LINKAGE: Aq Data (Pooled 5 yr median; 2016-2019) vs. DRMP Black Bass Yr Std 350mm [Hg] (2016-2019)
randomSampled_AqSafeConc <- randomSampleLinkage(aqData=aqData,
                                                  fishData=blackBass_2016_2019,
                                                  fishgoal=BBImpGoal,
                                                  iterations=10000, bylocationColumn=Subarea, stdzFishLength=350, extrapAllowance=extrapAllowed, minFishSampleSize=8, aqPeriod=5, aqSampPerYr=12)
##                                                                                     
  |                                                                                |   0%
  |                                                                                      
  |========                                                                        |  10%
  |                                                                                      
  |================                                                                |  20%
  |                                                                                      
  |========================                                                        |  30%
  |                                                                                      
  |================================                                                |  40%Error in mutate(., Model = purrr::map(.x = data, .f = ~eval(parse(text = paste0("nls(",  : 
##   Problem while computing `Model = purrr::map(...)`.
## Caused by error in `nls()`:
## ! step factor 0.000488281 reduced below 'minFactor' of 0.000976562
## 
  |                                                                                      
  |========================================                                        |  50%
  |                                                                                      
  |================================================                                |  60%
  |                                                                                      
  |========================================================                        |  70%
  |                                                                                      
  |================================================================                |  80%Error in mutate(., Model = purrr::map(.x = data, .f = ~eval(parse(text = paste0("nls(",  : 
##   Problem while computing `Model = purrr::map(...)`.
## Caused by error in `nls()`:
## ! step factor 0.000488281 reduced below 'minFactor' of 0.000976562
## 
  |                                                                                      
  |========================================================================        |  90%
  |                                                                                      
  |================================================================================| 100%
save(randomSampled_AqSafeConc, file='Reeval_Impl_Goals_Linkage_Analysis/eval2_Linkage & Aq Impl Goal/Output/randomSampled_FINAL_median_5yr DRMP Aq & BB 2016-2019.RData')

The R session information (including the OS info, R version and all packages used):

    sessionInfo()
## R version 4.2.2 (2022-10-31 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 22621)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.utf8  LC_CTYPE=C                            
## [3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.utf8    
## system code page: 65001
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] mgcv_1.8-41        nlme_3.1-160       lubridate_1.8.0    plotly_4.10.0     
##  [5] readxl_1.4.1       actuar_3.3-0       NADA_1.6-1.1       forcats_0.5.2     
##  [9] stringr_1.4.1      dplyr_1.0.9        purrr_0.3.4        readr_2.1.2       
## [13] tidyr_1.2.0        tibble_3.1.8       ggplot2_3.3.6      tidyverse_1.3.2   
## [17] fitdistrplus_1.1-8 survival_3.4-0     MASS_7.3-58.1     
## 
## loaded via a namespace (and not attached):
##  [1] lattice_0.20-45     assertthat_0.2.1    digest_0.6.29       utf8_1.2.2         
##  [5] R6_2.5.1            cellranger_1.1.0    backports_1.4.1     reprex_2.0.2       
##  [9] evaluate_0.16       highr_0.9           httr_1.4.4          pillar_1.8.1       
## [13] rlang_1.0.5         lazyeval_0.2.2      googlesheets4_1.0.1 data.table_1.14.2  
## [17] rstudioapi_0.14     Matrix_1.5-1        rmarkdown_2.16      splines_4.2.2      
## [21] googledrive_2.0.0   htmlwidgets_1.5.4   munsell_0.5.0       broom_1.0.1        
## [25] compiler_4.2.2      modelr_0.1.9        xfun_0.32           pkgconfig_2.0.3    
## [29] htmltools_0.5.3     tidyselect_1.1.2    viridisLite_0.4.1   fansi_1.0.3        
## [33] crayon_1.5.1        tzdb_0.3.0          dbplyr_2.2.1        withr_2.5.0        
## [37] grid_4.2.2          jsonlite_1.8.0      gtable_0.3.1        lifecycle_1.0.1    
## [41] DBI_1.1.3           magrittr_2.0.3      scales_1.2.1        writexl_1.4.0      
## [45] cli_3.3.0           stringi_1.7.8       fs_1.5.2            xml2_1.3.3         
## [49] ellipsis_0.3.2      generics_0.1.3      vctrs_0.4.1         expint_0.1-7       
## [53] tools_4.2.2         glue_1.6.2          hms_1.1.2           fastmap_1.1.0      
## [57] yaml_2.3.5          colorspace_2.0-3    gargle_1.2.0        rvest_1.0.3        
## [61] knitr_1.40          haven_2.5.1
    Sys.time()
## [1] "2024-01-09 10:58:43 PST"