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)

theme_set(
    gfplot::theme_pbs(base_size = 14) 
)
species <- params$species
region <- params$region
covariates <- params$covariates
covs <- params$covs
knots <- params$knots
priors <- FALSE

paste("region =", region)
paste("model covariates =", covariates)
paste("model label =", covs)
paste("priors =", priors)
paste("knots =", knots)
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
}

Build spatiotemporal density model

Load and filter data

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

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

# covars <- readRDS("data/event-covariates.rds")
# data <- dplyr::left_join(biomass, covars)
data <- biomass
# 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))
)
data <- data %>%
  mutate(depth = raw_depth) %>%
  filter(ssid %in% model_ssid) 

Make mesh

if (region == "Both odd year surveys") {
  spde <- sdmTMB::make_spde(data$X, data$Y, n_knots = 250)
}

if (region == "West Coast Vancouver Island") {
  spde <- sdmTMB::make_spde(data$X, data$Y, n_knots = 200)
}

if (region == "West Coast Haida Gwaii") {
  spde <- sdmTMB::make_spde(data$X, data$Y, n_knots = 200)
}

if (region == "All synoptic surveys") {
  spde <- sdmTMB::make_spde(data$X, data$Y, n_knots = knots)
}

sdmTMB::plot_spde(spde)

Run climate independent sdmTMB model

if (params$update_model) {
  tictoc::tic()
  if (any(names(data) == "adult_density")) {
    adult_formula <- as.formula(paste(
      "adult_density ~ 0 + as.factor(year)", covariates, ""
    ))

    adult_biomass <- sdmTMB::sdmTMB(
      data = data,
      adult_formula,
      time_varying = ~ 0 + depth_scaled + depth_scaled2, #+ trawled
      time = "year", spde = spde,
      family = tweedie(link = "log"),
      ar1_fields = params$AR1,
      include_spatial = params$fixed_spatial,
      reml = TRUE,
      enable_priors = priors,
      # control = sdmTMBcontrol(step.min = 0.01, step.max = 1),
      silent = FALSE
    )

    saveRDS(adult_biomass,
      file = paste0(
        "data/", spp,
        "/mod-mat-biomass-", spp, covs, "-", ssid_string, "-ar1-", params$AR1, "-reml.rds"
      )
    )


   try({ imm_formula <- as.formula(paste(
      "imm_density ~ 0 + as.factor(year)", covariates, ""
    ))

    imm_biomass <- sdmTMB::sdmTMB(
      data = data,
      imm_formula,
      time_varying = ~ 0 + depth_scaled + depth_scaled2, #+ trawled
      time = "year", spde = spde,
      family = tweedie(link = "log"),
      ar1_fields = params$AR1, # changed to TRUE on Oct 17 2019
      reml = TRUE,
      include_spatial = params$fixed_spatial,
      enable_priors = priors,
      control = sdmTMBcontrol(step.min = 0.01, step.max = 1),
      silent = FALSE
    )

    saveRDS(imm_biomass,
      file = paste0("data/", spp, "/mod-imm-biomass-", 
        spp, covs, "-", ssid_string, "-ar1-", params$AR1, "-reml.rds"))
   })

  } else {
    dens_formula <- as.formula(paste("density ~ 0 + as.factor(year)", covariates, ""))
    # dens_formula <- as.formula(paste("density ~ 0"))

    total_biomass <- sdmTMB::sdmTMB(
      data = data,
      dens_formula,
      time_varying = ~ 0 + depth_scaled + depth_scaled2, # + trawled
      time = "year", spde = spde,
      family = tweedie(link = "log"),
      ar1_fields = params$AR1,
      reml = TRUE,
      include_spatial = params$fixed_spatial,
      enable_priors = priors,
      # control = sdmTMBcontrol(step.min = 0.01, step.max = 1),
      silent = FALSE
    )

    saveRDS(total_biomass, file = paste0(
      "data/", spp,
      "/model-total-biomass-", spp, covs, "-", ssid_string, "-ar1-", params$AR1, "-reml.rds"
    ))
  }
  tictoc::toc()
}

Check residuals

if(params$update_model_check) {
try({
  biomass <- readRDS(paste0(
    "data/", spp,
    "/mod-mat-biomass-", spp, covs, "-", ssid_string, "-ar1-", params$AR1, "-reml.rds"
  ))
})

try({
  biomass <- readRDS(paste0(
    "data/", spp,
    "/model-total-biomass-", spp, covs, "-", ssid_string, "-ar1-", params$AR1, "-reml.rds"
  ))
})

point_predictions <- predict(biomass)
point_predictions$residuals <- residuals(biomass)

saveRDS(point_predictions, file = paste0(
  "data/", spp,
  "/check-mod-predictions-", spp, covs, "-", ssid_string, "-ar1-", params$AR1, "-reml.rds"
))
}
depth_only_predictions <- readRDS(paste0(
  "data/", spp,
  "/check-mod-predictions-", spp, covs, "-", ssid_string, "-ar1-", params$AR1, "-reml.rds"
))

