Revision 328689cf
Added by Benoit Parmentier over 12 years ago
climate/research/oregon/interpolation/GAM_LST_month_Kriging.R | ||
---|---|---|
1 |
################## Interpolation of Tmax for 10 dates. ####################################### |
|
2 |
########################### TWO-STAGE REGRESSION ############################################### |
|
3 |
#This script interpolates station values for the Oregon case study using a two-stage regression. # |
|
4 |
#For each input dates, it performs 1) Step 1: General Additive Model (GAM) # |
|
5 |
# 2) Step 2: Kriging on residuals from step 1 # |
|
6 |
# # |
|
7 |
#The script uses LST monthly averages as input variables and loads the station data # |
|
8 |
#from a shape file with projection information. # |
|
9 |
#Note that this program: # |
|
10 |
#1)assumes that the shape file is in the current working. # |
|
11 |
#2)relevant variables were extracted from raster images before performing the regressions # |
|
12 |
# and stored shapefile # |
|
13 |
#This scripts predicts tmax using ing GAM and LST derived from MOD11A1.Interactions terms are # |
|
14 |
#also included and assessed using the RMSE,MAE,ME and R2 from validation dataset. # |
|
15 |
#There are #10 dates used for the GAM interpolation. The dates must be provided as a textfile. # |
|
16 |
#AUTHOR: Benoit Parmentier # |
|
17 |
#DATE: 05/31/212 # |
|
18 |
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#364-- # |
|
19 |
################################################################################################## |
|
20 |
|
|
21 |
###Loading R library and packages |
|
22 |
library(gtools) # loading some useful tools |
|
23 |
library(mgcv) # GAM package by Simon Wood |
|
24 |
library(sp) # Spatial pacakge with class definition by Bivand et al. |
|
25 |
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al. |
|
26 |
library(rgdal) # GDAL wrapper for R, spatial utilities |
|
27 |
library(gstat) # Kriging and co-kriging by Pebesma et al. |
|
28 |
|
|
29 |
###Parameters and arguments |
|
30 |
|
|
31 |
infile1<- "ghcn_or_tmax_b_04142012_OR83M.shp" #GHCN shapefile containing variables |
|
32 |
infile2<-"list_10_dates_04212012.txt" #List of 10 dates for the regression |
|
33 |
#infile2<-"list_365_dates_04212012.txt" |
|
34 |
infile3<-"LST_dates_var_names.txt" #LST dates name |
|
35 |
infile4<-"models_interpolation_05142012.txt" #Interpolation model names |
|
36 |
infile5<-"mean_day244_rescaled.rst" #Raster or grid for the locations of predictions |
|
37 |
|
|
38 |
path<-"/data/computer/parmentier/Data/IPLANT_project/data_Oregon_stations" #Jupiter LOCATION on EOS |
|
39 |
#path<-"H:/Data/IPLANT_project/data_Oregon_stations" #Jupiter Location on XANDERS |
|
40 |
setwd(path) |
|
41 |
prop<-0.3 #Proportion of testing retained for validation |
|
42 |
seed_number<- 100 #Seed number for random sampling |
|
43 |
out_prefix<-"_05312012_2d_Kr_LST" #User defined output prefix |
|
44 |
|
|
45 |
############ START OF THE SCRIPT ################## |
|
46 |
|
|
47 |
###Reading the station data and setting up for models' comparison |
|
48 |
filename<-sub(".shp","",infile1) #Removing the extension from file. |
|
49 |
ghcn<-readOGR(".", filename) #reading shapefile |
|
50 |
|
|
51 |
CRS<-proj4string(ghcn) #Storing projectionminformation (ellipsoid, datum,etc.) |
|
52 |
|
|
53 |
mean_LST<- readGDAL(infile5) #Reading the whole raster in memory. This provides a grid for kriging |
|
54 |
proj4string(mean_LST)<-CRS #Assigning coordinate information to prediction grid. |
|
55 |
|
|
56 |
ghcn = transform(ghcn,Northness = cos(ASPECT)) #Adding a variable to the dataframe |
|
57 |
ghcn = transform(ghcn,Eastness = sin(ASPECT)) #adding variable to the dataframe. |
|
58 |
ghcn = transform(ghcn,Northness_w = sin(slope)*cos(ASPECT)) #Adding a variable to the dataframe |
|
59 |
ghcn = transform(ghcn,Eastness_w = sin(slope)*sin(ASPECT)) #adding variable to the dataframe. |
|
60 |
|
|
61 |
set.seed(seed_number) #Using a seed number allow results based on random number to be compared... |
|
62 |
|
|
63 |
dates <-readLines(paste(path,"/",infile2, sep="")) |
|
64 |
LST_dates <-readLines(paste(path,"/",infile3, sep="")) |
|
65 |
models <-readLines(paste(path,"/",infile4, sep="")) |
|
66 |
|
|
67 |
#Model assessment: specific diagnostic/metrics for GAM |
|
68 |
results_AIC<- matrix(1,length(dates),length(models)+3) |
|
69 |
results_GCV<- matrix(1,length(dates),length(models)+3) |
|
70 |
results_DEV<- matrix(1,length(dates),length(models)+3) |
|
71 |
results_RMSE_f<- matrix(1,length(dates),length(models)+3) |
|
72 |
|
|
73 |
#Model assessment: general diagnostic/metrics |
|
74 |
results_RMSE <- matrix(1,length(dates),length(models)+3) |
|
75 |
results_RMSE_kr <- matrix(1,length(dates),length(models)+3) |
|
76 |
results_MAE <- matrix(1,length(dates),length(models)+3) |
|
77 |
results_MAE_kr <- matrix(1,length(dates),length(models)+3) |
|
78 |
results_ME <- matrix(1,length(dates),length(models)+3) |
|
79 |
results_ME_kr <- matrix(1,length(dates),length(models)+3) #ME corresponds to the bias |
|
80 |
results_R2 <- matrix(1,length(dates),length(models)+3) #Coef. of determination for the validation dataset |
|
81 |
results_R2_kr <- matrix(1,length(dates),length(models)+3) |
|
82 |
results_RMSE_f<- matrix(1,length(dates),length(models)+3) #RMSE fit, RMSE for the training dataset |
|
83 |
results_RMSE_f_kr<- matrix(1,length(dates),length(models)+3) |
|
84 |
|
|
85 |
#Tracking relationship between LST AND LC |
|
86 |
cor_LST_LC1<-matrix(1,10,1) #correlation LST-LC1 |
|
87 |
cor_LST_LC3<-matrix(1,10,1) #correlation LST-LC3 |
|
88 |
cor_LST_tmax<-matrix(1,10,1) #correlation LST-tmax |
|
89 |
|
|
90 |
#Screening for bad values |
|
91 |
ghcn_all<-ghcn |
|
92 |
ghcn_test<-subset(ghcn,ghcn$tmax>-150 & ghcn$tmax<400) |
|
93 |
ghcn_test2<-subset(ghcn_test,ghcn_test$ELEV_SRTM>0) |
|
94 |
ghcn<-ghcn_test2 |
|
95 |
#coords<- ghcn[,c('x_OR83M','y_OR83M')] |
|
96 |
|
|
97 |
month_var<-c("mm_01","mm_02","mm_03","mm_04","mm_05","mm_06","mm_07","mm_08","mm_09", "mm_10", "mm_11", "mm_12") |
|
98 |
ghcn.subsets <-lapply(dates, function(d) subset(ghcn, date==d)) #this creates a list of 10 or 365 subsets dataset based on dates |
|
99 |
|
|
100 |
## looping through the dates...this is the main part of the code |
|
101 |
#i=1 #for debugging |
|
102 |
#j=1 #for debugging |
|
103 |
for(i in 1:length(dates)){ # start of the for loop #1 |
|
104 |
|
|
105 |
date<-strptime(dates[i], "%Y%m%d") # interpolation date being processed |
|
106 |
month<-strftime(date, "%m") # current month of the date being processed |
|
107 |
LST_month<-paste("mm_",month,sep="") # name of LST month to be matched |
|
108 |
|
|
109 |
###Regression part 1: Creating a validation dataset by creating training and testing datasets |
|
110 |
|
|
111 |
mod_LST <-ghcn.subsets[[i]][,match(LST_month, names(ghcn.subsets[[i]]))] #Match interpolation date and monthly LST average |
|
112 |
ghcn.subsets[[i]] = transform(ghcn.subsets[[i]],LST = mod_LST) #Add the variable LST to the subset dataset |
|
113 |
n<-nrow(ghcn.subsets[[i]]) |
|
114 |
ns<-n-round(n*prop) #Create a sample from the data frame with 70% of the rows |
|
115 |
nv<-n-ns #create a sample for validation with prop of the rows |
|
116 |
ind.training <- sample(nrow(ghcn.subsets[[i]]), size=ns, replace=FALSE) #This selects the index position for 70% of the rows taken randomly |
|
117 |
ind.testing <- setdiff(1:nrow(ghcn.subsets[[i]]), ind.training) |
|
118 |
data_s <- ghcn.subsets[[i]][ind.training, ] #Training dataset currently used in the modeling |
|
119 |
data_v <- ghcn.subsets[[i]][ind.testing, ] #Testing/validation dataset |
|
120 |
|
|
121 |
####Regression part 2: GAM models (REGRESSION STEP1) |
|
122 |
|
|
123 |
#Model can be changed without affecting the script |
|
124 |
mod1<- gam(tmax~ s(lat) + s (lon) + s (ELEV_SRTM), data=data_s) |
|
125 |
mod2<- gam(tmax~ s(lat,lon,ELEV_SRTM), data=data_s) |
|
126 |
mod3<- gam(tmax~ s(lat) + s (lon) + s (ELEV_SRTM) + s (Northness)+ s (Eastness) + s(DISTOC), data=data_s) |
|
127 |
mod4<- gam(tmax~ s(lat) + s (lon) + s(ELEV_SRTM) + s(Northness) + s (Eastness) + s(DISTOC) + s(LST), data=data_s) |
|
128 |
mod5<- gam(tmax~ s(lat,lon) +s(ELEV_SRTM) + s(Northness,Eastness) + s(DISTOC) + s(LST), data=data_s) |
|
129 |
mod6<- gam(tmax~ s(lat,lon) +s(ELEV_SRTM) + s(Northness,Eastness) + s(DISTOC) + s(LST,LC1), data=data_s) |
|
130 |
mod7<- gam(tmax~ s(lat,lon) +s(ELEV_SRTM) + s(Northness,Eastness) + s(DISTOC) + s(LST,LC3), data=data_s) |
|
131 |
mod8<- gam(tmax~ s(lat,lon) +s(ELEV_SRTM) + s(Northness,Eastness) + s(DISTOC) + s(LST) + s(LC1), data=data_s) |
|
132 |
|
|
133 |
####Regression part 3: Calculating and storing diagnostic measures |
|
134 |
#listmod can be created and looped over. In this case we loop around the GAM objects in memory... |
|
135 |
|
|
136 |
for (j in 1:length(models)){ |
|
137 |
|
|
138 |
##Model assessment: specific diagnostic/metrics for GAM |
|
139 |
|
|
140 |
name<-paste("mod",j,sep="") #modj is the name of The "j" model (mod1 if j=1) |
|
141 |
mod<-get(name) #accessing GAM model ojbect "j" |
|
142 |
results_AIC[i,1]<- dates[i] #storing the interpolation dates in the first column |
|
143 |
results_AIC[i,2]<- ns #number of stations used in the training stage |
|
144 |
results_AIC[i,3]<- "AIC" |
|
145 |
results_AIC[i,j+3]<- AIC (mod) |
|
146 |
|
|
147 |
results_GCV[i,1]<- dates[i] #storing the interpolation dates in the first column |
|
148 |
results_GCV[i,2]<- ns #number of stations used in the training |
|
149 |
results_GCV[i,3]<- "GCV" |
|
150 |
results_GCV[i,j+3]<- mod$gcv.ubre |
|
151 |
|
|
152 |
results_DEV[i,1]<- dates[i] #storing the interpolation dates in the first column |
|
153 |
results_DEV[i,2]<- ns #number of stations used in the training stage |
|
154 |
results_DEV[i,3]<- "DEV" |
|
155 |
results_DEV[i,j+3]<- mod$deviance |
|
156 |
|
|
157 |
results_RMSE_f[i,1]<- dates[i] #storing the interpolation dates in the first column |
|
158 |
results_RMSE_f[i,2]<- ns #number of stations used in the training stage |
|
159 |
results_RMSE_f[i,3]<- "RSME" |
|
160 |
results_RMSE_f[i,j+3]<- sqrt(sum((mod$residuals)^2)/nv) |
|
161 |
|
|
162 |
##Model assessment: general diagnostic/metrics |
|
163 |
##validation: using the testing data |
|
164 |
|
|
165 |
#Automate this using a data frame of size?? |
|
166 |
y_mod<- predict(mod, newdata=data_v, se.fit = TRUE) #Using the coeff to predict new values. |
|
167 |
res_mod<- data_v$tmax - y_mod$fit #Residuals for the model |
|
168 |
RMSE_mod <- sqrt(sum(res_mod^2)/nv) #RMSE FOR REGRESSION STEP 1: GAM |
|
169 |
MAE_mod<- sum(abs(res_mod))/nv #MAE, Mean abs. Error FOR REGRESSION STEP 1: GAM |
|
170 |
ME_mod<- sum(res_mod)/nv #ME, Mean Error or bias FOR REGRESSION STEP 1: GAM |
|
171 |
R2_mod<- cor(data_v$tmax,y_mod$fit)^2 #R2, coef. of var FOR REGRESSION STEP 1: GAM |
|
172 |
|
|
173 |
results_RMSE[i,1]<- dates[i] #storing the interpolation dates in the first column |
|
174 |
results_RMSE[i,2]<- ns #number of stations used in the training stage |
|
175 |
results_RMSE[i,3]<- "RMSE" |
|
176 |
results_RMSE[i,j+3]<- RMSE_mod #Storing RMSE for the model j |
|
177 |
results_MAE[i,1]<- dates[i] #storing the interpolation dates in the first column |
|
178 |
results_MAE[i,2]<- ns #number of stations used in the training stage |
|
179 |
results_MAE[i,3]<- "MAE" |
|
180 |
results_MAE[i,j+3]<- MAE_mod #Storing MAE for the model j |
|
181 |
results_ME[i,1]<- dates[i] #storing the interpolation dates in the first column |
|
182 |
results_ME[i,2]<- ns #number of stations used in the training stage |
|
183 |
results_ME[i,3]<- "ME" |
|
184 |
results_ME[i,j+3]<- ME_mod #Storing ME for the model j |
|
185 |
results_R2[i,1]<- dates[i] #storing the interpolation dates in the first column |
|
186 |
results_R2[i,2]<- ns #number of stations used in the training stage |
|
187 |
results_R2[i,3]<- "R2" |
|
188 |
results_R2[i,j+3]<- R2_mod #Storing R2 for the model j |
|
189 |
|
|
190 |
#Saving residuals and prediction in the dataframes: tmax predicted from GAM |
|
191 |
pred<-paste("pred_mod",j,sep="") |
|
192 |
data_v[[pred]]<-as.numeric(y_mod$fit) |
|
193 |
data_s[[pred]]<-as.numeric(mod$fit) #Storing model fit values (predicted on training sample) |
|
194 |
|
|
195 |
name2<-paste("res_mod",j,sep="") |
|
196 |
data_v[[name2]]<-as.numeric(res_mod) |
|
197 |
data_s[[name2]]<-as.numeric(mod$residuals) |
|
198 |
#end of loop calculating RMSE |
|
199 |
|
|
200 |
} |
|
201 |
|
|
202 |
###BEFORE Kringing the data object must be transformed to SDF |
|
203 |
|
|
204 |
coords<- data_v[,c('x_OR83M','y_OR83M')] |
|
205 |
coordinates(data_v)<-coords |
|
206 |
proj4string(data_v)<-CRS #Need to assign coordinates... |
|
207 |
coords<- data_s[,c('x_OR83M','y_OR83M')] |
|
208 |
coordinates(data_s)<-coords |
|
209 |
proj4string(data_s)<-CRS #Need to assign coordinates.. |
|
210 |
|
|
211 |
#KRIGING ON GAM RESIDUALS: REGRESSION STEP2 |
|
212 |
|
|
213 |
for (j in 1:length(models)){ |
|
214 |
name<-paste("res_mod",j,sep="") |
|
215 |
data_s$residuals<-data_s[[name]] |
|
216 |
X11() |
|
217 |
hscat(residuals~1,data_s,(0:9)*20000) # 9 lag classes with 20,000m width |
|
218 |
v<-variogram(residuals~1, data_s) |
|
219 |
plot(v) # This plot may be saved at a later stage... |
|
220 |
dev.off() |
|
221 |
v.fit<-fit.variogram(v,vgm(1,"Sph", 150000,1)) |
|
222 |
res_krige<-krige(residuals~1, data_s,mean_LST, v.fit)#mean_LST provides the data grid/raster image for the kriging locations. |
|
223 |
|
|
224 |
res_krig1_s <- overlay(res_krige,data_s) #This overlays the kriged surface tmax and the location of weather stations |
|
225 |
res_krig1_v <- overlay(res_krige,data_v) #This overlays the kriged surface tmax and the location of weather stations |
|
226 |
|
|
227 |
name2<-paste("pred_kr_mod",j,sep="") |
|
228 |
#Adding the results back into the original dataframes. |
|
229 |
data_s[[name2]]<-res_krig1_s$var1.pred |
|
230 |
data_v[[name2]]<-res_krig1_v$var1.pred |
|
231 |
|
|
232 |
#NEED TO ADD IT BACK TO THE PREDICTION FROM GAM |
|
233 |
gam_kr<-paste("pred_gam_kr",j,sep="") |
|
234 |
pred_gam<-paste("pred_mod",j,sep="") |
|
235 |
data_s[[gam_kr]]<-data_s[[pred_gam]]+ data_s[[name2]] |
|
236 |
data_v[[gam_kr]]<-data_v[[pred_gam]]+ data_v[[name2]] |
|
237 |
|
|
238 |
#Model assessment: RMSE and then krig the residuals....! |
|
239 |
|
|
240 |
res_mod_kr_s<- data_s$tmax - data_s[[gam_kr]] #Residuals from kriging training |
|
241 |
res_mod_kr_v<- data_v$tmax - data_v[[gam_kr]] #Residuals from kriging validation |
|
242 |
|
|
243 |
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 |
|
244 |
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 |
|
245 |
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 |
|
246 |
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 |
|
247 |
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 |
|
248 |
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 |
|
249 |
R2_mod_kr_s<- cor(data_s$tmax,data_s[[gam_kr]],use="complete.obs")^2 #R2, coef. of determination FOR REGRESSION STEP 1: GAM |
|
250 |
R2_mod_kr_v<- cor(data_v$tmax,data_v[[gam_kr]],use="complete.obs")^2 #R2, coef. of determinationFOR REGRESSION STEP 1: GAM |
|
251 |
#(nv-sum(is.na(res_mod2))) |
|
252 |
#Writing out results |
|
253 |
|
|
254 |
results_RMSE_kr[i,1]<- dates[i] #storing the interpolation dates in the first column |
|
255 |
results_RMSE_kr[i,2]<- ns #number of stations used in the training stage |
|
256 |
results_RMSE_kr[i,3]<- "RMSE" |
|
257 |
results_RMSE_kr[i,j+3]<- RMSE_mod_kr_v |
|
258 |
#results_RMSE_kr[i,3]<- res_mod_kr_v |
|
259 |
|
|
260 |
results_MAE_kr[i,1]<- dates[i] #storing the interpolation dates in the first column |
|
261 |
results_MAE_kr[i,2]<- ns #number of stations used in the training stage |
|
262 |
results_MAE_kr[i,3]<- "MAE" |
|
263 |
results_MAE_kr[i,j+3]<- MAE_mod_kr_v |
|
264 |
#results_RMSE_kr[i,3]<- res_mod_kr_v |
|
265 |
|
|
266 |
results_ME_kr[i,1]<- dates[i] #storing the interpolation dates in the first column |
|
267 |
results_ME_kr[i,2]<- ns #number of stations used in the training stage |
|
268 |
results_ME_kr[i,3]<- "ME" |
|
269 |
results_ME_kr[i,j+3]<- ME_mod_kr_v |
|
270 |
#results_RMSE_kr[i,3]<- res_mod_kr_v |
|
271 |
|
|
272 |
results_R2_kr[i,1]<- dates[i] #storing the interpolation dates in the first column |
|
273 |
results_R2_kr[i,2]<- ns #number of stations used in the training stage |
|
274 |
results_R2_kr[i,3]<- "R2" |
|
275 |
results_R2_kr[i,j+3]<- R2_mod_kr_v |
|
276 |
#results_RMSE_kr[i,3]<- res_mod_kr_v |
|
277 |
|
|
278 |
name3<-paste("res_kr_mod",j,sep="") |
|
279 |
#as.numeric(res_mod) |
|
280 |
#data_s[[name3]]<-res_mod_kr_s |
|
281 |
data_s[[name3]]<-as.numeric(res_mod_kr_s) |
|
282 |
#data_v[[name3]]<-res_mod_kr_v |
|
283 |
data_v[[name3]]<-as.numeric(res_mod_kr_v) |
|
284 |
#Writing residuals from kriging |
|
285 |
|
|
286 |
} |
|
287 |
|
|
288 |
###SAVING THE DATA FRAME IN SHAPEFILES AND TEXTFILES |
|
289 |
|
|
290 |
data_name<-paste("ghcn_v_",out_prefix,"_",dates[[i]],sep="") |
|
291 |
assign(data_name,data_v) |
|
292 |
#write.table(data_v, file= paste(path,"/",data_name,".txt",sep=""), sep=" ") |
|
293 |
#write out a new shapefile (including .prj component) |
|
294 |
#outfile<-sub(".shp","",data_name) #Removing extension if it is present |
|
295 |
#writeOGR(data_v,".", outfile, driver ="ESRI Shapefile") |
|
296 |
|
|
297 |
data_name<-paste("ghcn_s_",out_prefix,"_",dates[[i]],sep="") |
|
298 |
assign(data_name,data_s) |
|
299 |
#write.table(data_s, file= paste(path,"/",data_name,".txt",sep=""), sep=" ") |
|
300 |
#outfile<-sub(".shp","",data_name) #Removing extension if it is present |
|
301 |
#writeOGR(data_s,".", outfile, driver ="ESRI Shapefile") |
|
302 |
|
|
303 |
# end of the for loop1 |
|
304 |
|
|
305 |
} |
|
306 |
|
|
307 |
## Plotting and saving diagnostic measures |
|
308 |
|
|
309 |
#Specific diagnostic measures related to the testing datasets |
|
310 |
results_table_AIC<-as.data.frame(results_AIC) |
|
311 |
results_table_GCV<-as.data.frame(results_GCV) |
|
312 |
results_table_DEV<-as.data.frame(results_DEV) |
|
313 |
results_table_RMSE_f<-as.data.frame(results_RMSE_f) |
|
314 |
|
|
315 |
results_table_RMSE<-as.data.frame(results_RMSE) |
|
316 |
results_table_MAE<-as.data.frame(results_MAE) |
|
317 |
results_table_ME<-as.data.frame(results_ME) |
|
318 |
results_table_R2<-as.data.frame(results_R2) |
|
319 |
|
|
320 |
cname<-c("dates","ns","metric","mod1", "mod2","mod3", "mod4", "mod5", "mod6", "mod7","mod8") |
|
321 |
colnames(results_table_RMSE)<-cname |
|
322 |
colnames(results_table_MAE)<-cname |
|
323 |
colnames(results_table_ME)<-cname |
|
324 |
colnames(results_table_R2)<-cname |
|
325 |
#Specific diagnostic measures |
|
326 |
colnames(results_table_AIC)<-cname |
|
327 |
colnames(results_table_GCV)<-cname |
|
328 |
colnames(results_table_DEV)<-cname |
|
329 |
colnames(results_table_RMSE_f)<-cname |
|
330 |
|
|
331 |
#General diagnostic measures |
|
332 |
|
|
333 |
results_table_RMSE_kr<-as.data.frame(results_RMSE_kr) |
|
334 |
results_table_MAE_kr<-as.data.frame(results_MAE_kr) |
|
335 |
results_table_ME_kr<-as.data.frame(results_ME_kr) |
|
336 |
results_table_R2_kr<-as.data.frame(results_R2_kr) |
|
337 |
|
|
338 |
cname<-c("dates","ns","metric","mod1k", "mod2k","mod3k", "mod4k", "mod5k", "mod6k", "mod7k","mod8k") |
|
339 |
colnames(results_table_RMSE_kr)<-cname |
|
340 |
colnames(results_table_MAE_kr)<-cname |
|
341 |
colnames(results_table_ME_kr)<-cname |
|
342 |
colnames(results_table_R2_kr)<-cname |
|
343 |
|
|
344 |
#Summary of diagnostic measures are stored in a data frame |
|
345 |
tb_diagnostic1<-rbind(results_table_RMSE,results_table_MAE, results_table_ME, results_table_R2) # |
|
346 |
tb_diagnostic1_kr<-rbind(results_table_RMSE_kr,results_table_MAE_kr, results_table_ME_kr, results_table_R2_kr) |
|
347 |
tb_diagnostic2<-rbind(results_table_AIC,results_table_GCV, results_table_DEV,results_table_RMSE_f) |
|
348 |
|
|
349 |
write.table(tb_diagnostic1, file= paste(path,"/","results_GAM_Assessment_measure1",out_prefix,".txt",sep=""), sep=",") |
|
350 |
write.table(tb_diagnostic1_kr, file= paste(path,"/","results_GAM_Assessment_measure1_kr_",out_prefix,".txt",sep=""), sep=",") |
|
351 |
write.table(tb_diagnostic2, file= paste(path,"/","results_GAM_Assessment_measure2_",out_prefix,".txt",sep=""), sep=",") |
|
352 |
|
|
353 |
# ##Visualization of results## |
|
354 |
# |
|
355 |
# for(i in 1:length(dates)){ |
|
356 |
# X11() |
|
357 |
# RMSE_kr<-results_table_RMSE_kr[i,] |
|
358 |
# RMSE_ga<-results_table_RMSE[i,] |
|
359 |
# |
|
360 |
# RMSE_kr<-RMSE_kr[,1:length(models)+2] |
|
361 |
# RMSE_ga<-RMSE_ga[,1:length(models)+2] |
|
362 |
# colnames(RMSE_kr)<-names(RMSE_ga) |
|
363 |
# height<-rbind(RMSE_ga,RMSE_kr) |
|
364 |
# rownames(height)<-c("GAM","GAM_KR") |
|
365 |
# height<-as.matrix(height) |
|
366 |
# barplot(height,ylim=c(14,36),ylab="RMSE in tenth deg C",beside=TRUE, |
|
367 |
# legend.text=rownames(height), |
|
368 |
# args.legend=list(x="topright"), |
|
369 |
# main=paste("RMSE for date ",dates[i], sep="")) |
|
370 |
# savePlot(paste("Barplot_results_RMSE_GAM_KR_",dates[i],out_prefix,".png", sep=""), type="png") |
|
371 |
# dev.off() |
|
372 |
# } |
|
373 |
# |
|
374 |
# r1<-(results_table_RMSE[,3:10]) #selecting only the columns related to models and method 1 |
|
375 |
# r2<-(results_table_RMSE_kr[,3:10]) #selecting only the columns related to models and method 1 |
|
376 |
# mean_r1<-mean(r1) |
|
377 |
# mean_r2<-mean(r2) |
|
378 |
# median_r1<-sapply(r1, median) #Calulcating the mean for every model (median of columns) |
|
379 |
# median_r2<-sapply(r2, median) |
|
380 |
# sd_r1<-sapply(r1, sd) |
|
381 |
# sd_r2<-sapply(r2, sd) |
|
382 |
# |
|
383 |
# barplot(mean_r1,ylim=c(23,26),ylab="RMSE in tenth deg C") |
|
384 |
# barplot(mean_r2,ylim=c(23,26),ylab="RMSE in tenth deg C") |
|
385 |
# barplot(median_r1,ylim=c(23,26),ylab="RMSE in tenth deg C",add=TRUE,inside=FALSE,beside=TRUE) # put both on the same plot |
|
386 |
# barplot(median_r2,ylim=c(23,26),ylab="RMSE in tenth deg C",add=TRUE,inside=FALSE,beside=TRUE) # put both on the same plot |
|
387 |
# |
|
388 |
# barplot(sd_r1,ylim=c(6,8),ylab="RMSE in tenth deg C") # put both on the same plot |
|
389 |
# barplot(sd_r2,ylim=c(6,8),ylab="RMSE in tenth deg C") # put both on the same plot |
|
390 |
# |
|
391 |
# height<-rbind(mean_r1,mean_r2) |
|
392 |
# barplot(height,ylim=c(20,26),ylab="RMSE in tenth deg C",beside=TRUE,legend=rownames(height)) |
|
393 |
# barplot(height,ylim=c(20,26),ylab="RMSE in tenth deg C",beside=TRUE, col=c("darkblue","red"),legend=rownames(height)) # put both on the same plot |
|
394 |
# |
|
395 |
# height<-rbind(median_r1,median_r2) |
|
396 |
# barplot(height,ylim=c(20,26),ylab="RMSE in tenth deg C",beside=TRUE,legend=rownames(height)) |
|
397 |
# barplot(height,ylim=c(20,26),ylab="RMSE in tenth deg C",beside=TRUE, col=c("darkblue","red"),legend=rownames(height)) # put both on the same plot |
|
398 |
# |
|
399 |
# height<-rbind(mean_r2,median_r2) |
|
400 |
# barplot(height,ylim=c(20,26),ylab="RMSE in tenth deg C",beside=TRUE,legend=rownames(height)) |
|
401 |
# barplot(height,ylim=c(20,26),ylab="RMSE in tenth deg C",beside=TRUE, col=c("darkblue","red"),legend=rownames(height)) # put both on the same plot |
|
402 |
|
|
403 |
#barplot2(mean_r,median_r,ylim=c(23,26),ylab="RMSE in tenth deg C") # put both on the same plot |
|
404 |
#Collect var explained and p values for each var... |
|
405 |
|
|
406 |
|
|
407 |
##### END OF SCRIPT ########## |
|
408 |
|
|
409 |
|
|
410 |
|
Also available in: Unified diff
GAM LST, added specific and general diagnostic measures (e.g.MAE) GAM+Kriging, task #364 and #406