Project

General

Profile

« Previous | Next » 

Revision 902f729b

Added by Benoit Parmentier over 12 years ago

KRIGING, raster prediction full year, function used in main script

View differences:

climate/research/oregon/interpolation/queue/kriging_prediction_reg_function.R
1

  
2
runKriging <- function(i) {            # loop over dates
3
  
4
  #This allows to change only one name of the 
5
  
6
  date<-strptime(dates[i], "%Y%m%d")
7
  month<-strftime(date, "%m")
8
  LST_month<-paste("mm_",month,sep="")
9

  
10
  mod <-ghcn.subsets[[i]][,match(LST_month, names(ghcn.subsets[[i]]))]
11
  ghcn.subsets[[i]]$LST <-mod[[1]]
12
  #                    
13
  #   n<-nrow(ghcn.subsets[[i]])
14
  #   ns<-n-round(n*prop)                             #Create a sample from the data frame with 70% of the rows
15
  #   nv<-n-ns                                        #create a sample for validation with prop of the rows
16
  #   ind.training <- sample(nrow(ghcn.subsets[[i]]), size=ns, replace=FALSE)  #This selects the index position for 70% of the rows taken randomly
17
  ind.training<-sampling[[i]]
18
  ind.testing <- setdiff(1:nrow(ghcn.subsets[[i]]), ind.training)         #This selects the index position for testing subset stations.
19
  data_s <- ghcn.subsets[[i]][ind.training, ]
20
  data_v <- ghcn.subsets[[i]][ind.testing, ]
21
  
22
  #adding to SpatialGridDataFrame
23

  
24
  pos<-match(LST_month,layerNames(s_raster)) #Find column with the current month for instance mm12
25
  r1<-raster(s_raster,layer=pos)             #Select layer from stack
26
  layerNames(r1)<-"LST"
27
  s_raster<-addLayer(s_raster,r1)
28
  s_sgdf<-as(s_raster,"SpatialGridDataFrame") #Conversion to spatial grid data frame
29
  
30
  ###BEFORE Kringing the data object must be transformed to SDF
31
  
32
  coords<- data_v[,c('x_OR83M','y_OR83M')]
33
  coordinates(data_v)<-coords
34
  proj4string(data_v)<-CRS  #Need to assign coordinates...
35
  coords<- data_s[,c('x_OR83M','y_OR83M')]
36
  coordinates(data_s)<-coords
37
  proj4string(data_s)<-CRS  #Need to assign coordinates..
38
  
39
  #This allows to change only one name of the data.frame
40
  pos<-match("value",names(data_s)) #Find column with name "value"
41
  names(data_s)[pos]<-c("tmax")
42
  data_s$tmax<-data_s$tmax/10                #TMax is the average max temp for months
43
  pos<-match("value",names(data_v)) #Find column with name "value"
44
  names(data_v)[pos]<-c("tmax")
45
  data_v$tmax<-data_v$tmax/10
46
  #dstjan=dst[dst$month==9,]  #dst contains the monthly averages for tmax for every station over 2000-2010
47
  ##############
48
  ###STEP 2 KRIGING###
49
  
50
  #Kriging tmax
51
  
52
  #   hscat(tmax~1,data_s,(0:9)*20000)                       # 9 lag classes with 20,000m width
53
  #   v<-variogram(tmax~1, data_s)                           # This plots a sample varigram for date 10 fir the testing dataset
54
  #   plot(v)
55
  #   v.fit<-fit.variogram(v,vgm(2000,"Sph", 150000,1000))   #Model variogram: sill is 2000, spherical, range 15000 and nugget 1000
56
  #   plot(v, v.fit)                                         #Compare model and sample variogram via a graphical plot
57
  #   tmax_krige<-krige(tmax~1, data_s,mean_LST, v.fit)      #mean_LST provides the data grid/raster image for the kriging locations to be predicted.
58
  
59
#   krmod1<-try(autoKrige(tmax~1, data_s,s_sgdf,data_s)) #Use autoKrige instead of krige: with data_s for fitting on a grid
60
#   krmod2<-try(autoKrige(tmax~x_OR83M+y_OR83M,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
61
#   krmod3<-try(autoKrige(tmax~x_OR83M+y_OR83M+ELEV_SRTM,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
62
#   krmod4<-try(autoKrige(tmax~x_OR83M+y_OR83M+DISTOC,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
63
#   krmod5<-try(autoKrige(tmax~x_OR83M+y_OR83M+ELEV_SRTM+DISTOC,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
64
#   krmod6<-try(autoKrige(tmax~x_OR83M+y_OR83M+Northness+Eastness,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
65
#   krmod7<-try(autoKrige(tmax~x_OR83M+y_OR83M+Northness+Eastness,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
66
#   
67
  krmod1<-try(autoKrige(tmax~1, data_s,s_sgdf,data_s)) #Use autoKrige instead of krige: with data_s for fitting on a grid
68
  krmod2<-try(autoKrige(tmax~lat+lon,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
69
  krmod3<-try(autoKrige(tmax~lat+lon+ELEV_SRTM,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
70
  krmod4<-try(autoKrige(tmax~lat+lon+DISTOC,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
71
  krmod5<-try(autoKrige(tmax~lat+lon+ELEV_SRTM+DISTOC,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
72
  krmod6<-try(autoKrige(tmax~lat+lon+Northness+Eastness,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
73
  krmod7<-try(autoKrige(tmax~lat+lon+Northness+Eastness,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
74
  
75
  krmod8<-try(autoKrige(tmax~LST,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
76
  krmod9<-try(autoKrige(tmax~lat+lon+LST,input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
77
  
78
#   krig1<-krmod1$krige_output                   #Extracting Spatial Grid Data frame                    
79
#   krig2<-krmod2$krige_output
80
#   krig3<-krmod3$krige_outpu
81
#   krig4<-krmod4$krige_output
82
#   krig5<-krmod5$krige_output
83
#   krig6<-krmod6$krige_output                   #Extracting Spatial Grid Data frame                    
84
#   krig7<-krmod7$krige_output
85
  #krig8<-krmod8$krige_outpu
86
  #krig9<-krmod9$krige_output
87
  
88
  #tmax_krig1_s <- overlay(krige,data_s)             #This overlays the kriged surface tmax and the location of weather stations
89
  #tmax_krig1_v <- overlay(krige,data_v)
90
  #   
91
  #   #Cokriging tmax
92
  #   g<-gstat(NULL,"tmax", tmax~1, data_s)                   #This creates a gstat object "g" that acts as container for kriging specifications.
93
  #   g<-gstat(g, "SRTM_elev",ELEV_SRTM~1,data_s)            #Adding variables to gstat object g
94
  #   g<-gstat(g, "LST", LST~1,data_s)
95
  
96
  #   vm_g<-variogram(g)                                     #Visualizing multivariate sample variogram.
97
  #   vm_g.fit<-fit.lmc(vm_g,g,vgm(2000,"Sph", 100000,1000)) #Fitting variogram for all variables at once.
98
  #   plot(vm_g,vm_g.fit)                                    #Visualizing variogram fit and sample
99
  #   vm_g.fit$set <-list(nocheck=1)                         #Avoid checking and allow for different range in variogram
100
  #   co_kriged_surf<-predict(vm_g.fit,mean_LST) #Prediction using co-kriging with grid location defined from input raster image.
101
  #   #co_kriged_surf$tmax.pred                              #Results stored in SpatialGridDataFrame with tmax prediction accessible in dataframe.
102
  
103
  #spplot.vcov(co_kriged_surf)                           #Visualizing the covariance structure
104
  
105
  #   tmax_cokrig1_s<- overlay(co_kriged_surf,data_s)        #This overalys the cokriged surface tmax and the location of weather stations
106
  #   tmax_cokrig1_v<- overlay(co_kriged_surf,data_v)
107
  
108
  for (j in 1:models){
109
    
110
    #mod<-paste("krig",j,sep="")
111
    mod<-paste("krmod",j,sep="")
112
    
113
    krmod_auto<-get(mod)
114
    
115
    #If mod "j" is not a model object
116
    if (inherits(krmod_auto,"try-error")) {
117
      
118
      #Model assessment:results are NA
119
      
120
      results_RMSE[1,1]<- dates[i]  #storing the interpolation dates in the first column
121
      results_RMSE[1,2]<- ns        #number of stations used in the training stage
122
      results_RMSE[1,3]<- "RMSE"
123
      results_RMSE[1,j+3]<- NA
124
      #results_RMSE_kr[i,3]<- res_mod_kr_v
125
      
126
      results_MAE[1,1]<- dates[i]  #storing the interpolation dates in the first column
127
      results_MAE[1,2]<- ns        #number of stations used in the training stage
128
      results_MAE[1,3]<- "MAE"
129
      results_MAE[1,j+3]<- NA
130
      #results_RMSE_kr[i,3]<- res_mod_kr_v
131
      
132
      results_ME[1,1]<- dates[i]  #storing the interpolation dates in the first column
133
      results_ME[1,2]<- ns        #number of stations used in the training stage
134
      results_ME[1,3]<- "ME"
135
      results_ME[1,j+3]<- NA
136
      #results_RMSE_kr[i,3]<- res_mod_kr_v
137
      
138
      results_R2[1,1]<- dates[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
142
      #results_RMSE_kr[i,3]<- res_mod_kr_v
143
      
144
      results_RMSE_f[1,1]<- dates[i]  #storing the interpolation dates in the first column
145
      results_RMSE_f[1,2]<- ns        #number of stations used in the training stage
146
      results_RMSE_f[1,3]<- "RMSE_f"
147
      results_RMSE_f[1,j+3]<- NA
148
      #results_RMSE_kr[i,3]<- res_mod_kr_v
149
      
150
      results_MAE_f[1,1]<- dates[i]  #storing the interpolation dates in the first column
151
      results_MAE_f[1,2]<- ns        #number of stations used in the training stage
152
      results_MAE_f[1,3]<- "MAE_f"
153
      results_MAE_f[1,j+3]<- NA
154
      name3<-paste("res_kr_mod",j,sep="")
155
      
156
    }
157
    
158
    #If mod "j" is not a model object
159
    if (inherits(krmod_auto,"autoKrige")) {
160
      krmod<-krmod_auto$krige_output                   #Extracting Spatial Grid Data frame                    
161
      
162
      krig_val_s <- overlay(krmod,data_s)             #This overlays the kriged surface tmax and the location of weather stations
163
      krig_val_v <- overlay(krmod,data_v)             #This overlays the kriged surface tmax and the location of weather stations
164
      
165
      pred_krmod<-paste("pred_krmod",j,sep="")
166
      #Adding the results back into the original dataframes.
167
      data_s[[pred_krmod]]<-krig_val_s$var1.pred
168
      data_v[[pred_krmod]]<-krig_val_v$var1.pred  
169
      
170
      #Model assessment: RMSE and then krig the residuals....!
171
      
172
      res_mod_kr_s<- data_s$tmax - data_s[[pred_krmod]]           #Residuals from kriging training
173
      res_mod_kr_v<- data_v$tmax - data_v[[pred_krmod]]           #Residuals from kriging validation
174
      
175
      RMSE_mod_kr_s <- sqrt(sum(res_mod_kr_s^2,na.rm=TRUE)/(nv-sum(is.na(res_mod_kr_s))))         #RMSE from kriged surface training
176
      RMSE_mod_kr_v <- sqrt(sum(res_mod_kr_v^2,na.rm=TRUE)/(nv-sum(is.na(res_mod_kr_v))))         #RMSE from kriged surface validation
177
      MAE_mod_kr_s<- sum(abs(res_mod_kr_s),na.rm=TRUE)/(nv-sum(is.na(res_mod_kr_s)))        #MAE from kriged surface training                    #MAE, Mean abs. Error FOR REGRESSION STEP 1: GAM   
178
      MAE_mod_kr_v<- sum(abs(res_mod_kr_v),na.rm=TRUE)/(nv-sum(is.na(res_mod_kr_v)))        #MAE from kriged surface validation
179
      ME_mod_kr_s<- sum(res_mod_kr_s,na.rm=TRUE)/(nv-sum(is.na(res_mod_kr_s)))                    #ME, Mean Error or bias FOR REGRESSION STEP 1: GAM
180
      ME_mod_kr_v<- sum(res_mod_kr_v,na.rm=TRUE)/(nv-sum(is.na(res_mod_kr_v)))                    #ME, Mean Error or bias FOR REGRESSION STEP 1: GAM
181
      R2_mod_kr_s<- cor(data_s$tmax,data_s[[pred_krmod]],use="complete.obs")^2                  #R2, coef. of determination FOR REGRESSION STEP 1: GAM
182
      R2_mod_kr_v<- cor(data_v$tmax,data_v[[pred_krmod]],use="complete.obs")^2                  #R2, coef. of determinationFOR REGRESSION STEP 1: GAM
183
      #(nv-sum(is.na(res_mod2)))
184
      #Writing out results
185
      
186
      results_RMSE[1,1]<- dates[i]  #storing the interpolation dates in the first column
187
      results_RMSE[1,2]<- ns        #number of stations used in the training stage
188
      results_RMSE[1,3]<- "RMSE"
189
      results_RMSE[1,j+3]<- RMSE_mod_kr_v
190
      #results_RMSE_kr[i,3]<- res_mod_kr_v
191
      
192
      results_MAE[1,1]<- dates[i]  #storing the interpolation dates in the first column
193
      results_MAE[1,2]<- ns        #number of stations used in the training stage
194
      results_MAE[1,3]<- "MAE"
195
      results_MAE[1,j+3]<- MAE_mod_kr_v
196
      #results_RMSE_kr[i,3]<- res_mod_kr_v
197
      
198
      results_ME[1,1]<- dates[i]  #storing the interpolation dates in the first column
199
      results_ME[1,2]<- ns        #number of stations used in the training stage
200
      results_ME[1,3]<- "ME"
201
      results_ME[1,j+3]<- ME_mod_kr_v
202
      #results_RMSE_kr[i,3]<- res_mod_kr_v
203
      
204
      results_R2[1,1]<- dates[i]  #storing the interpolation dates in the first column
205
      results_R2[1,2]<- ns        #number of stations used in the training stage
206
      results_R2[1,3]<- "R2"
207
      results_R2[1,j+3]<- R2_mod_kr_v
208
      #results_RMSE_kr[i,3]<- res_mod_kr_v
209
      
210
      results_RMSE_f[1,1]<- dates[i]  #storing the interpolation dates in the first column
211
      results_RMSE_f[1,2]<- ns        #number of stations used in the training stage
212
      results_RMSE_f[1,3]<- "RMSE_f"
213
      results_RMSE_f[1,j+3]<- RMSE_mod_kr_s
214
      #results_RMSE_kr[i,3]<- res_mod_kr_v
215
      
216
      results_MAE_f[1,1]<- dates[i]  #storing the interpolation dates in the first column
217
      results_MAE_f[1,2]<- ns        #number of stations used in the training stage
218
      results_MAE_f[1,3]<- "MAE_f"
219
      results_MAE_f[1,j+3]<- MAE_mod_kr_s
220
      name3<-paste("res_kr_mod",j,sep="")
221
      
222
      #as.numeric(res_mod)
223
      #data_s[[name3]]<-res_mod_kr_s
224
      data_s[[name3]]<-as.numeric(res_mod_kr_s)
225
      #data_v[[name3]]<-res_mod_kr_v 
226
      data_v[[name3]]<-as.numeric(res_mod_kr_v)
227
      #Writing residuals from kriging
228
      
229
      #Saving kriged surface in raster images
230
      data_name<-paste("mod",j,"_",dates[[i]],sep="")
231
      #krig_raster_name<-paste("krmod_",data_name,out_prefix,".tif", sep="")
232
      #writeGDAL(krmod,fname=krig_raster_name, driver="GTiff", type="Float32",options ="INTERLEAVE=PIXEL", overwrite=TRUE)
233
      krig_raster_name<-paste("krmod_",data_name,out_prefix,".rst", sep="")
234
      writeRaster(raster(krmod), filename=krig_raster_name, overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
235
      
236
      #krig_raster_name<-paste("Kriged_tmax_",data_name,out_prefix,".tif", sep="")
237
      #writeGDAL(tmax_krige,fname=krig_raster_name, driver="GTiff", type="Float32",options ="INTERLEAVE=PIXEL")
238
      #X11()
239
      #plot(raster(co_kriged_surf))
240
      #title(paste("Tmax cokriging for date ",dates[[i]],sep=""))
241
      #savePlot(paste("Cokriged_tmax",data_name,out_prefix,".png", sep=""), type="png")
242
      #dev.off()
243
      #X11()
244
      #plot(raster(tmax_krige))
245
      #title(paste("Tmax Kriging for date ",dates[[i]],sep=""))
246
      #savePlot(paste("Kriged_res_",data_name,out_prefix,".png", sep=""), type="png")
247
      #dev.off()
248
      # end of if krige object
249
    }
250
    
251
  }
252
  
253
  #   #Co-kriging only on the validation sites for faster computing
254
  #   
255
  #   cokrig1_dv<-predict(vm_g.fit,data_v)
256
  #   cokrig1_ds<-predict(vm_g.fit,data_s)
257
  # #   data_s$tmax_cokr<-cokrig1_ds$tmax.pred    
258
  # #   data_v$tmax_cokr<-cokrig1_dv$tmax.pred
259
  #   
260
  #   #Calculate RMSE and then krig the residuals....!
261
  #   
262
  #   res_mod1<- data_v$tmax - data_v$tmax_kr              #Residuals from kriging.
263
  #   res_mod2<- data_v$tmax - data_v$tmax_cokr            #Residuals from cokriging.
264
  #   
265
  #   RMSE_mod1 <- sqrt(sum(res_mod1^2,na.rm=TRUE)/(nv-sum(is.na(res_mod1))))                  #RMSE from kriged surface.
266
  #   RMSE_mod2 <- sqrt(sum(res_mod2^2,na.rm=TRUE)/(nv-sum(is.na(res_mod2))))                  #RMSE from co-kriged surface.
267
  #   #(nv-sum(is.na(res_mod2)))       
268
  
269
  #Saving the subset in a dataframe
270
  data_name<-paste("ghcn_v_",dates[[i]],sep="")
271
  assign(data_name,data_v)
272
  data_name<-paste("ghcn_s_",dates[[i]],sep="")
273
  assign(data_name,data_s)
274
  
275
  #   results[i,1]<- dates[i]  #storing the interpolation dates in the first column
276
  #   results[i,2]<- ns     #number of stations in training
277
  #   results[i,3]<- RMSE_mod1
278
  #   results[i,4]<- RMSE_mod2  
279
  #   
280
  #   results_mod_n[i,1]<-dates[i]
281
  #   results_mod_n[i,2]<-(nv-sum(is.na(res_mod1)))
282
  #   results_mod_n[i,3]<-(nv-sum(is.na(res_mod2)))
283
  
284
  #Specific diagnostic measures related to the testing datasets
285
  #browser()
286
  results_table_RMSE<-as.data.frame(results_RMSE)
287
  results_table_MAE<-as.data.frame(results_MAE)
288
  results_table_ME<-as.data.frame(results_ME)
289
  results_table_R2<-as.data.frame(results_R2)
290
  results_table_RMSE_f<-as.data.frame(results_RMSE_f)
291
  results_table_MAE_f<-as.data.frame(results_MAE_f)
292
  
293
  results_table_AIC<-as.data.frame(results_AIC) #Other tables for kriging
294
  results_table_GCV<-as.data.frame(results_GCV)
295
  results_table_DEV<-as.data.frame(results_DEV)
296
  
297
  tb_metrics1<-rbind(results_table_RMSE,results_table_MAE, results_table_ME, results_table_R2,results_table_RMSE_f,results_table_MAE_f)   #
298
  tb_metrics2<-rbind(results_table_AIC,results_table_GCV, results_table_DEV)
299
  cname<-c("dates","ns","metric","mod1", "mod2","mod3", "mod4", "mod5", "mod6", "mod7")
300
  colnames(tb_metrics1)<-cname
301
  cname<-c("dates","ns","metric","mod1", "mod2","mod3", "mod4", "mod5", "mod6", "mod7")
302
  colnames(tb_metrics2)<-cname
303
  #colnames(results_table_RMSE)<-cname
304
  #colnames(results_table_RMSE_f)<-cname
305
  #tb_diagnostic1<-results_table_RMSE      #measures of validation
306
  #tb_diagnostic2<-results_table_RMSE_f    #measures of fit
307
  
308
  #write.table(tb_diagnostic1, file= paste(path,"/","results_fusion_Assessment_measure1",out_prefix,".txt",sep=""), sep=",")
309
  
310
  #}  
311
  print(paste(dates[i],"processed"))
312
  mod_obj<-list(krmod1,krmod2,krmod3,krmod4,krmod5,krmod6,krmod7)
313
  # end of the for loop1
314
  #results_list<-list(data_s,data_v,tb_metrics1,tb_metrics2)
315
  results_list<-list(data_s,data_v,tb_metrics1,tb_metrics2,mod_obj)
316
  return(results_list)
317
  #return(tb_diagnostic1)
318
}

Also available in: Unified diff