Project

General

Profile

« Previous | Next » 

Revision 69864891

Added by Benoit Parmentier about 12 years ago

Multisampling Kriging function interpolation initial commit raster prediction task #491

View differences:

climate/research/oregon/interpolation/KrigingUK_function_multisampling.R
1
runKriging <- function(i) {            # loop over dates
2
  
3
  #date<-strptime(dates[i], "%Y%m%d")   # interpolation date being processed
4
  date<-strptime(sampling_dat$date[i], "%Y%m%d")   # interpolation date being processed
5
  month<-strftime(date, "%m")          # current month of the date being processed
6
  LST_month<-paste("mm_",month,sep="") # name of LST month to be matched
7
  
8
  #i=1
9
  date_proc<-sampling_dat$date[i]
10
  date_proc<-strptime(sampling_dat$date[i], "%Y%m%d")   # interpolation date being processed
11
  mo<-as.integer(strftime(date_proc, "%m"))          # current month of the date being processed
12
  day<-as.integer(strftime(date_proc, "%d"))
13
  year<-as.integer(strftime(date_proc, "%Y"))
14
  
15
  
16
  #Adding layer LST to the raster stack
17
  
18
  pos<-match(LST_month,layerNames(s_raster)) #Find column with the current month for instance mm12
19
  r1<-raster(s_raster,layer=pos)             #Select layer from stack
20
  layerNames(r1)<-"LST"
21
  s_raster<-addLayer(s_raster,r1)            #Adding current month
22
  s_sgdf<-as(s_raster,"SpatialGridDataFrame") #Conversion to spatial grid data frame
23
  
24
  ###Regression part 1: Creating a validation dataset by creating training and testing datasets
25
  
26
  mod_LST <-ghcn.subsets[[i]][,match(LST_month, names(ghcn.subsets[[i]]))]  #Match interpolation date and monthly LST average
27
  ghcn.subsets[[i]] = transform(ghcn.subsets[[i]],LST = mod_LST)            #Add the variable LST to the subset dataset
28
  #n<-nrow(ghcn.subsets[[i]])
29
  #ns<-n-round(n*prop)   #Create a sample from the data frame with 70% of the rows
30
  #nv<-n-ns              #create a sample for validation with prop of the rows
31
  #ind.training <- sample(nrow(ghcn.subsets[[i]]), size=ns, replace=FALSE) #This selects the index position for 70% of the rows taken randomly
32
  ind.training<-sampling[[i]]
33
  ind.testing <- setdiff(1:nrow(ghcn.subsets[[i]]), ind.training)
34
  data_s <- ghcn.subsets[[i]][ind.training, ]   #Training dataset currently used in the modeling
35
  data_v <- ghcn.subsets[[i]][ind.testing, ]    #Testing/validation dataset using input sampling
36
  
37
  ns<-nrow(data_s)
38
  nv<-nrow(data_v)
39
  
40
  ###BEFORE model prediction the data object must be transformed to SDF
41
  
42
  coords<- data_v[,c('x_OR83M','y_OR83M')]
43
  coordinates(data_v)<-coords
44
  proj4string(data_v)<-CRS  #Need to assign coordinates...
45
  coords<- data_s[,c('x_OR83M','y_OR83M')]
46
  coordinates(data_s)<-coords
47
  proj4string(data_s)<-CRS  #Need to assign coordinates..
48
  
49
  ns<-nrow(data_s) #This is added to because some loss of data might have happened because of the averaging...
50
  nv<-nrow(data_v)
51
  
52
  ### PREDICTION/ Interpolation
53
  
54
  pos<-match("value",names(data_s)) #Find column with name "value"
55
  names(data_s)[pos]<-y_var_name
56
  pos<-match("value",names(data_v)) #Find column with name "value"
57
  names(data_v)[pos]<-y_var_name
58
  
59
  #if y_var_name=="dailyTmax"
60
  data_v$y_var<-data_v[[y_var_name]]/10  #Note that values are divided by 10 because the var is temp
61
  data_s$y_var<-data_s[[y_var_name]]/10
62
  
63
  #Model and response variable can be changed without affecting the script
64
  
65
  formula1 <- as.formula("y_var ~1", env=.GlobalEnv)
66
  formula2 <- as.formula("y_var~ x_OR83M+y_OR83M", env=.GlobalEnv)
67
  formula3 <- as.formula("y_var~ x_OR83M+y_OR83M+ELEV_SRTM", env=.GlobalEnv)
68
  formula4 <- as.formula("y_var~ x_OR83M+y_OR83M+DISTOC", env=.GlobalEnv)
69
  formula5 <- as.formula("y_var~ x_OR83M+y_OR83M+ELEV_SRTM+DISTOC", env=.GlobalEnv)
70
  formula6 <- as.formula("y_var~ x_OR83M+y_OR83M+Northness+Eastness", env=.GlobalEnv)
71
  formula7 <- as.formula("y_var~ LST", env=.GlobalEnv)
72
  formula8 <- as.formula("y_var~ x_OR83M+y_OR83M+LST", env=.GlobalEnv)
73
  formula9 <- as.formula("y_var~ x_OR83M+y_OR83M+ELEV_SRTM+LST", env=.GlobalEnv)
74
                        
75
  mod1<- try(autoKrige(formula1, input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
76
  mod2<- try(autoKrige(formula2, input_data=data_s,new_data=s_sgdf,data_variogram=data_s)) 
77
  mod3<- try(autoKrige(formula3, input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
78
  mod4<- try(autoKrige(formula4, input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
79
  mod5<- try(autoKrige(formula5, input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
80
  mod6<- try(autoKrige(formula6, input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
81
  mod7<- try(autoKrige(formula7, input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
82
  mod8<- try(autoKrige(formula8, input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
83
  mod9<- try(autoKrige(formula9, input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
84

  
85
  #tmax_predicted=themolst+daily_delta_rast-bias_rast #Final surface?? but daily_rst
86
  
87
  ### Model assessment
88
  
89
  for (j in 1:nmodels){
90
    
91
    ##Model assessment: specific diagnostic/metrics for GAM
92
    
93
    name<-paste("mod",j,sep="")  #modj is the name of The "j" model (mod1 if j=1) 
94
    mod<-get(name)               #accessing GAM model ojbect "j"
95
    #krmod_auto<-get(mod)
96

  
97
    #If mod "j" is not a model object
98
    if (inherits(mod,"try-error")) {
99
      
100
      #results_m1[1,1]<- dates[i]  #storing the interpolation dates in the first column
101
      results_m1[1,1]<- sampling_dat$date[i]
102
      results_m1[1,2]<- ns        #number of stations used in the training stage
103
      results_m1[1,3]<- "SSERR"
104
      results_m1[1,j+3]<- NA
105
      
106
      results_m2[1,1]<- results_m1[1,1]<-  #storing the interpolation dates in the first column
107
      results_m2[1,2]<- ns        #number of stations used in the training 
108
      results_m2[1,3]<- "GCV"
109
      results_m2[1,j+3]<- NA
110
      
111
      results_m3[1,1]<- sampling_dat$date[i] #storing the interpolation dates in the first column
112
      results_m3[1,2]<- ns        #number of stations used in the training stage
113
      results_m3[1,3]<- "DEV"
114
      results_m3[1,j+3]<- NA
115
      
116
      results_RMSE_f[1,1]<- sampling_dat$date[i]  #storing the interpolation dates in the first column
117
      results_RMSE_f[1,2]<- ns        #number of stations used in the training stage
118
      results_RMSE_f[1,3]<- "RSME_f"
119
      results_RMSE_f[1,j+3]<- NA
120
      
121
      results_MAE_f[1,1]<- sampling_dat$date[i]  #storing the interpolation dates in the first column
122
      results_MAE_f[1,2]<- ns        #number of stations used in the training stage
123
      results_MAE_f[1,3]<- "MAE_f"
124
      results_MAE_f[1,j+3]<-NA
125
      
126
      results_RMSE[1,1]<- sampling_dat$date[i]    #storing the interpolation dates in the first column
127
      results_RMSE[1,2]<- ns          #number of stations used in the training stage
128
      results_RMSE[1,3]<- "RMSE"
129
      results_RMSE[1,j+3]<- NA  #Storing RMSE for the model j
130
      results_MAE[1,1]<- sampling_dat$date[i]     #storing the interpolation dates in the first column
131
      results_MAE[1,2]<- ns           #number of stations used in the training stage
132
      results_MAE[1,3]<- "MAE"
133
      results_MAE[1,j+3]<- NA    #Storing MAE for the model j
134
      results_ME[1,1]<- sampling_dat$date[i]      #storing the interpolation dates in the first column
135
      results_ME[1,2]<- ns            #number of stations used in the training stage
136
      results_ME[1,3]<- "ME"
137
      results_ME[1,j+3]<- NA      #Storing ME for the model j
138
      results_R2[1,1]<- sampling_dat$date[i]      #storing the interpolation dates in the first column
139
      results_R2[1,2]<- ns            #number of stations used in the training stage
140
      results_R2[1,3]<- "R2"
141
      results_R2[1,j+3]<- NA      #Storing R2 for the model j
142
      
143
    }
144
    
145
    #If mod is a modelobject
146
    
147
    #If mod "j" is not a model object
148
    if (inherits(mod,"autoKrige")) {
149
      
150
      rpred<-mod$krige_output  #Extracting the SptialGriDataFrame from the autokrige object
151

  
152
      #rpred<- predict(mod, newdata=s_sgdf, se.fit = TRUE) #Using the coeff to predict new values.
153
      y_pred<-rpred$var1.pred                  #is the order the same?
154
      #y_prederr<-rpred$var1.var
155
      raster_pred<-r1
156
      layerNames(raster_pred)<-"y_pred"
157
      clearValues(raster_pred)        #Clear values in memory, just in case...
158
      values(raster_pred)<-as.numeric(y_pred)  #Assign values to every pixels 
159
      
160
      #data_name<-paste("predicted_mod",j,"_",dates[[i]],sep="")
161
      data_name<-paste("predicted_mod",j,"_",sampling_dat$date[i],"_",sampling_dat$prop[i],
162
                       "_",sampling_dat$run_samp[i],sep="")
163
      raster_name<-paste("Kriging_",data_name,out_prefix,".rst", sep="")
164
      writeRaster(raster_pred, filename=raster_name,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
165
      #writeRaster(r2, filename=raster_name,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
166
        
167
      #Save png plot here...
168
      #data_name<-paste("predicted_mod",j,"_",dates[[i]],sep="")
169
      data_name<-paste("predicted_mod",j,"_",sampling_dat$date[i],"_",sampling_dat$prop[i],
170
                       "_",sampling_dat$run_samp[i],sep="")
171
      png_name<-paste("Kriging_plot_",data_name,out_prefix,".png", sep="")
172
      png(png_name) #Create file to write a plot
173
      #png(paste("Delta_surface_LST_TMax_",sampling_dat$date[i],"_",sampling_dat$prop[i],
174
             #   "_",sampling_dat$run_samp[i],out_prefix,".png", sep=""))
175
      #datelabel2=format(ISOdate(year,mo,day),"%B ") #Plot label
176
      plot(mod) #Plot to file the autokrige object
177
      #savePlot(paste("Bias_surface_LST_TMax_",dates[i],out_prefix,".png", sep=""), type="png")
178
      dev.off()  #Release the hold to the file
179
      
180
      pred_sgdf<-as(raster_pred,"SpatialGridDataFrame" ) #Conversion to spatial grid data frame
181
      #rpred_val_s <- overlay(raster_pred,data_s)             #This overlays the kriged surface tmax and the location of weather stations
182
        
183
      rpred_val_s <- overlay(pred_sgdf,data_s)             #This overlays the kriged surface tmax and the location of weather stations
184
      rpred_val_v <- overlay(pred_sgdf,data_v)             #This overlays the kriged surface tmax and the location of weather stations
185
        
186
      pred_mod<-paste("pred_mod",j,sep="")
187
      #Adding the results back into the original dataframes.
188
      data_s[[pred_mod]]<-rpred_val_s$y_pred
189
      data_v[[pred_mod]]<-rpred_val_v$y_pred  
190
        
191
      #Model assessment: RMSE and then krig the residuals....!
192
        
193
      res_mod_s<- data_s$y_var - data_s[[pred_mod]]           #Residuals from kriging training
194
      res_mod_v<- data_v$y_var - data_v[[pred_mod]]           #Residuals from kriging validation
195

  
196
      ####ADDED ON JULY 20th
197
      res_mod<-res_mod_v
198
      
199
      #RMSE_mod <- sqrt(sum(res_mod^2)/nv)                 #RMSE FOR REGRESSION STEP 1: GAM  
200
      RMSE_mod<- sqrt(mean(res_mod^2,na.rm=TRUE))
201
      #MAE_mod<- sum(abs(res_mod),na.rm=TRUE)/(nv-sum(is.na(res_mod)))        #MAE from kriged surface validation
202
      MAE_mod<- mean(abs(res_mod), na.rm=TRUE)
203
      #ME_mod<- sum(res_mod,na.rm=TRUE)/(nv-sum(is.na(res_mod)))                    #ME, Mean Error or bias FOR REGRESSION STEP 1: GAM
204
      ME_mod<- mean(res_mod,na.rm=TRUE)                            #ME, Mean Error or bias FOR REGRESSION STEP 1: GAM
205
      #R2_mod<- cor(data_v$y_var,data_v[[pred_mod]])^2              #R2, coef. of var FOR REGRESSION STEP 1: GAM
206
      R2_mod<- cor(data_v$y_var,data_v[[pred_mod]], use="complete")^2
207
      
208
      R2_mod_f<- cor(data_s$y_var,data_s[[pred_mod]], use="complete")^2
209
      RMSE_mod_f<- sqrt(mean(res_mod_s^2,na.rm=TRUE))
210
      #MAE_mod<- sum(abs(res_mod),na.rm=TRUE)/(nv-sum(is.na(res_mod)))        #MAE from kriged surface validation
211
      MAE_mod_f<- mean(abs(res_mod_s), na.rm=TRUE)
212
      
213
      results_m1[1,1]<- sampling_dat$date[i]  #storing the interpolation dates in the first column
214
      results_m1[1,2]<- ns        #number of stations used in the training stage
215
      results_m1[1,3]<- "SSERR"
216
      results_m1[1,j+3]<- mod$sserr
217
      
218
      results_m2[1,1]<- sampling_dat$date[i]  #storing the interpolation dates in the first column
219
      results_m2[1,2]<- ns        #number of stations used in the training 
220
      results_m2[1,3]<- "GCV"
221
      results_m2[1,j+3]<- NA
222
      
223
      results_m3[1,1]<- sampling_dat$date[i]  #storing the interpolation dates in the first column
224
      results_m3[1,2]<- ns        #number of stations used in the training stage
225
      results_m3[1,3]<- "DEV"
226
      results_m3[1,j+3]<- NA
227
      
228
      results_RMSE_f[1,1]<- sampling_dat$date[i]  #storing the interpolation dates in the first column
229
      results_RMSE_f[1,2]<- ns        #number of stations used in the training stage
230
      results_RMSE_f[1,3]<- "RSME_f"
231
      results_RMSE_f[1,j+3]<-RMSE_mod_f
232
      
233
      results_MAE_f[1,1]<- sampling_dat$date[i]  #storing the interpolation dates in the first column
234
      results_MAE_f[1,2]<- ns        #number of stations used in the training stage
235
      results_MAE_f[1,3]<- "MAE_f"
236
      results_MAE_f[1,j+3]<-MAE_mod_f
237
      
238
      results_R2_f[1,1]<- sampling_dat$date[i]      #storing the interpolation dates in the first column
239
      results_R2_f[1,2]<- ns            #number of stations used in the training stage
240
      results_R2_f[1,3]<- "R2_f"
241
      results_R2_f[1,j+3]<- R2_mod_f      #Storing R2 for the model j
242
      
243
      results_RMSE[1,1]<- sampling_dat$date[i]    #storing the interpolation dates in the first column
244
      results_RMSE[1,2]<- ns          #number of stations used in the training stage
245
      results_RMSE[1,3]<- "RMSE"
246
      results_RMSE[1,j+3]<- RMSE_mod  #Storing RMSE for the model j
247
      results_MAE[1,1]<- sampling_dat$date[i]     #storing the interpolation dates in the first column
248
      results_MAE[1,2]<- ns           #number of stations used in the training stage
249
      results_MAE[1,3]<- "MAE"
250
      results_MAE[1,j+3]<- MAE_mod    #Storing MAE for the model j
251
      results_ME[1,1]<- sampling_dat$date[i]      #storing the interpolation dates in the first column
252
      results_ME[1,2]<- ns            #number of stations used in the training stage
253
      results_ME[1,3]<- "ME"
254
      results_ME[1,j+3]<- ME_mod      #Storing ME for the model j
255
      results_R2[1,1]<- sampling_dat$date[i]      #storing the interpolation dates in the first column
256
      results_R2[1,2]<- ns            #number of stations used in the training stage
257
      results_R2[1,3]<- "R2"
258
      results_R2[1,j+3]<- R2_mod      #Storing R2 for the model j
259
      
260
      #Saving residuals and prediction in the dataframes: tmax predicted from GAM
261

  
262
      name2<-paste("res_mod",j,sep="")
263
      data_v[[name2]]<-as.numeric(res_mod_v)
264
      data_s[[name2]]<-as.numeric(res_mod_s)
265
      #end of loop calculating RMSE
266
    }
267
  }
268
  
269
  #if (i==length(dates)){
270
  
271
  #Specific diagnostic measures related to the testing datasets
272

  
273
  results_table_RMSE<-as.data.frame(results_RMSE)
274
  results_table_MAE<-as.data.frame(results_MAE)
275
  results_table_ME<-as.data.frame(results_ME)
276
  results_table_R2<-as.data.frame(results_R2)
277
  results_table_RMSE_f<-as.data.frame(results_RMSE_f)
278
  results_table_MAE_f<-as.data.frame(results_MAE_f)
279
  results_table_R2_f<-as.data.frame(results_R2_f)
280
                        
281
  results_table_m1<-as.data.frame(results_m1)
282
  results_table_m2<-as.data.frame(results_m2)
283
  results_table_m3<-as.data.frame(results_m3)
284
  
285
  tb_metrics1<-rbind(results_table_RMSE,results_table_MAE, results_table_ME, 
286
                     results_table_R2,results_table_RMSE_f,results_table_MAE_f,results_table_R2_f)   #
287
  tb_metrics2<-rbind(results_table_m1,results_table_m2, results_table_m3)
288
  cname<-c("dates","ns","metric","mod1", "mod2","mod3", "mod4", "mod5", "mod6", "mod7","mod8","mod9")
289
  colnames(tb_metrics1)<-cname
290
  cname<-c("dates","ns","metric","mod1", "mod2","mod3", "mod4", "mod5", "mod6", "mod7","mod8","mod9")
291
  colnames(tb_metrics2)<-cname
292
  #colnames(results_table_RMSE)<-cname
293
  #colnames(results_table_RMSE_f)<-cname
294
  #tb_diagnostic1<-results_table_RMSE      #measures of validation
295
  #tb_diagnostic2<-results_table_RMSE_f    #measures of fit
296
  
297
  #write.table(tb_diagnostic1, file= paste(path,"/","results_fusion_Assessment_measure1",out_prefix,".txt",sep=""), sep=",")
298
  
299
  #}  
300
  #print(paste(date_proc,"processed"))
301
  print(paste(sampling_dat$date[i],"processed"))
302
  
303
  # Kriging object may need to be modified...because it contains the full image of prediction!!
304
  ##loop through model objects data frame and set field to zero...
305

  
306
  mod_obj<-list(mod1,mod2,mod3,mod4,mod5,mod6,mod7,mod8,mod9)
307
  names(mod_obj)<-c("mod1","mod2","mod3","mod4","mod5","mod6","mod7","mod8","mod9") #generate names automatically??
308
  #results_list<-list(data_s,data_v,tb_metrics1,tb_metrics2)
309
  #save(mod_obj,file= paste(path,"/","results_list_mod_objects_",dates[i],out_prefix,".RData",sep=""))
310
  
311
  for (j in 1:nmodels){
312
    if (inherits(mod_obj[[j]],"autoKrige")){
313
      mod_obj[[j]]$krige_output<-NULL
314
    }
315
  }
316
  results_list<-list(data_s,data_v,tb_metrics1,tb_metrics2,mod_obj)
317
  names(results_list)<-c("data_s","data_v","tb_metrics1","tb_metrics2","mod_obj")
318
  save(results_list,file= paste(path,"/","results_list_metrics_objects_",sampling_dat$date[i],"_",sampling_dat$prop[i],
319
                                "_",sampling_dat$run_samp[i],out_prefix,".RData",sep=""))
320
  return(results_list)
321
  #return(tb_diagnostic1)
322
}

Also available in: Unified diff