Project

General

Profile

« Previous | Next » 

Revision 57a4fede

Added by Benoit Parmentier almost 12 years ago

GAM fusion function, adding gam CAI function and reorgnization of script and functions

View differences:

climate/research/oregon/interpolation/GAM_fusion_function_multisampling.R
1 1
#Function to be used with GAM_fusion_analysis_raster_prediction_mutlisampling.R
2 2
#runClimFusion<-function(r_stack,data_training,data_testing,data_training){
3 3

  
4
#Functions used in the script
5

  
6
predict_raster_model<-function(in_models,r_stack,out_filename){
7
  #This functions performs predictions on a raster grid given input models.
8
  #Arguments: list of fitted models, raster stack of covariates
9
  #Output: spatial grid data frame of the subset of tiles
10
  list_rast_pred<-vector("list",length(in_models))
11
  for (i in 1:length(in_models)){
12
    mod <-in_models[[i]] #accessing GAM model ojbect "j"
13
    raster_name<-out_filename[[i]]
14
    if (inherits(mod,"gam")) {           #change to c("gam","autoKrige")
15
      raster_pred<- predict(object=s_raster,model=mod,na.rm=FALSE) #Using the coeff to predict new values.
16
      names(raster_pred)<-"y_pred"  
17
      writeRaster(raster_pred, filename=raster_name,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
18
      print(paste("Interpolation:","mod", j ,sep=" "))
19
      list_rast_pred[[i]]<-raster_name
20
    }
21
  }
22
  if (inherits(mod,"try-error")) {
23
    print(paste("no gam model fitted:",mod[1],sep=" ")) #change message for any model type...
24
  }
25
  return(list_rast_pred)
26
}
27

  
28
fit_models<-function(list_formulas,data_training){
29
  #This functions several models and returns model objects.
30
  #Arguments: - list of formulas for GAM models
31
  #           - fitting data in a data.frame or SpatialPointDataFrame
32
  #Output: list of model objects 
33
  list_fitted_models<-vector("list",length(list_formulas))
34
  for (k in 1:length(list_formulas)){
35
    formula<-list_formulas[[k]]
36
    mod<- try(gam(formula, data=data_training)) #change to any model!!
37
    #mod<- try(autoKrige(formula, input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
38
    model_name<-paste("mod",k,sep="")
39
    assign(model_name,mod) 
40
    list_fitted_models[[k]]<-mod
41
  }
42
  return(list_fitted_models) 
43
}
44

  
4 45
####
5 46
#TODO:
6 47
#Add log file and calculate time and sizes for processes-outputs
48
runClimCAI<-function(j){
49
  #Make this a function with multiple argument that can be used by mcmapply??
50
  #This creates clim fusion layers...still needs more code testing
51
  
52
  #Model and response variable can be changed without affecting the script
53
  prop_month<-0 #proportion retained for validation
54
  run_samp<-1
55
  
56
  list_formulas<-lapply(list_models,as.formula,env=.GlobalEnv) #mulitple arguments passed to lapply!!
57
  
58
  data_month<-dst[dst$month==j,] #Subsetting dataset for the relevant month of the date being processed
59
  LST_name<-lst_avg[j] # name of LST month to be matched
60
  data_month$LST<-data_month[[LST_name]]
61
  
62
  #TMax to model...
63
  data_month$y_var<-data_month$TMax #Adding TMax as the variable modeled
64
  mod_list<-fit_models(list_formulas,data_month) #only gam at this stage
65
  cname<-paste("mod",1:length(mod_list),sep="") #change to more meaningful name?
66
  names(mod_list)<-cname
67
  #Adding layer LST to the raster stack  
68
  pos<-match("elev",names(s_raster))
69
  layerNames(s_raster)[pos]<-"elev_1"
70
  
71
  pos<-match("LST",names(s_raster)) #Find the position of the layer with name "LST", if not present pos=NA
72
  s_raster<-dropLayer(s_raster,pos)      # If it exists drop layer
73
  LST<-subset(s_raster,LST_name)
74
  names(LST)<-"LST"
75
  #Screen for extreme values": this needs more thought, min and max val vary with regions
76
  #min_val<-(-15+273.16) #if values less than -15C then screen out (note the Kelvin units that will need to be changed later in all datasets)
77
  #r1[r1 < (min_val)]<-NA
78
  s_raster<-addLayer(s_raster,LST)            #Adding current month
79
  
80
  #Now generate file names for the predictions...
81
  list_out_filename<-vector("list",length(mod_list))
82
  names(list_out_filename)<-cname  
83
  
84
  for (k in 1:length(list_out_filename)){
85
    #j indicate which month is predicted
86
    data_name<-paste("clim_month_",j,"_",cname[k],"_",prop_month,
87
                     "_",run_samp,sep="")
88
    raster_name<-paste("fusion_",data_name,out_prefix,".tif", sep="")
89
    list_out_filename[[k]]<-raster_name
90
  }
91
  
92
  #now predict values for raster image...
93
  rast_clim_list<-predict_raster_model(mod_list,s_raster,list_out_filename)
94
  names(rast_clim_list)<-cname
95
  #Some modles will not be predicted...remove them
96
  rast_clim_list<-rast_clim_list[!sapply(rast_clim_list,is.null)] #remove NULL elements in list
97
  
98
  #Adding Kriging for Climatology options
99
  
100
  clim_xy<-coordinates(data_month)
101
  fitclim<-Krig(clim_xy,data_month$TMax,theta=1e5) #use TPS or krige 
102
  mod_krtmp1<-fitclim
103
  model_name<-"mod_kr"
104
  
105
  clim_rast<-interpolate(LST,fitclim) #interpolation using function from raster package
106
  #Saving kriged surface in raster images
107
  #data_name<-paste("clim_month_",j,"_",model_name,"_",prop_month,
108
  #                 "_",run_samp,sep="")
109
  #raster_name_clim<-paste("fusion_",data_name,out_prefix,".tif", sep="")
110
  #writeRaster(clim_rast, filename=raster_name_clim,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
111
  
112
  #now climatology layer
113
  #clim_rast<-LST-bias_rast
114
  data_name<-paste("clim_month_",j,"_",model_name,"_",prop_month,
115
                   "_",run_samp,sep="")
116
  raster_name_clim<-paste("fusion_",data_name,out_prefix,".tif", sep="")
117
  writeRaster(clim_rast, filename=raster_name_clim,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
118
  
119
  #Adding to current objects
120
  mod_list[[model_name]]<-mod_krtmp1
121
  #rast_bias_list[[model_name]]<-raster_name_bias
122
  rast_clim_list[[model_name]]<-raster_name_clim
123
  
124
  #Prepare object to return
125
  clim_obj<-list(rast_clim_list,data_month,mod_list,list_formulas)
126
  names(clim_obj)<-c("clim","data_month","mod","formulas")
127
  
128
  save(clim_obj,file= paste("clim_obj_month_",j,"_",out_prefix,".RData",sep=""))
129
  
130
  return(clim_obj) 
131
}
132
#
7 133

  
8 134
runClim_KGFusion<-function(j){
9 135
  #Make this a function with multiple argument that can be used by mcmapply??
10 136
  #This creates clim fusion layers...
11 137
  
12
  #Functions used in the script
13
  predict_raster_model<-function(in_models,r_stack,out_filename){
14
    #This functions performs predictions on a raster grid given input models.
15
    #Arguments: list of fitted models, raster stack of covariates
16
    #Output: spatial grid data frame of the subset of tiles
17
    list_rast_pred<-vector("list",length(in_models))
18
    for (i in 1:length(in_models)){
19
      mod <-in_models[[i]] #accessing GAM model ojbect "j"
20
      raster_name<-out_filename[[i]]
21
      if (inherits(mod,"gam")) {           #change to c("gam","autoKrige")
22
        raster_pred<- predict(object=s_raster,model=mod,na.rm=FALSE) #Using the coeff to predict new values.
23
        names(raster_pred)<-"y_pred"  
24
        writeRaster(raster_pred, filename=raster_name,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
25
        print(paste("Interpolation:","mod", j ,sep=" "))
26
        list_rast_pred[[i]]<-raster_name
27
      }
28
    }
29
    if (inherits(mod,"try-error")) {
30
      print(paste("no gam model fitted:",mod[1],sep=" ")) #change message for any model type...
31
    }
32
    return(list_rast_pred)
33
  }
34 138
  
35
  fit_models<-function(list_formulas,data_training){
36
    #This functions several models and returns model objects.
37
    #Arguments: - list of formulas for GAM models
38
    #           - fitting data in a data.frame or SpatialPointDataFrame
39
    #Output: list of model objects 
40
    list_fitted_models<-vector("list",length(list_formulas))
41
    for (k in 1:length(list_formulas)){
42
      formula<-list_formulas[[k]]
43
      mod<- try(gam(formula, data=data_training)) #change to any model!!
44
      #mod<- try(autoKrige(formula, input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
45
      model_name<-paste("mod",k,sep="")
46
      assign(model_name,mod) 
47
      list_fitted_models[[k]]<-mod
48
    }
49
    return(list_fitted_models) 
50
  }
51 139
  #Model and response variable can be changed without affecting the script
52 140
  prop_month<-0 #proportion retained for validation
53 141
  run_samp<-1

Also available in: Unified diff