knitr::opts_chunk$set(
  fig.width = 11, fig.height = 8.5,
  # fig.path=paste0("figs/", spp, "/"),
  echo = FALSE, warning = FALSE, message = FALSE
)
library(dplyr)
library(ggplot2)
library(gfplot)
library(gfdata)
library(sdmTMB)
library(gfranges)
species <- params$species
region <- params$region
covariates <- params$covariates
covs <- params$covs
priors <- FALSE

paste("region =", region)
paste("model covariates =", covariates)
paste("model label =", covs)
paste("priors =", priors)
spp <- gsub(" ", "-", gsub("\\/", "-", tolower(species)))

# folder to hold figs for this species
dir.create(file.path("figs", spp))
dir.create(file.path("data", spp))

if (region == "Both odd year surveys") {
  survey <- c("SYN QCS", "SYN HS")
  model_ssid <- c(1, 3)
  ssid_string <- paste0(model_ssid, collapse = "n")
  years <- NULL
}

if (region == "West Coast Vancouver Island") {
  survey <- c("SYN WCVI")
  model_ssid <- c(4)
  ssid_string <- paste0(model_ssid, collapse = "n")
  years <- NULL
}

if (region == "West Coast Haida Gwaii") {
  survey <- c("SYN WCHG")
  model_ssid <- c(16)
  ssid_string <- paste0(model_ssid, collapse = "n")
  years <- NULL
}

if (region == "All synoptic surveys") {
  survey <- c("SYN QCS", "SYN HS", "SYN WCVI", "SYN WCHG")
  model_ssid <- c(1, 3, 4, 16)
  ssid_string <- paste0(model_ssid, collapse = "n")
  years <- NULL
}

Check max depth depth?

biomass <- readRDS(paste0(
  "data/", spp, "/data-by-maturity-", spp, "-", ssid_string, ".rds"
))

if(nrow(biomass)<4000) {stop("Need to recalculate split by maturity!")}

covars <- readRDS("data/event-covariates.rds") %>% select(-geometry)
data <- dplyr::left_join(biomass, covars)

# scale predictors before filtering to ensure mean and SD are global
data <- data %>% mutate(raw_depth = depth, depth = log(raw_depth))
data <- gfranges::scale_predictors(data, # predictors = c(quo(depth)))
  predictors = c(quo(depth), quo(mixed), quo(muddy), quo(sandy), quo(rocky), quo(any_rock))
)
data <- data %>%
  mutate(depth = raw_depth) %>%
  filter(ssid %in% model_ssid) %>%
  filter(year < 2019)

# max_depth_found <- max(data[data$present == 1, ]$raw_depth, na.rm = TRUE)
#max_depth_found <- 800

Model without environmental covariates

rm(depth_model_list)
rm(adult_biomass)
rm(imm_biomass)
rm(total_biomass)

try({
  adult_biomass <- readRDS(paste0(
    "data/", spp,
    "/mod-mat-biomass-", spp, covs, "-1n3n4n16-prior-", priors, ".rds"
  ))
  adult_biomass <- sdmTMB:::update_model(adult_biomass)
})


try({
  imm_biomass <- readRDS(paste0(
    "data/", spp,
    "/mod-imm-biomass-", spp, covs, "-1n3n4n16.rds"
  ))

  imm_biomass <- sdmTMB:::update_model(imm_biomass)
  # depth_model_list <- list(adult = adult_biomass, imm = imm_biomass)
})

if (!exists("adult_biomass")) {
  try({
    total_biomass <- readRDS(paste0(
      "data/", spp,
      "/model-total-biomass-", spp, covs, "-1n3n4n16.rds"
    ))
  })
}


if (!exists("adult_biomass")) adult_biomass <- NULL
if (!exists("imm_biomass")) imm_biomass <- NULL
if (!exists("total_biomass")) total_biomass <- NULL

depth_model_list <- list(adult = adult_biomass, imm = imm_biomass, total = total_biomass)
depth_model_list <- depth_model_list[!sapply(depth_model_list, is.null)]
depth_model_list
signif(max(depth_model_list$adult$gradients), digits = 8)
signif(max(depth_model_list$imm$gradients), digits = 8)
signif(max(depth_model_list$total$gradients), digits = 8)
write_grads <- function(x, macro, ...) {
  paste0("\\newcommand{\\", macro, "}{", x, "}") %>%
    readr::write_lines("model-grads.tex", append = TRUE)
}
write_grads(signif(max(depth_model_list$adult$gradients), digits = 8), paste0("oct2020-", spp, "-mat"))
write_grads(signif(max(depth_model_list$imm$gradients), digits = 8), paste0("oct2020-", spp, "-imm"))
write_grads(signif(max(depth_model_list$total$gradients), digits = 8), paste0("oct2020-", spp, "-total"))
write_knots <- function(x, macro, ...) {
  paste0("\\newcommand{\\", macro, "}{", x, "}") %>%
    readr::write_lines("model-knots.tex", append = TRUE)
}
write_knots(nrow(depth_model_list$adult$spde$loc_centers), paste0(spp, "-mat"))
write_knots(nrow(depth_model_list$imm$spde$loc_centers), paste0(spp, "-imm"))
write_knots(nrow(depth_model_list$total$spde$loc_centers), paste0(spp, "-total"))


pbs-assess/gfranges documentation built on Dec. 13, 2021, 4:50 p.m.