1 |
20a4e4bb
|
Benoit Parmentier
|
################## 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 |
3b657271
|
Benoit Parmentier
|
#DATE: 07/15/2012 #
|
15 |
20a4e4bb
|
Benoit Parmentier
|
#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
|
26 |
7da7872a
|
Benoit Parmentier
|
library(spgwr)
|
27 |
|
|
library(gpclib)
|
28 |
|
|
library(maptools)
|
29 |
3be0f72b
|
Benoit Parmentier
|
library(graphics)
|
30 |
20a4e4bb
|
Benoit Parmentier
|
|
31 |
7da7872a
|
Benoit Parmentier
|
###Parameters and arguments
|
32 |
|
|
|
33 |
20a4e4bb
|
Benoit Parmentier
|
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 |
3b657271
|
Benoit Parmentier
|
infile5<-"mean_day244_rescaled.rst"
|
39 |
|
|
inlistf<-"list_files_05032012.txt" #Stack of images containing the Covariates
|
40 |
20a4e4bb
|
Benoit Parmentier
|
|
41 |
3b657271
|
Benoit Parmentier
|
path<-"/home/parmentier/Data/IPLANT_project/data_Oregon_stations_07152012" #Jupiter LOCATION on Atlas for kriging
|
42 |
20a4e4bb
|
Benoit Parmentier
|
#path<-"H:/Data/IPLANT_project/data_Oregon_stations" #Jupiter Location on XANDERS
|
43 |
3b657271
|
Benoit Parmentier
|
|
44 |
20a4e4bb
|
Benoit Parmentier
|
setwd(path)
|
45 |
|
|
prop<-0.3 #Proportion of testing retained for validation
|
46 |
|
|
seed_number<- 100 #Seed number for random sampling
|
47 |
3b657271
|
Benoit Parmentier
|
models<-7 #Number of kriging model
|
48 |
20a4e4bb
|
Benoit Parmentier
|
out_prefix<-"_07132012_auto_krig_" #User defined output prefix
|
49 |
3be0f72b
|
Benoit Parmentier
|
|
50 |
|
|
###STEP 1 DATA PREPARATION AND PROCESSING#####
|
51 |
7da7872a
|
Benoit Parmentier
|
|
52 |
20a4e4bb
|
Benoit Parmentier
|
###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 |
3b657271
|
Benoit Parmentier
|
##Extracting the variables values from the raster files
|
62 |
|
|
|
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
|
67 |
|
|
|
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
|
71 |
|
|
|
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
|
86 |
|
|
|
87 |
|
|
### adding var
|
88 |
20a4e4bb
|
Benoit Parmentier
|
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.
|
92 |
|
|
|
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
|
97 |
|
|
|
98 |
|
|
set.seed(seed_number) #Using a seed number allow results based on random number to be compared...
|
99 |
|
|
|
100 |
|
|
dates <-readLines(paste(path,"/",infile2, sep=""))
|
101 |
|
|
LST_dates <-readLines(paste(path,"/",infile3, sep=""))
|
102 |
3b657271
|
Benoit Parmentier
|
#models <-readLines(paste(path,"/",infile4, sep=""))
|
103 |
|
|
|
104 |
|
|
#models<-5
|
105 |
20a4e4bb
|
Benoit Parmentier
|
#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)
|
108 |
|
|
|
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 |
3b657271
|
Benoit Parmentier
|
#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)
|
122 |
|
|
ghcn<-ghcn_test2
|
123 |
|
|
#coords<- ghcn[,c('x_OR83M','y_OR83M')]
|
124 |
7da7872a
|
Benoit Parmentier
|
|
125 |
|
|
|
126 |
|
|
|
127 |
3be0f72b
|
Benoit Parmentier
|
###CREATING SUBSETS BY INPUT DATES AND SAMPLING
|
128 |
|
|
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 |
|
|
|
130 |
88248d6c
|
Benoit Parmentier
|
for(i in 1:length(dates)){ # start of the for loop #1
|
131 |
|
|
#i<-3 #Date 10 is used to test kriging
|
132 |
3b657271
|
Benoit Parmentier
|
|
133 |
|
|
#This allows to change only one name of the
|
134 |
|
|
|
135 |
328528e2
|
Benoit Parmentier
|
date<-strptime(dates[i], "%Y%m%d")
|
136 |
|
|
month<-strftime(date, "%m")
|
137 |
|
|
LST_month<-paste("mm_",month,sep="")
|
138 |
3b657271
|
Benoit Parmentier
|
#adding to SpatialGridDataFrame
|
139 |
|
|
#t<-s_sgdf[,match(LST_month, names(s_sgdf))]
|
140 |
|
|
#s_sgdf$LST<-s_sgdf[c(LST_month)]
|
141 |
328528e2
|
Benoit Parmentier
|
mod <-ghcn.subsets[[i]][,match(LST_month, names(ghcn.subsets[[i]]))]
|
142 |
|
|
ghcn.subsets[[i]]$LST <-mod[[1]]
|
143 |
|
|
|
144 |
88248d6c
|
Benoit Parmentier
|
n<-nrow(ghcn.subsets[[i]])
|
145 |
|
|
ns<-n-round(n*prop) #Create a sample from the data frame with 70% of the rows
|
146 |
|
|
nv<-n-ns #create a sample for validation with prop of the rows
|
147 |
|
|
ind.training <- sample(nrow(ghcn.subsets[[i]]), size=ns, replace=FALSE) #This selects the index position for 70% of the rows taken randomly
|
148 |
|
|
ind.testing <- setdiff(1:nrow(ghcn.subsets[[i]]), ind.training) #This selects the index position for testing subset stations.
|
149 |
|
|
data_s <- ghcn.subsets[[i]][ind.training, ]
|
150 |
|
|
data_v <- ghcn.subsets[[i]][ind.testing, ]
|
151 |
|
|
|
152 |
3b657271
|
Benoit Parmentier
|
|
153 |
|
|
###BEFORE Kringing the data object must be transformed to SDF
|
154 |
|
|
|
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..
|
161 |
|
|
|
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 |
88248d6c
|
Benoit Parmentier
|
###STEP 2 KRIGING###
|
172 |
|
|
|
173 |
|
|
#Kriging tmax
|
174 |
|
|
|
175 |
20a4e4bb
|
Benoit Parmentier
|
# 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.
|
181 |
88248d6c
|
Benoit Parmentier
|
|
182 |
3b657271
|
Benoit Parmentier
|
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)
|
191 |
88248d6c
|
Benoit Parmentier
|
|
192 |
20a4e4bb
|
Benoit Parmentier
|
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 |
3b657271
|
Benoit Parmentier
|
krig6<-krmod6$krige_output #Extracting Spatial Grid Data frame
|
198 |
|
|
krig7<-krmod7$krige_output
|
199 |
|
|
#krig8<-krmod8$krige_outpu
|
200 |
|
|
#krig9<-krmod9$krige_output
|
201 |
|
|
|
202 |
20a4e4bb
|
Benoit Parmentier
|
#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)
|
209 |
88248d6c
|
Benoit Parmentier
|
|
210 |
20a4e4bb
|
Benoit Parmentier
|
# 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.
|
216 |
88248d6c
|
Benoit Parmentier
|
|
217 |
20a4e4bb
|
Benoit Parmentier
|
#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)
|
221 |
88248d6c
|
Benoit Parmentier
|
|
222 |
20a4e4bb
|
Benoit Parmentier
|
for (j in 1:models){
|
223 |
|
|
|
224 |
3b657271
|
Benoit Parmentier
|
mod<-paste("krig",j,sep="")
|
225 |
|
|
krmod<-get(mod)
|
226 |
20a4e4bb
|
Benoit Parmentier
|
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 |
3b657271
|
Benoit Parmentier
|
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 |
20a4e4bb
|
Benoit Parmentier
|
#(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 |
3b657271
|
Benoit Parmentier
|
#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 |
20a4e4bb
|
Benoit Parmentier
|
}
|
304 |
88248d6c
|
Benoit Parmentier
|
|
305 |
20a4e4bb
|
Benoit Parmentier
|
# #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)))
|
320 |
88248d6c
|
Benoit Parmentier
|
|
321 |
|
|
#Saving the subset in a dataframe
|
322 |
|
|
data_name<-paste("ghcn_v_",dates[[i]],sep="")
|
323 |
|
|
assign(data_name,data_v)
|
324 |
|
|
data_name<-paste("ghcn_s_",dates[[i]],sep="")
|
325 |
|
|
assign(data_name,data_s)
|
326 |
3b657271
|
Benoit Parmentier
|
|
327 |
20a4e4bb
|
Benoit Parmentier
|
# 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)))
|
335 |
88248d6c
|
Benoit Parmentier
|
}
|
336 |
|
|
|
337 |
|
|
## Plotting and saving diagnostic measures
|
338 |
20a4e4bb
|
Benoit Parmentier
|
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=",")
|
358 |
88248d6c
|
Benoit Parmentier
|
|
359 |
|
|
|
360 |
20a4e4bb
|
Benoit Parmentier
|
#### END OF SCRIPT #####
|