Skip to content

Commit 0c5bc68

Browse files
committed
remove lat, lon filter from example; rewrite code to do so; remove file_name and species code from function, just create file name from common_name; cleanup some vignette output
1 parent a952a80 commit 0c5bc68

File tree

4 files changed

+36
-29
lines changed

4 files changed

+36
-29
lines changed

R/clean_and_resample.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
#' spp_list <- data.frame(
4949
#' srvy = "CA",
5050
#' common_name = "arrowtooth flounder",
51-
#' file_name = "arrowtooth_flounder",
5251
#' filter_lat_gt = 34,
5352
#' filter_lat_lt = NA,
5453
#' filter_depth = NA,
@@ -141,13 +140,16 @@ clean_and_resample <- function(
141140
)
142141

143142
# Apply depth and latitude filters
144-
if (!is.na(spp_info$filter_lat_lt) | is.null(spp_info$filter_lat_lt)) {
143+
if (!is.null(spp_info[["filter_lat_lt"]]) &&
144+
length(spp_info$filter_lat_lt) > 0 && !is.na(spp_info$filter_lat_lt)) {
145145
bio_df <- bio_df |> dplyr::filter(latitude_dd < spp_info$filter_lat_lt)
146146
}
147-
if (!is.na(spp_info$filter_lat_gt) | is.null(spp_info$filter_lat_gt)) {
147+
if (!is.null(spp_info[["filter_lat_gt"]])
148+
&& length(spp_info$filter_lat_gt) > 0 && !is.na(spp_info$filter_lat_gt)) {
148149
bio_df <- bio_df |> dplyr::filter(latitude_dd > spp_info$filter_lat_gt)
149150
}
150-
if (!is.na(spp_info$filter_depth) | is.null(spp_info$filter_depth)) {
151+
if (!is.null(spp_info[["filter_depth"]])
152+
&& length(spp_info$filter_depth) > 0 && !is.na(spp_info$filter_depth)) {
151153
bio_df <- bio_df |> dplyr::filter(depth_m < spp_info$filter_depth)
152154
}
153155

@@ -163,9 +165,10 @@ clean_and_resample <- function(
163165

164166
bio_spp_dfs <- dplyr::bind_rows(bio_resampled)
165167

168+
file_name <- gsub(" ", "_", spp_info$common_name)
166169
dir_spp <- paste0(
167170
dir_out,
168-
paste0(spp_info$srvy, "_", spp_info$file_name, "/")
171+
paste0(spp_info$srvy, "_", file_name, "/")
169172
)
170173
if (!dir.exists(dir_spp)) {
171174
dir.create(dir_spp, showWarnings = FALSE)

R/cleanup_by_species.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#' spp_info <- data.frame(
2525
#' srvy = "CA",
2626
#' common_name = "arrowtooth flounder",
27-
#' file_name = "arrowtooth_flounder",
2827
#' filter_lat_gt = 34,
2928
#' filter_lat_lt = NA,
3029
#' filter_depth = NA,
@@ -60,13 +59,16 @@ cleanup_by_species <- function(
6059
)
6160

6261
# Implement latitude and depth filters
63-
if (!is.na(spp_info$filter_lat_lt) | is.null(spp_info$filter_lat_lt)) {
62+
if (!is.null(spp_info[["filter_lat_lt"]]) &&
63+
length(spp_info$filter_lat_lt) > 0 && !is.na(spp_info$filter_lat_lt)) {
6464
df <- df |> dplyr::filter(latitude_dd < spp_info$filter_lat_lt)
6565
}
66-
if (!is.na(spp_info$filter_lat_gt) | is.null(spp_info$filter_lat_gt)) {
66+
if (!is.null(spp_info[["filter_lat_gt"]])
67+
&& length(spp_info$filter_lat_gt) > 0 && !is.na(spp_info$filter_lat_gt)) {
6768
df <- df |> dplyr::filter(latitude_dd > spp_info$filter_lat_gt)
6869
}
69-
if (!is.na(spp_info$filter_depth) | is.null(spp_info$filter_depth)) {
70+
if (!is.null(spp_info[["filter_depth"]])
71+
&& length(spp_info$filter_depth) > 0 && !is.na(spp_info$filter_depth)) {
7072
df <- df |> dplyr::filter(depth_m < spp_info$filter_depth)
7173
}
7274

R/resample_tests.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ resample_tests <- function(spp_dfs, spp_info, grid_yrs, dir_out, test = FALSE,
5252
}
5353

5454
# set directories for outputs
55+
file_name <- gsub(" ", "_", spp_info$common_name)
5556
dir_spp <- paste0(
5657
dir_out,
57-
paste0(spp_info$srvy, "_", spp_info$file_name, "/")
58+
paste0(spp_info$srvy, "_", file_name, "/")
5859
)
5960

6061
if (!dir.exists(dir_spp)) {

vignettes/a-simple-example.Rmd

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ performs several essential tasks:
2929
- **Package Management**: It installs and loads all the necessary R packages, including surveyresamplr, dplyr, purrr, ggplot2, and flextable. A helper function, `pkg_install()`, is used to check for and install packages if they're not already present.
3030
- **Memory Allocation**: The `options(future.globals.maxSize = 1 * 1024^4)` line is crucial for parallel processing. It increases the memory limit for global variables to 1 TB, preventing memory-related errors when running complex models or processing large datasets.
3131

32-
```{r setup}
32+
```{r setup, message = FALSE, warning = FALSE}
3333
# Get rid of memory limits -----------------------------------------------------
3434
options(future.globals.maxSize = 1 * 1024^4) # Allow up to 1 TB for globals
3535
3636
# Install Libraries ------------------------------------------------------------
3737
# Here we list all the packages we will need for this vignette
3838
PKG <- c(
39-
"surveyresamplr",
4039
"dplyr",
4140
"purrr",
4241
"ggplot2",
@@ -51,6 +50,9 @@ pkg_install <- function(p) {
5150
require(p, character.only = TRUE)
5251
}
5352
base::lapply(unique(PKG), pkg_install)
53+
54+
devtools::load_all()
55+
library(surveyresamplr)
5456
```
5557

5658
## Defining the Species and Model in `spp_list`
@@ -63,10 +65,6 @@ For this example, we've defined a model for eastern Bering Sea (EBS) walleye pol
6365

6466
- **`common_name`**: The common name of the species(s)
6567

66-
- **`file_name`**:
67-
68-
- **`species_code`**:
69-
7068
- **`model_fn`**: The model formula: total_catch_wt_kg ~ 0 + factor(year). This tells the model to fit catch weight in kg as a function of year, without an intercept.
7169

7270
- **`model_family`**: The statistical distribution family for the model, "delta_gamma". This is an advanced family from the sdmTMB package that models both the probability of catching a species (presence/absence) and the magnitude of the catch.
@@ -79,8 +77,6 @@ For this example, we've defined a model for eastern Bering Sea (EBS) walleye pol
7977
spp_list <- data.frame(
8078
srvy = "EBS",
8179
common_name = "walleye pollock",
82-
file_name = "simple_walleye_pollock",
83-
species_code = as.character(21740),
8480
model_fn = "total_catch_wt_kg ~ 0 + factor(year)",
8581
model_family = "delta_gamma",
8682
model_anisotropy = TRUE,
@@ -97,7 +93,13 @@ The `noaa_afsc_catch` data frame contains catch information, including zero-catc
9793

9894
```{r explore-catch}
9995
head(surveyresamplr::noaa_afsc_catch) |>
100-
flextable::flextable()
96+
dplyr::mutate(trawlid = as.character(trawlid),
97+
species_code = as.character(species_code),
98+
cpue_kgkm2 = round(cpue_kgkm2, digits = 2),
99+
year = as.character(year)) |>
100+
dplyr::rename_with(~ gsub("_", " ", .x)) |>
101+
flextable::flextable() |>
102+
flextable::autofit()
101103
```
102104

103105

@@ -161,7 +163,10 @@ ggplot2::ggplot(
161163
label = "Prediction Grid",
162164
subtitle = "AFSC Eastern Bering Sea bottom trawl survey"
163165
) +
164-
ggplot2::scale_color_gradient(name = "Depth (m)") +
166+
ggplot2::scale_color_gradient(name = "Depth (m)",
167+
guide = ggplot2::guide_colorbar(reverse = TRUE),
168+
low = "#56B1F7",
169+
high = "#132B43") +
165170
ggplot2::theme_bw()
166171
```
167172

@@ -173,13 +178,11 @@ Here we load the data for the model run, cropping it to the data we would like t
173178

174179
```{r load-data}
175180
### Load survey data -----------------------------------------------------------
176-
177181
catch <- surveyresamplr::noaa_afsc_catch |>
178182
dplyr::filter(srvy == "EBS") |>
179183
dplyr::filter(year >= 2020)
180184
181185
### Load grid data -------------------------------------------------------------
182-
183186
grid_yrs <- sdmTMB::replicate_df(
184187
dat = surveyresamplr::noaa_afsc_ebs_pred_grid_depth,
185188
time_name = "year",
@@ -191,6 +194,7 @@ The resulting `grid_yrs` data frame now contains a year column, allowing the mod
191194

192195
```{r grid-yrs}
193196
head(grid_yrs) |>
197+
dplyr::mutate(year = as.character(year)) |>
194198
flextable::flextable()
195199
```
196200

@@ -210,7 +214,6 @@ The code below defines key parameters that control the resampling process:
210214

211215
For this example, we are creating the following effort levels: 0.5, 0.75, and 1 which translates to 50% effort, 75% effort, and 100% effort. We then specify that we want 7 replicates for each effort.
212216

213-
214217
tot_dataframes = effort x replicates - (replicates - 1). TOLEDO: is this hard and fast?
215218

216219
```{r set-vars}
@@ -242,9 +245,6 @@ The `purrr::map` function is used to apply the `clean_and_resample` function to
242245
The number you input for `n_knots` can make or break your model. We have more details on considerations when choosing `n_knots` or allowing the function to select the number of knots for you in the [Importance of `n_knots` in `{sdmTMB}` Models section](#importance-of-sdmtmb-models).
243246
:::
244247

245-
TODO: explain why `purrr::map` is important, what the sink files are for.
246-
TODO: n_knots is lower here because the sample size is small
247-
248248
```{r run-models, eval = FALSE}
249249
start.time <- Sys.time()
250250
purrr::map(
@@ -271,7 +271,7 @@ a <- read.csv(file = paste0(dir_final, srvy, "_simple_time.csv"))
271271
print(paste0("Completed in: ", round(a$time, 2), " ", a$units))
272272
```
273273

274-
```{r sink-results-backup}
274+
```{r sink-results-backup, include = FALSE}
275275
# EBS walleye pollock
276276
# Starting cleanup of catch data
277277
# ...Starting parallel SDM processing
@@ -484,6 +484,8 @@ print(paste0("Completed in: ", round(a$time, 2), " ", a$units))
484484
# ...Parallel SDM processing complete
485485
```
486486

487+
## Viewing output of Resampled Models
488+
### Plotting output
487489
```{r results-run1}
488490
out <- plot_results(
489491
srvy = paste0(srvy, "_simple"), dir_out = dir_out,
@@ -500,8 +502,7 @@ load(file = paste0(dir_final, "analysisoutput.rdata"))
500502
out$plots
501503
```
502504

503-
Parameter output:
504-
505+
### Parameter output
505506
```{r results-tables-1}
506507
i <- 1
507508
print(names(out$tables)[i])

0 commit comments

Comments
 (0)