Project

General

Profile

« Previous | Next » 

Revision 848798ac

Added by Benoit Parmentier about 11 years ago

modification to add monthly hold out for fusion methods

View differences:

climate/research/oregon/interpolation/GAM_fusion_analysis_raster_prediction_multisampling.R
213 213
  if (interpolation_method %in% c("gam_fusion","kriging_fusion","gwr_fusion")){
214 214
    list_param_runClim_KGFusion<-list(j,s_raster,covar_names,lst_avg,list_models,dst,sampling_month_obj,var,y_var_name, out_prefix,out_path)
215 215
    names(list_param_runClim_KGFusion)<-c("list_index","covar_rast","covar_names","lst_avg","list_models","dst","sampling_month_obj","var","y_var_name","out_prefix","out_path")
216
    #source(file.path(script_path,"GAM_fusion_function_multisampling_03122013.R"))
217
    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
218
    #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
216
    #debug(runClim_KGFusion)
219 217
    #test<-runClim_KGFusion(1,list_param=list_param_runClim_KGFusion)
218
    clim_method_mod_obj<-mclapply(1:length(sampling_month_obj$ghcn_data), list_param=list_param_runClim_KGFusion, runClim_KGFusion,mc.preschedule=FALSE,mc.cores = 11) #This is the end bracket from mclapply(...) statement
220 219
    save(clim_method_mod_obj,file= file.path(out_path,paste(interpolation_method,"_mod_",y_var_name,out_prefix,".RData",sep="")))
220
    #Use function to extract list
221 221
    list_tmp<-vector("list",length(clim_method_mod_obj))
222 222
    for (i in 1:length(clim_method_mod_obj)){
223 223
      tmp<-clim_method_mod_obj[[i]]$clim
......
231 231
    names(list_param_runClim_KGCAI)<-c("list_index","covar_rast","covar_names","lst_avg","list_models","dst","sampling_month_obj","var","y_var_name","out_prefix","out_path")
232 232
    clim_method_mod_obj<-mclapply(1:length(sampling_month_obj$ghcn_data), list_param=list_param_runClim_KGCAI, runClim_KGCAI,mc.preschedule=FALSE,mc.cores = 11) #This is the end bracket from mclapply(...) statement
233 233
    #test<-runClim_KGCAI(1,list_param=list_param_runClim_KGCAI)
234
    #gamclim_fus_mod<-mclapply(1:6, list_param=list_param_runClim_KGFusion, runClim_KGFusion,mc.preschedule=FALSE,mc.cores = 6) 
235 234
    save(clim_method_mod_obj,file= file.path(out_path,paste(interpolation_method,"_mod_",y_var_name,out_prefix,".RData",sep="")))
236 235
    list_tmp<-vector("list",length(clim_method_mod_obj))
237 236
    for (i in 1:length(clim_method_mod_obj)){
climate/research/oregon/interpolation/GAM_fusion_function_multisampling.R
8 8
# 5)runGAMFusion <- function(i,list_param) : daily step for fusion method, perform daily prediction
9 9
#
10 10
#AUTHOR: Benoit Parmentier                                                                       
11
#DATE: 08/30/2013                                                                                 
11
#DATE: 09/04/2013                                                                                 
12 12
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--   
13 13

  
14 14
##Comments and TODO:
......
282 282
  out_prefix<-list_param$out_prefix
283 283
  out_path<-list_param$out_path
284 284
  
285
  #inserted #
286
  sampling_month_obj<-list_param$sampling_month_obj
287
  ghcn.month.subsets<-sampling_month_obj$ghcn_data
288
  sampling_month_dat <- sampling_month_obj$sampling_dat
289
  sampling_month_index <- sampling_month_obj$sampling_index
290
  
285 291
  #Model and response variable can be changed without affecting the script
286
  prop_month<-0 #proportion retained for validation
287
  run_samp<-1 #This option can be added later on if/when neeeded
292
  #prop_month<-0 #proportion retained for validation...
293
  #run_samp<-1 #sample number, can be introduced later...
294
  
295
  prop_month <- sampling_month_dat$prop[j] #proportion retained for validation...
296
  run_samp <- sampling_month_dat$run_samp[j] #sample number if multisampling...
297
  #will need create mulitple prediction at daily!!! could be complicated
298
  #possibility is to average per proportion !!!
299
  
300
  date_month <-strptime(sampling_month_dat$date[j], "%Y%m%d")   # interpolation date being processed
301
  month_no <-strftime(date_month, "%m")          # current month of the date being processed
302
  LST_month<-paste("mm_",month_no,sep="") # name of LST month to be matched
303
  LST_name <-LST_month  
304
  #### STEP 2: PREPARE DATA
305
  
306
  #change here...use training data...
307
  ###Regression part 1: Creating a validation dataset by creating training and testing datasets
308
  
309
  #LST_name <-lst_avg[j] # name of LST month to be matched
310
  #data_month$LST<-data_month[[LST_name]]
311
  
312
  dataset_month <-ghcn.month.subsets[[j]]
313
  mod_LST <- ghcn.month.subsets[[j]][,match(LST_month, names(ghcn.month.subsets[[j]]))]  #Match interpolation date and monthly LST average
314
  dataset_month$LST <- as.data.frame(mod_LST)[,1] #Add the variable LST to the dataset
315
  #change here...
316
  dst$LST<-dst[[LST_month]] #Add the variable LST to the monthly dataset
317
  proj_str<-proj4string(dst) #get the local projection information from monthly data
318
    
319
  ind.training <- sampling_month_index[[j]]
320
  ind.testing  <- setdiff(1:nrow(dataset_month), ind.training)
321
  data_month_s <- dataset_month[ind.training, ]   #Training dataset currently used in the modeling
322
  data_month_v <- dataset_month[ind.testing, ]    #Testing/validation dataset using input sampling
323
  
324
  data_month <- data_month_s #training data for  monthhly predictions...
325
  
326
  #date_proc<-strptime(sampling_dat$date[i], "%Y%m%d")   # interpolation date being processed
327
  #mo<-as.integer(strftime(date_proc, "%m"))          # current month of the date being processed
328
  #day<-as.integer(strftime(date_proc, "%d"))
329
  #year<-as.integer(strftime(date_proc, "%Y"))
330
  ## end of pasted
331
  
332
  #end of insert...09/04
288 333
  
289 334
  #### STEP 2: PREPARE DATA
290 335
  
291
  data_month<-dst[dst$month==j,] #Subsetting dataset for the relevant month of the date being processed
292
  LST_name<-lst_avg[j] # name of LST month to be matched
293
  data_month$LST<-data_month[[LST_name]]
336
  #data_month<-dst[dst$month==j,] #Subsetting dataset for the relevant month of the date being processed
337
  #LST_name<-lst_avg[j] # name of LST month to be matched
338
  #data_month$LST<-data_month[[LST_name]]
294 339
  
295 340
  #Adding layer LST to the raster stack  
296 341
  covar_rast<-s_raster
......
311 356
    data_month$y_var<-data_month$LSTD_bias #Adding bias as the variable modeled
312 357
  }
313 358
  
359
  #If CAI model then...
360
  #TMax to model..., add precip later
361
  #if (var=="TMAX"){   
362
  #  dataset_month$y_var<-dataset_month$TMax #Adding TMax as the variable modeled
363
  #}
364
  #if (var=="TMIN"){   
365
  #  dataset_month$y_var<-dataset_month$TMin #Adding TMin as the variable modeled
366
  #}
367
  
314 368
  #### STEP3:  NOW FIT AND PREDICT  MODEL
315 369
  
316 370
  list_formulas<-lapply(list_models,as.formula,env=.GlobalEnv) #mulitple arguments passed to lapply!!
......
320 374
  list_out_filename<-vector("list",length(list_formulas))
321 375
  names(list_out_filename)<-cname  
322 376
  
377
  ##Change name...
323 378
  for (k in 1:length(list_out_filename)){
324 379
    #j indicate which month is predicted, var indicates TMIN or TMAX
325
    data_name<-paste(var,"_bias_LST_month_",j,"_",cname[k],"_",prop_month,
380
    data_name<-paste(var,"_bias_LST_month_",as.integer(month_no),"_",cname[k],"_",prop_month,
326 381
                     "_",run_samp,sep="")
327 382
    raster_name<-file.path(out_path,paste("fusion_",interpolation_method,"_",data_name,out_prefix,".tif", sep=""))
328 383
    list_out_filename[[k]]<-raster_name
329 384
  }
330 385
  
386
  #for (k in 1:length(list_out_filename)){
387
  #  #j indicate which month is predicted
388
  #  data_name<-paste(var,"_clim_month_",as.integer(month_no),"_",cname[k],"_",prop_month,
389
  #                   "_",run_samp,sep="")
390
  #  raster_name<-file.path(out_path,paste("CAI_",data_name,out_prefix,".tif", sep=""))
391
  #  list_out_filename[[k]]<-raster_name
392
  #}
393
  
331 394
  ## Select the relevant method...
332 395
  
333 396
  if (interpolation_method=="gam_fusion"){
......
367 430
  names(rast_clim_list)<-names(rast_bias_list)
368 431
  for (k in 1:nlayers(mod_rast)){
369 432
    clim_fus_rast<-LST-subset(mod_rast,k)
370
    data_name<-paste(var,"_clim_LST_month_",j,"_",names(rast_clim_list)[k],"_",prop_month,
433
    data_name<-paste(var,"_clim_LST_month_",as.integer(month_no),"_",names(rast_clim_list)[k],"_",prop_month,
371 434
                     "_",run_samp,sep="")
372 435
    raster_name<-file.path(out_path,paste("fusion_",interpolation_method,"_",data_name,out_prefix,".tif", sep=""))
373 436
    rast_clim_list[[k]]<-raster_name
......
385 448
  if (inherits(fitbias,"Krig")){
386 449
    #Saving kriged surface in raster images
387 450
    bias_rast<-bias_rast<-interpolate(LST,fitbias) #interpolation using function from raster package
388
    data_name<-paste(var,"_bias_LST_month_",j,"_",model_name,"_",prop_month,
451
    data_name<-paste(var,"_bias_LST_month_",as.integer(month_no),"_",model_name,"_",prop_month,
389 452
                     "_",run_samp,sep="")
390
    raster_name_bias<-file.path(out_path,paste("fusion_",data_name,out_prefix,".tif", sep=""))
453
    raster_name_bias<-file.path(out_path,paste("fusion_",interpolation_method,"_",data_name,out_prefix,".tif", sep=""))
391 454
    writeRaster(bias_rast, filename=raster_name_bias,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
392 455
    
393 456
    #now climatology layer
394 457
    clim_rast<-LST-bias_rast
395
    data_name<-paste(var,"_clim_LST_month_",j,"_",model_name,"_",prop_month,
458
    data_name<-paste(var,"_clim_LST_month_",as.integer(month_no),"_",model_name,"_",prop_month,
396 459
                     "_",run_samp,sep="")
397
    raster_name_clim<-file.path(out_path,paste("fusion_",data_name,out_prefix,".tif", sep=""))
460
    raster_name_clim<-file.path(out_path,paste("fusion_",interpolation_method,"_",data_name,out_prefix,".tif", sep=""))
398 461
    writeRaster(clim_rast, filename=raster_name_clim,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
399 462
    #Adding to current objects
400 463
    mod_list[[model_name]]<-fitbias
......
413 476

  
414 477
  #### STEP 5: Prepare object and return
415 478
  
416
  clim_obj<-list(rast_bias_list,rast_clim_list,data_month,mod_list,list_formulas)
417
  names(clim_obj)<-c("bias","clim","data_month","mod","formulas")
418
  
419
  save(clim_obj,file= file.path(out_path,paste("clim_obj_month_",j,"_",var,"_",out_prefix,".RData",sep="")))
479
  #Prepare object to return
480
  clim_obj<-list(rast_bias_list,rast_clim_list,data_month,data_month_v,sampling_month_dat[j,],mod_list,list_formulas)
481
  names(clim_obj)<-c("bias","clim","data_month","data_month_v","sampling_month_dat","mod","formulas")
420 482
  
483
  save(clim_obj,file= file.path(out_path,paste("clim_obj_fusion_month_",as.integer(month_no),"_",var,"_",prop_month,
484
                                               "_",run_samp,"_",out_prefix,".RData",sep="")))  
421 485
  return(clim_obj)
422 486
}
423 487

  

Also available in: Unified diff