forked from SnBuenafe/LarvaDistModels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11a_SAIL_Data.R
More file actions
59 lines (46 loc) · 2.46 KB
/
11a_SAIL_Data.R
File metadata and controls
59 lines (46 loc) · 2.46 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
# DESCRIPTION: Assembling sailfish dataset
# Load preliminaries
source("00_SetupGrid.R")
source("00_Preliminaries.R")
species <- "SAIL"
figure_dir <- here::here(figure_dir, species)
# Function to restrict adult distribution predictor to just sailfish
restrict_predictor <- function(x){
x %<>%
dplyr::select(c(1:21, 34, 51)) %>% # restrict the predictors
dplyr::mutate(adult = ifelse(is.na(Istiophorus_platypterus), yes = 0, no = Istiophorus_platypterus)) # replace NAs of adult predictions to 0s
}
# Create species sf object
sf <- combineFish(species = "sailfish") %>%
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 sailfish datasets
SAIL_ds1 <- read_csv(here::here(input_dir, paste(species, "jan-mar.csv", sep = "_")), show_col_types = FALSE) %>% # January-March
restrict_predictor()
SAIL_ds2 <- read_csv(here::here(input_dir, paste(species, "apr-jun.csv", sep = "_")), show_col_types = FALSE) %>% # April-June
restrict_predictor()
SAIL_ds3 <- read_csv(here::here(input_dir, paste(species, "jul-sept.csv", sep = "_")), show_col_types = FALSE) %>% # July-September
restrict_predictor()
SAIL_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
SAIL_build <- dplyr::bind_rows(SAIL_ds1 %>% dplyr::filter(!is.na(abundance)),
SAIL_ds2 %>% dplyr::filter(!is.na(abundance)),
SAIL_ds3 %>% dplyr::filter(!is.na(abundance)),
SAIL_ds4 %>% dplyr::filter(!is.na(abundance))) %>%
organize_build()
# We divide the data into train (training and validation) and test
nrow(SAIL_build) * 0.8 # = 9834.4
set.seed(5492)
train <- slice_sample(SAIL_build, n = 9834, replace = FALSE) # 80% training set
test <- SAIL_build[!SAIL_build$row %in% train$row, ] # 20% testing set
# Prepare data frame for predictions
SAIL_predict_season1 <- organize_predict(SAIL_ds1) # January-March
SAIL_predict_season2 <- organize_predict(SAIL_ds2) # April-June
SAIL_predict_season3 <- organize_predict(SAIL_ds3) # July-September
SAIL_predict_season4 <- organize_predict(SAIL_ds4) # October-December