Project

General

Profile

« Previous | Next » 

Revision 06ee668c

Added by Benoit Parmentier about 11 years ago

modifications for monthly hold out in raster prediction script

View differences:

climate/research/oregon/interpolation/GAM_fusion_analysis_raster_prediction_multisampling.R
12 12
   # gam_daily, kriging_daily,gwr_daily,gam_CAI,gam_fusion,kriging_fusion,gwr_fusion and other options added.
13 13
#For multiple time scale methods, the interpolation is done first at the monthly time scale then delta surfaces are added.
14 14
#AUTHOR: Benoit Parmentier                                                                        
15
#DATE: 07/30/2013                                                                                 
15
#DATE: 08/26/2013                                                                                 
16 16
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#568--     
17 17
#
18 18
# TO DO:
19
#Add methods to for CAI
20 19

  
21 20
###################################################################################################
22 21

  
......
41 40
  #12)step
42 41
  #13)constant
43 42
  #14)prop_minmax
44
  #15)dates_selected
43
  #15)seed_number_month
44
  #16)nb_sample_month
45
  #17)step_month
46
  #18)constant_month
47
  #19)prop_minmax_month
48
  #20)dates_selected
45 49
  #
46 50
  #6 additional parameters for monthly climatology and more
47
  #16)list_models: model formulas in character vector
48
  #17)lst_avg: LST climatology name in the brick of covariate--change later
49
  #18)n_path
50
  #19)out_path
51
  #20)script_path: path to script
52
  #21)interpolation_method: c("gam_fusion","gam_CAI") #other otpions to be added later
51
  #21)list_models: model formulas in character vector
52
  #22)lst_avg: LST climatology name in the brick of covariate--change later
53
  #23)n_path
54
  #24)out_path
55
  #25)script_path: path to script
56
  #26)interpolation_method: c("gam_fusion","gam_CAI") #other otpions to be added later
53 57
  
54 58
  ###Loading R library and packages     
55 59
  
......
98 102
  prop_minmax<-list_param_raster_prediction$prop_minmax
99 103
  dates_selected<-list_param_raster_prediction$dates_selected
100 104
  
105
  seed_number_month <-list_param_raster_prediction$seed_number_month
106
  nb_sample_month <-list_param_raster_prediction$nb_sample_month
107
  step_month <-list_param_raster_prediction$step_month
108
  constant_month <-list_param_raster_prediction$constant_month
109
  prop_minmax_month <-list_param_raster_prediction$prop_minmax_month
110
  
101 111
  #6 additional parameters for monthly climatology and more
102 112
  list_models<-list_param_raster_prediction$list_models
103 113
  lst_avg<-list_param_raster_prediction$lst_avg
......
134 144
  cat("Starting script process time:",file=log_fname,sep="\n",append=TRUE)
135 145
  cat(as.character(time1),file=log_fname,sep="\n",append=TRUE)    
136 146
  
137
  ############### READING INPUTS: DAILY STATION DATA AND OTEHR DATASETS  #################
147
  ############### READING INPUTS: DAILY STATION DATA AND OTHER DATASETS  #################
138 148
  
139 149
  ghcn<-readOGR(dsn=dirname(infile_daily),layer=sub(".shp","",basename(infile_daily)))
140 150
  CRS_interp<-proj4string(ghcn)                       #Storing projection information (ellipsoid, datum,etc.)
......
155 165
  #Reading monthly data
156 166
  dst<-readOGR(dsn=dirname(infile_monthly),layer=sub(".shp","",basename(infile_monthly)))
157 167
    
158
  ########### CREATE SAMPLING -TRAINING AND TESTING STATIONS ###########
168
  #construct date based on input end_year !!!
169
  day_tmp <- rep("15",length=nrow(dst))
170
  year_tmp <- rep(as.character(end_year),length=nrow(dst))
171
  #dates_month <-do.call(paste,c(list(day_tmp,sprintf( "%02d", dst$month ),year_tmp),sep="")) #reformat integer using formatC or sprintf
172
  dates_month <-do.call(paste,c(list(year_tmp,sprintf( "%02d", dst$month ),day_tmp),sep="")) #reformat integer using formatC or sprintf
159 173
  
160
  #Input for sampling function...
174
  dst$date <- dates_month
161 175
  
176
  ########### CREATE SAMPLING -TRAINING AND TESTING STATIONS ###########
177

  
162 178
  #dates #list of dates for prediction
163 179
  #ghcn_name<-"ghcn" #infile daily data 
164 180
  
