# Get rid of memory limits -----------------------------------------------------
options(future.globals.maxSize = 1 * 1024^4) # Allow up to 1 TB for globals

# Install Libraries ------------------------------------------------------------

# Here we list all the packages we will need for this whole process
# We'll also use this in our works cited page.
PKG <- c(
  "surveyresamplr",
  "dplyr",
  "tidyr",
  "purrr",
  "ggplot2",
  "future.callr",
  "flextable", 
  "sdmTMB"
)

pkg_install <- function(p) {
  if (!require(p, character.only = TRUE)) {
    install.packages(p)
  }
  require(p, character.only = TRUE)
}
base::lapply(unique(PKG), pkg_install)
#> Loading required package: surveyresamplr
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> Loading required package: tidyr
#> Loading required package: purrr
#> Loading required package: ggplot2
#> Loading required package: future.callr
#> Loading required package: future
#> Loading required package: flextable
#> 
#> Attaching package: 'flextable'
#> The following object is masked from 'package:purrr':
#> 
#>     compose
#> Loading required package: sdmTMB
#> [[1]]
#> [1] TRUE
#> 
#> [[2]]
#> [1] TRUE
#> 
#> [[3]]
#> [1] TRUE
#> 
#> [[4]]
#> [1] TRUE
#> 
#> [[5]]
#> [1] TRUE
#> 
#> [[6]]
#> [1] TRUE
#> 
#> [[7]]
#> [1] TRUE
#> 
#> [[8]]
#> [1] TRUE

# Set directories --------------------------------------------------------------
wd <- paste0(here::here(), "/vignettes/")
dir_out <- paste0(wd, "output/")
dir_final <- paste0(dir_out, "GOA_0results/")

Run scenarios


### Define study species -------------------------------------------------------

spp_list <- data.frame(
  srvy = "GOA",
  common_name = c(
    "walleye pollock", "Pacific cod",
    "Pacific ocean perch", "flathead sole",
    "northern rockfish", "arrowtooth flounder"
  ),
  species_code = as.character(c(
    21740, 21720,
    30060, 10130,
    30420, 10110
  )),
  filter_lat_lt = NA,
  filter_lat_gt = NA,
  filter_depth = NA,
  model_fn = "total_catch_wt_kg ~ 0 + factor(year)",
  model_family = "delta_gamma",
  model_anisotropy = TRUE,
  model_spatiotemporal = "iid, iid"
) |>
  dplyr::mutate(
    file_name = gsub(pattern = " ", replacement = "_", x = (tolower(common_name)))
  )

### Load survey data -----------------------------------------------------------

# source(paste0(wd, "code/data_dl_ak.r"))
catch <- surveyresamplr::noaa_afsc_catch |>
  dplyr::filter(srvy == "GOA")

### Load grid data -------------------------------------------------------------

grid_yrs <- replicate_df(surveyresamplr::noaa_afsc_goa_pred_grid_depth, "year", unique(catch$year))

### Variables ------------------------------------------------------------------

srvy <- "GOA"
seq_from <- 0.2
seq_to <- 1.0
seq_by <- 0.2
tot_dataframes <- 13
replicate_num <- 3

### Run ------------------------------------------------------------------------

purrr::map(
  1:nrow(spp_list),
  ~ clean_and_resample(spp_list[.x, ],
    catch, seq_from, seq_to, seq_by,
    tot_dataframes, replicate_num, grid_yrs, dir_out,
    test = TRUE
  )
)

### Plot indices --------------------------------------------------------------

out <- plot_results(srvy = srvy, dir_out = dir_out, dir_final = dir_final)
out$plots
#> $index_boxplot_log_biomass

#> 
#> $index_boxplot_log_biomass_SE

#> 
#> $index_boxplot_biomass

#> 
#> $index_timeseries_biomass

Parameter output:

i <- 1
print(names(out$tables)[i])
#> [1] "fit_df"
head(out$tables[i][[1]]) |>
  flextable::flextable()

