From d0972f6c04c9f25d4e91dc8c9f2da44a0d29437c Mon Sep 17 00:00:00 2001 From: iantaylor-NOAA Date: Thu, 30 May 2024 19:29:13 +0000 Subject: [PATCH] style and docs: run devtools::document() and styler::style_pkg() --- DESCRIPTION | 4 ++-- man/RunFn.Rd | 19 +++++++++++-------- man/SimulatorFn.Rd | 41 ++++++++++++++++++++++++----------------- man/StepwiseFn.Rd | 29 +++++++++++++++++++---------- man/estgrowth.vb.Rd | 26 +++++++++++++++++--------- 5 files changed, 73 insertions(+), 46 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c6c21a0..761ea61 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -32,9 +32,9 @@ Depends: Imports: cli, fs, + knitr, stats, - TMB (>= 1.8.0), - knitr + TMB (>= 1.8.0) Suggests: spelling LinkingTo: diff --git a/man/RunFn.Rd b/man/RunFn.Rd index 9b89583..1981b2c 100644 --- a/man/RunFn.Rd +++ b/man/RunFn.Rd @@ -203,7 +203,8 @@ to one. example(SimulatorFn) \dontrun{ utils::write.csv(AgeReads, - file = file.path(getwd(), "Simulated_data_example.csv")) + file = file.path(getwd(), "Simulated_data_example.csv") +) } ##### Format data @@ -232,23 +233,23 @@ for (RowI in 2:nrow(AgeReads)) { DupRow <- NA for (PreviousRowJ in 1:nrow(AgeReads2)) { if (all( - AgeReads[RowI,1:Nreaders] == AgeReads2[PreviousRowJ,1:Nreaders+1] + AgeReads[RowI, 1:Nreaders] == AgeReads2[PreviousRowJ, 1:Nreaders + 1] )) { DupRow <- PreviousRowJ } } - if (is.na(DupRow)) {# Add new row to AgeReads2 + if (is.na(DupRow)) { # Add new row to AgeReads2 AgeReads2 <- rbind(AgeReads2, c(1, AgeReads[RowI, ])) } - if(!is.na(DupRow)){# Increment number of samples for previous duplicate - AgeReads2[DupRow,1] <- AgeReads2[DupRow,1] + 1 + if (!is.na(DupRow)) { # Increment number of samples for previous duplicate + AgeReads2[DupRow, 1] <- AgeReads2[DupRow, 1] + 1 } } ######## Determine settings for ADMB # Define minimum and maximum ages for integral across unobserved ages MinAge <- 1 -MaxAge <- ceiling(max(AgeReads2[,-1])/10)*10 +MaxAge <- ceiling(max(AgeReads2[, -1]) / 10) * 10 BiasOpt <- c(0, -1, 0, -3) SigOpt <- c(1, -1, 6, -3) # Necessary for SigOpt option 5 or 6 @@ -258,12 +259,14 @@ KnotAges <- list(NA, NA, c(1, 10, 20, MaxAge), NA) \dontrun{ fileloc <- file.path(tempdir(), "age") dir.create(fileloc, showWarnings = FALSE) -RunFn(Data = AgeReads2, SigOpt = SigOpt, KnotAges = KnotAges, +RunFn( + Data = AgeReads2, SigOpt = SigOpt, KnotAges = KnotAges, BiasOpt = BiasOpt, NDataSets = 1, MinAge = MinAge, MaxAge = MaxAge, RefAge = 10, MinusAge = 1, PlusAge = 30, SaveFile = fileloc, AdmbFile = file.path(system.file("executables", - package = "nwfscAgeingError"), .Platform$file.sep), + package = "nwfscAgeingError" + ), .Platform$file.sep), EffSampleSize = 0, Intern = FALSE, JustWrite = FALSE, CallType = "shell" ) } diff --git a/man/SimulatorFn.Rd b/man/SimulatorFn.Rd index d08d9b8..c99e253 100644 --- a/man/SimulatorFn.Rd +++ b/man/SimulatorFn.Rd @@ -55,35 +55,42 @@ A function to generate simulated double reading data with given properties \examples{ # Parameters for generating data # This represents 2 unique readers - # Row 1 -- Otoliths read only once by reader - # Row 2 -- Otoliths read twice by reader 1 - # Row 2 -- Otoliths read only once by reader 2 - # Row 4 -- Otoliths read twice by reader 2 - # Row 5 -- Otoliths read once by reader 1 and once by reader 2 -ReadsMat <- structure(matrix(nrow = 5, ncol = 5, - c(rep(25, 5), +# Row 1 -- Otoliths read only once by reader +# Row 2 -- Otoliths read twice by reader 1 +# Row 2 -- Otoliths read only once by reader 2 +# Row 4 -- Otoliths read twice by reader 2 +# Row 5 -- Otoliths read once by reader 1 and once by reader 2 +ReadsMat <- structure(matrix( + nrow = 5, ncol = 5, + c( + rep(25, 5), 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, - 0, 0, 0, 1, 0) - ), dimnames = list( - c("Reader1_Only", "Reader1_DoubleReads", - "Reader2_Only", "Reader2_DoubleReads", - "Reader1_&_Reader2" - ), - c("NumberOfReads", + 0, 0, 0, 1, 0 + ) +), dimnames = list( + c( + "Reader1_Only", "Reader1_DoubleReads", + "Reader2_Only", "Reader2_DoubleReads", + "Reader1_&_Reader2" + ), + c( + "NumberOfReads", "Reader1", "Reader1_DoubleReads", "Reader2", "Reader2_DoubleReads" - )) ) +)) # Generate data set.seed(2) -AgeReads <- SimulatorFn(Nreaders = 4, M = 0.2, +AgeReads <- SimulatorFn( + Nreaders = 4, M = 0.2, SelexForm = "Logistic", SelexParams = c(5, 0.2), BiasParams = c(1, 1, 1.1, 1.1), ErrorParams = c(0.2, 0.2, 0.2, 0.2), ReadsMat = ReadsMat, - RecCv = 0.6, RecAr1 = 0.8, Amax = 100) + RecCv = 0.6, RecAr1 = 0.8, Amax = 100 +) } \references{ Punt, A.E., Smith, D.C., KrusicGolub, K., and Robertson, S. 2008. diff --git a/man/StepwiseFn.Rd b/man/StepwiseFn.Rd index 84f776d..b8171f5 100644 --- a/man/StepwiseFn.Rd +++ b/man/StepwiseFn.Rd @@ -114,16 +114,19 @@ example(RunFn) ##### Run the model (MAY TAKE 5-10 MINUTES) fileloc <- file.path(tempdir(), "age") dir.create(fileloc, showWarnings = FALSE) -RunFn(Data = AgeReads2, SigOpt = SigOpt, KnotAges = KnotAges, +RunFn( + Data = AgeReads2, SigOpt = SigOpt, KnotAges = KnotAges, BiasOpt = BiasOpt, NDataSets = 1, MinAge = MinAge, MaxAge = MaxAge, RefAge = 10, MinusAge = 1, PlusAge = 30, SaveFile = fileloc, AdmbFile = file.path(system.file("executables", - package = "nwfscAgeingError"), .Platform$file.sep), + package = "nwfscAgeingError" + ), .Platform$file.sep), EffSampleSize = 0, Intern = FALSE, JustWrite = FALSE, CallType = "shell" ) # Plot output -PlotOutputFn(Data = AgeReads2, MaxAge = MaxAge, +PlotOutputFn( + Data = AgeReads2, MaxAge = MaxAge, SaveFile = fileloc, PlotType = "PDF" ) } @@ -148,9 +151,13 @@ StartPlusAge <- 30 # across for that reader SearchMat <- array(NA, dim = c(Nreaders * 2 + 2, 7), - dimnames = list(c(paste("Error_Reader", 1:Nreaders), - paste("Bias_Reader", 1:Nreaders), "MinusAge", "PlusAge"), - paste("Option", 1:7)) + dimnames = list( + c( + paste("Error_Reader", 1:Nreaders), + paste("Bias_Reader", 1:Nreaders), "MinusAge", "PlusAge" + ), + paste("Option", 1:7) + ) ) # Readers 1 and 3 search across options 1-3 for ERROR SearchMat[c(1, 3), 1:3] <- rep(1, 2) \%o\% c(1, 2, 3) @@ -176,7 +183,7 @@ SearchMat[9, 1:7] <- c( StartMinusAge + 4, StartMinusAge + 10 ) -SearchMat[9, 1:7] <- ifelse(SearchMat[9,1:7] < MinAge, +SearchMat[9, 1:7] <- ifelse(SearchMat[9, 1:7] < MinAge, NA, SearchMat[9, 1:7] ) # PlusAge searches with a search kernal of -10,-4,-1,+0,+1,+4,+10 @@ -189,8 +196,9 @@ SearchMat[10, 1:7] <- c( StartPlusAge + 4, StartPlusAge + 10 ) -SearchMat[10,1:7] <- ifelse(SearchMat[10, 1:7] > MaxAge, - NA, SearchMat[10, 1:7]) +SearchMat[10, 1:7] <- ifelse(SearchMat[10, 1:7] > MaxAge, + NA, SearchMat[10, 1:7] +) # Run model selection # This outputs a series of files @@ -207,7 +215,8 @@ SearchMat[10,1:7] <- ifelse(SearchMat[10, 1:7] > MaxAge, # WARNING: One run of this stepwise model building example can take # 8+ hours, and should be run overnight \dontrun{ -StepwiseFn(SearchMat = SearchMat, Data = AgeReads2, +StepwiseFn( + SearchMat = SearchMat, Data = AgeReads2, NDataSets = 1, MinAge = MinAge, MaxAge = MaxAge, RefAge = 10, MaxSd = 40, MaxExpectedAge = MaxAge + 10, SaveFile = fileloc, InformationCriterion = c("AIC", "AICc", "BIC")[3] diff --git a/man/estgrowth.vb.Rd b/man/estgrowth.vb.Rd index e0cbe50..2e519ea 100644 --- a/man/estgrowth.vb.Rd +++ b/man/estgrowth.vb.Rd @@ -15,7 +15,7 @@ Names will be assigned if they are not provided.} \item{Lengths}{A vector of Lengths in cm. Lengths can be \code{NULL} if \code{ReturnType == "Pred"} because you are only predicting using ages, where -the lengths are just needed for estimation purpopses. If not \code{NULL}, +the lengths are just needed for estimation purposes. If not \code{NULL}, ensure that there is one length measurement for every age measurement. Values of \code{NA} are accepted.} @@ -42,23 +42,31 @@ predicted lengths given ages and input parameters. } \examples{ \dontrun{ -bio_dat <- data.frame(Age = rep(0:30, each = 20), - Length_cm = rnorm(n = 31 * 20, mean = 50, sd = 5)) +bio_dat <- data.frame( + Age = rep(0:30, each = 20), + Length_cm = rnorm(n = 31 * 20, mean = 50, sd = 5) +) pars_in <- lapply(FUN = log, X = list( "K" = 0.13, "Linf" = 55, "L0" = 5, "CV0" = 0.1, - "CV1" = 0.1)) -solve <- optim(fn = estgrowth.vb, par = unlist(pars_in), hessian = FALSE, + "CV1" = 0.1 +)) +solve <- optim( + fn = estgrowth.vb, par = unlist(pars_in), hessian = FALSE, Ages = bio_dat[, "Age"], - Lengths = bio_dat[, "Length_cm"]) -predictions <- estgrowth.vb(Par = solve$par, ReturnType = "Pred", + Lengths = bio_dat[, "Length_cm"] +) +predictions <- estgrowth.vb( + Par = solve$par, ReturnType = "Pred", sdFactor = 1, Ages = bio_dat[, "Age"], - Lengths = bio_dat[, "Length_cm"]) + Lengths = bio_dat[, "Length_cm"] +) plot(bio_dat$Age, predictions[, "Lhat_pred"], - xlab = "Age (years)", ylab = "Predicted length (cm)") + xlab = "Age (years)", ylab = "Predicted length (cm)" +) exp(solve$par) } }