......
166 182
  #list_param_sampling<-list(seed_number,nb_sample,step,constant,prop_minmax,dates,ghcn_name)
167 183
  names(list_param_sampling)<-c("seed_number","nb_sample","step","constant","prop_minmax","dates","ghcn")
168 184
  
169
  #run function, note that dates must be a character vector!!
185
  #run function, note that dates must be a character vector!! Daily sampling
170 186
  sampling_obj<-sampling_training_testing(list_param_sampling)
187

  
188
  #Now run monthly sampling
189
  dates_month<-as.character(sort(unique(dst$date)))
190
  list_param_sampling<-list(seed_number_month,nb_sample_month,step_month,constant_month,prop_minmax_month,dates_month,dst)
191
  #list_param_sampling<-list(seed_number,nb_sample,step,constant,prop_minmax,dates,ghcn_name)
192

  
193
  names(list_param_sampling)<-c("seed_number","nb_sample","step","constant","prop_minmax","dates","ghcn")
194
  
195
  sampling_month_obj <- sampling_training_testing(list_param_sampling)
171 196
  
172 197
  ########### PREDICT FOR MONTHLY SCALE  ##################
173 198
  
......
180 205
  #browser() #Missing out_path for gam_fusion list param!!!
181 206
  #if (interpolation_method=="gam_fusion"){
182 207
  if (interpolation_method %in% c("gam_fusion","kriging_fusion","gwr_fusion")){
183
    list_param_runClim_KGFusion<-list(j,s_raster,covar_names,lst_avg,list_models,dst,var,y_var_name, out_prefix,out_path)
184
    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")
208
    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)
209
    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")
185 210
    #source(file.path(script_path,"GAM_fusion_function_multisampling_03122013.R"))
186 211
    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
187 212
    #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
......
196 221
  }
197 222
  
198 223
  if (interpolation_method %in% c("gam_CAI","kriging_CAI", "gwr_CAI")){
199
    list_param_runClim_KGCAI<-list(j,s_raster,covar_names,lst_avg,list_models,dst,var,y_var_name, out_prefix,out_path)
200
    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")
201
    clim_method_mod_obj<-mclapply(1:12, list_param=list_param_runClim_KGCAI, runClim_KGCAI,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement
224
    list_param_runClim_KGCAI<-list(j,s_raster,covar_names,lst_avg,list_models,dst,sampling_month_obj,var,y_var_name, out_prefix,out_path)
225
    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")
226
    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
202 227
    #test<-runClim_KGCAI(1,list_param=list_param_runClim_KGCAI)
203 228
    #gamclim_fus_mod<-mclapply(1:6, list_param=list_param_runClim_KGFusion, runClim_KGFusion,mc.preschedule=FALSE,mc.cores = 6) 
204 229
    save(clim_method_mod_obj,file= file.path(out_path,paste(interpolation_method,"_mod_",y_var_name,out_prefix,".RData",sep="")))
......
228 253
  
229 254
  #TODO : Same call for all functions!!! Replace by one "if" for all multi time scale methods...
230 255
  #The methods could be defined earlier as constant??
256
  #Create data.frame combining sampling at daily and monthly time scales:
257
  
258
  daily_dev_sampling_dat <- combine_sampling_daily_monthly_for_daily_deviation_pred(sampling_obj,sampling_month_obj)
259
    
260
  use_clim_image<- TRUE
261
  #use_clim_image<-FALSE
262
  join_daily <- FALSE
263
  
231 264
  if (interpolation_method %in% c("gam_CAI","kriging_CAI","gwr_CAI","gam_fusion","kriging_fusion","gwr_fusion")){
232 265
    #input a list:note that ghcn.subsets is not sampling_obj$data_day_ghcn
233 266
    i<-1
234
    list_param_run_prediction_daily_deviation <-list(i,clim_yearlist,sampling_obj,dst,var,y_var_name, interpolation_method,out_prefix,out_path)
235
    names(list_param_run_prediction_daily_deviation)<-c("list_index","clim_yearlist","sampling_obj","dst","var","y_var_name","interpolation_method","out_prefix","out_path")
267
    list_param_run_prediction_daily_deviation <-list(i,clim_yearlist,daily_dev_sampling_dat,sampling_month_obj,sampling_obj,dst,
268
                                                     use_clim_image,join_daily,var,y_var_name, interpolation_method,out_prefix,out_path)
269
    names(list_param_run_prediction_daily_deviation)<-c("list_index","clim_yearlist","daily_dev_sampling_dat","sampling_month_obj","sampling_obj","dst",
270
                                                        "use_clim_image","join_daily","var","y_var_name","interpolation_method","out_prefix","out_path")
236 271
    #test<-mclapply(1:18, runGAMFusion,list_param=list_param_runGAMFusion,mc.preschedule=FALSE,mc.cores = 9)
237 272
    #test<-runGAMFusion(1,list_param=list_param_runGAMFusion)
273
    #method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data),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
274
    #test <- run_prediction_daily_deviation(1,list_param=list_param_run_prediction_daily_deviation) #This is the end bracket from mclapply(...) statement
275
    #method_mod_obj<-mclapply(1:nrow(daily_dev_sampling_dat),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
238 276
    
239
    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
277
    method_mod_obj<-mclapply(1:nrow(daily_dev_sampling_dat),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 278
    save(method_mod_obj,file= file.path(out_path,paste("method_mod_obj_",interpolation_method,"_",y_var_name,out_prefix,".RData",sep="")))
241 279
  }
242 280
  
......
248 286
    names(list_param_run_prediction_gam_daily)<-c("list_index","covar_rast","covar_names","lst_avg","list_models","dst","screen_data_training","var","y_var_name","sampling_obj","interpolation_method","out_prefix","out_path")
249 287
    #test <- runGAM_day_fun(1,list_param_run_prediction_gam_daily)
250 288
    
251
    method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data_day),list_param=list_param_run_prediction_gam_daily,runGAM_day_fun,mc.preschedule=FALSE,mc.cores = 11) #This is the end bracket from mclapply(...) statement
289
    method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data),list_param=list_param_run_prediction_gam_daily,runGAM_day_fun,mc.preschedule=FALSE,mc.cores = 11) #This is the end bracket from mclapply(...) statement
