From fe8ac35eb1f8852f19b42ab2dbcf00cdc7514670 Mon Sep 17 00:00:00 2001 From: Chantel Wetzel - NOAA Federal Date: Fri, 13 Dec 2024 07:41:55 -0800 Subject: [PATCH] clean up general run script --- data-raw/configuration.R | 90 +++++++++------------------------------- 1 file changed, 19 insertions(+), 71 deletions(-) diff --git a/data-raw/configuration.R b/data-raw/configuration.R index 2c67904..fdc905c 100644 --- a/data-raw/configuration.R +++ b/data-raw/configuration.R @@ -1,42 +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") )) -configuration <- configuration[39:40,] -configuration$spatiotemporal1 <- "off" -configuration$spatiotemporal2 <- "off" - -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::mutate(region = ifelse(latitude > 40.1666667, "N", "S"))) - ) %>% + )) + ) |> dplyr::ungroup() -# Confirm no data in the south in 2007: -dplyr::filter(data$data_filtered[[2]], catch_weight > 0) |> - dplyr::group_by(region, year) |> - dplyr::summarise(n = n()) - - - -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))), @@ -48,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 - ) - )