-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_results.Rmd
432 lines (327 loc) · 13.2 KB
/
03_results.Rmd
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
---
title: "03_results.Rmd"
author: "Adam Hanbury-Brown"
date: "3/31/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Model 1; Shive + 2018 (-April 1 snowpack); site conditions / climate
```{r pressure, echo=FALSE}
mod_predictors <- c("recoveryTime",'slope',"aspect","CWDhist","AEThist","postFireSAP","snowPack")
k.default = 10
ShiveTrial <- 4
sampleSize <- 35
#using RRI end of trajectory
modData <- df4 %>%
filter(postFirePlanting < 0.01,
mediumReburns < 0.01,
lowReburns < 0.01) %>%
mutate(across(where(is.numeric),scale_this))
#using a standard time since fire
# modData <- df3 %>%
# filter(postFirePlanting < 0.01,
# mediumReburns < 0.01,
# lowReburns < 0.01,
# timeSinceFire == 20) %>%
# mutate(RRI_end = RRI) %>%
# mutate(across(where(is.numeric),scale_this))
modDataSub <- tibble()
for(fa in unique(modData$focalAreaID)){
d <- modData %>% filter(focalAreaID == fa)
if(nrow(d) < sampleSize){
tmp <- d
}else{
tmp <- sample_n(d,size = sampleSize)
}
modDataSub <- rbind(modDataSub,tmp)
}
modData <- modDataSub
modData <- read_csv("data/Shive_mod_3-2022-04-02 18:45:18data.csv")
mod <- gam(formula = RRI_end ~
recoveryTime +
s(x, y, bs = "gp", k = 65, m = 2) +
s(slope, k = k.default) +
s(aspect, k = k.default) +
s(postFireSAP, k = k.default) +
#s(CWDhist, k = k.default) +
s(AEThist, k = 6) +
s(snowPack, k = k.default),
data = modData)
getCorrelationMatrix(d = modData, varsForCorr = mod_predictors)
modName <- paste0("Shive_mod_",ShiveTrial,"-",Sys.time(),'.RDS')
dataName <- paste0("Shive_mod_",ShiveTrial,"-",Sys.time(),"data.csv")
saveRDS(object = mod, file = paste0('data/',modName))
write_csv(modData,file = paste0('data/',dataName))
summary(mod)
gam.check(mod)
plot(mod)
AIC(mod)
pixel.dists <- as.matrix(dist(cbind(modData$x, modData$y)))
pixel.dists.inv <- 1/pixel.dists
diag(pixel.dists.inv) <- 0
print(Moran.I(residuals(mod),pixel.dists.inv))
print(paste0("AIC:",AIC(mod)))
#write_csv(modData,"Shive_model_reduced_data.csv")
#saveRDS(object = mod, file = 'data/Shive_model_reduced.RDS')
```
Results of Shive Mod:
Trial 1:
Including Shive's variables in a model resulted in a model with significant multicollinearity with AET and CWD and slope and AET.
Trial 2 (data/Shive_model_reduce):
A reduced version of Shive's model produced expected outcomes, but residual were spatially autocorrelated. AIC = 876.
Trial 3. Reduced the sample size.
--result: AIC = 762; fixed spatial auto, could imprve AET relationship with lower k.
Trial 4. Reduce k on AET. This resulted in a better relationship with AET, and similar AIC to above.
Prior testing:
1. Everything is significant except aspect. So it pretty much works.
2. Generally the relationships are pretty reasonable (except CWD). Could remove CWD because its correlated with AET and doesn't give a meaningful relationship.
3. Not clear why SAP is negative at low SAP
AIC is lower than if all terms are linear
AIC is slightly higher if you use adjusted folded northness instead of aspect
If I use conCov here instead of RRI end then SAP_400 has a more reasonable result
##Model 2. Stewart: post-fire weather
The goal of this model is to test if the same predictors that Stewart et al. 2020 found to predict the probability of conifer regeneration also predict conifer canopy recovery 20-35 years after fire.
```{r,echo=F}
mod_predictors <- c("recoveryTime","slope","aspect","pptYr1_3_annual_mean","burnSev","postFireSAP")
#getCorrelationMatrix(mod_predictors)
k.default = 9
StewTrial <- 2
modData <- df4 %>%
filter(postFirePlanting < 0.01,
mediumReburns < 0.01,
lowReburns < 0.01) %>%
mutate(across(where(is.numeric),scale_this))
#n_per_fa <- modData %>% group_by(focalAreaID) %>% summarise(n = length(RRI_end)) %>% pull(n)
modDataSub <- tibble()
sampleSize <- 45
for(fa in unique(modData$focalAreaID)){
d <- modData %>% filter(focalAreaID == fa)
if(nrow(d) < sampleSize){
tmp <- d
}else{
tmp <- sample_n(d,size = sampleSize)
}
modDataSub <- rbind(modDataSub,tmp)
}
modData <- modDataSub
#modData <- read_csv("data/modDataSub_StewartModel.csv")
modData <- read_csv("data/Shive_mod_3-2022-04-02 18:45:18data.csv")
#modData <- read_csv("data/mod_3-2022-04-02 17:13:55data.csv")
mod <- gam(formula = RRI_end ~
recoveryTime +
s(x, y, bs = "gp", k = 65, m = 2) +
burnSev +
s(slope, k = k.default) +
s(aspect, k = k.default) +
s(postFireSAP, k = k.default) +
s(pptYr1_3_annual_mean,k = 12),
data = modData)
modName <- paste0("Stewart_mod_",StewTrial,"-",Sys.time(),'.RDS')
dataName <- paste0("Stewart_mod_",StewTrial,"-",Sys.time(),"data.csv")
#saveRDS(object = mod, file = paste0('data/',modName))
#write_csv(modData,file = paste0('data/',dataName))
summary(mod)
gam.check(mod)
plot(mod)
AIC(mod)
pixel.dists <- as.matrix(dist(cbind(modData$x, modData$y)))
pixel.dists.inv <- 1/pixel.dists
diag(pixel.dists.inv) <- 0
print(Moran.I(residuals(mod),pixel.dists.inv))
print(paste("AIC:",AIC(mod)))
#write_csv(modDataSub,file = "data/modDataSub_StewartModel.csv")
```
Trial 1. Things working pretty well, but would like to use the exact same data as in the Shive trial.
Trial 2. Using the same exact data as the Shive trial produced pretty good results except that the ppt_1_3 was not significant. This is workable though.
Trial 3. Use a different subset of data to see if we can get the significance up on the post-fire precip. This didn't have a good effect.
Trial 4. Same as 2, but linear for post-fire SAP. Didn't improve things.
Vars to choose from:
```{r}
topoVars <- c("hli","tpi","northness","eastness","adjustedNorthness","adj_fold_northness","slope","elevation")
histClimateVars <- c("AEThist","CWDhist","TmaxHist","PPThist","PPThistSD","TmaxHistSD")
seedVars <- c("preFireConCov","disturbanceSize","burnSev","postFireConCov","postFireSAP","postFirePlanting")
postFireWeatherVars <- c("pptYr1_3_annual_mean","ppt_Yr1_3_mean_annual_anom",
"min_annual_ppt_Yr1_3", "min_annual_ppt_Yr1_3_anom",
"pptYr1_3_annual_mean_t_adjusted",
"min_annual_ppt_t_adjusted_Yr1_3")
otherVars <- c("x","y","wilderness","forestType","mediumReburns","lowReburns","recoveryTime","fire")
```
##Model 3. Best model including planting etc.
The goal of model three is to find the best model of a series of candidate models to predict RRI
while including areas that were planted and had reburned.
```{r,echo=F}
mods <- list()
```
```{r,echo=F}
trialN <- 21
sub <- T
k.default = 10
sampleSize <- 30
source('00_setup.R')
# modData <- df4 %>%
# mutate(across(where(is.numeric),scale_this))
# modDataSub <- tibble()
#
# for(fa in unique(modData$focalAreaID)){
# d <- modData %>% filter(focalAreaID == fa)
# if(nrow(d) < sampleSize){
# tmp <- d
# }else{
# tmp <- sample_n(d,size = sampleSize)
# }
# modDataSub <- rbind(modDataSub,tmp)
# }
#
# if(sub == T){
# modData <- modDataSub
# }
modData <- read_csv('data/mod_3-2022-04-02 17:13:55data.csv')
modData <- modData %>% filter(pixelID %in% includePix$pixelID)
#modData <- modData %>% mutate_at(.vars = "RRI_end", .funs = function(x){ (x * 0.3856) + 0.5110378 } )
mod <- gam(formula = RRI_end ~
recoveryTime +
s(x, y, bs = "gp", k = 75, m = 2) +
#s(fire, bs="re") +
mediumReburns +
#s(lowReburns, k = k.default) +
#postFirePlanting +
burnSev +
#tpi +
#s(PPThist) +
s(TmaxHist, k = 15) +
#s(tmaxYr1_3_mean, k = 15) +
#adj_fold_northness +
s(adj_fold_northness, k = 15) +
#s(hli, k = k.default) +
#s(slope, k = k.default) +
#s(CWDhist, k = k.default) +
#s(AEThist, k = 15) +
s(postFireSAP, k = 15) +
s(pptYr1_3_annual_mean, k = 15),
#s(ppt_Yr1_3_mean_annual_anom, k = 15),
data = modData)
#for correlation matrix
otherVars.x <- c("recoveryTime","mediumReburns","lowReburns")
topoVar.x <- c("slope","hli")
histClimateVars.x <- c("AEThist","CWDhist")
seedVars.x <- c("burnSev","postFireSAP","postFirePlanting")
postFireWeather.x <- c("pptYr1_3_annual_mean")
predictors.x <- c(otherVars.x,topoVar.x,histClimateVars.x,seedVars.x,postFireWeather.x)
getCorrelationMatrix(d = modData, varsForCorr = predictors.x)
#check spatial autocorrelation
pixel.dists <- as.matrix(dist(cbind(modData$x, modData$y)))
pixel.dists.inv <- 1/pixel.dists
diag(pixel.dists.inv) <- 0
#store model for making table and comparing AICs later
modName <- paste0("mod_",trialN,"-",Sys.time(),'.RDS')
dataName <- paste0("mod_",trialN,"-",Sys.time(),"data.csv")
#saveRDS(object = mod, file = paste0('data/',modName))
#write_csv(modData,file = paste0('data/',dataName))
#mod <- readRDS("data/mod_3-2022-04-02 17:13:55.RDS")
#view results
summary(mod)
gam.check(mod)
plot(mod)
print(Moran.I(residuals(mod),pixel.dists.inv))
print(concurvity(mod))
paste0("AIC:",AIC(mod))
#write_csv(modDataSub,file = "data/modDataSub_StewartModel.csv")
```
```{r}
plot(modData$pptYr1_3_annual_mean,modData$RRI_end)
plot(df4$pptYr1_3_annual_mean, df4$RRI_end)
```
Results from best fitting model
Trial 1:
-result: spatial auto., CWD and AET negatively correlated, slope and postFireSAP positively correlated (?)., low reburns don't seem to matter, medium reburns linear, AET could be linear, ppt_1_3 not significant, but could be linear.
--next: reduce sample size to 35, take out AET, take out low Reburns, take out slope.
--interesting finding: slope and medium reburn are positively correlated
Trial 2:
--result: Still spatial auto, but otherwise, this is a good model, ppt in the first 3 years after fire seems to nullify AET. Other vars make sense.
--AIC is 1325
--adjusted R2 is 80
Trial 3
--goal: smaller sample size, larger k in x,y to see if we can get rid of spatial auto.
-- results: The precip. in the first 3 years seems to be more important that AEThist. Post-fire planting likely not significant because they plant where the fires are the worst. Perhaps post fire SAP not having an effect because this is best analyzed within a fire.
Trial 4
--same as above, but increased k x,y to account for spatial auto.
Trial 5
--take AET and postfire planting out. This improved the AIC.
--AIC:1080
Trial 6
--move postfire SAP to linear
--results raised AIC a lot, and re-introduced unexplained spatial structure
Trial 7
--trying to put ppt hist and t hist in the model
--result: AIC lowered (1069), tmax hist was signficant, ppthist was not as a smooth function.
Trial 8
--put ppt hist as linear term, increase k for t max hist. (from default to 15)
--result: AIC 1066, but ppthist is not significant.
Trial 9
--take out ppt hist.
--result: AIC 1067 (slightly higher than with ppthist in)
--best model so far
Trial 10
--try t adjusted precip 1-3 instead.
-- was not as good. AIC went up and spatial auto corr. went up.
Trial 11
--try min precip 1-3 instead.
--not as good. AIC went up and spatal auto went up.
Trial 12
--try precip anom instead. Not as good.
Trial 13
--try to get a topography variable in there.
--adjusted, folded, northness lowered the AIC to 1060 (7 lower than without); best model so far
Trial 14
--move adjusted northness to linear
--slightly higher than Trial 13 with adjusted folded northness as non-linear
Trial 15
--same as 13, but with CWD instead of Tmax.
Trail 16
--same as 13 with adjusted northness instead of adjusted, folded northness
Trial 17. add postfire planting to 13, but didn't improve anything.
Trial 18. added tpi to 13. no change. tpi as smooth not significant
Trial 19. added tpi as linear term.
Trial 20. same as 13, but tried tmax_1-3 instead of tmax_hist.
Trial 21. same as 13, but without standardizing the response variable.
Pick up here. Perhaps don't do Shive's model explicitly, but save that for the discussion.
##Analysis of SAP (perhaps look at interaction between CWD and seed availability at the site level)
Try running one focal area at a time
```{r}
mod_predictors <- c("recoveryTime","slope","aspect","pptYr1_3_annual_mean","burnSev","postFireSAP")
#getCorrelationMatrix(mod_predictors)
k.default = 5
modData <- df4 %>%
filter(fire == forSiteLevelAnalysis[5]) %>%
mutate(across(where(is.numeric),scale_this)) %>%
ggplot(aes(x,y,color = RRI_end)) +
geom_point()
#
mod <- gam(formula = RRI_end ~ s(x,y,k=15) +
s(burnSev, k = k.default) +
s(slope, k = k.default) + s(aspect, k = k.default) +
s(postFireSAP, k = k.default),
data = modData)
pixel.dists <- as.matrix(dist(cbind(modData$x, modData$y)))
pixel.dists.inv <- 1/pixel.dists
diag(pixel.dists.inv) <- 0
summary(mod)
gam.check(mod)
plot(mod)
AIC(mod)
print(Moran.I(residuals(mod),pixel.dists.inv))
```
Check if residuals are spatially correlated
```{r}
```
```{r}
mod2 <- fitGAM(df4 %>% filter(postFirePlanting < 0.1,
mediumReburns < 0.01,
lowReburns < 0.01),
mod2_predictors,response = "RRI_end", scaleData = T)
plot(mod2)
summary(mod2)
print(paste("AIC",AIC(mod2)))
```