252 290
    #method_mod_obj<-mclapply(1:11,list_param=list_param_run_prediction_gam_daily,runGAM_day_fun,mc.preschedule=FALSE,mc.cores = 11) #This is the end bracket from mclapply(...) statement
253 291
    
254 292
    save(method_mod_obj,file= file.path(out_path,paste("method_mod_obj_",interpolation_method,"_",y_var_name,out_prefix,".RData",sep="")))
......
261 299
    list_param_run_prediction_kriging_daily <-list(i,s_raster,covar_names,lst_avg,list_models,dst,var,y_var_name, sampling_obj,interpolation_method,out_prefix,out_path)
262 300
    names(list_param_run_prediction_kriging_daily)<-c("list_index","covar_rast","covar_names","lst_avg","list_models","dst","var","y_var_name","sampling_obj","interpolation_method","out_prefix","out_path")
263 301
    #test <- runKriging_day_fun(1,list_param_run_prediction_kriging_daily)
264
    method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data_day),list_param=list_param_run_prediction_kriging_daily,runKriging_day_fun,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
302
    method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data),list_param=list_param_run_prediction_kriging_daily,runKriging_day_fun,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
265 303
    #method_mod_obj<-mclapply(1:18,list_param=list_param_run_prediction_kriging_daily,runKriging_day_fun,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
266 304
    
267 305
    save(method_mod_obj,file= file.path(out_path,paste("method_mod_obj_",interpolation_method,"_",y_var_name,out_prefix,".RData",sep="")))
......
274 312
    list_param_run_prediction_gwr_daily <-list(i,s_raster,covar_names,lst_avg,list_models,dst,var,y_var_name, sampling_obj,interpolation_method,out_prefix,out_path)
275 313
    names(list_param_run_prediction_gwr_daily)<-c("list_index","covar_rast","covar_names","lst_avg","list_models","dst","var","y_var_name","sampling_obj","interpolation_method","out_prefix","out_path")
276 314
    #test <- run_interp_day_fun(1,list_param_run_prediction_gwr_daily)
277
    method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data_day),list_param=list_param_run_prediction_gwr_daily,run_interp_day_fun,mc.preschedule=FALSE,mc.cores = 11) #This is the end bracket from mclapply(...) statement
315
    method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data),list_param=list_param_run_prediction_gwr_daily,run_interp_day_fun,mc.preschedule=FALSE,mc.cores = 11) #This is the end bracket from mclapply(...) statement
278 316
    #method_mod_obj<-mclapply(1:9,list_param=list_param_run_prediction_gwr_daily,run_interp_day_fun,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
279 317
    #method_mod_obj<-mclapply(1:18,list_param=list_param_run_prediction_kriging_daily,runKriging_day_fun,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
280 318
    

Also available in: Unified diff