Project

General

Profile

« Previous | Next » 

Revision 1c15fc49

Added by Adam M. Wilson over 10 years ago

Revert "Merge branch 'ag/interp' of code.nceas.ucsb.edu:environmental-layers into aw/precip"

This reverts commit f9c712987ba814b83b2c2cc058c6c1f9b07933c1, reversing
changes made to f0375becc9fb9e13e55c5b7a48be5c5f98064f87.

View differences:

climate/research/oregon/interpolation/kriging_reg.R
1
##################    Interpolation of Tmax Using Kriging  #######################################
2
########################### Kriging and Cokriging   ###############################################
3
#This script interpolates station values for the Oregon case study using Kriging and Cokring.    #
4
#The script uses LST monthly averages as input variables and  loads the station data             # 
5
#from a shape file with projection information.                                                  #
6
#Note that this program:                                                                         #
7
#1)assumes that the shape file is in the current working.                                        # 
8
#2)relevant variables were extracted from raster images before performing the regressions        #
9
#  and stored shapefile                                                                          #
10
#This scripts predicts tmax using autokrige, gstat and LST derived from MOD11A1.                 #
11
#also included and assessed using the RMSE,MAE,ME and R2 from validation dataset.                #
12
#TThe dates must be provided as a textfile.                                                      #
13
#AUTHOR: Benoit Parmentier                                                                       #
14
#DATE: 07/15/2012                                                                                #
15
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#364--                                  #
16
##################################################################################################
17

  
18
###Loading R library and packages                                                      
19
#library(gtools)                                         # loading some useful tools 
20
library(mgcv)                                           # GAM package by Wood 2006 (version 2012)
21
library(sp)                                             # Spatial pacakge with class definition by Bivand et al. 2008
22
library(spdep)                                          # Spatial pacakge with methods and spatial stat. by Bivand et al. 2012
23
library(rgdal)                                          # GDAL wrapper for R, spatial utilities (Keitt et al. 2012)
24
library(gstat)                                          # Kriging and co-kriging by Pebesma et al. 2004
25
library(automap)                                        # Automated Kriging based on gstat module by Hiemstra et al. 2008
1
####################GWR of Tmax for one Date#####################
2
#This script generates predicted values from station values for the Oregon case study. This program loads the station data from a shp file 
3
#and performs Kriging and co-kriging on tmax regression.
4
#Script created by Benoit Parmentier on April 17, 2012. 
5

  
6
###Loading r library and packages
7
library(sp)
8
library(spdep)
9
library(rgdal)
26 10
library(spgwr)
27 11
library(gpclib)
28 12
library(maptools)
13
library(gstat)
29 14
library(graphics)
30

  
31 15
###Parameters and arguments
32 16

  
33
infile1<- "ghcn_or_tmax_covariates_06262012_OR83M.shp"             #GHCN shapefile containing variables for modeling 2010                 
34
infile2<-"list_10_dates_04212012.txt"                     #List of 10 dates for the regression
35
#infile2<-"list_365_dates_04212012.txt"
36
infile3<-"LST_dates_var_names.txt"                        #LST dates name
37
infile4<-"models_interpolation_05142012.txt"              #Interpolation model names
38
infile5<-"mean_day244_rescaled.rst"                       
39
inlistf<-"list_files_05032012.txt"                        #Stack of images containing the Covariates
40

  
41
path<-"/home/parmentier/Data/IPLANT_project/data_Oregon_stations_07152012"     #Jupiter LOCATION on Atlas for kriging
42
#path<-"H:/Data/IPLANT_project/data_Oregon_stations"                                 #Jupiter Location on XANDERS
43

  
44
setwd(path) 
45
prop<-0.3                                                                       #Proportion of testing retained for validation   
46
seed_number<- 100                                                               #Seed number for random sampling
47
models<-7                                                                       #Number of kriging model
48
out_prefix<-"_07132012_auto_krig_"                                              #User defined output prefix
17
path<- "/data/computer/parmentier/Data/IPLANT_project/data_Oregon_stations/"         #Path to all datasets
18
setwd(path)
19
infile1<-"ghcn_or_tmax_b_04142012_OR83M.shp" #Weather station location in Oregon with input variables
20
infile2<-"dates_interpolation_03052012.txt"  # list of 10 dates for the regression, more thatn 10 dates may be used
21
infile3<-"mean_day244_rescaled.rst"          #This image serves as the reference grid for kriging
22
infile4<- "orcnty24_OR83M.shp"               #Vector file defining the study area: Oregon state and its counties.
23
prop<-0.3                                    #Propotion of weather stations retained for validation/testing
24
out_prefix<-"_LST_04172012_RMSE"                 #output name used in the text file result
49 25

  
50 26
###STEP 1 DATA PREPARATION AND PROCESSING#####
51 27

  
52
###Reading the station data and setting up for models' comparison
53
filename<-sub(".shp","",infile1)             #Removing the extension from file.
54
ghcn<-readOGR(".", filename)                 #reading shapefile 
55

  
56
CRS<-proj4string(ghcn)                       #Storing projection information (ellipsoid, datum,etc.)
57

  
58
mean_LST<- readGDAL(infile5)                 #Reading the whole raster in memory. This provides a grid for kriging
59
proj4string(mean_LST)<-CRS                   #Assigning coordinate information to prediction grid.
60

  
61
##Extracting the variables values from the raster files                                             
28
###Reading the shapefile and raster image from the local directory
62 29

  
63
lines<-read.table(paste(path,"/",inlistf,sep=""), sep=" ")                  #Column 1 contains the names of raster files
64
inlistvar<-lines[,1]
65
inlistvar<-paste(path,"/",as.character(inlistvar),sep="")
66
covar_names<-as.character(lines[,2])                                         #Column two contains short names for covaraites
30
mean_LST<- readGDAL(infile3)                  #This reads the whole raster in memory and provide a grid for kriging in a SpatialGridDataFrame object
31
filename<-sub(".shp","",infile1)              #Removing the extension from file.
32
ghcn<-readOGR(".", filename)                  #Reading station locations from vector file using rgdal and creating a SpatialPointDataFrame
33
CRS_ghcn<-proj4string(ghcn)                   #This retrieves the coordinate system information for the SDF object (PROJ4 format)
34
proj4string(mean_LST)<-CRS_ghcn               #Assigning coordinates information to SpatialGridDataFrame object
67 35

  
68
s_raster<- stack(inlistvar)                                                  #Creating a stack of raster images from the list of variables.
69
layerNames(s_raster)<-covar_names                                            #Assigning names to the raster layers
70
projection(s_raster)<-CRS
36
# Creating state outline from county
71 37

  
72
#stat_val<- extract(s_raster, ghcn3)                                          #Extracting values from the raster stack for every point location in coords data frame.
73
pos<-match("ASPECT",layerNames(s_raster)) #Find column with name "value"
74
r1<-raster(s_raster,layer=pos)             #Select layer from stack
75
pos<-match("slope",layerNames(s_raster)) #Find column with name "value"
76
r2<-raster(s_raster,layer=pos)             #Select layer from stack
77
N<-cos(r1*pi/180)
78
E<-sin(r1*pi/180)
79
Nw<-sin(r2*pi/180)*cos(r1*pi/180)   #Adding a variable to the dataframe
80
Ew<-sin(r2*pi/180)*sin(r1*pi/180)   #Adding variable to the dataframe.
81
r<-stack(N,E,Nw,Ew)
82
rnames<-c("Northness","Eastness","Northness_w","Eastness_w")
83
layerNames(r)<-rnames
84
s_raster<-addLayer(s_raster, r)
85
s_sgdf<-as(s_raster,"SpatialGridDataFrame") #Conversion to spatial grid data frame
38
orcnty<-readOGR(".", "orcnty24_OR83M")
39
proj4string(orcnty)                           #This retrieves the coordinate system for the SDF
40
lps <-getSpPPolygonsLabptSlots(orcnty)        #Getting centroids county labels
41
IDOneBin <- cut(lps[,1], range(lps[,1]), include.lowest=TRUE)  #Creating one bin var
42
gpclibPermit()                                #Set the gpclib to True to allow union
43
OR_state <- unionSpatialPolygons(orcnty ,IDOneBin) #Dissolve based on bin var
86 44

  
87
### adding var
88
ghcn = transform(ghcn,Northness = cos(ASPECT*pi/180)) #Adding a variable to the dataframe
89
ghcn = transform(ghcn,Eastness = sin(ASPECT*pi/180))  #adding variable to the dataframe.
90
ghcn = transform(ghcn,Northness_w = sin(slope*pi/180)*cos(ASPECT*pi/180)) #Adding a variable to the dataframe
91
ghcn = transform(ghcn,Eastness_w = sin(slope*pi/180)*sin(ASPECT*pi/180))  #adding variable to the dataframe.
45
# Adding variables for the regressions
92 46

  
93
#Remove NA for LC and CANHEIGHT
94
ghcn$LC1[is.na(ghcn$LC1)]<-0
95
ghcn$LC3[is.na(ghcn$LC3)]<-0
96
ghcn$CANHEIGHT[is.na(ghcn$CANHEIGHT)]<-0
47
ghcn$Northness<- cos(ghcn$ASPECT)             #Adding a variable to the dataframe by calculating the cosine of Aspect
48
ghcn$Eastness <- sin(ghcn$ASPECT)             #Adding variable to the dataframe.
49
ghcn$Northness_w <- sin(ghcn$slope)*cos(ghcn$ASPECT)  #Adding a variable to the dataframe
50
ghcn$Eastness_w  <- sin(ghcn$slope)*sin(ghcn$ASPECT)  #Adding variable to the dataframe.
97 51

  
98
set.seed(seed_number)                        #Using a seed number allow results based on random number to be compared...
52
set.seed(100)                                 #This set a seed number for the random sampling to make results reproducible.
99 53

  
100
dates <-readLines(paste(path,"/",infile2, sep=""))
101
LST_dates <-readLines(paste(path,"/",infile3, sep=""))
102
#models <-readLines(paste(path,"/",infile4, sep=""))
54
dates <-readLines(paste(path,"/",infile2, sep=""))  #Reading dates in a list from the textile.
55
results <- matrix(1,length(dates),4)            #This is a matrix containing the diagnostic measures from the GAM models.
56
results_mod_n<-matrix(1,length(dates),3)
103 57

  
104
#models<-5
105
#Model assessment: specific diagnostic/metrics for GAM
106
results_AIC<- matrix(1,length(dates),models+3)  
107
results_GCV<- matrix(1,length(dates),models+3)
58
#Screening for bad values and setting the valid range
108 59

  
109
#Model assessment: general diagnostic/metrics 
110
results_RMSE <- matrix(1,length(dates),models+3)
111
results_MAE <- matrix(1,length(dates),models+3)
112
results_ME <- matrix(1,length(dates),models+3)
113
results_R2 <- matrix(1,length(dates),models+3)       #Coef. of determination for the validation dataset
114
results_RMSE_f<- matrix(1,length(dates),models+3)
115

  
116

  
117
#Screening for bad values: value is tmax in this case
118
#ghcn$value<-as.numeric(ghcn$value)
119
ghcn_all<-ghcn
120
ghcn_test<-subset(ghcn,ghcn$value>-150 & ghcn$value<400)
121
ghcn_test2<-subset(ghcn_test,ghcn_test$ELEV_SRTM>0)
60
ghcn_test<-subset(ghcn,ghcn$tmax>-150 & ghcn$tmax<400) #Values are in tenth of degrees C
61
ghcn_test2<-subset(ghcn_test,ghcn_test$ELEV_SRTM>0)    #No elevation below sea leve is allowed.
122 62
ghcn<-ghcn_test2
123
#coords<- ghcn[,c('x_OR83M','y_OR83M')]
124

  
125

  
126 63

  
127 64
###CREATING SUBSETS BY INPUT DATES AND SAMPLING
128 65
ghcn.subsets <-lapply(dates, function(d) subset(ghcn, ghcn$date==as.numeric(d)))   #Producing a list of data frame, one data frame per date.
129 66

  
130 67
for(i in 1:length(dates)){            # start of the for loop #1
131 68
#i<-3                                           #Date 10 is used to test kriging
132
  
133
  #This allows to change only one name of the 
134
  
135 69
  date<-strptime(dates[i], "%Y%m%d")
136 70
  month<-strftime(date, "%m")
137 71
  LST_month<-paste("mm_",month,sep="")
138
  #adding to SpatialGridDataFrame
139
  #t<-s_sgdf[,match(LST_month, names(s_sgdf))]
140
  #s_sgdf$LST<-s_sgdf[c(LST_month)]
141 72
  mod <-ghcn.subsets[[i]][,match(LST_month, names(ghcn.subsets[[i]]))]
142 73
  ghcn.subsets[[i]]$LST <-mod[[1]]
143 74
                   
......
149 80
  data_s <- ghcn.subsets[[i]][ind.training, ]
150 81
  data_v <- ghcn.subsets[[i]][ind.testing, ]
151 82
  
83
  ###STEP 2 KRIGING###
84
  
85
  #Kriging tmax
152 86
  
153
  ###BEFORE Kringing the data object must be transformed to SDF
87
  hscat(tmax~1,data_s,(0:9)*20000)                       # 9 lag classes with 20,000m width
88
  v<-variogram(tmax~1, data_s)                           # This plots a sample varigram for date 10 fir the testing dataset
89
  plot(v)
90
  v.fit<-fit.variogram(v,vgm(2000,"Sph", 150000,1000))   #Model variogram: sill is 2000, spherical, range 15000 and nugget 1000
91
  plot(v, v.fit)                                         #Compare model and sample variogram via a graphical plot
92
  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.
154 93
  
155
  coords<- data_v[,c('x_OR83M','y_OR83M')]
156
  coordinates(data_v)<-coords
157
  proj4string(data_v)<-CRS  #Need to assign coordinates...
158
  coords<- data_s[,c('x_OR83M','y_OR83M')]
159
  coordinates(data_s)<-coords
160
  proj4string(data_s)<-CRS  #Need to assign coordinates..
94
  #Cokriging tmax
95
  g<-gstat(NULL,"tmax", tmax~1, data_s)                   #This creates a gstat object "g" that acts as container for kriging specifications.
96
  g<-gstat(g, "SRTM_elev",ELEV_SRTM~1,data_s)            #Adding variables to gstat object g
97
  g<-gstat(g, "LST", LST~1,data_s)
161 98
  
162
  #This allows to change only one name of the data.frame
163
  pos<-match("value",names(data_s)) #Find column with name "value"
164
  names(data_s)[pos]<-c("tmax")
165
  data_s$tmax<-data_s$tmax/10                #TMax is the average max temp for months
166
  pos<-match("value",names(data_v)) #Find column with name "value"
167
  names(data_v)[pos]<-c("tmax")
168
  data_v$tmax<-data_v$tmax/10
169
  #dstjan=dst[dst$month==9,]  #dst contains the monthly averages for tmax for every station over 2000-2010
170
  ##############
171
  ###STEP 2 KRIGING###
99
  vm_g<-variogram(g)                                     #Visualizing multivariate sample variogram.
100
  vm_g.fit<-fit.lmc(vm_g,g,vgm(2000,"Sph", 100000,1000)) #Fitting variogram for all variables at once.
101
  plot(vm_g,vm_g.fit)                                    #Visualizing variogram fit and sample
102
  vm_g.fit$set <-list(nocheck=1)                         #Avoid checking and allow for different range in variogram
103
  co_kriged_surf<-predict(vm_g.fit,mean_LST) #Prediction using co-kriging with grid location defined from input raster image.
104
  #co_kriged_surf$tmax.pred                              #Results stored in SpatialGridDataFrame with tmax prediction accessible in dataframe.
172 105
  
173
  #Kriging tmax
174 106
  
175
#   hscat(tmax~1,data_s,(0:9)*20000)                       # 9 lag classes with 20,000m width
176
#   v<-variogram(tmax~1, data_s)                           # This plots a sample varigram for date 10 fir the testing dataset
177
#   plot(v)
178
#   v.fit<-fit.variogram(v,vgm(2000,"Sph", 150000,1000))   #Model variogram: sill is 2000, spherical, range 15000 and nugget 1000
179
#   plot(v, v.fit)                                         #Compare model and sample variogram via a graphical plot
180
#   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.
107
  #spplot.vcov(co_kriged_surf)                           #Visualizing the covariance structure
181 108
  
182
  krmod1<-autoKrige(tmax~1, data_s,s_sgdf,data_s) #Use autoKrige instead of krige: with data_s for fitting on a grid
183
  krmod2<-autoKrige(tmax~x_OR83M+y_OR83M,input_data=data_s,new_data=s_sgdf,data_variogram=data_s)
184
  krmod3<-autoKrige(tmax~x_OR83M+y_OR83M+ELEV_SRTM,input_data=data_s,new_data=s_sgdf,data_variogram=data_s)
185
  krmod4<-autoKrige(tmax~x_OR83M+y_OR83M+DISTOC,input_data=data_s,new_data=s_sgdf,data_variogram=data_s)
186
  krmod5<-autoKrige(tmax~x_OR83M+y_OR83M+ELEV_SRTM+DISTOC,input_data=data_s,new_data=s_sgdf,data_variogram=data_s)
187
  krmod6<-autoKrige(tmax~x_OR83M+y_OR83M+Northness+Eastness,input_data=data_s,new_data=s_sgdf,data_variogram=data_s)
188
  krmod7<-autoKrige(tmax~x_OR83M+y_OR83M+Northness+Eastness,input_data=data_s,new_data=s_sgdf,data_variogram=data_s)
189
  #krmod8<-autoKrige(tmax~LST,input_data=data_s,new_data=s_sgdf,data_variogram=data_s)
190
  #krmod9<-autoKrige(tmax~x_OR83M+y_OR83M+LST,input_data=data_s,new_data=s_sgdf,data_variogram=data_s)
109
  tmax_krig1_s <- overlay(tmax_krige,data_s)             #This overlays the kriged surface tmax and the location of weather stations
110
  tmax_cokrig1_s<- overlay(co_kriged_surf,data_s)        #This overalys the cokriged surface tmax and the location of weather stations
111
  tmax_krig1_v <- overlay(tmax_krige,data_v)             #This overlays the kriged surface tmax and the location of weather stations
112
  tmax_cokrig1_v<- overlay(co_kriged_surf,data_v)
191 113
  
192
  krig1<-krmod1$krige_output                   #Extracting Spatial Grid Data frame                    
193
  krig2<-krmod2$krige_output
194
  krig3<-krmod3$krige_outpu
195
  krig4<-krmod4$krige_output
196
  krig5<-krmod5$krige_output
197
  krig6<-krmod6$krige_output                   #Extracting Spatial Grid Data frame                    
198
  krig7<-krmod7$krige_output
199
  #krig8<-krmod8$krige_outpu
200
  #krig9<-krmod9$krige_output
114
  data_s$tmax_kr<-tmax_krig1_s$var1.pred                 #Adding the results back into the original dataframes.
115
  data_v$tmax_kr<-tmax_krig1_v$var1.pred  
116
  data_s$tmax_cokr<-tmax_cokrig1_s$tmax.pred    
117
  data_v$tmax_cokr<-tmax_cokrig1_v$tmax.pred
201 118
  
202
  #tmax_krig1_s <- overlay(krige,data_s)             #This overlays the kriged surface tmax and the location of weather stations
203
  #tmax_krig1_v <- overlay(krige,data_v)
204
#   
205
#   #Cokriging tmax
206
#   g<-gstat(NULL,"tmax", tmax~1, data_s)                   #This creates a gstat object "g" that acts as container for kriging specifications.
207
#   g<-gstat(g, "SRTM_elev",ELEV_SRTM~1,data_s)            #Adding variables to gstat object g
208
#   g<-gstat(g, "LST", LST~1,data_s)
119
  #Co-kriging only on the validation sites for faster computing
209 120
  
210
#   vm_g<-variogram(g)                                     #Visualizing multivariate sample variogram.
211
#   vm_g.fit<-fit.lmc(vm_g,g,vgm(2000,"Sph", 100000,1000)) #Fitting variogram for all variables at once.
212
#   plot(vm_g,vm_g.fit)                                    #Visualizing variogram fit and sample
213
#   vm_g.fit$set <-list(nocheck=1)                         #Avoid checking and allow for different range in variogram
214
#   co_kriged_surf<-predict(vm_g.fit,mean_LST) #Prediction using co-kriging with grid location defined from input raster image.
215
#   #co_kriged_surf$tmax.pred                              #Results stored in SpatialGridDataFrame with tmax prediction accessible in dataframe.
121
  cokrig1_dv<-predict(vm_g.fit,data_v)
122
  cokrig1_ds<-predict(vm_g.fit,data_s)
123
  data_s$tmax_cokr<-cokrig1_ds$tmax.pred    
124
  data_v$tmax_cokr<-cokrig1_dv$tmax.pred
216 125
  
217
  #spplot.vcov(co_kriged_surf)                           #Visualizing the covariance structure
218
    
219
#   tmax_cokrig1_s<- overlay(co_kriged_surf,data_s)        #This overalys the cokriged surface tmax and the location of weather stations
220
#   tmax_cokrig1_v<- overlay(co_kriged_surf,data_v)
126
  #Calculate RMSE and then krig the residuals....!
221 127
  
222
  for (j in 1:models){
223
    
224
    mod<-paste("krig",j,sep="")
225
    krmod<-get(mod)
226
    krig_val_s <- overlay(krmod,data_s)             #This overlays the kriged surface tmax and the location of weather stations
227
    krig_val_v <- overlay(krmod,data_v)             #This overlays the kriged surface tmax and the location of weather stations
228
    
229
    pred_krmod<-paste("pred_krmod",j,sep="")
230
    #Adding the results back into the original dataframes.
231
    data_s[[pred_krmod]]<-krig_val_s$var1.pred
232
    data_v[[pred_krmod]]<-krig_val_v$var1.pred  
233
    
234
    #Model assessment: RMSE and then krig the residuals....!
235
    
236
    res_mod_kr_s<- data_s$tmax - data_s[[pred_krmod]]           #Residuals from kriging training
237
    res_mod_kr_v<- data_v$tmax - data_v[[pred_krmod]]           #Residuals from kriging validation
238
    
239
    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
240
    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
241
    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   
242
    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
243
    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
244
    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
245
    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
246
    R2_mod_kr_v<- cor(data_v$tmax,data_v[[pred_krmod]],use="complete.obs")^2                  #R2, coef. of determinationFOR REGRESSION STEP 1: GAM
247
    #(nv-sum(is.na(res_mod2)))
248
    #Writing out results
249
    
250
    results_RMSE[i,1]<- dates[i]  #storing the interpolation dates in the first column
251
    results_RMSE[i,2]<- ns        #number of stations used in the training stage
252
    results_RMSE[i,3]<- "RMSE"
253
    results_RMSE[i,j+3]<- RMSE_mod_kr_v
254
    #results_RMSE_kr[i,3]<- res_mod_kr_v
255
    
256
    results_MAE[i,1]<- dates[i]  #storing the interpolation dates in the first column
257
    results_MAE[i,2]<- ns        #number of stations used in the training stage
258
    results_MAE[i,3]<- "MAE"
259
    results_MAE[i,j+3]<- MAE_mod_kr_v
260
    #results_RMSE_kr[i,3]<- res_mod_kr_v
261
    
262
    results_ME[i,1]<- dates[i]  #storing the interpolation dates in the first column
263
    results_ME[i,2]<- ns        #number of stations used in the training stage
264
    results_ME[i,3]<- "ME"
265
    results_ME[i,j+3]<- ME_mod_kr_v
266
    #results_RMSE_kr[i,3]<- res_mod_kr_v
267
    
268
    results_R2[i,1]<- dates[i]  #storing the interpolation dates in the first column
269
    results_R2[i,2]<- ns        #number of stations used in the training stage
270
    results_R2[i,3]<- "R2"
271
    results_R2[i,j+3]<- R2_mod_kr_v
272
    #results_RMSE_kr[i,3]<- res_mod_kr_v
273
    
274
    name3<-paste("res_kr_mod",j,sep="")
275
    #as.numeric(res_mod)
276
    #data_s[[name3]]<-res_mod_kr_s
277
    data_s[[name3]]<-as.numeric(res_mod_kr_s)
278
    #data_v[[name3]]<-res_mod_kr_v 
279
    data_v[[name3]]<-as.numeric(res_mod_kr_v)
280
    #Writing residuals from kriging
281
    
282
    #Saving kriged surface in raster images
283
    data_name<-paste("mod",j,"_",dates[[i]],sep="")
284
    krig_raster_name<-paste("krmod_",data_name,out_prefix,".tif", sep="")
285
    writeGDAL(krmod,fname=krig_raster_name, driver="GTiff", type="Float32",options ="INTERLEAVE=PIXEL")
286
    krig_raster_name<-paste("krmod_",data_name,out_prefix,".rst", sep="")
287
    writeRaster(raster(krmod), filename=krig_raster_name)  #Writing the data in a raster file format...(IDRISI)
288
    
289
    #krig_raster_name<-paste("Kriged_tmax_",data_name,out_prefix,".tif", sep="")
290
    #writeGDAL(tmax_krige,fname=krig_raster_name, driver="GTiff", type="Float32",options ="INTERLEAVE=PIXEL")
291
    #X11()
292
    #plot(raster(co_kriged_surf))
293
    #title(paste("Tmax cokriging for date ",dates[[i]],sep=""))
294
    #savePlot(paste("Cokriged_tmax",data_name,out_prefix,".png", sep=""), type="png")
295
    #dev.off()
296
    #X11()
297
    #plot(raster(tmax_krige))
298
    #title(paste("Tmax Kriging for date ",dates[[i]],sep=""))
299
    #savePlot(paste("Kriged_res_",data_name,out_prefix,".png", sep=""), type="png")
300
    #dev.off()
301
    #
302
    
303
  }
128
  res_mod1<- data_v$tmax - data_v$tmax_kr              #Residuals from kriging.
129
  res_mod2<- data_v$tmax - data_v$tmax_cokr                #Residuals from cokriging.
304 130
  
305
#   #Co-kriging only on the validation sites for faster computing
306
#   
307
#   cokrig1_dv<-predict(vm_g.fit,data_v)
308
#   cokrig1_ds<-predict(vm_g.fit,data_s)
309
# #   data_s$tmax_cokr<-cokrig1_ds$tmax.pred    
310
# #   data_v$tmax_cokr<-cokrig1_dv$tmax.pred
311
#   
312
#   #Calculate RMSE and then krig the residuals....!
313
#   
314
#   res_mod1<- data_v$tmax - data_v$tmax_kr              #Residuals from kriging.
315
#   res_mod2<- data_v$tmax - data_v$tmax_cokr            #Residuals from cokriging.
316
#   
317
#   RMSE_mod1 <- sqrt(sum(res_mod1^2,na.rm=TRUE)/(nv-sum(is.na(res_mod1))))                  #RMSE from kriged surface.
318
#   RMSE_mod2 <- sqrt(sum(res_mod2^2,na.rm=TRUE)/(nv-sum(is.na(res_mod2))))                  #RMSE from co-kriged surface.
319
#   #(nv-sum(is.na(res_mod2)))       
131
  RMSE_mod1 <- sqrt(sum(res_mod1^2,na.rm=TRUE)/(nv-sum(is.na(res_mod1))))                  #RMSE from kriged surface.
132
  RMSE_mod2 <- sqrt(sum(res_mod2^2,na.rm=TRUE)/(nv-sum(is.na(res_mod2))))                  #RMSE from co-kriged surface.
133
  #(nv-sum(is.na(res_mod2)))       
320 134

  
321 135
  #Saving the subset in a dataframe
322 136
  data_name<-paste("ghcn_v_",dates[[i]],sep="")
323 137
  assign(data_name,data_v)
324 138
  data_name<-paste("ghcn_s_",dates[[i]],sep="")
325 139
  assign(data_name,data_s)
326
    
327
#   results[i,1]<- dates[i]  #storing the interpolation dates in the first column
328
#   results[i,2]<- ns     #number of stations in training
329
#   results[i,3]<- RMSE_mod1
330
#   results[i,4]<- RMSE_mod2  
331
#   
332
#   results_mod_n[i,1]<-dates[i]
333
#   results_mod_n[i,2]<-(nv-sum(is.na(res_mod1)))
334
#   results_mod_n[i,3]<-(nv-sum(is.na(res_mod2)))
140
  
141
  krig_raster_name<-paste("coKriged_tmax_",data_name,out_prefix,".tif", sep="")
142
  writeGDAL(co_kriged_surf,fname=krig_raster_name, driver="GTiff", type="Float32",options ="INTERLEAVE=PIXEL")
143
  krig_raster_name<-paste("Kriged_tmax_",data_name,out_prefix,".tif", sep="")
144
  writeGDAL(tmax_krige,fname=krig_raster_name, driver="GTiff", type="Float32",options ="INTERLEAVE=PIXEL")
145
  X11()
146
  plot(raster(co_kriged_surf))
147
  title(paste("Tmax cokriging for date ",dates[[i]],sep=""))
148
  savePlot(paste("Cokriged_tmax",data_name,out_prefix,".png", sep=""), type="png")
149
  dev.off()
150
  X11()
151
  plot(raster(tmax_krige))
152
  title(paste("Tmax Kriging for date ",dates[[i]],sep=""))
153
  savePlot(paste("Kriged_res_",data_name,out_prefix,".png", sep=""), type="png")
154
  dev.off()
155
  
156
  results[i,1]<- dates[i]  #storing the interpolation dates in the first column
157
  results[i,2]<- ns     #number of stations in training
158
  results[i,3]<- RMSE_mod1
159
  results[i,4]<- RMSE_mod2  
160
  
161
  results_mod_n[i,1]<-dates[i]
162
  results_mod_n[i,2]<-(nv-sum(is.na(res_mod1)))
163
  results_mod_n[i,3]<-(nv-sum(is.na(res_mod2)))
335 164
  }
