forked from SnBuenafe/LarvaDistModels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05a_SWO_Data.R
More file actions
60 lines (47 loc) · 2.45 KB
/
05a_SWO_Data.R
File metadata and controls
60 lines (47 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# DESCRIPTION: Assembling swordfish dataset
# Load preliminaries
source("00_SetupGrid.R")
source("00_Preliminaries.R")
species <- "SWO"
figure_dir <- here::here(figure_dir, species)
# Define functions
# Function to restrict adult distribution predictor to just swordfish
restrict_predictor <- function(x){
x %<>%
dplyr::select(c(1:21, 29, 51)) %>% # restrict the predictors
dplyr::mutate(adult = ifelse(is.na(Xiphias_gladius), yes = 0, no = Xiphias_gladius)) # replace NAs of adult predictions to 0s
}
# Create species sf object
sf <- combineFish(species = "swordfish") %>%
fSpatPlan_Convert2PacificCentered(., cCRS = cCRS) %>%
sf::st_centroid() # transform into point data
seasons <- c("jan-mar", "apr-jun", "jul-sept", "oct-dec")
for(s in 1:length(seasons)) {
gridded <- assembleGrid(grid, sf %>% dplyr::filter(season == seasons[s]))
assign(paste("grid", species, seasons[s], sep = "_"), gridded)
}
# Load swordfish datasets
SWO_ds1 <- read_csv(here::here(input_dir, paste(species, "jan-mar.csv", sep = "_")), show_col_types = FALSE) %>% # January-March
restrict_predictor()
SWO_ds2 <- read_csv(here::here(input_dir, paste(species, "apr-jun.csv", sep = "_")), show_col_types = FALSE) %>% # April-June
restrict_predictor()
SWO_ds3 <- read_csv(here::here(input_dir, paste(species, "jul-sept.csv", sep = "_")), show_col_types = FALSE) %>% # July-September
restrict_predictor()
SWO_ds4 <- read_csv(here::here(input_dir, paste(species, "oct-dec.csv", sep = "_")), show_col_types = FALSE) %>% # October-December
restrict_predictor()
# Build model with known data only
SWO_build <- dplyr::bind_rows(SWO_ds1 %>% dplyr::filter(!is.na(abundance)),
SWO_ds2 %>% dplyr::filter(!is.na(abundance)),
SWO_ds3 %>% dplyr::filter(!is.na(abundance)),
SWO_ds4 %>% dplyr::filter(!is.na(abundance))) %>%
organize_build()
# We divide the data into train (training and validation) and test
nrow(SWO_build) * 0.8 # = 9834.4
set.seed(471832519)
train <- slice_sample(SWO_build, n = 9834, replace = FALSE) # 80% training set
test <- SWO_build[!SWO_build$row %in% train$row, ] # 20% testing set
# Prepare data frame for predictions
SWO_predict_season1 <- organize_predict(SWO_ds1) # January-March
SWO_predict_season2 <- organize_predict(SWO_ds2) # April-June
SWO_predict_season3 <- organize_predict(SWO_ds3) # July-September
SWO_predict_season4 <- organize_predict(SWO_ds4) # October-December