depth_only_predictions <- filter(
  depth_only_predictions,
  # ssid %in% c(1,3)) %>% filter (
  year > 2003
)

try({
  g <- ggplot(depth_only_predictions, aes(adult_density, residuals, colour = adult_density)) +
    geom_point() + scale_x_continuous(trans = "log10") + facet_wrap(~year)
})

try({
  g <- ggplot(depth_only_predictions, aes(density, residuals, colour = density)) +
    geom_point() + scale_x_continuous(trans = "log10") + facet_wrap(~year)
})

g
try({
  g2 <- ggplot(depth_only_predictions, aes((est), residuals, colour = adult_density)) +
    geom_point() + # scale_x_continuous(trans = 'log10') +
    facet_wrap(~year)
})

try({
  g2 <- ggplot(depth_only_predictions, aes((est), residuals, colour = density)) +
    geom_point() + # scale_x_continuous(trans = 'log10') +
    facet_wrap(~year)
})

g2
ggplot(depth_only_predictions, aes(X, Y, colour = (residuals))) +
  geom_point() +
  scale_colour_gradient2() +
  scale_x_continuous(trans = "log10") +
  facet_wrap(~year)

Time-varying depth plots

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

try({
  adult_biomass <- readRDS(paste0(
    "data/", spp,
    "/mod-mat-biomass-", spp, covs, "-", ssid_string, "-ar1-", params$AR1, "-reml.rds"
  ))
})
# adult_biomass<-sdmTMB:::update_model(adult_biomass)

