-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path12_probability-of-high-severity-build-models.R
266 lines (236 loc) · 21.5 KB
/
12_probability-of-high-severity-build-models.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# predicting probability of high severity fire (using a threshold of RBR)
# based on heterogeneity, 100 hour fuel moisture, whether fuel moisture
# conditions are extreme, topographic ruggedness, a proxy for canopy density
library(lubridate)
library(sf)
library(tidyverse)
library(here)
library(lubridate)
library(brms)
if(!file.exists(here::here("data/data_output/burned-fire-samples_configured.geoJSON"))) {
source(here::here("data/data_carpentry/11_configure-fire-samples.R"))
}
# This .csv file represents the burned samples extracted from Earth Engine
# That is, the severity was over the threshold corresponding to a CBI of 0.1
ss_burned <- sf::st_read(here::here("data/data_output/burned-fire-samples_configured.geoJSON")) %>% st_drop_geometry()
# Severe or not as bernoulli response, heterogeneity, preFire NDVI, prefire neighborhood mean NDVI, heterogeneity interacts with fm100, preFire NDVI, and neighborhood mean NDVI, only samples that were burned
(start <- Sys.time())
set.seed(1)
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm <- brm(stand_replacing ~
het_ndvi_1_s +
focal_mean_ndvi_1_s +
preFire_ndvi_s +
fm100_s +
pahl_s +
topo_roughness_1_s +
focal_mean_ndvi_1_s:preFire_ndvi_s +
het_ndvi_1_s:focal_mean_ndvi_1_s +
het_ndvi_1_s:preFire_ndvi_s +
het_ndvi_1_s:fm100_s +
(1 | fire_id),
family = bernoulli(),
data = ss_burned,
sample_prior = TRUE,
iter = 5000,
chains = 4,
cores = 4,
prior = c(
prior(prior = normal(0, 1), class = b),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_1_s),
prior(prior = normal(0, 1), class = b, coef = focal_mean_ndvi_1_s),
prior(prior = normal(0, 1), class = b, coef = preFire_ndvi_s),
prior(prior = normal(0, 1), class = b, coef = fm100_s),
prior(prior = normal(0, 1), class = b, coef = pahl_s),
prior(prior = normal(0, 1), class = b, coef = topo_roughness_1_s),
prior(prior = normal(0, 1), class = b, coef = focal_mean_ndvi_1_s:preFire_ndvi_s),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_1_s:focal_mean_ndvi_1_s),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_1_s:preFire_ndvi_s),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_1_s:fm100_s),
prior(prior = student_t(3, 0, 10), class = Intercept),
prior(prior = student_t(3, 0, 10), class = sd),
prior(prior = normal(0, 1), class = sd, group = fire_id)
))
(Sys.time() - start)
(start <- Sys.time())
set.seed(2)
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm <- brm(stand_replacing ~
het_ndvi_2_s +
focal_mean_ndvi_2_s +
preFire_ndvi_s +
fm100_s +
pahl_s +
topo_roughness_2_s +
focal_mean_ndvi_2_s:preFire_ndvi_s +
het_ndvi_2_s:focal_mean_ndvi_2_s +
het_ndvi_2_s:preFire_ndvi_s +
het_ndvi_2_s:fm100_s +
(1 | fire_id),
family = bernoulli(),
data = ss_burned,
sample_prior = TRUE,
iter = 5000,
chains = 4,
cores = 4,
prior = c(
prior(prior = normal(0, 1), class = b),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_2_s),
prior(prior = normal(0, 1), class = b, coef = focal_mean_ndvi_2_s),
prior(prior = normal(0, 1), class = b, coef = preFire_ndvi_s),
prior(prior = normal(0, 1), class = b, coef = fm100_s),
prior(prior = normal(0, 1), class = b, coef = pahl_s),
prior(prior = normal(0, 1), class = b, coef = topo_roughness_2_s),
prior(prior = normal(0, 1), class = b, coef = focal_mean_ndvi_2_s:preFire_ndvi_s),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_2_s:focal_mean_ndvi_2_s),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_2_s:preFire_ndvi_s),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_2_s:fm100_s),
prior(prior = student_t(3, 0, 10), class = Intercept),
prior(prior = student_t(3, 0, 10), class = sd),
prior(prior = normal(0, 1), class = sd, group = fire_id)
))
(Sys.time() - start)
(start <- Sys.time())
set.seed(3)
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm <- brm(stand_replacing ~
het_ndvi_3_s +
focal_mean_ndvi_3_s +
preFire_ndvi_s +
fm100_s +
pahl_s +
topo_roughness_3_s +
focal_mean_ndvi_3_s:preFire_ndvi_s +
het_ndvi_3_s:focal_mean_ndvi_3_s +
het_ndvi_3_s:preFire_ndvi_s +
het_ndvi_3_s:fm100_s +
(1 | fire_id),
family = bernoulli(),
data = ss_burned,
sample_prior = TRUE,
iter = 5000,
chains = 4,
cores = 4,
prior = c(
prior(prior = normal(0, 1), class = b),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_3_s),
prior(prior = normal(0, 1), class = b, coef = focal_mean_ndvi_3_s),
prior(prior = normal(0, 1), class = b, coef = preFire_ndvi_s),
prior(prior = normal(0, 1), class = b, coef = fm100_s),
prior(prior = normal(0, 1), class = b, coef = pahl_s),
prior(prior = normal(0, 1), class = b, coef = topo_roughness_3_s),
prior(prior = normal(0, 1), class = b, coef = focal_mean_ndvi_3_s:preFire_ndvi_s),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_3_s:focal_mean_ndvi_3_s),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_3_s:preFire_ndvi_s),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_3_s:fm100_s),
prior(prior = student_t(3, 0, 10), class = Intercept),
prior(prior = student_t(3, 0, 10), class = sd),
prior(prior = normal(0, 1), class = sd, group = fire_id)
))
(Sys.time() - start)
(start <- Sys.time())
set.seed(4)
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm <- brm(stand_replacing ~
het_ndvi_4_s +
focal_mean_ndvi_4_s +
preFire_ndvi_s +
fm100_s +
pahl_s +
topo_roughness_4_s +
focal_mean_ndvi_4_s:preFire_ndvi_s +
het_ndvi_4_s:focal_mean_ndvi_4_s +
het_ndvi_4_s:preFire_ndvi_s +
het_ndvi_4_s:fm100_s +
(1 | fire_id),
family = bernoulli(),
data = ss_burned,
sample_prior = TRUE,
iter = 5000,
chains = 4,
cores = 4,
prior = c(
prior(prior = normal(0, 1), class = b),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_4_s),
prior(prior = normal(0, 1), class = b, coef = focal_mean_ndvi_4_s),
prior(prior = normal(0, 1), class = b, coef = preFire_ndvi_s),
prior(prior = normal(0, 1), class = b, coef = fm100_s),
prior(prior = normal(0, 1), class = b, coef = pahl_s),
prior(prior = normal(0, 1), class = b, coef = topo_roughness_4_s),
prior(prior = normal(0, 1), class = b, coef = focal_mean_ndvi_4_s:preFire_ndvi_s),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_4_s:focal_mean_ndvi_4_s),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_4_s:preFire_ndvi_s),
prior(prior = normal(0, 1), class = b, coef = het_ndvi_4_s:fm100_s),
prior(prior = student_t(3, 0, 10), class = Intercept),
prior(prior = student_t(3, 0, 10), class = sd),
prior(prior = normal(0, 1), class = sd, group = fire_id)
))
(Sys.time() - start)
# Save the models as .rds objects
# saveRDS(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm, file = here::here("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm.rds"))
# saveRDS(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm, file = here::here("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm.rds"))
# saveRDS(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm, file = here::here("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm.rds"))
# saveRDS(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm, file = here::here("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm.rds"))
# Add information criteria
# Reload the brms fit models into the environment if needed
# fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm <- readRDS("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm.rds")
# fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm <- readRDS("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm.rds")
# fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm <- readRDS("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm.rds")
# fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm <- readRDS("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm.rds")
# First, the 1-pixel window model
# Start by adding less computationally intensive measures, Bayesian R^2 and WAIC
(start <- Sys.time())
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm <-
add_criterion(x = fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm, criterion = c("R2", "waic"))
# Save the updated model with the WAIC and Bayesian R^2 values attached
saveRDS(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm, file = here::here("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm.rds"))
# 2-pixel window model
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm <-
add_criterion(x = fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm, criterion = c("R2", "waic"))
# Save the updated model with the WAIC and Bayesian R^2 values attached
saveRDS(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm, file = here::here("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm.rds"))
# 3-pixel window model
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm <-
add_criterion(x = fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm, criterion = c("R2", "waic"))
saveRDS(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm, file = here::here("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm.rds"))
# 4-pixel window model
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm <-
add_criterion(x = fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm, criterion = c("R2", "waic"))
saveRDS(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm, file = here::here("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm.rds"))
(Sys.time() - start)
loo_compare(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm,
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm,
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm,
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm, criterion = "waic")
# print method for the WAIC values suggest to try loo instead of WAIC because of p_waic estimates greater than 0.4
# Adding the LOO information criteria, which are much more computationally intensive
# I opt to try to calculate the log-likelihood of the whole matrix (# of observations [~55,000] X # of samples [6000]) at once
# This uses up the whole 64GB of RAM I have at my disposal, but it does work (takes about 1 hour and 20 minutes)
# Trying to use the pointwise = TRUE method to calculate values for each observation independently took >30 hours and didn't finish
# 1-pixel model
# We use reloo = TRUE to refit the model for problematic observations and
# get the pointwise ELPD value for those two observations
# This will refit the model XXX times to compute the ELPDs for the XXX problematic observations directly.
(start <- Sys.time())
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm <-
add_criterion(x = fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm, criterion = "loo", reloo = TRUE)
(Sys.time() - start)
saveRDS(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm, file = here::here("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm.rds"))
# 2-pixel model
(start <- Sys.time())
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm <-
add_criterion(x = fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm, criterion = "loo", reloo = TRUE)
saveRDS(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm, file = here::here("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm.rds"))
(Sys.time() - start)
# 3-pixel model
(start <- Sys.time())
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm <-
add_criterion(x = fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm, criterion = "loo", reloo = TRUE)
saveRDS(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm, file = here::here("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm.rds"))
(Sys.time() - start)
# 4-pixel model
(start <- Sys.time())
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm <-
add_criterion(x = fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm, criterion = "loo", reloo = TRUE)
saveRDS(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm, file = here::here("analyses/analyses_output/fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm.rds"))
(Sys.time() - start)
loo_compare(fm_sevOrNot_het_neighborhoodMean_preFireNDVI_1_ssBurned_brm,
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_2_ssBurned_brm,
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_3_ssBurned_brm,
fm_sevOrNot_het_neighborhoodMean_preFireNDVI_4_ssBurned_brm, criterion = "loo")