Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 2022update #44

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
20 changes: 16 additions & 4 deletions R/VAST_do.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,16 @@ VAST_do <- function(Database, settings, conditiondir, compiledir,
fine_scale = settings[["fine_scale"]],
strata.limits = settings[["strata"]],
#zone = NA, #default
# automatically detecting zone in `FishStatsUtils::make_extrapolation_info()`
# DO NOT allow users to control just use default
FieldConfig = settings[["FieldConfig"]],
RhoConfig = settings[["RhoConfig"]],
OverdispersionConfig = settings[["overdispersion"]],
ObsModel = settings[["ObsModelcondition"]],
bias.correct = TRUE,
#calculate derived quantities of interest, no harm
#Options = ,#default is more than original VASTWestCoast
#use_anisotropy = TRUE, #default
use_anisotropy = settings[["use_anisotropy"]],
Version = settings[["version"]],
#treat_nonencounter_as_zero = TRUE, #default
#VamConfig = c(Method = 0, Rank = 0, Timing = 0), #default
Expand Down Expand Up @@ -130,7 +132,8 @@ VAST_do <- function(Database, settings, conditiondir, compiledir,
model_args = list(CompileDir = compiledir),
silent = TRUE,
run_model = TRUE), error = function(e) e)
if ("simpleError" %in% class(out)) {
if ("simpleError" %in% class(out) |
is.null(out$parameter_estimates$SD)) {
save(out, file = file.path(conditiondir, "Error.RData"))
save(list = ls(all.names = TRUE), file = file.path(conditiondir, "Save.RData"))
return(out)
Expand All @@ -142,6 +145,7 @@ VAST_do <- function(Database, settings, conditiondir, compiledir,
error = function(e) e)

index <- suppressWarnings(FishStatsUtils::plot_biomass_index(
fit = out,
DirName = file.path(conditiondir, .Platform$file.sep),
TmbData = out$data_list, Sdreport = out$parameter_estimates$SD,
use_biascorr = TRUE,
Expand All @@ -154,8 +158,16 @@ VAST_do <- function(Database, settings, conditiondir, compiledir,
parameter_estimates = out$parameter_estimates,
savedir = conditiondir)
fileindex <- file.path(conditiondir, "Table_for_SS3.csv")
indexdata <- utils::read.csv(fileindex)
indexdata[,"Year"] <- out$year_labels[indexdata[,"Year"]]
indexdata <- data.frame(
Year = out$year_labels[index[["Table"]][, "Time"]],
# Change units to mt since converting from kg to mt below
Unit = "mt",
Fleet = index[["Table"]][, "Stratum"],
# Go from kg to mt to keep backwards compatibility with VASTWestCoast
Estimate_metric_tons = index[["Table"]][, "Estimate"] / 1000,
SD_log = index[["Table"]][, "Std. Error for ln(Estimate)"],
SD_mt = index[["Table"]][, "Std. Error for Estimate"] / 1000
)
utils::write.csv(x = indexdata, file = fileindex, row.names = FALSE)
plot_ss(file.in = fileindex,
lab.survey = survey,
Expand Down
5 changes: 3 additions & 2 deletions R/VAST_spp.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
#' }
#'
VAST_spp <- function(dir, species,
dist = c("lognormal", "gamma")) {
dist = c("lognormal", "gamma", "tweedie")) {

#### Get species, survey, and strata info
info <- nwfscSurvey::GetSpp.fn(species)
Expand All @@ -107,7 +107,8 @@ VAST_spp <- function(dir, species,
for (obs in dist) {
obs_model <- switch(obs,
lognormal = c(1, 0),
gamma = c(2, 0)
gamma = c(2, 0),
tweedie = c(10, 2)
)
modeldir <- file.path(sppdir, paste(survey, obs, sep = "_"))
settings <- list(
Expand Down
9 changes: 8 additions & 1 deletion R/get_settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ get_settings <- function(settings = NULL, verbose = FALSE) {
"field" = NULL,
"rho" = NULL,
"fine_scale" = TRUE,
"overdispersion" = NULL)
"overdispersion" = NULL,
"use_anisotropy" = TRUE
)
need <- !names(Settings_all) %in% names(settings)
if (verbose) {
message("Adding the following objects to settings:\n",
Expand Down Expand Up @@ -67,6 +69,11 @@ get_settings <- function(settings = NULL, verbose = FALSE) {
default = c(Beta1 = 0, Beta2 = 0, Epsilon1 = 0, Epsilon2 = 0))
Settings_all$FieldConfig <- get_settings_single(Settings_all$field,
default = c(Omega1 = 1, Epsilon1 = 1, Omega2 = 1, Epsilon2 = 1))
if (Settings_all[["ObsModelcondition"]][1] == 10) {
# Following items are specific to the Tweedie distribution #43
Settings_all[["FieldConfig"]][c("Omega1", "Epsilon1")] <- 0
Settings_all[["RhoConfig"]][c("Beta1")] <- 3
}

# Overdispersion
if (is.null(Settings_all[["overdispersion"]])) {
Expand Down
14 changes: 9 additions & 5 deletions R/plot_qq.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,28 @@ plot_qq <- function(outdir, model) {
n_e <- model[["data_list"]][["n_c"]]
e_i <- model[["data_list"]][["n_e"]]
sigmaM <- (rep(1, n_e) %o% model[["Report"]][["SigmaM"]])[1, 1, 1]
Which <- which(model[["data_list"]][["b_i"]] > 0)
Which <- which(as.vector(model[["data_list"]][["b_i"]]) > 0)

if (model[["data_list"]][["ObsModel_ez"]][1] == 1) {
Qvals <- na.omit(mapply(plnorm,
q = model[["data_list"]][["b_i"]][Which],
meanlog = log(model[["data_list"]][["a_i"]][Which] * exp(model[["Report"]][["P2_iz"]][Which])) -
q = as.vector(model[["data_list"]][["b_i"]])[Which],
meanlog = log(as.vector(model[["data_list"]][["a_i"]])[Which] * exp(model[["Report"]][["P2_iz"]][Which])) -
(sigmaM^2) / 2,
MoreArgs = list(sdlog = sigmaM)))
}
if (model[["data_list"]][["ObsModel_ez"]][1] == 2) {
Qvals <- na.omit(mapply(pgamma,
q = model[["data_list"]][["b_i"]][Which],
q = as.vector(model[["data_list"]][["b_i"]])[Which],
scale = (sigmaM^2) *
(model[["data_list"]][["a_i"]][Which] *
(as.vector(model[["data_list"]][["a_i"]])[Which] *
exp(model[["Report"]][["P2_iz"]][Which])
),
MoreArgs = list(shape = 1 / (sigmaM^2))))
}
if (model[["data_list"]][["ObsModel_ez"]][1] == 10) {
message("QQ plot is not yet implemented for the Tweedie distribution. See #45.")
return(invisible())
}

grDevices::png(file.path(outdir, "VASTWestCoast_QQ.png"), height = 18, width = 18, units = "cm",
res = 300)
Expand Down
6 changes: 3 additions & 3 deletions R/plot_ss.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ plot_ss <- function(file.in = "Table_for_SS3.csv", savefile,
savefile <- normalizePath(savefile, mustWork = FALSE)
dir.create(dirname(savefile), recursive = TRUE, showWarnings = FALSE)

colors <- c("black", "blue", "darkorchid1", "red")
colors <- c("black", "red", "darkorchid1", "blue")
pch.vec <- c(21, 22, 23, 24)
cex.vec <- c(1.6, 1.4, 1.4, 1.4)
cex.lab <- 2.1
cex.axis <- 1.6
colors <- c("black", "blue", "darkorchid1", "red")

if (length(strat) > 4) {
stop("plot_ss is not designed to work with more than 4 strata.",
"\nPlease contact the maintainer(s) to request additional",
"\nfunctionality here or limit the number of strata in the csv.")
}
if ("north" %in% tolower(strat) && length(strat) == 3) {
colors <- c("black", "blue", "red")
colors <- c("black", "red", "blue")
}

grDevices::png(filename = savefile,
Expand Down
4 changes: 3 additions & 1 deletion R/summary_nwfsc.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ summary_nwfsc <- function(obj, parameter_estimates, savedir = NULL) {
"Poisson-link",
switch(as.character(obj$env$data$ObsModel[1]),
"1" = "Lognormal",
"2" = "Gamma")))
"2" = "Gamma",
"10" = "Tweedie"))
)
TableA[6, ] <- c("Spatial effect for encounter probability",
ifelse(obj$env$data$FieldConfig[1, 2] < 0, "No", "Yes"))
TableA[7, ] <- c("Spatio-temporal effect for encounter probability",
Expand Down
2 changes: 1 addition & 1 deletion man/VAST_spp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.