-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodeling_brt.R
200 lines (154 loc) · 7.58 KB
/
modeling_brt.R
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#############################################
#############################################
##### Modeling Boosted Regression Trees #####
#############################################
#############################################
#Loading everything needed for modeling
source("00_loading_data.R")
{
#Only modeling common species
for(s in sp){
print(paste('Modeling', s))
sites_sp <- sites[, c("x", "y", "ua", s,
"PC1", "PC2", "PC3", "PC4", "PC5")]
names(sites_sp)[names(sites_sp) == s] <- "occ"
if(sum(sites_sp$occ) >= 30){
pa_data <- st_as_sf(sites_sp,
coords = c("x", "y"),
crs = crs(env_raster))
#Separating the data into blocks of 165 km size (no more spatial autocorrelation)
sb <- spatialBlock(speciesData = pa_data,
species = "occ",
rasterLayer = env_raster,
theRange = 165000, #Size of the blocks (m) from prior SAC analysis
k = 5, #Arbitrary
selection = "random",
iteration = 200, #Find evenly dispersed folds
biomod2Format = FALSE,
verbose = FALSE,
progress = FALSE
)
#Adding the partitioned blocks info to the initial data set
sites_sp$block <- sb$foldID
#Separate training-testing by pre-defined spatial blocks
split <- initial_split(sites_sp, prop = .7, strata = block) #70/30 in each block
train <- training(split)
test <- testing(split)
#Response must be a factor
train$occ <- as.factor(train$occ)
levels(train$occ) <- c("C0", "C1")
#Calculating case weights
pr_num <- as.numeric(table(train$occ)["C1"]) #N of presences
ab_num <- as.numeric(table(train$occ)["C0"]) #N of absences
wt <- ifelse(train$occ == "C1", 1, pr_num / ab_num)
#Creating a tuning grid
tune_grid <- expand.grid(n.trees = seq(from = 500, to = 5000, by = 250),
interaction.depth = seq(from = 1, to = 3, by = 1),
shrinkage = c(0.001, 0.01, 0.1),
n.minobsinnode = 10)
#Using caret for cross-validation
train_control <- trainControl(method = "cv",
number = 10,
classProbs = TRUE,
summaryFunction = twoClassSummary,
allowParallel = TRUE)
#Starting cluster
cluster <- makeCluster(8)
registerDoParallel(cluster)
#Running the BRT model
#Model training here
brt <- caret::train(x = train[, 5:9],
y = train$occ,
method = "gbm",
metric = "ROC",
trControl = train_control,
tuneGrid = tune_grid,
weights = wt)
#Ending cluster
stopCluster(cluster)
registerDoSEQ()
#Model testing here
test_pred <- predict(brt,
test,
type = "prob")$C1
#ROC-AUC and PRC-AUC evaluation
precrec_obj <- evalmod(scores = test_pred,
labels = test$occ)
#Prediction maps based on trained data
brtpred <- raster::predict(model = brt,
object = env_raster,
type = "prob",
index = 2)
#Saving the output prediction raster
writeRaster(brtpred,
paste("results/brt/rasters/", s, ".tif", sep = ""),
overwrite = TRUE)
#Evaluation table for each species
result = rbind(result,
data.frame(spp = s,
roc = round(precrec::auc(precrec_obj)$aucs[1], 4),
prc = round(precrec::auc(precrec_obj)$aucs[2], 4)))
} else {
#Separate training-testing by pre-defined spatial blocks
split <- initial_split(sites_sp, prop = .7) #70/30 in each partition
train <- training(split)
test <- testing(split)
#Response must be a factor
train$occ <- as.factor(train$occ)
levels(train$occ) <- c("C0", "C1")
#Calculating case weights
pr_num <- as.numeric(table(train$occ)["C1"]) #N of presences
ab_num <- as.numeric(table(train$occ)["C0"]) #N of absences
wt <- ifelse(train$occ == "C1", 1, pr_num / ab_num)
#Creating a tuning grid
tune_grid <- expand.grid(n.trees = seq(from = 500, to = 5000, by = 250),
interaction.depth = seq(from = 1, to = 3, by = 1),
shrinkage = c(0.001, 0.01, 0.1),
n.minobsinnode = 10)
#Using caret for cross-validation
train_control <- trainControl(method = "cv",
number = 10,
classProbs = TRUE,
summaryFunction = twoClassSummary,
allowParallel = TRUE)
#Starting cluster
cluster <- makeCluster(8)
registerDoParallel(cluster)
#Running the BRT model
#Model training here
brt <- caret::train(x = train[, 5:9],
y = train$occ,
method = "gbm",
metric = "ROC",
trControl = train_control,
tuneGrid = tune_grid,
weights = wt)
#Ending cluster
stopCluster(cluster)
registerDoSEQ()
#Model testing here
test_pred <- predict(brt,
test,
type = "prob")$C1
#ROC-AUC and PRC-AUC evaluation
precrec_obj <- evalmod(scores = test_pred,
labels = test$occ)
#Prediction maps based on trained data
brtpred <- raster::predict(model = brt,
object = env_raster,
type = "prob",
index = 2)
#Saving the output prediction raster
writeRaster(brtpred,
paste("results/brt/rasters/", s, ".tif", sep = ""),
overwrite = TRUE)
#Evaluation table for each species
result = rbind(result,
data.frame(spp = s,
roc = round(precrec::auc(precrec_obj)$aucs[1], 4),
prc = round(precrec::auc(precrec_obj)$aucs[2], 4)))
}
}
#Table
write.csv(result, "results/brt/evaluation.csv")
}