Revision c4734e5d
Added by Benoit Parmentier over 12 years ago
climate/research/oregon/interpolation/GWR_prediction_reg_function.R | ||
---|---|---|
1 |
runGWR <- function(i) { # loop over dates |
|
2 |
|
|
3 |
date<-strptime(dates[i], "%Y%m%d") # interpolation date being processed |
|
4 |
month<-strftime(date, "%m") # current month of the date being processed |
|
5 |
LST_month<-paste("mm_",month,sep="") # name of LST month to be matched |
|
6 |
|
|
7 |
#i=1 |
|
8 |
date_proc<-dates[i] |
|
9 |
date_proc<-strptime(dates[i], "%Y%m%d") # interpolation date being processed |
|
10 |
mo<-as.integer(strftime(date_proc, "%m")) # current month of the date being processed |
|
11 |
day<-as.integer(strftime(date_proc, "%d")) |
|
12 |
year<-as.integer(strftime(date_proc, "%Y")) |
|
13 |
|
|
14 |
|
|
15 |
#Adding layer LST to the raster stack and preparing terms |
|
16 |
|
|
17 |
pos<-match(LST_month,layerNames(s_raster)) #Find column with the current month for instance mm12 |
|
18 |
r1<-raster(s_raster,layer=pos) #Select layer from stack |
|
19 |
layerNames(r1)<-"LST" |
|
20 |
s_raster_f<-s_raster #make a local copy for debugging |
|
21 |
s_raster_f<-addLayer(s_raster_f,r1) #Adding current month |
|
22 |
#s_sgdf<-as(s_raster,"SpatialGridDataFrame") #Conversion to spatial grid data frame |
|
23 |
|
|
24 |
nel<-12 |
|
25 |
#tab_range<-data.frame(varname=character(nel),varterm=character(nel),vmin=numeric(nel),vmax=numeric(nel)) |
|
26 |
val_range<-vector("list",nel) #list of one row data.frame |
|
27 |
val_rst<-vector("list",nel) #list of one row data.frame |
|
28 |
|
|
29 |
val_range[[1]]<-data.frame(varname="lon",varterm="lon",vmin=-180,vmax=180) |
|
30 |
val_range[[2]]<-data.frame(varname="lat",varterm="lat",vmin=-90,vmax=90) |
|
31 |
val_range[[3]]<-data.frame(varname="ELEV_SRTM",varterm="ELEV_SRTM",vmin=0,vmax=6000) |
|
32 |
val_range[[4]]<-data.frame(varname="Eastness",varterm="Eastness",vmin=-1,vmax=1) |
|
33 |
val_range[[5]]<-data.frame(varname="Northness",varterm="Northness",vmin=-1,vmax=1) |
|
34 |
val_range[[6]]<-data.frame(varname="Northness_w",varterm="Northness_w",vmin=-1,vmax=1) |
|
35 |
val_range[[7]]<-data.frame(varname="Eastness_w",varterm="Eastness_w",vmin=-1,vmax=1) |
|
36 |
val_range[[8]]<-data.frame(varname="mm_01",varterm="LST",vmin=-258.16,vmax=313.16) |
|
37 |
val_range[[9]]<-data.frame(varname="DISTOC",varterm="DISTOC",vmin=-0,vmax=10000000) |
|
38 |
val_range[[10]]<-data.frame(varname="LC1",varterm="LC1",vmin=0,vmax=100) |
|
39 |
val_range[[11]]<-data.frame(varname="LC3",varterm="LC3",vmin=0,vmax=100) |
|
40 |
val_range[[12]]<-data.frame(varname="slope",varterm="slope",vmin=0,vmax=90) |
|
41 |
tab_range<-do.call(rbind,val_range) |
|
42 |
|
|
43 |
#pos<-match("ELEV_SRTM",layerNames(s_raster)) #Find column with the current month for instance mm12 |
|
44 |
#ELEV_SRTM<-raster(s_raster,pos) |
|
45 |
|
|
46 |
for (k in 1:length(val_range)){ |
|
47 |
avl<-c(-Inf,tab_range$vmin[k],NA, tab_range$vmax[k],+Inf,NA) #This creates a input vector...val 1 are -9999, 2 neg, 3 positive |
|
48 |
rclmat<-matrix(avl,ncol=3,byrow=TRUE) |
|
49 |
s_raster_r<-raster(s_raster_f,match(tab_range$varterm[k],layerNames(s_raster_f))) |
|
50 |
s_raster_r<-reclass(s_raster_r,rclmat) #Loss of layer names when using reclass |
|
51 |
layerNames(s_raster_r)<-tab_range$varterm[k] |
|
52 |
val_rst[[k]]<-s_raster_r |
|
53 |
} |
|
54 |
s_rst_m<-stack(val_rst) #This a stacked with valid range of values |
|
55 |
|
|
56 |
###Regression part 1: Creating a validation dataset by creating training and testing datasets |
|
57 |
|
|
58 |
mod_LST <-ghcn.subsets[[i]][,match(LST_month, names(ghcn.subsets[[i]]))] #Match interpolation date and monthly LST average |
|
59 |
ghcn.subsets[[i]] = transform(ghcn.subsets[[i]],LST = mod_LST) #Add the variable LST to the subset dataset |
|
60 |
#n<-nrow(ghcn.subsets[[i]]) |
|
61 |
#ns<-n-round(n*prop) #Create a sample from the data frame with 70% of the rows |
|
62 |
#nv<-n-ns #create a sample for validation with prop of the rows |
|
63 |
#ind.training <- sample(nrow(ghcn.subsets[[i]]), size=ns, replace=FALSE) #This selects the index position for 70% of the rows taken randomly |
|
64 |
ind.training<-sampling[[i]] |
|
65 |
ind.testing <- setdiff(1:nrow(ghcn.subsets[[i]]), ind.training) |
|
66 |
data_s <- ghcn.subsets[[i]][ind.training, ] #Training dataset currently used in the modeling |
|
67 |
data_v <- ghcn.subsets[[i]][ind.testing, ] #Testing/validation dataset using input sampling |
|
68 |
|
|
69 |
ns<-nrow(data_s) |
|
70 |
nv<-nrow(data_v) |
|
71 |
|
|
72 |
###BEFORE model prediction the data object must be transformed to SDF |
|
73 |
|
|
74 |
coords<- data_v[,c('x_OR83M','y_OR83M')] |
|
75 |
coordinates(data_v)<-coords |
|
76 |
proj4string(data_v)<-CRS #Need to assign coordinates... |
|
77 |
coords<- data_s[,c('x_OR83M','y_OR83M')] |
|
78 |
coordinates(data_s)<-coords |
|
79 |
proj4string(data_s)<-CRS #Need to assign coordinates.. |
|
80 |
|
|
81 |
ns<-nrow(data_s) #This is added to because some loss of data might have happened because of the averaging... |
|
82 |
nv<-nrow(data_v) |
|
83 |
|
|
84 |
### PREDICTION/ Interpolation |
|
85 |
|
|
86 |
pos<-match("value",names(data_s)) #Find column with name "value" |
|
87 |
names(data_s)[pos]<-y_var_name |
|
88 |
pos<-match("value",names(data_v)) #Find column with name "value" |
|
89 |
names(data_v)[pos]<-y_var_name |
|
90 |
|
|
91 |
#if y_var_name=="dailyTmax" |
|
92 |
data_v$y_var<-data_v[[y_var_name]]/10 #Note that values are divided by 10 because the var is temp |
|
93 |
data_s$y_var<-data_s[[y_var_name]]/10 |
|
94 |
|
|
95 |
#Model and response variable can be changed without affecting the script |
|
96 |
|
|
97 |
formula1 <- as.formula("y_var~ lat + lon + ELEV_SRTM", env=.GlobalEnv) |
|
98 |
formula2 <- as.formula("y_var~ I(lat*lon) + ELEV_SRTM", env=.GlobalEnv) |
|
99 |
formula3 <- as.formula("y_var~ lat + lon + ELEV_SRTM + Northness + Eastness + DISTOC", env=.GlobalEnv) |
|
100 |
formula4 <- as.formula("y_var~ I(lat*lon) + ELEV_SRTM + I(Northness*Eastness) + DISTOC + LST", env=.GlobalEnv) |
|
101 |
formula5 <- as.formula("y_var~ lat + lon + ELEV_SRTM + Northness_w + Eastness_w + DISTOC + LST", env=.GlobalEnv) |
|
102 |
formula6 <- as.formula("y_var~ lat + lon + ELEV_SRTM + Northness_w + Eastness_w + DISTOC + LST + LC1", env=.GlobalEnv) |
|
103 |
formula7 <- as.formula("y_var~ lat + lon + ELEV_SRTM + Northness_w + Eastness_w + DISTOC + LST + LC3", env=.GlobalEnv) |
|
104 |
formula8 <- as.formula("y_var~ lat + lon + ELEV_SRTM + Northness_w + Eastness_w + DISTOC + LST + I(LC1*LC3)", env=.GlobalEnv) |
|
105 |
|
|
106 |
# bwG <- gwr.sel(tmax~ lon + lat + ELEV_SRTM + Eastness + Northness + DISTOC,data=data_s,gweight=gwr.Gauss, verbose = FALSE) |
|
107 |
# gwrG<- gwr(tmax~ lon + lat + ELEV_SRTM + Eastness + Northness + DISTOC, data=data_s, bandwidth=bwG, gweight=gwr.Gauss, hatmatrix=TRUE) |
|
108 |
|
|
109 |
bwGm1 <-try(gwr.sel(formula1,data=data_s,gweight=gwr.Gauss, verbose = FALSE)) |
|
110 |
bwGm2 <-try(gwr.sel(formula2,data=data_s,gweight=gwr.Gauss, verbose = FALSE)) |
|
111 |
bwGm3 <-try(gwr.sel(formula3,data=data_s,gweight=gwr.Gauss, verbose = FALSE)) |
|
112 |
bwGm4 <-try(gwr.sel(formula4,data=data_s,gweight=gwr.Gauss, verbose = FALSE)) |
|
113 |
bwGm5 <-try(gwr.sel(formula5,data=data_s,gweight=gwr.Gauss, verbose = FALSE)) |
|
114 |
bwGm6 <-try(gwr.sel(formula6,data=data_s,gweight=gwr.Gauss, verbose = FALSE)) |
|
115 |
bwGm7 <-try(gwr.sel(formula7,data=data_s,gweight=gwr.Gauss, verbose = FALSE)) |
|
116 |
bwGm8 <-try(gwr.sel(formula8,data=data_s,gweight=gwr.Gauss, verbose = FALSE)) |
|
117 |
|
|
118 |
mod1<- try(gwr(formula1, data=data_s, bandwidth=bwGm1, gweight=gwr.Gauss, hatmatrix=TRUE)) |
|
119 |
mod2<- try(gwr(formula2, data=data_s, bandwidth=bwGm2, gweight=gwr.Gauss, hatmatrix=TRUE)) |
|
120 |
mod3<- try(gwr(formula3, data=data_s, bandwidth=bwGm3, gweight=gwr.Gauss, hatmatrix=TRUE)) |
|
121 |
mod4<- try(gwr(formula4, data=data_s, bandwidth=bwGm4, gweight=gwr.Gauss, hatmatrix=TRUE)) |
|
122 |
mod5<- try(gwr(formula5, data=data_s, bandwidth=bwGm5, gweight=gwr.Gauss, hatmatrix=TRUE)) |
|
123 |
mod6<- try(gwr(formula6, data=data_s, bandwidth=bwGm6, gweight=gwr.Gauss, hatmatrix=TRUE)) |
|
124 |
mod7<- try(gwr(formula7, data=data_s, bandwidth=bwGm7, gweight=gwr.Gauss, hatmatrix=TRUE)) |
|
125 |
mod8<- try(gwr(formula8, data=data_s, bandwidth=bwGm8, gweight=gwr.Gauss, hatmatrix=TRUE)) |
|
126 |
|
|
127 |
#tmax_predicted=themolst+daily_delta_rast-bias_rast #Final surface?? but daily_rst |
|
128 |
#pred1 <- gwr(formula1, data_s, bandwidth=bwGm1, fit.points =s_sgdf_m,predict=TRUE, se.fit=TRUE,fittedGWRobject=mod1) |
|
129 |
#pred2 <- gwr(formula1, data_s, bandwidth=bwGm2, fit.points =s_sgdf_m,predict=TRUE, se.fit=TRUE,fittedGWRobject=mod1) |
|
130 |
|
|
131 |
pred_gwr<-vector("list",nmodels) #This will contain the nine gwr predictions... |
|
132 |
mod_obj<-vector("list",nmodels) #This will contain the nine gwr fitting |
|
133 |
|
|
134 |
for (j in 1:nmodels){ |
|
135 |
|
|
136 |
##Model assessment: specific diagnostic/metrics for GAM |
|
137 |
|
|
138 |
name<-paste("mod",j,sep="") #modj is the name of The "j" model (mod1 if j=1) |
|
139 |
mod<-get(name) #accessing GAM model ojbect "j" |
|
140 |
|
|
141 |
mod_obj[[j]]<-mod #Storing the current mod in a list |
|
142 |
formula_name<-paste("formula",j,sep="") |
|
143 |
formula<-get(formula_name) |
|
144 |
bwGm_name<-paste("bwGm",j,sep="") |
|
145 |
bwGm<-get(bwGm_name) |
|
146 |
t<-terms(formula) #extract information from formula |
|
147 |
t_l<-labels(t) #extract labels from formula: this is a string (character), this can be used directly to find the variables in the stack!! |
|
148 |
|
|
149 |
#inter<-grepl(pattern="I", t_l,ignore.case=FALSE) |
|
150 |
inter<-grepl(pattern="I\\(", t_l,ignore.case=FALSE) |
|
151 |
if (sum(inter)>0){ |
|
152 |
interaction<-vector("list",length(sum(inter))) |
|
153 |
ks<-0 |
|
154 |
for(k in 1:length(inter)){ |
|
155 |
if (inter[k]==TRUE){ |
|
156 |
ks<-ks+1 |
|
157 |
postion<-regexpr("\\(.*\\)$",t_l[k]) |
|
158 |
x<-substring(t_l[k],first=postion+1,last=postion+attr(postion,"match.length")-2) #extract characters between brackets |
|
159 |
t_inter <-as.character(unlist(strsplit(x, "[*]"))) #strsplit creates a list!! |
|
160 |
interaction[[ks]]<-trim(t_inter) |
|
161 |
} |
|
162 |
} |
|
163 |
mod_varn<-c(unlist(interaction),t_l[inter==FALSE]) #"trim" white space in string character |
|
164 |
} |
|
165 |
|
|
166 |
if (sum(inter)==0){ |
|
167 |
#interaction<-vector("list",length(sum(inter))) |
|
168 |
mod_varn<-t_l |
|
169 |
} |
|
170 |
#browser() |
|
171 |
list_rst<-vector("list",length(mod_varn)) |
|
172 |
pos<-match(mod_varn,layerNames(s_rst_m)) #Find column with the current month for instance mm12 |
|
173 |
s_rst_mod<-subset(s_rst_m,pos) |
|
174 |
|
|
175 |
#NOW CREATE THE MASK?? OR REMOVE NA AT THE data.frame stage... |
|
176 |
### Model assessment |
|
177 |
s_sgdf<-as(s_rst_mod,"SpatialGridDataFrame") |
|
178 |
s_spdf<-as.data.frame(s_sgdf) #Note that this automatically removes all NA rows |
|
179 |
s_spdf<-na.omit(s_spdf) #removes all rows that have na... |
|
180 |
coords<- s_spdf[,c('s1','s2')] |
|
181 |
coordinates(s_spdf)<-coords |
|
182 |
proj4string(s_spdf)<-CRS #Need to assign coordinates... |
|
183 |
|
|
184 |
|
|
185 |
#If mod "j" is not a model object |
|
186 |
if (inherits(mod,"try-error")) { |
|
187 |
|
|
188 |
results_m1[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
189 |
results_m1[1,2]<- ns #number of stations used in the training stage |
|
190 |
results_m1[1,3]<- "SSERR" |
|
191 |
results_m1[1,j+3]<- NA |
|
192 |
|
|
193 |
results_m2[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
194 |
results_m2[1,2]<- ns #number of stations used in the training |
|
195 |
results_m2[1,3]<- "GCV" |
|
196 |
results_m2[1,j+3]<- NA |
|
197 |
|
|
198 |
results_m3[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
199 |
results_m3[1,2]<- ns #number of stations used in the training stage |
|
200 |
results_m3[1,3]<- "DEV" |
|
201 |
results_m3[1,j+3]<- NA |
|
202 |
|
|
203 |
results_RMSE_f[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
204 |
results_RMSE_f[1,2]<- ns #number of stations used in the training stage |
|
205 |
results_RMSE_f[1,3]<- "RSME_f" |
|
206 |
results_RMSE_f[1,j+3]<- NA |
|
207 |
|
|
208 |
results_MAE_f[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
209 |
results_MAE_f[1,2]<- ns #number of stations used in the training stage |
|
210 |
results_MAE_f[1,3]<- "MAE_f" |
|
211 |
results_MAE_f[1,j+3]<-NA |
|
212 |
|
|
213 |
results_RMSE[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
214 |
results_RMSE[1,2]<- ns #number of stations used in the training stage |
|
215 |
results_RMSE[1,3]<- "RMSE" |
|
216 |
results_RMSE[1,j+3]<- NA #Storing RMSE for the model j |
|
217 |
results_MAE[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
218 |
results_MAE[1,2]<- ns #number of stations used in the training stage |
|
219 |
results_MAE[1,3]<- "MAE" |
|
220 |
results_MAE[1,j+3]<- NA #Storing MAE for the model j |
|
221 |
results_ME[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
222 |
results_ME[1,2]<- ns #number of stations used in the training stage |
|
223 |
results_ME[1,3]<- "ME" |
|
224 |
results_ME[1,j+3]<- NA #Storing ME for the model j |
|
225 |
results_R2[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
226 |
results_R2[1,2]<- ns #number of stations used in the training stage |
|
227 |
results_R2[1,3]<- "R2" |
|
228 |
results_R2[1,j+3]<- NA #Storing R2 for the model j |
|
229 |
|
|
230 |
} |
|
231 |
|
|
232 |
#If mod is a modelobject |
|
233 |
|
|
234 |
#If mod "j" is not a model object |
|
235 |
if (inherits(mod,"gwr")) { |
|
236 |
|
|
237 |
pred <- gwr(formula1, data_s, bandwidth=bwGm, fit.points =s_spdf,predict=TRUE, se.fit=TRUE,fittedGWRobject=mod) |
|
238 |
|
|
239 |
pred_gwr[[j]]<-pred #prediction stored in a list |
|
240 |
|
|
241 |
raster_pred<-rasterize(pred$SDF,r1,"pred",fun=mean) |
|
242 |
|
|
243 |
layerNames(raster_pred)<-"y_pred" |
|
244 |
|
|
245 |
data_name<-paste("predicted_mod",j,"_",dates[[i]],sep="") |
|
246 |
raster_name<-paste("GWR_",data_name,out_prefix,".rst", sep="") |
|
247 |
writeRaster(raster_pred, filename=raster_name,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI) |
|
248 |
#writeRaster(r2, filename=raster_name,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI) |
|
249 |
|
|
250 |
#Save png plot here... |
|
251 |
data_name<-paste("predicted_mod",j,"_",dates[[i]],sep="") |
|
252 |
png_name<-paste("GWR_plot_",data_name,out_prefix,".png", sep="") |
|
253 |
png(png_name) #Create file to write a plot |
|
254 |
#datelabel2=format(ISOdate(year,mo,day),"%B ") #Plot label |
|
255 |
plot(raster_pred) #Plot to file the autokrige object |
|
256 |
#savePlot(paste("Bias_surface_LST_TMax_",dates[i],out_prefix,".png", sep=""), type="png") |
|
257 |
dev.off() #Release the hold to the file |
|
258 |
|
|
259 |
|
|
260 |
#rpred<-as #Extracting the SptialGriDataFrame from the autokrige object |
|
261 |
|
|
262 |
#rpred<- predict(mod, newdata=s_sgdf, se.fit = TRUE) #Using the coeff to predict new values. |
|
263 |
#y_pred<-rpred$var1.pred #is the order the same? |
|
264 |
#y_prederr<-rpred$var1.var |
|
265 |
|
|
266 |
pred_sgdf<-as(raster_pred,"SpatialGridDataFrame" ) #Conversion to spatial grid data frame |
|
267 |
#rpred_val_s <- overlay(raster_pred,data_s) #This overlays the kriged surface tmax and the location of weather stations |
|
268 |
|
|
269 |
rpred_val_s <- overlay(pred_sgdf,data_s) #This overlays the kriged surface tmax and the location of weather stations |
|
270 |
rpred_val_v <- overlay(pred_sgdf,data_v) #This overlays the kriged surface tmax and the location of weather stations |
|
271 |
|
|
272 |
pred_mod<-paste("pred_mod",j,sep="") |
|
273 |
#Adding the results back into the original dataframes. |
|
274 |
data_s[[pred_mod]]<-rpred_val_s$y_pred |
|
275 |
data_v[[pred_mod]]<-rpred_val_v$y_pred |
|
276 |
|
|
277 |
#Model assessment: RMSE and then krig the residuals....! |
|
278 |
|
|
279 |
res_mod_s<- data_s$y_var - data_s[[pred_mod]] #Residuals from kriging training |
|
280 |
res_mod_v<- data_v$y_var - data_v[[pred_mod]] #Residuals from kriging validation |
|
281 |
|
|
282 |
####ADDED ON JULY 20th |
|
283 |
res_mod<-res_mod_v |
|
284 |
|
|
285 |
#RMSE_mod <- sqrt(sum(res_mod^2)/nv) #RMSE FOR REGRESSION STEP 1: GAM |
|
286 |
RMSE_mod<- sqrt(mean(res_mod^2,na.rm=TRUE)) |
|
287 |
#MAE_mod<- sum(abs(res_mod),na.rm=TRUE)/(nv-sum(is.na(res_mod))) #MAE from kriged surface validation |
|
288 |
MAE_mod<- mean(abs(res_mod), na.rm=TRUE) |
|
289 |
#ME_mod<- sum(res_mod,na.rm=TRUE)/(nv-sum(is.na(res_mod))) #ME, Mean Error or bias FOR REGRESSION STEP 1: GAM |
|
290 |
ME_mod<- mean(res_mod,na.rm=TRUE) #ME, Mean Error or bias FOR REGRESSION STEP 1: GAM |
|
291 |
#R2_mod<- cor(data_v$y_var,data_v[[pred_mod]])^2 #R2, coef. of var FOR REGRESSION STEP 1: GAM |
|
292 |
R2_mod<- cor(data_v$y_var,data_v[[pred_mod]], use="complete")^2 |
|
293 |
|
|
294 |
R2_mod_f<- cor(data_s$y_var,data_s[[pred_mod]], use="complete")^2 |
|
295 |
RMSE_mod_f<- sqrt(mean(res_mod_s^2,na.rm=TRUE)) |
|
296 |
#MAE_mod<- sum(abs(res_mod),na.rm=TRUE)/(nv-sum(is.na(res_mod))) #MAE from kriged surface validation |
|
297 |
MAE_mod_f<- mean(abs(res_mod_s), na.rm=TRUE) |
|
298 |
|
|
299 |
results_m1[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
300 |
results_m1[1,2]<- ns #number of stations used in the training stage |
|
301 |
results_m1[1,3]<- "AICb" |
|
302 |
results_m1[1,j+3]<- mod$results$AICb |
|
303 |
|
|
304 |
results_m2[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
305 |
results_m2[1,2]<- ns #number of stations used in the training |
|
306 |
results_m2[1,3]<- "RSS" |
|
307 |
results_m2[1,j+3]<- mod$results$rss |
|
308 |
|
|
309 |
results_m3[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
310 |
results_m3[1,2]<- ns #number of stations used in the training stage |
|
311 |
results_m3[1,3]<- "AICc" |
|
312 |
results_m3[1,j+3]<- mod$results$AICc |
|
313 |
|
|
314 |
results_RMSE_f[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
315 |
results_RMSE_f[1,2]<- ns #number of stations used in the training stage |
|
316 |
results_RMSE_f[1,3]<- "RSME_f" |
|
317 |
results_RMSE_f[1,j+3]<-RMSE_mod_f |
|
318 |
|
|
319 |
results_MAE_f[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
320 |
results_MAE_f[1,2]<- ns #number of stations used in the training stage |
|
321 |
results_MAE_f[1,3]<- "MAE_f" |
|
322 |
results_MAE_f[1,j+3]<-MAE_mod_f |
|
323 |
|
|
324 |
results_R2_f[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
325 |
results_R2_f[1,2]<- ns #number of stations used in the training stage |
|
326 |
results_R2_f[1,3]<- "R2_f" |
|
327 |
results_R2_f[1,j+3]<- R2_mod_f #Storing R2 for the model j |
|
328 |
|
|
329 |
results_RMSE[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
330 |
results_RMSE[1,2]<- ns #number of stations used in the training stage |
|
331 |
results_RMSE[1,3]<- "RMSE" |
|
332 |
results_RMSE[1,j+3]<- RMSE_mod #Storing RMSE for the model j |
|
333 |
results_MAE[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
334 |
results_MAE[1,2]<- ns #number of stations used in the training stage |
|
335 |
results_MAE[1,3]<- "MAE" |
|
336 |
results_MAE[1,j+3]<- MAE_mod #Storing MAE for the model j |
|
337 |
results_ME[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
338 |
results_ME[1,2]<- ns #number of stations used in the training stage |
|
339 |
results_ME[1,3]<- "ME" |
|
340 |
results_ME[1,j+3]<- ME_mod #Storing ME for the model j |
|
341 |
results_R2[1,1]<- dates[i] #storing the interpolation dates in the first column |
|
342 |
results_R2[1,2]<- ns #number of stations used in the training stage |
|
343 |
results_R2[1,3]<- "R2" |
|
344 |
results_R2[1,j+3]<- R2_mod #Storing R2 for the model j |
|
345 |
|
|
346 |
#Saving residuals and prediction in the dataframes: tmax predicted from GAM |
|
347 |
|
|
348 |
name2<-paste("res_mod",j,sep="") |
|
349 |
data_v[[name2]]<-as.numeric(res_mod_v) |
|
350 |
data_s[[name2]]<-as.numeric(res_mod_s) |
|
351 |
#end of loop calculating RMSE |
|
352 |
} |
|
353 |
} |
|
354 |
|
|
355 |
#if (i==length(dates)){ |
|
356 |
|
|
357 |
#Specific diagnostic measures related to the testing datasets |
|
358 |
|
|
359 |
results_table_RMSE<-as.data.frame(results_RMSE) |
|
360 |
results_table_MAE<-as.data.frame(results_MAE) |
|
361 |
results_table_ME<-as.data.frame(results_ME) |
|
362 |
results_table_R2<-as.data.frame(results_R2) |
|
363 |
results_table_RMSE_f<-as.data.frame(results_RMSE_f) |
|
364 |
results_table_MAE_f<-as.data.frame(results_MAE_f) |
|
365 |
results_table_R2_f<-as.data.frame(results_R2_f) |
|
366 |
|
|
367 |
results_table_m1<-as.data.frame(results_m1) |
|
368 |
results_table_m2<-as.data.frame(results_m2) |
|
369 |
results_table_m3<-as.data.frame(results_m3) |
|
370 |
|
|
371 |
#Preparing labels |
|
372 |
mod_labels<-rep("mod",nmodels) |
|
373 |
index<-as.character(1:nmodels) |
|
374 |
mod_labels<-paste(mod_labels,index,sep="") |
|
375 |
|
|
376 |
tb_metrics1<-rbind(results_table_RMSE,results_table_MAE, results_table_ME, |
|
377 |
results_table_R2,results_table_RMSE_f,results_table_MAE_f,results_table_R2_f) # |
|
378 |
tb_metrics2<-rbind(results_table_m1,results_table_m2, results_table_m3) |
|
379 |
cname<-c("dates","ns","metric", mod_labels) |
|
380 |
#cname<-c("dates","ns","metric","mod1", "mod2","mod3", "mod4", "mod5", "mod6", "mod7","mod8") |
|
381 |
colnames(tb_metrics1)<-cname |
|
382 |
#cname<-c("dates","ns","metric","mod1", "mod2","mod3", "mod4", "mod5", "mod6", "mod7","mod8") |
|
383 |
colnames(tb_metrics2)<-cname |
|
384 |
|
|
385 |
#} |
|
386 |
print(paste(dates[i],"processed")) |
|
387 |
# Kriging object may need to be modified...because it contains the full image of prediction!! |
|
388 |
##loop through model objects data frame and set field to zero... |
|
389 |
|
|
390 |
#mod_obj<-list(mod1,mod2,mod3,mod4,mod5,mod6,mod7,mod8) |
|
391 |
#names(mod_obj)<-c("mod1","mod2","mod3","mod4","mod5","mod6","mod7","mod8") #generate names automatically?? |
|
392 |
names(mod_obj)<-mod_labels |
|
393 |
#results_list<-list(data_s,data_v,tb_metrics1,tb_metrics2) |
|
394 |
save(mod_obj,file= paste(path,"/","results_list_mod_objects_",dates[i],out_prefix,".RData",sep="")) |
|
395 |
|
|
396 |
names(pred_gwr)<-mod_labels |
|
397 |
save(pred_gwr,file= paste(path,"/","results_list_pred_mod_objects_",dates[i],out_prefix,".RData",sep="")) |
|
398 |
|
|
399 |
results_list<-list(data_s,data_v,tb_metrics1,tb_metrics2,mod_obj) |
|
400 |
names(results_list)<-c("data_s","data_v","tb_metrics1","tb_metrics2","mod_obj") |
|
401 |
save(results_list,file= paste(path,"/","results_list_metrics_objects_",dates[i],out_prefix,".RData",sep="")) |
|
402 |
return(results_list) |
|
403 |
#return(tb_diagnostic1) |
|
404 |
} |
Also available in: Unified diff
GWR, raster prediction -function used in the main script