forked from SnBuenafe/LarvaDistModels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14a_SHOS_Data.R
More file actions
62 lines (49 loc) · 2.58 KB
/
14a_SHOS_Data.R
File metadata and controls
62 lines (49 loc) · 2.58 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
61
62
# DESCRIPTION: Assembling shortbill spearfish dataset
# Load preliminaries
source("00_SetupGrid.R")
source("00_Preliminaries.R")
species <- "SHOS"
figure_dir <- here::here(figure_dir, species)
# Function to restrict adult distribution predictor to just shortbill spearfish
restrict_predictor <- function(x){
x %<>%
dplyr::select(c(1:21, 31, 51)) %>% # restrict the predictors
rowwise() %>%
dplyr::mutate(adult = mean(c(Tetrapturus_angustirostris), na.rm = TRUE)) %>%
ungroup() %>%
dplyr::mutate(adult = ifelse(is.na(adult), yes = 0, no = adult)) # replace NAs of adult predictions to 0s
}
# Create species sf object
sf <- combineFish(species = "shortbill-spearfish") %>%
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 shortbill spearfish datasets
SHOS_ds1 <- read_csv(here::here(input_dir, paste(species, "jan-mar.csv", sep = "_")), show_col_types = FALSE) %>% # January-March
restrict_predictor()
SHOS_ds2 <- read_csv(here::here(input_dir, paste(species, "apr-jun.csv", sep = "_")), show_col_types = FALSE) %>% # April-June
restrict_predictor()
SHOS_ds3 <- read_csv(here::here(input_dir, paste(species, "jul-sept.csv", sep = "_")), show_col_types = FALSE) %>% # July-September
restrict_predictor()
SHOS_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
SHOS_build <- dplyr::bind_rows(SHOS_ds1 %>% dplyr::filter(!is.na(abundance)),
SHOS_ds2 %>% dplyr::filter(!is.na(abundance)),
SHOS_ds3 %>% dplyr::filter(!is.na(abundance)),
SHOS_ds4 %>% dplyr::filter(!is.na(abundance))) %>%
organize_build()
# We divide the data into train (training and validation) and test
nrow(SHOS_build) * 0.8 # = 9834.4
set.seed(368899)
train <- slice_sample(SHOS_build, n = 9834, replace = FALSE) # 80% training set
test <- SHOS_build[!SHOS_build$row %in% train$row, ] # 20% testing set
# Prepare data frame for predictions
SHOS_predict_season1 <- organize_predict(SHOS_ds1) # January-March
SHOS_predict_season2 <- organize_predict(SHOS_ds2) # April-June
SHOS_predict_season3 <- organize_predict(SHOS_ds3) # July-September
SHOS_predict_season4 <- organize_predict(SHOS_ds4) # October-December