diff --git a/DESCRIPTION b/DESCRIPTION index 562697a..f751a39 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: indexwc Title: Run indices for West Coast Groundfish assessments -Version: 0.6 +Version: 0.7 Authors@R: c( person(c("Kelli", "F."), "Johnson", , "kelli.johnson@noaa.gov", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-5149-451X")), @@ -53,5 +53,6 @@ Remotes: github::r4ss/r4ss Encoding: UTF-8 LazyData: true +LazyDataCompression: xz Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.2 diff --git a/R/lookup_grid.R b/R/lookup_grid.R index 701d704..f6ed852 100644 --- a/R/lookup_grid.R +++ b/R/lookup_grid.R @@ -82,7 +82,11 @@ lookup_grid <- function(x, latitude, depth, depth_scaled, - depth_scaled_squared + depth_scaled_squared, + split_mendocino, + split_conception, + split_monterey, + split_state ) year_grid <- purrr::map_dfr( diff --git a/R/run_sdmtmb.R b/R/run_sdmtmb.R index b4a7b51..3014d1f 100644 --- a/R/run_sdmtmb.R +++ b/R/run_sdmtmb.R @@ -8,8 +8,14 @@ #' mesh that is created by {fmesher}. More knots is not always better. The #' default is to use 500 knots. Future work will look at specifying a #' threshold distance between points rather than number of knots. +#' @param share_range Logical, whether or not to share the range between the +#' spatial and spatiotemporal fields. This defaults to FALSE, but adds extra +#' parameters. The default in sdmTMB is TRUE, and sharing the range may improve +#' estimation for data limited applications +#' @param sdmtmb_control Optional list, in the format of [sdmTMB::sdmTMBcontrol()]. +#' By default, this is includes 3 newton loops #' @param ... Optional arguments passed to [sdmTMB::sdmTMB()]. Note that users -#' cannot pass `anisotropy` or `sdmTMBcontrol` because both of these are set +#' cannot pass `anisotropy` because this is set #' internal to this function, where `anisotropy = TRUE` because the coastline #' of the western coastline of the U.S.A. is not perpendicular to the country #' and three newton loops are specified in the control parameters. @@ -24,6 +30,8 @@ run_sdmtmb <- function(dir_main = getwd(), family, formula, n_knots = 500, + share_range = FALSE, + sdmtmb_control = sdmTMB::sdmTMBcontrol(newton_loops = 3), ...) { # Checks stopifnot(inherits(family, "family")) @@ -110,8 +118,8 @@ run_sdmtmb <- function(dir_main = getwd(), data = data_truncated, mesh = mesh, family = family, - control = sdmTMB::sdmTMBcontrol(newton_loops = 3), - share_range = FALSE, + control = sdmtmb_control, + share_range = share_range, ... ) # Refit the model if the hessian is not positive definite diff --git a/data-raw/configuration.R b/data-raw/configuration.R index a144e15..fdc905c 100644 --- a/data-raw/configuration.R +++ b/data-raw/configuration.R @@ -1,30 +1,28 @@ -# TODO list -# * Only pull data once per species and then combine the pull results with the -# configuration matrix again -# * Fix how format_data returns an object without the nwfscSurvey class instead -# it is class(data) > [1] "tbl_df" "tbl" "data.frame" -# * for vessel_year, might want a different level scaling things might not have -# to give this to grid +# Read in the configuration file within data-raw that define what model set-up +# to apply by species configuration <- tibble::as_tibble(read.csv( file.path("data-raw", "configuration.csv") )) -data <- configuration %>% +# Download the data and filter the data based upon species-specific +# depths and latitudes in the configuration file +data <- configuration |> # Row by row ... do stuff then ungroup - dplyr::rowwise() %>% + dplyr::rowwise() |> # Pull the data based on the function found in fxn column dplyr::mutate( data_raw = list(format_data(eval(parse(text = fxn)))), - data_filtered = list(data_raw %>% + data_filtered = list(data_raw |> dplyr::filter( depth <= min_depth, depth >= max_depth, latitude >= min_latitude, latitude <= max_latitude, year >= min_year, year <= max_year )) - ) %>% + ) |> dplyr::ungroup() -best <- data %>% +# Run the model across all species in the configuration file +best <- data |> dplyr::mutate( # Evaluate the call in family family = purrr::map(family, .f = ~ eval(parse(text = .x))), @@ -36,56 +34,18 @@ best <- data %>% family = family, anisotropy = anisotropy, n_knots = knots, + share_range = share_range, spatiotemporal = purrr::map2(spatiotemporal1, spatiotemporal2, list) ), .f = indexwc::run_sdmtmb ) ) -indices <- dir( - file.path("canary_rockfish", "wcgbts"), - pattern = "sdmTMB_save", - recursive = TRUE, - full.names = TRUE -) %>% - unlist() %>% - purrr::map_df( - .f = function(x) { - load(x) - if (exists("index_areas")) { - return(data.frame(i = x, index_areas)) - } else { - return(NULL) - } - } - ) -gg <- ggplot2::ggplot( - data = indices %>% - dplyr::mutate( - dist = basename(dirname(dirname(i))) - ) %>% - dplyr::filter(area == "coastwide"), - ggplot2::aes(x = year, y = est, lty = dist, col = dist, group = i) -) + - ggplot2::geom_line() + - ggplot2::theme_bw() -ggsave(gg, filename = "indexwc_copper_rockfish.png") - +# TODO list +# * Only pull data once per species and then combine the pull results with the +# configuration matrix again +# * Fix how format_data returns an object without the nwfscSurvey class instead +# it is class(data) > [1] "tbl_df" "tbl" "data.frame" +# * for vessel_year, might want a different level scaling things might not have +# to give this to grid -best <- data[2, ] %>% - dplyr::mutate( - # Evaluate the call in family - family = purrr::map(family, .f = ~ eval(parse(text = .x))), - # Run the model on each row in data - results = purrr::pmap( - .l = list( - data = data_filtered, - formula = formula, - family = family, - anisotropy = anisotropy - ), - spatiotemporal = list("iid", "off"), - n_knots = 200, - .f = indexwc::run_sdmtmb - ) - ) diff --git a/data-raw/configuration.csv b/data-raw/configuration.csv index 4d4f68f..14f317f 100644 --- a/data-raw/configuration.csv +++ b/data-raw/configuration.csv @@ -1,41 +1,41 @@ -species,fxn,source,family,formula,min_depth,max_depth,min_latitude,max_latitude,min_year,max_year,anisotropy,knots,spatiotemporal1,spatiotemporal2 -"petrale sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-675,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"petrale sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","Triennial",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-366,37,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"canary rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-275,-Inf,Inf,-Inf,Inf,FALSE,200,"iid","off" -"canary rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","Triennial",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-366,37,Inf,-Inf,Inf,TRUE,200,"iid","off" -"rex sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-700,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"rex sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","Triennial",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-500,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"shortspine thornyhead","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled + depth_scaled + depth_scaled_squared",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"shortspine thornyhead","nwfscSurvey::pull_catch(common_name = species,survey=source)","Triennial",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + depth_scaled + depth_scaled_squared",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"sablefish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"sablefish","nwfscSurvey::pull_catch(common_name = species,survey=source)","Triennial",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"sablefish","nwfscSurvey::pull_catch(common_name = species,survey=source)","AFSC.Slope",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"sablefish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Slope",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"arrowtooth flounder","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-Inf,34,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"aurora rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-183,-Inf,-Inf,Inf,-Inf,Inf,TRUE,250,"off","iid" -"big skate","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-500,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"blackgill rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear",-183,-850,35,Inf,-Inf,Inf,TRUE,200,"off","off" -"bocaccio","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-500,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"chilipepper","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-500,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"curlfin sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::tweedie(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-450,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"darkblotched rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-675,33.5,Inf,-Inf,Inf,TRUE,250,"off","iid" -"Dover sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"English sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-475,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"flathead sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-450,41,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"greenspotted rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-350,-Inf,Inf,-Inf,Inf,TRUE,200,"iid","iid" -"greenstriped rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear",-55,-675,-Inf,Inf,-Inf,Inf,TRUE,300,"iid","iid" -"lingcod","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-450,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"longnose skate","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"longspine thornyhead","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::tweedie(),"catch_weight ~ 0 + fyear",-300,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"Pacific cod","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-300,39,Inf,-Inf,Inf,TRUE,200,"iid","iid" -"Pacific ocean perch","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-500,35,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"Pacific sanddab","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-500,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"Pacific spiny dogfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-700,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid" -"redbanded rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear",-55,-675,33.5,Inf,-Inf,Inf,TRUE,200,"off","iid" -"rosethorn rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-675,-Inf,Inf,-Inf,Inf,TRUE,200,"iid","off" -"sharpchin rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-475,33,Inf,-Inf,Inf,TRUE,200,"iid","off" -"splitnose rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-700,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","off" -"stripetail rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear",-55,-500,-Inf,Inf,-Inf,Inf,TRUE,500,"off","iid" -"widow rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-675,33.5,Inf,-Inf,Inf,TRUE,200,"off","off" -"yelloweye rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-325,34.5,Inf,-Inf,Inf,TRUE,200,"off","off" -"yellowtail rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-425,33.5,Inf,-Inf,Inf,TRUE,500,"iid","iid" +species,fxn,source,family,formula,min_depth,max_depth,min_latitude,max_latitude,min_year,max_year,anisotropy,knots,spatiotemporal1,spatiotemporal2,share_range +"petrale sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-675,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"petrale sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","Triennial",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-366,37,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"canary rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-275,-Inf,Inf,-Inf,Inf,FALSE,200,"iid","off",FALSE +"canary rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","Triennial",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-366,37,Inf,-Inf,Inf,TRUE,200,"iid","off",FALSE +"rex sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-700,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"rex sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","Triennial",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-500,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"shortspine thornyhead","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled + depth_scaled + depth_scaled_squared",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"shortspine thornyhead","nwfscSurvey::pull_catch(common_name = species,survey=source)","Triennial",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + depth_scaled + depth_scaled_squared",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"sablefish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"sablefish","nwfscSurvey::pull_catch(common_name = species,survey=source)","Triennial",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"sablefish","nwfscSurvey::pull_catch(common_name = species,survey=source)","AFSC.Slope",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"sablefish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Slope",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"arrowtooth flounder","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-Inf,34,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"aurora rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-183,-Inf,-Inf,Inf,-Inf,Inf,TRUE,250,"off","iid",FALSE +"big skate","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-500,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"blackgill rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear",-183,-850,35,Inf,-Inf,Inf,TRUE,200,"off","off",FALSE +"bocaccio","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-500,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"chilipepper","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-500,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"curlfin sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::tweedie(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-450,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"darkblotched rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-675,33.5,Inf,-Inf,Inf,TRUE,250,"off","iid",FALSE +"Dover sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"English sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-475,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"flathead sole","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-450,41,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"greenspotted rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear",-55,-350,-Inf,Inf,-Inf,Inf,TRUE,200,"iid","iid",FALSE +"greenstriped rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear",-55,-675,-Inf,Inf,-Inf,Inf,TRUE,300,"iid","iid",FALSE +"lingcod","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-450,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"longnose skate","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"longspine thornyhead","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::tweedie(),"catch_weight ~ 0 + fyear",-300,-Inf,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"Pacific cod","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-300,39,Inf,-Inf,Inf,TRUE,200,"iid","iid",FALSE +"Pacific ocean perch","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-500,35,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"Pacific sanddab","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-500,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"Pacific spiny dogfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-700,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","iid",FALSE +"redbanded rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear",-55,-675,33.5,Inf,-Inf,Inf,TRUE,200,"off","iid",FALSE +"rosethorn rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-675,-Inf,Inf,-Inf,Inf,TRUE,200,"iid","off",FALSE +"sharpchin rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_lognormal(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-475,33,Inf,-Inf,Inf,TRUE,200,"iid","off",FALSE +"splitnose rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-700,-Inf,Inf,-Inf,Inf,TRUE,500,"iid","off",FALSE +"stripetail rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear",-55,-500,-Inf,Inf,-Inf,Inf,TRUE,500,"off","iid",FALSE +"widow rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-675,33.5,Inf,-Inf,Inf,TRUE,200,"off","off",FALSE +"yelloweye rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-325,34.5,Inf,-Inf,Inf,TRUE,200,"off","off",FALSE +"yellowtail rockfish","nwfscSurvey::pull_catch(common_name = species,survey=source)","NWFSC.Combo",sdmTMB::delta_gamma(),"catch_weight ~ 0 + fyear + pass_scaled",-55,-425,33.5,Inf,-Inf,Inf,TRUE,400,"iid",off,TRUE diff --git a/data-raw/configuration_yellowtail.R b/data-raw/configuration_yellowtail.R new file mode 100644 index 0000000..93fc403 --- /dev/null +++ b/data-raw/configuration_yellowtail.R @@ -0,0 +1,83 @@ +# TODO list +# * move dplyr::mutate(split_mendocino = ifelse(latitude > 40.1666667, "N", "S"))) to data formatting -- making +# columns available for user +# * could automate coefficient mapping + +library(dplyr) +library(indexwc) +configuration <- tibble::as_tibble(read.csv( + file.path("data-raw", "configuration.csv") +)) + +configuration <- configuration |> + dplyr::filter(species == "yellowtail rockfish") +# Change the covariates to include a split at cape mendocino +configuration$formula <- "catch_weight ~ 0 + fyear*split_mendocino + pass_scaled" + +data <- configuration |> + # Row by row ... do stuff then ungroup + dplyr::rowwise() |> + # Pull the data based on the function found in fxn column + dplyr::mutate( + data_raw = list(format_data(eval(parse(text = fxn)))), + data_filtered = list(data_raw |> + dplyr::filter( + depth <= min_depth, depth >= max_depth, + latitude >= min_latitude, latitude <= max_latitude, + year >= min_year, year <= max_year + ) |> + dplyr::mutate(split_mendocino = ifelse(latitude > 40.1666667, "N", "S"))) + ) |> + dplyr::ungroup() + +# Confirm no data in the south in 2007: +dplyr::filter(data$data_filtered[[1]], catch_weight > 0) |> + dplyr::group_by(split_mendocino, year) |> + dplyr::summarise(n = dplyr::n()) + +# Find variables that aren't identifiable for presence-absence model +lm <- lm(formula = as.formula(configuration$formula), + data = data$data_filtered[[1]]) +#not_identifiable <- names(which(is.na(coef(lm)))) +# Find variables that aren't identifiable for positive model +lm_pos <- lm(formula = as.formula(configuration$formula), + data = dplyr::filter(data$data_filtered[[1]], catch_weight>0)) +pos_not_identifiable <- names(which(is.na(coef(lm_pos)))) + +# Create variables to be not estimated/ mapped off +coef_names <- names(coef(lm)) +.map_pos <- coef_names +.map_pos[coef_names %in% pos_not_identifiable] <- NA +.map_pos <- factor(.map_pos) +.start_pos <- rep(0, length(coef_names)) +.start_pos[coef_names %in% pos_not_identifiable] <- -20 + +best <- data |> + dplyr::mutate( + # Evaluate the call in family + family = purrr::map(family, .f = ~ eval(parse(text = .x))), + # Run the model on each row in data + results = purrr::pmap( + .l = list( + data = data_filtered, + formula = formula, + family = family, + anisotropy = anisotropy, + n_knots = knots, + share_range = share_range, + spatiotemporal = purrr::map2(spatiotemporal1, spatiotemporal2, list), + sdmtmb_control = list( + sdmTMB::sdmTMBcontrol( + map = list(b_j = .map_pos, b_j2 = .map_pos), + start = list(b_j = .start_pos, b_j2 = .start_pos), + newton_loops = 3 + ) + ) + ), + .f = indexwc::run_sdmtmb + ) + ) + + + + diff --git a/data-raw/grid_nwfscSurvey.R b/data-raw/grid_nwfscSurvey.R index 23ef8f9..0e0c439 100644 --- a/data-raw/grid_nwfscSurvey.R +++ b/data-raw/grid_nwfscSurvey.R @@ -1,18 +1,24 @@ path <- fs::path(tempdir(), "california_current_grid.rda") url <- "https://github.com/James-Thorson-NOAA/FishStatsUtils/raw/main/data/california_current_grid.rda" -download.file( +downloader::download( url = url, destfile = path ) + load(path) unlink(path) -california_current_grid <- california_current_grid %>% - dplyr::rowwise() %>% +california_current_grid <- california_current_grid |> + dplyr::rowwise() |> dplyr::mutate( pass_scaled = 0, vessel_year = 0, + region = 0, + split_mendocino = dplyr::case_when(Lat > 40.1666667 ~"N", .default = "S"), + split_conception = dplyr::case_when(Lat > 34.45 ~"N", .default = "S"), + split_monterey = dplyr::case_when(Lat > 36.0 ~"N", .default = "S"), + split_state = dplyr::case_when(Lat > 46.25 ~"W", Lat < 42.0 ~"C", .default = "O"), propInTriennial = ifelse(Lat < 34.5, 0, propInTriennial), depth = Depth_km * -1000, dplyr::across( @@ -26,12 +32,13 @@ california_current_grid <- california_current_grid %>% ) ) +utm_zone_10 <- 32610 california_current_grid <- suppressWarnings(sdmTMB::add_utm_columns( california_current_grid, utm_crs = utm_zone_10, ll_names = c("Lon", "Lat"), utm_names = c("x", "y") -)) %>% +)) |> dplyr::rename( longitude = "Lon", latitude = "Lat" diff --git a/data-raw/internal.R b/data-raw/internal.R index f7e4572..73c542f 100644 --- a/data-raw/internal.R +++ b/data-raw/internal.R @@ -1,5 +1,5 @@ southern_BC <- 49.0 -southern_WA <- 46.0 +southern_WA <- 46.25 southern_OR <- 42.0 southern_CA <- 32.0 diff --git a/data/california_current_grid.rda b/data/california_current_grid.rda index 3eec0f2..fd36458 100644 Binary files a/data/california_current_grid.rda and b/data/california_current_grid.rda differ diff --git a/man/run_sdmtmb.Rd b/man/run_sdmtmb.Rd index 6dbfb09..d8f13ec 100644 --- a/man/run_sdmtmb.Rd +++ b/man/run_sdmtmb.Rd @@ -4,7 +4,16 @@ \alias{run_sdmtmb} \title{Run \code{\link[sdmTMB:sdmTMB]{sdmTMB::sdmTMB()}}} \usage{ -run_sdmtmb(dir_main = getwd(), data, family, formula, n_knots = 500, ...) +run_sdmtmb( + dir_main = getwd(), + data, + family, + formula, + n_knots = 500, + share_range = FALSE, + sdmtmb_control = sdmTMB::sdmTMBcontrol(newton_loops = 3), + ... +) } \arguments{ \item{dir_main}{A string specifying a path where results will be saved. The @@ -46,8 +55,16 @@ mesh that is created by {fmesher}. More knots is not always better. The default is to use 500 knots. Future work will look at specifying a threshold distance between points rather than number of knots.} +\item{share_range}{Logical, whether or not to share the range between the +spatial and spatiotemporal fields. This defaults to FALSE, but adds extra +parameters. The default in sdmTMB is TRUE, and sharing the range may improve +estimation for data limited applications} + +\item{sdmtmb_control}{Optional list, in the format of \code{\link[sdmTMB:sdmTMBcontrol]{sdmTMB::sdmTMBcontrol()}}. +By default, this is includes 3 newton loops} + \item{...}{Optional arguments passed to \code{\link[sdmTMB:sdmTMB]{sdmTMB::sdmTMB()}}. Note that users -cannot pass \code{anisotropy} or \code{sdmTMBcontrol} because both of these are set +cannot pass \code{anisotropy} because this are set internal to this function, where \code{anisotropy = TRUE} because the coastline of the western coastline of the U.S.A. is not perpendicular to the country and three newton loops are specified in the control parameters.}