-
Notifications
You must be signed in to change notification settings - Fork 0
/
BurnArea_GAM_Obs_Forecasting_Ziter_validation_SWEImod_PCA.R
292 lines (265 loc) · 11.8 KB
/
BurnArea_GAM_Obs_Forecasting_Ziter_validation_SWEImod_PCA.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
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
#load in libraries
library(splines)
library(foreach)
library(nlme)
library(mgcv)
library(Metrics)
library(MASS)
library(sm)
library(mgcv.helper)
library(tibble)
library(dplyr)
library(fmsb)
library(randomForest)
library(KRLS)
library(matrixStats)
#remove variables from prior run
rm(list = ls())
#define model concurvity threshold:
concurvity_threshold = 0.4
##=========================pick best model based on BurnArea_GAM_v0*.R
#read in data
#0-1100m:
Climate_Fire_Data = read.table("/Volumes/Pruina_External_Elements/DroughtFireSnow/Data/AnlysisData/Annual_Fire_Climate_Obs_forecast_Ziter1.csv",sep=",")
#define variables:
WYs = Climate_Fire_Data[1:37,1]
BAs = Climate_Fire_Data[1:37,2]
BAs_Z1 = (BAs*0.000247105) /(10^6);
SWEIs_Z1 = Climate_Fire_Data[1:37,3]
WinPRCP_Z1 = Climate_Fire_Data[1:37,4]
WinTMP_Z1 = Climate_Fire_Data[1:37,5]
WinterPDSI_Z1 = Climate_Fire_Data[1:37,6]
Spring_PRCP_Z1 = Climate_Fire_Data[1:37,7]
Spring_TMP_Z1 = Climate_Fire_Data[1:37,8]
Spring_PDSI_Z1 = Climate_Fire_Data[1:37,9]
Spring_SWEI_Z1 = Climate_Fire_Data[1:37,10]
Spring_VPD_Z1 = Climate_Fire_Data[1:37,11]
Winter_VPD_Z1 = Climate_Fire_Data[1:37,12]
MODIS_BA_Z1 = Climate_Fire_Data[1:37,13]
Spring_ET_Z1 = Climate_Fire_Data[1:37,14]
Spring_PET_Z1 = Climate_Fire_Data[1:37,15]
Spring_PETminusET_Z1 = Climate_Fire_Data[1:37,16]
Winter_ET_Z1 = Climate_Fire_Data[1:37,17]
Winter_PET_Z1 = Climate_Fire_Data[1:37,18]
Winter_PETminusET_Z1 = Climate_Fire_Data[1:37,19]
Winter_DroughtArea_Z1 = Climate_Fire_Data[1:37,31]
Spring_DroughtArea_Z1 = Climate_Fire_Data[1:37,32]
Z1_ID = rep(1,37)
#relate MODIS and MTBS BA linearly to allow MODIS to gap fill MTBS 2020 BA:
BA_MTBS <- BAs_Z1[18:36]
BA_MODIS <- MODIS_BA_Z1[18:36]
BA_DF = data.frame(BA_MTBS=BA_MTBS,BA_MODIS=BA_MODIS)
BA_lm <- lm(data=BA_DF,BA_MTBS~BA_MODIS)
data_2020 = data.frame(BA_MODIS = MODIS_BA_Z1[37])
BA_2020_MTBS=predict(BA_lm,newdata=data_2020)
BAs_Z1[37] = BA_2020_MTBS
if (BAs_Z1[37] < 0){
BAs_Z1[37] = 0
}
#1100-2200:
Climate_Fire_Data = read.table("/Volumes/Pruina_External_Elements/DroughtFireSnow/Data/AnlysisData/Annual_Fire_Climate_Obs_forecast_Ziter2.csv",sep=",")
#define variables:
#define variables:
WYs = Climate_Fire_Data[1:37,1]
BAs = Climate_Fire_Data[1:37,2]
BAs_Z2 = (BAs*0.000247105) /(10^6);
SWEIs_Z2 = Climate_Fire_Data[1:37,3]
WinPRCP_Z2 = Climate_Fire_Data[1:37,4]
WinTMP_Z2 = Climate_Fire_Data[1:37,5]
WinterPDSI_Z2 = Climate_Fire_Data[1:37,6]
Spring_PRCP_Z2 = Climate_Fire_Data[1:37,7]
Spring_TMP_Z2 = Climate_Fire_Data[1:37,8]
Spring_PDSI_Z2 = Climate_Fire_Data[1:37,9]
Spring_SWEI_Z2 = Climate_Fire_Data[1:37,10]
Spring_VPD_Z2 = Climate_Fire_Data[1:37,11]
Winter_VPD_Z2 = Climate_Fire_Data[1:37,12]
MODIS_BA_Z2 = Climate_Fire_Data[1:37,13]
Spring_ET_Z2 = Climate_Fire_Data[1:37,14]
Spring_PET_Z2 = Climate_Fire_Data[1:37,15]
Spring_PETminusET_Z2 = Climate_Fire_Data[1:37,16]
Winter_ET_Z2 = Climate_Fire_Data[1:37,17]
Winter_PET_Z2 = Climate_Fire_Data[1:37,18]
Winter_PETminusET_Z2 = Climate_Fire_Data[1:37,19]
Winter_DroughtArea_Z2 = Climate_Fire_Data[1:37,31]
Spring_DroughtArea_Z2 = Climate_Fire_Data[1:37,32]
Z2_ID = rep(2,37)
#relate MODIS and MTBS BA linearly to allow MODIS to gap fill MTBS 2020 BA:
BA_MTBS <- BAs_Z2[18:36]
BA_MODIS <- MODIS_BA_Z2[18:36]
BA_DF = data.frame(BA_MTBS=BA_MTBS,BA_MODIS=BA_MODIS)
BA_lm <- lm(data=BA_DF,BA_MTBS~BA_MODIS)
data_2020 = data.frame(BA_MODIS = MODIS_BA_Z2[37])
BA_2020_MTBS=predict(BA_lm,newdata=data_2020)
BAs_Z2[37] = BA_2020_MTBS
if (BAs_Z2[37] < 0){
BAs_Z2[37] = 0
}
#2200-3300:
Climate_Fire_Data = read.table("/Volumes/Pruina_External_Elements/DroughtFireSnow/Data/AnlysisData/Annual_Fire_Climate_Obs_forecast_Ziter3.csv",sep=",")
#define variables:
#define variables:
WYs = Climate_Fire_Data[1:37,1]
BAs = Climate_Fire_Data[1:37,2]
BAs_Z3 = (BAs*0.000247105) /(10^6);
SWEIs_Z3 = Climate_Fire_Data[1:37,3]
WinPRCP_Z3 = Climate_Fire_Data[1:37,4]
WinTMP_Z3 = Climate_Fire_Data[1:37,5]
WinterPDSI_Z3 = Climate_Fire_Data[1:37,6]
Spring_PRCP_Z3 = Climate_Fire_Data[1:37,7]
Spring_TMP_Z3 = Climate_Fire_Data[1:37,8]
Spring_PDSI_Z3 = Climate_Fire_Data[1:37,9]
Spring_SWEI_Z3 = Climate_Fire_Data[1:37,10]
Spring_VPD_Z3 = Climate_Fire_Data[1:37,11]
Winter_VPD_Z3 = Climate_Fire_Data[1:37,12]
MODIS_BA_Z3 = Climate_Fire_Data[1:37,13]
Spring_ET_Z3 = Climate_Fire_Data[1:37,14]
Spring_PET_Z3 = Climate_Fire_Data[1:37,15]
Spring_PETminusET_Z3 = Climate_Fire_Data[1:37,16]
Winter_ET_Z3 = Climate_Fire_Data[1:37,17]
Winter_PET_Z3 = Climate_Fire_Data[1:37,18]
Winter_PETminusET_Z3 = Climate_Fire_Data[1:37,19]
Winter_DroughtArea_Z3 = Climate_Fire_Data[1:37,31]
Spring_DroughtArea_Z3 = Climate_Fire_Data[1:37,32]
Z3_ID = rep(3,37)
#relate MODIS and MTBS BA linearly to allow MODIS to gap fill MTBS 2020 BA:
BA_MTBS <- BAs_Z3[18:36]
BA_MODIS <- MODIS_BA_Z3[18:36]
BA_DF = data.frame(BA_MTBS=BA_MTBS,BA_MODIS=BA_MODIS)
BA_lm <- lm(data=BA_DF,BA_MTBS~BA_MODIS)
data_2020 = data.frame(BA_MODIS = MODIS_BA_Z3[37])
BA_2020_MTBS=predict(BA_lm,newdata=data_2020)
BAs_Z3[37] = BA_2020_MTBS
if (BAs_Z3[37] < 0){
BAs_Z3[37] = 0
}
idx<-c(1:37)
WYs = rep(WYs[idx],3)
BAs = cbind(t(BAs_Z1[idx]),t(BAs_Z2[idx]),t(BAs_Z3[idx]))
SWEIs = cbind(t(SWEIs_Z1[idx]),t(SWEIs_Z2[idx]),t(SWEIs_Z3[idx]))
WinPRCP = cbind(t(WinPRCP_Z1[idx]),t(WinPRCP_Z2[idx]),t(WinPRCP_Z3[idx]))
WinTMP = cbind(t(WinTMP_Z1[idx]),t(WinTMP_Z2[idx]),t(WinTMP_Z3[idx]))
WinterPDSI = cbind(t(WinterPDSI_Z1[idx]),t(WinterPDSI_Z2[idx]),t(WinterPDSI_Z3[idx]))
Spring_PRCP = cbind(t(Spring_PRCP_Z1[idx]),t(Spring_PRCP_Z2[idx]),t(Spring_PRCP_Z3[idx]))
Spring_TMP = cbind(t(Spring_TMP_Z1[idx]),t(Spring_TMP_Z2[idx]),t(Spring_TMP_Z3[idx]))
Spring_PDSI = cbind(t(Spring_PDSI_Z1[idx]),t(Spring_PDSI_Z2[idx]),t(Spring_PDSI_Z3[idx]))
Spring_SWEI = cbind(t(Spring_SWEI_Z1[idx]),t(Spring_SWEI_Z2[idx]),t(Spring_SWEI_Z3[idx]))
Spring_VPD = cbind(t(Spring_VPD_Z1[idx]),t(Spring_VPD_Z2[idx]),t(Spring_VPD_Z3[idx]))
Winter_VPD = cbind(t(Winter_VPD_Z1[idx]),t(Winter_VPD_Z2[idx]),t(Winter_VPD_Z3[idx]))
Spring_ET = cbind(t(Spring_ET_Z1[idx]),t(Spring_ET_Z2[idx]),t(Spring_ET_Z3[idx]))
Spring_PET = cbind(t(Spring_PET_Z1[idx]),t(Spring_PET_Z2[idx]),t(Spring_PET_Z3[idx]))
Spring_PETminusET = cbind(t(Spring_PETminusET_Z1[idx]),t(Spring_PETminusET_Z2[idx]),t(Spring_PETminusET_Z3[idx]))
Winter_ET = cbind(t(Winter_ET_Z1[idx]),t(Winter_ET_Z2[idx]),t(Winter_ET_Z3[idx]))
Winter_PET = cbind(t(Winter_PET_Z1[idx]),t(Winter_PET_Z2[idx]),t(Winter_PET_Z3[idx]))
Winter_PETminusET = cbind(t(Winter_PETminusET_Z1[idx]),t(Winter_PETminusET_Z2[idx]),t(Winter_PETminusET_Z3[idx]))
Winter_DroughtArea = cbind(t(Winter_DroughtArea_Z1[idx]),t(Winter_DroughtArea_Z2[idx]),t(Winter_DroughtArea_Z3[idx]))
Spring_DroughtArea = cbind(t(Spring_DroughtArea_Z1[idx]),t(Spring_DroughtArea_Z2[idx]),t(Spring_DroughtArea_Z3[idx]))
Winter_Spring_Temp = (Spring_TMP+WinTMP)/2
Winter_Spring_Precip = (Spring_PRCP+WinPRCP)/2
Winter_Spring_VPD = (Winter_VPD+Spring_VPD)/2
Winter_Spring_PET = (Winter_PET+Spring_PET)/2
Winter_Spring_ET = (Winter_ET+Spring_ET)/2
Zs = cbind(t(Z1_ID[idx]),t(Z2_ID[idx]),t(Z3_ID[idx]))
Climate_Fire_DF = data.frame(Spring_SWEI = t(Spring_SWEI), WinPRCP = t(WinPRCP), WinTMP = t(WinTMP),Spring_PRCP = t(Spring_PRCP),Spring_TMP=t(Spring_TMP),Spring_VPD=t(Spring_VPD),Winter_VPD=t(Winter_VPD),Spring_ET=t(Spring_ET),Spring_PET=t(Spring_PET),Winter_ET=t(Winter_ET),Winter_PET=t(Winter_PET),Spring_DroughtArea=t(Spring_DroughtArea),Winter_Spring_Temp=t(Winter_Spring_Temp),Winter_Spring_Precip=t(Winter_Spring_Precip),Winter_Spring_VPD=t(Winter_Spring_VPD),Winter_Spring_PET=t(Winter_Spring_PET),Winter_Spring_ET=t(Winter_Spring_ET))
#loop through all combinations of covariates:
Y = as.vector(BAs)
#best model results from Select_BestMod_*.R
best_mods_idx = read.csv("/Volumes/Pruina_External_Elements/DroughtFireSnow/Data/AnlysisData/Best_Obs_Forecast_outputs/BAs_1984_2020_Obs_Forecast_SE_Zbins_SWEImod_PCA_bestmods_5vars.csv",sep=",",header=TRUE)
best_mods_idx=as.numeric(best_mods_idx)
nmods = length(best_mods_idx)
#define model combination IDs
nvars<- 17
n_predictors <- 5
ncombos = factorial(nvars)/(factorial(n_predictors)*factorial(nvars-n_predictors))
x=1:nvars
combo_IDs<-combn(x,n_predictors)
SWEI_iter=0 #count which of the best mods include SWEI as predictor
good_mod_iter=0
Good_mods=c()
store_R = c()
n=length(BAs)
index = 1:n
for (j in 1:nmods){
i <- best_mods_idx[j]
Y = as.vector(BAs)
col1 <- combo_IDs[1,i]
col2 <- combo_IDs[2,i]
col3 <- combo_IDs[3,i]
col4 <- combo_IDs[4,i]
col5 <- combo_IDs[5,i]
predictor1 <-Climate_Fire_DF[,col1]
predictor2 <-Climate_Fire_DF[,col2]
predictor3 <-Climate_Fire_DF[,col3]
predictor4 <-Climate_Fire_DF[,col4]
predictor5 <-Climate_Fire_DF[,col5]
#PCA combo model:
Covariates_DF = data.frame(predictor1=predictor1,predictor2=predictor2,predictor3=predictor3,predictor4=predictor4,predictor5=predictor5)
df.pca <- prcomp(Covariates_DF, center = TRUE,scale. = TRUE)
pcs = df.pca$x
pc1 = pcs[,1]
pc2 = pcs[,2]
pc3 = pcs[,3]
pc4 = pcs[,4]
pc5 = pcs[,5]
current_DF_pcs <- data.frame(Y=Y,predictor1=pc1,predictor2=pc2,predictor3=pc3,predictor4=pc4,predictor5=pc5,predictor6=as.vector(Zs))
mod <- gam(data=current_DF_pcs,Y~s(predictor1)+s(predictor2)+s(predictor3)+s(predictor4)+s(predictor5)+predictor6,family="gaussian")
#GAM drop 1 year
yest_gam=1
standar_error=1
Y_iter=0
for(Y in 1984:2020){
Y_iter = Y_iter+1
#drop 1 year
IDX_drop <-which(WYs == Y)
index1=index[-IDX_drop]
dropped_DF=Climate_Fire_DF[index1,]
#PCA combo model:
Zs_dropped=as.vector(Zs)
Zs_dropped = Zs_dropped[index1]
Y_dropped = as.vector(BAs)
Y_dropped = Y_dropped[index1]
dropped_DF_pcs <- data.frame(Y=Y_dropped,predictor1=pc1[index1],predictor2=pc2[index1],predictor3=pc3[index1],predictor4=pc4[index1],predictor5=pc5[index1],predictor6=as.vector(Zs_dropped))
S=dim(dropped_DF_pcs)
if(S[1]!=111-3){
S[1]
stop('wrong dim')
}
#model
fit_drop_gam = gam(mod$formula,data=dropped_DF_pcs,family="gaussian")
#now estimate at the point that was dropped
newdata = data.frame(predictor1=pc1[IDX_drop],predictor2=pc2[IDX_drop],predictor3=pc3[IDX_drop],predictor4=pc4[IDX_drop],predictor5=pc5[IDX_drop],predictor6=as.vector(Zs[IDX_drop]))
yest_gam[IDX_drop]=predict(fit_drop_gam,newdata=newdata)
#get the confidence interval:
yhat <- predict(fit_drop_gam,newdata=newdata,se.fit = TRUE)
standar_error[Y_iter] <- sum(yhat$se.fit)
}
IDX_neg <- which(yest_gam<0)
yest_gam[IDX_neg] <- min(yest_gam[-IDX_neg])
#sum BAs across all Z bins:
iter=0
store_BA_obs=0
store_BA_est=0
for (Y in 1984:2020){
iter <- iter+1
current_IDX <-which(WYs == Y)
store_BA_est[iter] = sum(yest_gam[current_IDX])
store_BA_obs[iter] = sum(BAs[current_IDX])
}
#record drop 1-year correlations:
Drop1_Ann_Cor <- cor(store_BA_est,store_BA_obs)
store_R = c(store_R,Drop1_Ann_Cor)
#export data for analysis in MATLAB from all models with R2>=0.0:
if (Drop1_Ann_Cor >= sqrt(0.0)) {
good_mod_iter = good_mod_iter+1
BAs_output<-data.frame(BA_obs=t(BAs),BA_est_drop1=yest_gam,BA_fit=predict(mod))
outfilename = sprintf("/Volumes/Pruina_External_Elements/DroughtFireSnow/Data/AnlysisData/Best_Obs_Forecast_outputs/BAs_1984_2020_Obs_Forecast_GAM_Zbins_mod%d_SWEImod_PCA.csv",i)
write.table(BAs_output,file=outfilename,sep=",",row.names = FALSE)
#record ID of good models to export and use in matlab analysis
Good_mods[good_mod_iter] = i
#record standard error:
outfilename = sprintf("/Volumes/Pruina_External_Elements/DroughtFireSnow/Data/AnlysisData/Best_Obs_Forecast_outputs/BAs_1984_2020_Obs_Forecast_SE_Zbins_mod%d_SWEImod_PCA.csv",i)
write.table(standar_error,file=outfilename,sep=",",row.names = FALSE)
}
}
outfilename = sprintf("/Volumes/Pruina_External_Elements/DroughtFireSnow/Data/AnlysisData/Best_Obs_Forecast_outputs/BAs_1984_2020_Obs_Good_Mods_SWEImod_PCA.csv")
write.table(Good_mods,file=outfilename,sep=",",row.names = FALSE)
store_R