Revision b493e133
Added by Benoit Parmentier over 11 years ago
climate/research/oregon/interpolation/GAM_fusion_analysis_raster_prediction_multisampling.R | ||
---|---|---|
11 | 11 |
#5)possibilty of running GAM+FUSION or GAM+CAI and other options added |
12 | 12 |
#The interpolation is done first at the monthly time scale then delta surfaces are added. |
13 | 13 |
#AUTHOR: Benoit Parmentier |
14 |
#DATE: 07/16/2013
|
|
14 |
#DATE: 07/26/2013
|
|
15 | 15 |
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#568-- |
16 | 16 |
# |
17 | 17 |
# TO DO: |
... | ... | |
180 | 180 |
t1<-proc.time() |
181 | 181 |
j=12 |
182 | 182 |
#browser() #Missing out_path for gam_fusion list param!!! |
183 |
if (interpolation_method=="gam_fusion"){ |
|
183 |
#if (interpolation_method=="gam_fusion"){ |
|
184 |
if (interpolation_method %in% c("gam_fusion","kriging_fusion","gwr_fusion")){ |
|
184 | 185 |
list_param_runClim_KGFusion<-list(j,s_raster,covar_names,lst_avg,list_models,dst,var,y_var_name, out_prefix,out_path) |
185 | 186 |
names(list_param_runClim_KGFusion)<-c("list_index","covar_rast","covar_names","lst_avg","list_models","dst","var","y_var_name","out_prefix","out_path") |
186 | 187 |
#source(file.path(script_path,"GAM_fusion_function_multisampling_03122013.R")) |
187 | 188 |
clim_method_mod_obj<-mclapply(1:12, list_param=list_param_runClim_KGFusion, runClim_KGFusion,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement |
188 | 189 |
#clim_method_mod_obj<-mclapply(1:6, list_param=list_param_runClim_KGFusion, runClim_KGFusion,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement |
189 |
#test<-runClim_KGFusion(3,list_param=list_param_runClim_KGFusion)
|
|
190 |
#test<-runClim_KGFusion(1,list_param=list_param_runClim_KGFusion)
|
|
190 | 191 |
save(clim_method_mod_obj,file= file.path(out_path,paste(interpolation_method,"_mod_",y_var_name,out_prefix,".RData",sep=""))) |
191 | 192 |
list_tmp<-vector("list",length(clim_method_mod_obj)) |
192 | 193 |
for (i in 1:length(clim_method_mod_obj)){ |
... | ... | |
196 | 197 |
clim_yearlist<-list_tmp |
197 | 198 |
} |
198 | 199 |
|
200 |
|
|
199 | 201 |
if (interpolation_method=="gam_CAI"){ |
200 | 202 |
list_param_runClim_KGCAI<-list(j,s_raster,covar_names,lst_avg,list_models,dst,var,y_var_name, out_prefix,out_path) |
201 | 203 |
names(list_param_runClim_KGCAI)<-c("list_index","covar_rast","covar_names","lst_avg","list_models","dst","var","y_var_name","out_prefix","out_path") |
... | ... | |
228 | 230 |
file=log_fname,sep="\n") |
229 | 231 |
|
230 | 232 |
#TODO : Same call for all functions!!! Replace by one "if" for all multi time scale methods... |
231 |
if (interpolation_method=="gam_CAI" | interpolation_method=="gam_fusion"){
|
|
233 |
if (interpolation_method %in% c("gam_CAI","gam_fusion","kriging_fusion")){
|
|
232 | 234 |
#input a list:note that ghcn.subsets is not sampling_obj$data_day_ghcn |
233 | 235 |
i<-1 |
234 | 236 |
list_param_run_prediction_daily_deviation <-list(i,clim_yearlist,sampling_obj,dst,var,y_var_name, interpolation_method,out_prefix,out_path) |
... | ... | |
238 | 240 |
|
239 | 241 |
method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data_day),list_param=list_param_run_prediction_daily_deviation,run_prediction_daily_deviation,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement |
240 | 242 |
save(method_mod_obj,file= file.path(out_path,paste("method_mod_obj_",interpolation_method,"_",y_var_name,out_prefix,".RData",sep=""))) |
241 |
}
|
|
243 |
} |
|
242 | 244 |
|
243 | 245 |
#TODO : Same call for all functions!!! Replace by one "if" for all daily single time scale methods... |
244 | 246 |
if (interpolation_method=="gam_daily"){ |
... | ... | |
304 | 306 |
names(list_param_validation)<-c("list_index","rast_day_year_list","method_mod_obj","y_var_name","out_prefix", "out_path") #same names for any method |
305 | 307 |
|
306 | 308 |
validation_mod_obj <-mclapply(1:length(method_mod_obj), list_param=list_param_validation, calculate_accuracy_metrics,mc.preschedule=FALSE,mc.cores = 9) |
307 |
#test_val<-calculate_accuracy_metrics(1,list_param_validation) |
|
309 |
#test_val<-calculate_accuracy_metrics(1,list_param_validation)
|
|
308 | 310 |
save(validation_mod_obj,file= file.path(out_path,paste(interpolation_method,"_validation_mod_obj_",y_var_name,out_prefix,".RData",sep=""))) |
309 | 311 |
t2<-proc.time()-t1 |
310 | 312 |
cat(as.character(t2),file=log_fname,sep="\n", append=TRUE) |
... | ... | |
336 | 338 |
################### PREPARE RETURN OBJECT ############### |
337 | 339 |
#Will add more information to be returned |
338 | 340 |
|
339 |
if (interpolation_method=="gam_CAI" | interpolation_method=="gam_fusion"){
|
|
341 |
if (interpolation_method %in% c("gam_CAI","gam_fusion","kriging_fusion","gwr_fusion")){
|
|
340 | 342 |
raster_prediction_obj<-list(clim_method_mod_obj,method_mod_obj,validation_mod_obj,tb_diagnostic_v, |
341 | 343 |
summary_metrics_v,summary_month_metrics_v) |
342 | 344 |
names(raster_prediction_obj)<-c("clim_method_mod_obj","method_mod_obj","validation_mod_obj","tb_diagnostic_v", |
climate/research/oregon/interpolation/GAM_fusion_function_multisampling.R | ||
---|---|---|
248 | 248 |
#### STEP3: NOW FIT AND PREDICT MODEL |
249 | 249 |
|
250 | 250 |
list_formulas<-lapply(list_models,as.formula,env=.GlobalEnv) #mulitple arguments passed to lapply!! |
251 |
|
|
252 |
mod_list<-fit_models(list_formulas,data_month) #only gam at this stage |
|
253 |
cname<-paste("mod",1:length(mod_list),sep="") #change to more meaningful name? |
|
254 |
names(mod_list)<-cname |
|
251 |
cname<-paste("mod",1:length(list_formulas),sep="") #change to more meaningful name? |
|
255 | 252 |
|
256 | 253 |
#Now generate file names for the predictions... |
257 |
list_out_filename<-vector("list",length(mod_list))
|
|
254 |
list_out_filename<-vector("list",length(list_formulas))
|
|
258 | 255 |
names(list_out_filename)<-cname |
259 | 256 |
|
260 | 257 |
for (k in 1:length(list_out_filename)){ |
261 | 258 |
#j indicate which month is predicted, var indicates TMIN or TMAX |
262 | 259 |
data_name<-paste(var,"_bias_LST_month_",j,"_",cname[k],"_",prop_month, |
263 | 260 |
"_",run_samp,sep="") |
264 |
raster_name<-file.path(out_path,paste("fusion_",data_name,out_prefix,".tif", sep="")) |
|
261 |
raster_name<-file.path(out_path,paste("fusion_",interpolation_method,"_",data_name,out_prefix,".tif", sep=""))
|
|
265 | 262 |
list_out_filename[[k]]<-raster_name |
266 | 263 |
} |
267 |
|
|
268 |
#now predict values for raster image...by providing fitted model list, raster brick and list of output file names |
|
269 |
rast_bias_list<-predict_raster_model(mod_list,s_raster,list_out_filename) |
|
270 |
names(rast_bias_list)<-cname |
|
264 |
|
|
265 |
## Select the relevant method... |
|
266 |
|
|
267 |
if (interpolation_method=="gam_fusion"){ |
|
268 |
|
|
269 |
#First fitting |
|
270 |
mod_list<-fit_models(list_formulas,data_month) #only gam at this stage |
|
271 |
names(mod_list)<-cname |
|
272 |
|
|
273 |
#Second predict values for raster image...by providing fitted model list, raster brick and list of output file names |
|
274 |
rast_bias_list<-predict_raster_model(mod_list,s_raster,list_out_filename) |
|
275 |
names(rast_bias_list)<-cname |
|
276 |
|
|
277 |
} |
|
278 |
|
|
279 |
## need to change to use combined gwr autokrige function |
|
280 |
if (interpolation_method=="kriging_fusion"){ |
|
281 |
method_interp <- "kriging" |
|
282 |
month_prediction_obj<-predict_auto_krige_raster_model(list_formulas,s_raster,data_month,list_out_filename) |
|
283 |
#month_prediction_obj<-predict_autokrige_gwr_raster_model(method_interp,list_formulas,s_raster,data_s,list_out_filename) |
|
284 |
|
|
285 |
mod_list <-month_prediction_obj$list_fitted_models |
|
286 |
rast_bias_list <-month_prediction_obj$list_rast_pred |
|
287 |
names(rast_bias_list)<-cname |
|
288 |
} |
|
289 |
|
|
290 |
if (interpolation_method=="gwr_fusion"){ |
|
291 |
method_interp <- "gwr" |
|
292 |
day_prediction_obj<-predict_autokrige_gwr_raster_model(method_interp,list_formulas,s_raster,data_s,list_out_filename) |
|
293 |
mod_list <-day_prediction_obj$list_fitted_models |
|
294 |
rast_day_list <-day_prediction_obj$list_rast_pred |
|
295 |
names(rast_day_list)<-cname |
|
296 |
} |
|
297 |
|
|
271 | 298 |
#Some modles will not be predicted...remove them |
272 | 299 |
rast_bias_list<-rast_bias_list[!sapply(rast_bias_list,is.null)] #remove NULL elements in list |
273 | 300 |
|
... | ... | |
278 | 305 |
clim_fus_rast<-LST-subset(mod_rast,k) |
279 | 306 |
data_name<-paste(var,"_clim_LST_month_",j,"_",names(rast_clim_list)[k],"_",prop_month, |
280 | 307 |
"_",run_samp,sep="") |
281 |
raster_name<-file.path(out_path,paste("fusion_",data_name,out_prefix,".tif", sep="")) |
|
308 |
raster_name<-file.path(out_path,paste("fusion_",interpolation_method,"_",data_name,out_prefix,".tif", sep=""))
|
|
282 | 309 |
rast_clim_list[[k]]<-raster_name |
283 | 310 |
writeRaster(clim_fus_rast, filename=raster_name,overwrite=TRUE) #Wri |
284 | 311 |
} |
Also available in: Unified diff
adding kriging fusion method, several modifications in raster predictions functions