Skip to content

Commit

Permalink
clean up general run script
Browse files Browse the repository at this point in the history
  • Loading branch information
chantelwetzel-noaa committed Dec 13, 2024
1 parent 01cce47 commit fe8ac35
Showing 1 changed file with 19 additions and 71 deletions.
90 changes: 19 additions & 71 deletions data-raw/configuration.R
Original file line number Diff line number Diff line change
@@ -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))),
Expand All @@ -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
)
)

0 comments on commit fe8ac35

Please sign in to comment.