X.1

X

srvy

common_name

species_code

filter_lat_lt

filter_lat_gt

filter_depth

model_fn

model_family

model_anisotropy

model_spatiotemporal

file_name

effort

term

estimate

std.error

conf.low

conf.high

1

1

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

factor(year)1990

22.20962

3,623.130

-7,078.994

7,123.414

2

2

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

factor(year)1993

22.15425

3,324.978

-6,494.683

6,538.992

3

3

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

factor(year)1996

22.02771

3,275.996

-6,398.807

6,442.862

4

4

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

factor(year)1999

23.34060

6,546.774

-12,808.102

12,854.783

5

5

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

factor(year)2001

21.66088

3,589.083

-7,012.812

7,056.134

6

6

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

factor(year)2003

22.09698

3,319.433

-6,483.873

6,528.066

i <- 1 + i
print(names(out$tables)[i])
#> [1] "fit_pars"
head(out$tables[i][[1]]) |>
  flextable::flextable()

X.1

X

srvy

common_name

species_code

filter_lat_lt

filter_lat_gt

filter_depth

model_fn

model_family

model_anisotropy

model_spatiotemporal

file_name

effort

term

estimate

std.error

conf.low

conf.high

1

1

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

range

0.4230446

3,307.7298

0

Inf

2

2

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

sigma_O

0.2235829

1,245.4340

0

Inf

3

3

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

sigma_E

0.2061478

797.8968

0

Inf

4

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

1_1

range

3.2400616

16,000.5812

0

Inf

5

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

1_1

sigma_O

0.3181234

1,172.7933

0

Inf

6

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

1_1

sigma_E

0.2486639

1,162.1914

0

Inf

i <- 1 + i
print(names(out$tables)[i])
#> [1] "fit_check"
head(out$tables[i][[1]]) |>
  flextable::flextable()

X.1

X

srvy

common_name

species_code

filter_lat_lt

filter_lat_gt

filter_depth

model_fn

model_family

model_anisotropy

model_spatiotemporal

file_name

effort

hessian_ok

eigen_values_ok

nlminb_ok

range_ok

gradients_ok

se_magnitude_ok

se_na_ok

sigmas_ok

all_ok

1

1

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

TRUE

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

FALSE

2

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

1_1

FALSE

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

FALSE

1

1

GOA

northern rockfish

30,420

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

northern_rockfish

08_3

FALSE

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

FALSE

2

GOA

northern rockfish

30,420

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

northern_rockfish

1_1

FALSE

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

FALSE

1

1

GOA

Pacific cod

21,720

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

pacific_cod

08_3

FALSE

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

FALSE

2

GOA

Pacific cod

21,720

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

pacific_cod

1_1

FALSE

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

TRUE

FALSE

i <- 1 + i
print(names(out$tables)[i])
#> [1] "index"
head(out$tables[i][[1]]) |>
  flextable::flextable()

X.1

X

srvy

common_name

species_code

filter_lat_lt

filter_lat_gt

filter_depth

model_fn

model_family

model_anisotropy

model_spatiotemporal

file_name

effort

year

est

lwr

upr

log_est

se

type

1

1

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

1,990

13,633,865

11,327,310

16,410,100

16.42807

0.09456306

index

2

2

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

1,993

12,573,858

10,014,298

15,787,616

16.34713

0.11612763

index

3

3

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

1,996

9,991,137

7,583,964

13,162,355

16.11721

0.14064669

index

4

4

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

1,999

7,146,546

5,486,662

9,308,594

15.78214

0.13485400

index

5

5

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

2,001

11,322,791

8,269,930

15,502,623

16.24233

0.16030479

index

6

6

GOA

flathead sole

10,130

total_catch_wt_kg ~ 0 + factor(year)

delta_gamma

TRUE

iid, iid

flathead_sole

08_3

2,003

11,073,005

8,457,789

14,496,865

16.22002

0.13746288

index