try({
  imm_biomass <- readRDS(paste0("data/", spp, "/mod-imm-biomass-", 
         spp, covs, "-", ssid_string, "-ar1-", params$AR1, "-reml.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, "-", ssid_string, "-ar1-", params$AR1,"-reml.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)]


d <- list()
depth_plots <- list()
for (i in seq_len(length(depth_model_list))) {
  #if (depth_model_list[[i]]$model$convergence == 0) {
    d[[i]] <- time_varying_density(depth_model_list[[i]], predictor = "depth")

    if (length(d[[i]]) == 0) {
      depth_plots[[i]] <- grid::grid.rect(gp = grid::gpar(col = "white"))
    } else {
      d[[i]]$x <- exp(d[[i]]$x)
      depth_plots[[i]] <- plot_mountains(d[[i]], variable_label = "Depth (without environmental variables)", xlimits = c(0, max_depth_found)) +
        ggtitle(paste(species, names(depth_model_list[i])))
    }
  # } else {
  #   depth_plots[[i]] <- grid::grid.rect(gp = grid::gpar(col = "white"))
  # }
}
print(depth_plots)
png(
  file = paste0(
    "figs/", spp,
    "/depth-", spp, covs, "-1n3-ar1-reml.png"
  ),
  res = 600,
  units = "in",
  width = 8.5,
  height = 6
)
gridExtra::grid.arrange(
  grobs = c(depth_plots),
  nrow = 2,
  top = grid::textGrob(paste(species, "(", covs, ")"))
)
dev.off()

Model summary

depth_model_list

Plot biomass predictions

Save predictions for spatial grid

rm(ad_predictions)
rm(im_predictions)
rm(predicted)

if(params$update_predictions) {
  # nd_all <- readRDS(paste0("data/nd_just_depth.rds"))
  # nd_all <- readRDS(paste0("data/nd_whole_coast_index.rds"))
  nd_all <- readRDS(paste0("data/nd_odd.rds"))

if (any(names(data) == "adult_density")) {

try({
  adult_biomass <- readRDS(paste0(
    "data/", spp,
    "/mod-mat-biomass-", spp, covs, "-", ssid_string, "-ar1-", params$AR1, "-reml.rds"
  ))

# adult_biomass<-sdmTMB:::update_model(adult_biomass)
    nd <- nd_all %>%
      filter(ssid %in% model_ssid) %>%
      filter(year %in% unique(adult_biomass$data$year))
    nd <- na.omit(nd)
    nd$year <- as.integer(nd$year)


    ad_predictions <- predict(adult_biomass, newdata = nd, return_tmb_object = TRUE)
    saveRDS(ad_predictions, file = paste0(
      "data/", spp,
      "/predictions-", spp, covs, "-", ssid_string, "-mature-biomass-ar1-", params$AR1, "-reml.rds"
    ))
})

  try({
    imm_biomass <- readRDS(paste0(
      "data/", spp,
      "/mod-imm-biomass-", spp, covs, "-", ssid_string, "-ar1-", params$AR1, "-reml.rds"
    ))
    nd <- nd_all %>%
      filter(ssid %in% model_ssid) %>%
      filter(year %in% unique(imm_biomass$data$year))
    nd <- na.omit(nd)
    nd$year <- as.integer(nd$year)
    im_predictions <- predict(imm_biomass, newdata = nd, return_tmb_object = TRUE)

    saveRDS(im_predictions, file = paste0(
      "data/", spp,
      "/predictions-", spp, covs, "-", ssid_string, "-imm-biomass-ar1-", params$AR1, "-reml.rds"
    ))
  })

} else {

  total_biomass <- readRDS(paste0(
    "data/", spp,
    "/model-total-biomass-", spp, covs, "-", ssid_string, "-ar1-", params$AR1, "-reml.rds"
  ))

  nd <- nd_all %>%
    filter(ssid %in% model_ssid) %>%
    filter(year %in% unique(total_biomass$data$year))
  nd <- na.omit(nd)
  nd$year <- as.integer(nd$year)

  predicted <- predict(total_biomass, newdata = nd, return_tmb_object = TRUE)

  saveRDS(predicted, file = paste0(
    "data/", spp,
    "/predictions-", spp, covs, "-", ssid_string, "-total-biomass-ar1-", params$AR1,"-reml.rds"
  ))
}
}

Transform estimates to kg/ha

rm(adult_predictions)
rm(imm_predictions)
rm(predictions)

rm(ad_predictions)
rm(im_predictions)
rm(predicted)

try({ad_predictions <- readRDS(paste0("data/", spp,
    "/predictions-", spp, covs, "-", ssid_string, "-mature-biomass-ar1-", params$AR1, "-reml.rds"
    ))
  })
  try({im_predictions <- readRDS(paste0("data/", spp,
    "/predictions-", spp, covs, "-", ssid_string, "-imm-biomass-ar1-", params$AR1, "-reml.rds"
    ))
  })
  if (!exists("ad_predictions")) {
  try({
    predicted <- readRDS(paste0("data/", spp,
    "/predictions-", spp, covs, "-", ssid_string, "-total-biomass-ar1-", params$AR1,"-reml.rds"
    ))
  })
  }

if (exists("ad_predictions")) {
  # to convert from kg/m2 to kg/hectare multiply by 10000
  adult_predictions <- ad_predictions$data
  adult_predictions$est_exp <- exp(adult_predictions$est) * 10000


  try({
    imm_predictions <- im_predictions$data
    imm_predictions$est_exp <- exp(imm_predictions$est) * 10000

    adult_predictions$total_bio <- imm_predictions$est_exp +   
        adult_predictions$est_exp
    imm_predictions$prop_imm <- imm_predictions$est_exp /
        (adult_predictions$est_exp + imm_predictions$est_exp)


  saveRDS(imm_predictions, file = paste0("data/", spp,
    "/sopo-predictions-", spp, covs, "-imm-biomass-ar1-", 
    params$AR1, "-reml.rds"))
  })

  saveRDS(adult_predictions, file = paste0("data/", spp,
    "/sopo-predictions-", spp, covs, "-mat-biomass-ar1-", 
    params$AR1, "-reml.rds"))

  max_raster <- quantile(adult_predictions$est_exp, 0.999)
  max_adult <- signif(max(adult_predictions$est_exp), digits = 2)
  max_imm <- signif(max(imm_predictions$est_exp), digits = 2)

  model_ssid <- unique(adult_predictions$ssid)
} else {

  predictions <- predicted$data
  predictions$est_exp <- exp(predictions$est) * 10000
  predictions$total_bio <- exp(predictions$est) * 10000
  max_raster <- quantile(predictions$total_bio, .99)
  max_bio <- signif(quantile(predictions$total_bio, .999), digits = 2)

  saveRDS(predictions, file = paste0(
    "data/", spp,
    "/sopo-predictions-", spp, covs, "-total-biomass-ar1-", params$AR1, "-reml.rds"
  ))

  #model_ssid <- unique(predictions$ssid)
}
legend_coords <- c(0.9, 0.17) #"none"

if (exists("adult_predictions")) {

 p_adult_all <- adult_predictions %>% 
   mutate(x = X, y = Y, X = 2 * round(X/2), Y = 2 * round(Y/2)) %>%  
   plot_facet_map("est_exp", 
      raster_limits = c(0, max_raster),
      legend_position =  legend_coords, 
      transform_col = fourth_root_power
    ) +
    labs(fill = "kg/ha") +
    ggtitle(paste0("", species, " mature biomass \n(max = ", max_adult, " kg/ha)"))

 print(p_adult_all)

try({
p_imm_all <- imm_predictions %>%
  mutate(x = X, y = Y, X = 2 * round(X/2), Y = 2 * round(Y/2)) %>%
  plot_facet_map("est_exp",
    # raster_limits = c(0, max_raster),
    legend_position =  legend_coords, 
    transform_col = fourth_root_power
  ) +
    labs(fill = "kg/ha") +
    ggtitle(paste0("", species, " immature biomass \n(max = ", max_imm, " kg/ha)"))

print(p_imm_all)
   })

} else {

p_adult_all <- predictions  %>% 
  mutate(x = X, y = Y, X = 2 * round(X/2), Y = 2 * round(Y/2)) %>% 
  plot_facet_map("total_bio",
    raster_limits = c(0, max_raster),
    legend_position =  legend_coords, 
    transform_col = fourth_root_power
  ) +
    labs(fill = "kg/ha") +
    ggtitle(paste0("", species, " total biomass \n(max = ", max_bio, " kg/ha)"))

  print(p_adult_all)
}

Calculate index of total biomass for full survey grid

if(params$update_index) {
if (exists("ad_predictions")) {
ind <- get_index(ad_predictions, bias_correct = FALSE)
ind$species <- species
ind$maturity <- "mature"
ind$ssid <- ssid_string
ind$covs <- covs

write.csv(ind, file = paste0("data/_indices/sopo-index-", 
  spp,"-", ssid_string, "-reml.csv"))

try ({
  ind_imm <- get_index(im_predictions, bias_correct = FALSE)
  ind_imm$species <- species
  ind_imm$maturity <- "immature"
  ind_imm$ssid <- ssid_string
  ind_imm$covs <- covs

  write.csv(ind_imm, file = paste0("data/_indices/sopo-index-imm-", 
    spp,"-", ssid_string, "-reml.csv"))
})
} else {
  ind <- get_index(predicted, bias_correct = FALSE)
  ind$species <- species
  ind$maturity <- "all"
  ind$ssid <- ssid_string
  ind$covs <- covs

  write.csv(ind, file = paste0("data/_indices/sopo-index-", 
    spp,"-", ssid_string, "-reml.csv"))
}
}
# scale <- 2 * 2 / 1000 # if density was in kg/km2: 2 x 2 km grid and converted from kg to tonnes 
scale <- 1000000 * 4 /1000 # if density was in kg/m2: m2 to km2 * 4 km2 grid size and kg to tonnes

ind <- read.csv(paste0("data/_indices/sopo-index-", 
  spp,"-", ssid_string, "-reml.csv"))

ind %>% filter(year > 2004) %>%
ggplot(aes(year, est*scale)) + geom_line(col="darkred") +
  geom_ribbon(aes(ymin = lwr*scale, ymax = upr*scale), fill="darkred", alpha = 0.4) +
  xlab('Year') +  
  ylab('Mature biomass estimate (metric tonnes)') +
  gfplot::theme_pbs()

Add immature trend if available

rm(ind_imm)

try({
  ind_imm <- read.csv(paste0("data/_indices/sopo-index-imm-", 
    spp,"-", ssid_string, "-reml.csv"))
})

if (exists("ind_imm")) {
ratio <- max(ind$upr)/max(ind_imm$upr)

ggplot(ind, aes(year, est*scale)) + geom_line(col = "darkred", ) +
  geom_ribbon(aes(ymin = lwr*scale, ymax = upr*scale), fill = "darkred", alpha = 0.4) +
# adding the relative humidity data, transformed to match roughly the range of the temperature
  geom_ribbon(aes(ymin = ind_imm$lwr*scale*ratio, ymax = ind_imm$upr*scale*ratio), 
    fill = "orangered", alpha = 0.4) + 
  geom_line(aes(ind_imm$year, ind_imm$est*scale*ratio), col = "orangered") +
# now adding the secondary axis, following the example in the help file ?scale_y_continuous and, reverting the above transformation
  scale_y_continuous(sec.axis = sec_axis(~./ratio, name = "Immature biomass estimate (metric tonnes)")) +
  xlab('Year') + 
  ylab('Mature biomass estimate (metric tonnes)')+
  gfplot::theme_pbs(base_size = 16)
}


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