336 165

  
337 166
## Plotting and saving diagnostic measures
338
results_table_RMSE<-as.data.frame(results_RMSE)
339
results_table_MAE<-as.data.frame(results_MAE)
340
results_table_ME<-as.data.frame(results_ME)
341
results_table_R2<-as.data.frame(results_R2)
342

  
343
cname<-c("dates","ns","metric","krmod1", "krmod2","krmod3", "krmod4", "mkrod5")
344
colnames(results_table_RMSE)<-cname
345
colnames(results_table_MAE)<-cname
346
colnames(results_table_ME)<-cname
347
colnames(results_table_R2)<-cname
348

  
349

  
350
#Summary of diagnostic measures are stored in a data frame
351
tb_diagnostic1<-rbind(results_table_RMSE,results_table_MAE, results_table_ME, results_table_R2)   #
352
#tb_diagnostic1_kr<-rbind(results_table_RMSE_kr,results_table_MAE_kr, results_table_ME_kr, results_table_R2_kr)
353
#tb_diagnostic2<-rbind(results_table_AIC,results_table_GCV, results_table_DEV,results_table_RMSE_f)
354

  
355
write.table(tb_diagnostic1, file= paste(path,"/","results_GAM_Assessment_measure1",out_prefix,".txt",sep=""), sep=",")
356
#write.table(tb_diagnostic1_kr, file= paste(path,"/","results_GAM_Assessment_measure1_kr_",out_prefix,".txt",sep=""), sep=",")
357
#write.table(tb_diagnostic2, file= paste(path,"/","results_GAM_Assessment_measure2_",out_prefix,".txt",sep=""), sep=",")
167
results_num <-results
168
mode(results_num)<- "numeric"
169
# Make it numeric first
170
# Now turn it into a data.frame...
358 171

  
172
results_table<-as.data.frame(results_num)
173
colnames(results_table)<-c("dates","ns","RMSE")
359 174

  
360
#### END OF SCRIPT #####
175
write.csv(results_table, file= paste(path,"/","results_Kriging_Assessment",out_prefix,".txt",sep=""))

Also available in: Unified diff