forked from SnBuenafe/LarvaDistModels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05b_SWO_ModelBuilding.R
More file actions
95 lines (76 loc) · 4.12 KB
/
05b_SWO_ModelBuilding.R
File metadata and controls
95 lines (76 loc) · 4.12 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# DESCRIPTION: Building optimal model for swordfish
# Load preliminaries
source("05a_SWO_Data.R") # Load SWO data
#### Create model ####
# With longitude and latitude
# 5-fold grid search
CVGrid <- CVgridSearch(train, test, tc = c(1, 2), bf = c(0.5, 0.75), lr = seq(0.005, 0.01, 0.001), pred_in = c(7:23, 25), resp_in = 5)
print(CVGrid %>% dplyr::arrange(desc(test_AUC)), n = 1) # BEST TEST AUC
# Building most optimal model
SWO_model <- dismo::gbm.step(data = train, gbm.x = c(7:23, 25),
gbm.y = 5, family = "bernoulli", n.folds = 5,
tree.complexity = 2, bag.fraction = 0.75, learning.rate = 0.009
)
saveRDS(SWO_model, here::here(model_dir, paste(species, "model.rds", sep = "_")))
# SWO_model <- readRDS(here::here(model_dir, paste(species, "model.rds", sep = "_")))
# Show the relative importance of each of the predictors
summary(SWO_model)
# Number of trees
SWO_model$n.trees
# Printing AUCs
SWO_model$self.statistics$discrimination # Training AUC Score
SWO_model$cv.statistics$discrimination.mean # Validation AUC Score
# Predict to the testing dataset
preds <- gbm::predict.gbm(SWO_model, test, n.trees =SWO_model$gbm.call$best.trees, type = "response")
dismo::calc.deviance(test[, "abundance_presence"], preds, family = "bernoulli")
get_testAUC(test$abundance_presence, preds) # Print testing AUC
#### Plotting maps ####
train_tmp <- train %>%
dplyr::mutate(model =SWO_model$fitted)
test_tmp <- test %>%
dplyr::mutate(model = preds)
limits = c(0, 0.8)
# January-March
gg_obj <- create_speciesMap(train_tmp, # training object with model column (fitted values)
test_tmp, # testing object with model column (predictions)
"jan-mar", # season
SWO_predict_season1, # rest of the ocean cells
SWO_model, # BRT model
`grid_SWO_jan-mar` # grid of species for specific season
)
gg <- plotModel(gg_obj, limits) # plot extrapolations for Jan-Mar
ggsave(plot = gg, filename = here::here(figure_dir, paste(species, "jan-mar", "base.png", sep = "_")),
width = 14, height = 5, dpi = 600)
# April-June
gg_obj <- create_speciesMap(train_tmp, # training object with model column (fitted values)
test_tmp, # testing object with model column (predictions)
"apr-jun", # season
SWO_predict_season2, # rest of the ocean cells
SWO_model, # BRT model
`grid_SWO_apr-jun` # grid of species for specific season
)
gg <- plotModel(gg_obj, limits) # plot extrapolations for Apr-Jun
ggsave(plot = gg, filename = here::here(figure_dir, paste(species, "apr-jun", "base.png", sep = "_")),
width = 14, height = 5, dpi = 600)
# July-September
gg_obj <- create_speciesMap(train_tmp, # training object with model column (fitted values)
test_tmp, # testing object with model column (predictions)
"jul-sept", # season
SWO_predict_season3, # rest of the ocean cells
SWO_model, # BRT model
`grid_SWO_jul-sept` # grid of species for specific season
)
gg <- plotModel(gg_obj, limits) # plot extrapolations for Jul-Sept
ggsave(plot = gg, filename = here::here(figure_dir, paste(species, "jul-sept", "base.png", sep = "_")),
width = 14, height = 5, dpi = 600)
# October-December
gg_obj <- create_speciesMap(train_tmp, # training object with model column (fitted values)
test_tmp, # testing object with model column (predictions)
"oct-dec", # season
SWO_predict_season4, # rest of the ocean cells
SWO_model, # BRT model
`grid_SWO_oct-dec` # grid of species for specific season
)
gg <- plotModel(gg_obj, limits) # plot extrapolations for Oct-Dec
ggsave(plot = gg, filename = here::here(figure_dir, paste(species, "oct-dec", "base.png", sep = "_")),
width = 14, height = 5, dpi = 600)