Project

General

Profile

Download (10.4 KB) Statistics
| Branch: | Revision:
1 bc59c6bb Benoit Parmentier
####################Interpolation of Tmax for 10 dates.#####################
2 d7b0ef36 Benoit Parmentier
#This script interpolates station values for the Oregon case study. This program loads the station data from a csv file 
3 7f643c83 Benoit Parmentier
#and perform two types  of regression: multiple linear model and general additive model (GAM). Note that this program:
4 e2d673e4 Benoit Parmentier
#1)assumes that the csv file is in the current working 
5
#2)extract relevant variables from raster images before performing the regressions. 
6 7f643c83 Benoit Parmentier
#The user must provide the list of raster images in  a textile.
7 2a89fa5f Benoit Parmentier
#Script created by Benoit Parmentier on March 3, 2012. 
8 89c6eee2 Benoit Parmentier
9 e2d673e4 Benoit Parmentier
###Loading r library and packages                                                                       # loading the raster package
10 92c36e33 Benoit Parmentier
library(gtools)                                                                        # loading ...
11 89c6eee2 Benoit Parmentier
library(mgcv)
12
13
###Parameters and arguments
14 4180b123 Benoit Parmentier
#infile1<-"ghcn_or_b_02122012_OR83M.csv"
15
infile1<-"ghcn_or_tmax_b_03032012_OR83M.csv"
16 fbc95c44 Benoit Parmentier
#path<-"C:/Data/Benoit/NCEAS/window_Oregon_data
17
path<-"/data/computer/parmentier/Data/IPLANT_project/data_Oregon_stations"
18 e2d673e4 Benoit Parmentier
setwd(path)
19 2a89fa5f Benoit Parmentier
#infile2<-"dates_interpolation_03012012.txt"  # list of 10 dates for the regression
20 25a68ae3 Benoit Parmentier
infile2<-"dates_interpolation_03052012.txt"
21 2a89fa5f Benoit Parmentier
prop<-0.3                                                                            #Proportion of testing retained for validation   
22 fbc95c44 Benoit Parmentier
out_prefix<-"_03042012_interaction_r1"
23 aac7c36c Benoit Parmentier
infile3<-"models_interpolation_03052012.txt"
24
25 89c6eee2 Benoit Parmentier
26 7f643c83 Benoit Parmentier
#######START OF THE SCRIPT #############
27
28 2a89fa5f Benoit Parmentier
###Reading the station data and setting up for models' comparison
29 e2d673e4 Benoit Parmentier
ghcn<-read.csv(paste(path,"/",infile1, sep=""), header=TRUE)                            #The "paste" function concatenates the path and file name in common string. 
30 25a68ae3 Benoit Parmentier
ghcn = transform(ghcn,Northness = cos(ASPECT)) #Adding a variable to the dataframe
31
ghcn = transform(ghcn,Eastness = sin(ASPECT))  #adding variable to the dataframe.
32
33 4180b123 Benoit Parmentier
ghcn = transform(ghcn,Northness_w = sin(slope)*cos(ASPECT)) #Adding a variable to the dataframe
34
ghcn = transform(ghcn,Eastness_w = sin(slope)*sin(ASPECT))  #adding variable to the dataframe.
35
set.seed(100)
36 bc59c6bb Benoit Parmentier
dates <-readLines(paste(path,"/",infile2, sep=""))
37 aac7c36c Benoit Parmentier
models <-readLines(paste(path,"/",infile3, sep=""))
38 ec229367 Benoit Parmentier
39 25a68ae3 Benoit Parmentier
results <- matrix(1,length(dates),14)            #This is a matrix containing the diagnostic measures from the GAM models.
40
41 fbc95c44 Benoit Parmentier
results_AIC<- matrix(1,length(dates),length(models)+2)  
42
results_GCV<- matrix(1,length(dates),length(models)+2)
43
results_DEV<- matrix(1,length(dates),length(models)+2)
44
results_RMSE<- matrix(1,length(dates),length(models)+2)
45
46
#Screening for bad values
47
48
49
#tmax~ lon + lat + ELEV_SRTM + Eastness + Northness + DISTOC
50
#tmax range: min max)
51
ghcn_all<-ghcn
52
ghcn_test<-subset(ghcn,ghcn$tmax>0 & ghcn$tmax<400)
53
ghcn_test2<-subset(ghcn_test,ghcn_test$ELEV_SRTM>0)
54
ghcn<-ghcn_test2
55
#lon range
56
#lat range
57
#ELEV_SRTM
58
#Eastness
59
#Northness
60
61 aac7c36c Benoit Parmentier
62 4180b123 Benoit Parmentier
ghcn.subsets <-lapply(dates, function(d) subset(ghcn, date==d)) #this creates a list of 10 subsets data
63
#note that compare to the previous version date_ column was changed to date
64 ec229367 Benoit Parmentier
65 2a89fa5f Benoit Parmentier
## looping through the dates...
66 aac7c36c Benoit Parmentier
#Change this into  a nested loop, looping through the number of models
67 bc59c6bb Benoit Parmentier
68 fbc95c44 Benoit Parmentier
69 2a89fa5f Benoit Parmentier
for(i in 1:length(dates)){            # start of the for loop #1
70
  
71
  ###Regression part 1: Creating a validation dataset by creating training and testing datasets
72
  
73
  n<-nrow(ghcn.subsets[[i]])
74
  ns<-n-round(n*prop)  #Create a sample from the data frame with 70% of the rows
75
  nv<-n-ns             #create a sample for validation with prop of the rows
76
  #ns<-n-round(n*prop)  #Create a sample from the data frame with 70% of the rows
77
  ind.training <- sample(nrow(ghcn.subsets[[i]]), size=ns, replace=FALSE) #This selects the index position for 70% of the rows taken randomly
78
  ind.testing <- setdiff(1:nrow(ghcn.subsets[[i]]), ind.training)
79
  data_s <- ghcn.subsets[[i]][ind.training, ]
80
  data_v <- ghcn.subsets[[i]][ind.testing, ]
81
  
82
  ####Regression part 2: GAM models
83
84 aac7c36c Benoit Parmentier
  mod1<-gam(tmax~ s(lat) + s (lon) + s (ELEV_SRTM), data=data_s)
85 fbc95c44 Benoit Parmentier
  mod2<- gam(tmax~ s(lat,lon) + s(ELEV_SRTM), data=data_s)
86 aac7c36c Benoit Parmentier
  mod3<-gam(tmax~ s(lat) + s (lon) + s (ELEV_SRTM) +  s (Northness)+ s (Eastness) + s(DISTOC), data=data_s)
87
  mod4<-gam(tmax~ s(lat) + s (lon) + s (ELEV_SRTM) + s (Northness_w)+ s (Eastness_w) + s(DISTOC), data=data_s)
88 fbc95c44 Benoit Parmentier
  mod5<- gam(tmax~ s(lat) + s (lon) + s (ELEV_SRTM) + s(Northness,Eastness) + s(DISTOC), data=data_s)
89
  mod6<- gam(tmax~ s(lat,lon) + s (ELEV_SRTM) + s(Northness,Eastness) + s(DISTOC), data=data_s)
90 4180b123 Benoit Parmentier
  
91 2a89fa5f Benoit Parmentier
  
92
  ####Regression part 3: Calculating and storing diagnostic measures
93 aac7c36c Benoit Parmentier
  results_AIC[i,1]<- dates[i]  #storing the interpolation dates in the first column
94
  results_AIC[i,2]<- ns        #number of stations used in the training stage
95
  results_AIC[i,3]<- AIC (mod1)
96
  results_AIC[i,4]<- AIC (mod2)
97
  results_AIC[i,5]<- AIC (mod3)
98
  results_AIC[i,6]<- AIC (mod4)
99
  results_AIC[i,7]<- AIC (mod5)
100
  results_AIC[i,8]<- AIC (mod6)
101 2a89fa5f Benoit Parmentier
  
102 aac7c36c Benoit Parmentier
  results_GCV[i,1]<- dates[i]  #storing the interpolation dates in the first column
103
  results_GCV[i,2]<- ns        #number of stations used in the training stage
104
  results_GCV[i,3]<- mod1$gcv.ubre
105
  results_GCV[i,4]<- mod2$gcv.ubre
106
  results_GCV[i,5]<- mod3$gcv.ubre
107
  results_GCV[i,6]<- mod4$gcv.ubre
108
  results_GCV[i,7]<- mod5$gcv.ubre
109
  results_GCV[i,8]<- mod6$gcv.ubre
110 25a68ae3 Benoit Parmentier
  
111 aac7c36c Benoit Parmentier
  results_DEV[i,1]<- dates[i]  #storing the interpolation dates in the first column
112
  results_DEV[i,2]<- ns        #number of stations used in the training stage
113
  results_DEV[i,3]<- mod1$deviance
114
  results_DEV[i,4]<- mod2$deviance
115
  results_DEV[i,5]<- mod3$deviance
116
  results_DEV[i,6]<- mod4$deviance
117
  results_DEV[i,7]<- mod5$deviance
118
  results_DEV[i,8]<- mod6$deviance
119 2a89fa5f Benoit Parmentier
  
120
  #####VALIDATION: Prediction checking the results using the testing data########
121
 
122 aac7c36c Benoit Parmentier
  y_mod1<- predict(mod1, newdata=data_v, se.fit = TRUE) #Using the coeff to predict new values.
123
  y_mod2<- predict(mod2, newdata=data_v, se.fit = TRUE)            
124
  y_mod3<- predict(mod3, newdata=data_v, se.fit = TRUE) 
125
  y_mod4<- predict(mod4, newdata=data_v, se.fit = TRUE) 
126
  y_mod5<- predict(mod5, newdata=data_v, se.fit = TRUE) 
127 fbc95c44 Benoit Parmentier
  y_mod6<- predict(mod6, newdata=data_v, se.fit = TRUE)
128 25a68ae3 Benoit Parmentier
  
129 aac7c36c Benoit Parmentier
  res_mod1<- data_v$tmax - y_mod1$fit #Residuals for GMA model that resembles the ANUSPLIN interpolation
130
  res_mod2<- data_v$tmax - y_mod2$fit   #Residuals for GAM model that resembles the PRISM interpolation                               
131
  res_mod3<- data_v$tmax - y_mod3$fit  
132
  res_mod4<- data_v$tmax - y_mod4$fit
133
  res_mod5<- data_v$tmax - y_mod5$fit
134
  res_mod6<- data_v$tmax - y_mod6$fit
135 25a68ae3 Benoit Parmentier
  
136 aac7c36c Benoit Parmentier
  RMSE_mod1 <- sqrt(sum(res_mod1^2)/nv)          
137
  RMSE_mod2 <- sqrt(sum(res_mod2^2)/nv)
138
  RMSE_mod3 <- sqrt(sum(res_mod3^2)/nv)
139
  RMSE_mod4 <- sqrt(sum(res_mod4^2)/nv)
140
  RMSE_mod5 <- sqrt(sum(res_mod5^2)/nv)
141
  RMSE_mod6 <- sqrt(sum(res_mod6^2)/nv)
142 2a89fa5f Benoit Parmentier
  
143 aac7c36c Benoit Parmentier
144
  results_RMSE[i,1]<- dates[i]  #storing the interpolation dates in the first column
145
  results_RMSE[i,2]<- ns        #number of stations used in the training stage
146
  results_RMSE[i,3]<- RMSE_mod1
147
  results_RMSE[i,4]<- RMSE_mod2
148
  results_RMSE[i,5]<- RMSE_mod3
149
  results_RMSE[i,6]<- RMSE_mod4
150
  results_RMSE[i,7]<- RMSE_mod5
151
  results_RMSE[i,8]<- RMSE_mod6
152 25a68ae3 Benoit Parmentier
  
153
  data_name<-paste("ghcn_v_",dates[[i]],sep="")
154
  assign(data_name,data_v)
155
  #ghcn_v<-ls(pattern="ghcn_v_")
156 2a89fa5f Benoit Parmentier
  
157
  # end of the for loop #1
158
  }
159
160
## Plotting and saving diagnostic measures
161
162 aac7c36c Benoit Parmentier
results_RMSEnum <-results_RMSE
163
mode(results_RMSEnum)<- "numeric"
164 2a89fa5f Benoit Parmentier
# Make it numeric first
165
# Now turn it into a data.frame...
166
167 aac7c36c Benoit Parmentier
results_table_RMSE<-as.data.frame(results_RMSEnum)
168
colnames(results_table_RMSE)<-c("dates","ns","mod1", "mod2","mod3", "mod4", "mod5", "mod6")
169 2a89fa5f Benoit Parmentier
170 fbc95c44 Benoit Parmentier
# win.graph()
171
# barplot(results_table$RMSE_A1/10,main="RMSE for the A1 models",names.arg=results_table$dates,ylab="Temp (deg. C)",xlab="interpolated date")
172
# savePlot(paste("GAM_ANUSPLIN1_RMSE",out_prefix,".emf", sep=""), type="emf")
173
# win.graph()
174
# barplot(results_table$RMSE_P1/10,main="RMSE for the P1 models",names.arg=results_table$dates,ylab="Temp ( deg. C)",xlab="interpolated date")
175
# savePlot(paste("GAM_PRISM1_RMSE",out_prefix,".emf", sep=""), type="emf")
176
# win.graph()
177
# barplot(results_table$RMSE_P2/10,main="RMSE for the P2 models",names.arg=results_table$dates,ylab="Temp ( deg. C)",xlab="interpolated date")
178
# savePlot(paste("GAM_PRISM1_RMSE",out_prefix,".emf", sep=""), type="emf")
179
# win.graph()
180
# barplot(results_table$AIC_A1,main="AIC for the A1 models",names.arg=results_table$dates,ylab="Temp ( deg. C)",xlab="interpolated date")
181
# savePlot(paste("GAM_PRISM1_RMSE",out_prefix,".emf", sep=""), type="emf")
182
# win.graph()
183
# barplot(results_table$AIC_P1/10,main="AIC for the P1 models",names.arg=results_table$dates,ylab="Temp ( deg. C)",xlab="interpolated date")
184
# savePlot(paste("GAM_PRISM1_RMSE",out_prefix,".emf", sep=""), type="emf")
185
# win.graph()
186
# barplot(results_table$AIC_P2/10,main="AIC for the P2 models",names.arg=results_table$dates,ylab="Temp (10 X deg. C)",xlab="interolated date")
187
# savePlot(paste("GAM_PRISM1_RMSE",out_prefix,".emf", s‰‰ep=""), type="emf")
188
# win.graph()
189
# barplot(results_table$Deviance_A1/10,main="Deviance for the A1 models",names.arg=results_table$dates,ylab="Temp (10 X deg. C)",xlab="interolated date")
190
# savePlot(paste("GAM_ANUSPLIN1_Deviance",out_prefix,".emf", sep=""), type="emf")
191
# win.graph()
192
# barplot(results_table$Deviance_P1/10,main="Deviance for the P1 models",names.arg=results_table$dates,ylab="Temp (10 X deg. C)",xlab="interolated date")
193
# savePlot(paste("GAM_PRISM1_Deviance",out_prefix,".emf", sep=""), type="emf")
194
# win.graph()
195
# barplot(results_table$Deviance_P2/10,main="Deviance for the P2 models",names.arg=results_table$dates,ylab="Temp (10 X deg. C)",xlab="interolated date")
196
# savePlot(paste("GAM_PRISM2_Deviance",out_prefix,".emf", sep=""), type="emf")
197
198
#results_table_RMSE
199
write.csv(results_table_RMSE, file= paste(path,"/","results_GAM_Assessment",out_prefix,".txt",sep=""))
200 2a89fa5f Benoit Parmentier
201 4180b123 Benoit Parmentier
202 2a89fa5f Benoit Parmentier
# End of script##########
203
204
# ###############################
205 bc59c6bb Benoit Parmentier
206
# 
207
# ############Diagnostic GAM plots#############
208
# win.graph()
209
# gam.check(GAM_ANUSPLIN1)
210
# savePlot("GAM_ANUSPLIN1_diagnostic1.emf", type="emf")
211
# win.graph()
212
# gam.check(GAM_PRISM1)   #This will produce basic plots of residuals
213
# savePlot("GAM_PRISM_diagnostic1.emf", type="emf")
214
# gam.check(GAM_ANUSPLIN1)
215
# win.graph()
216
# vis.gam(GAM_ANUSPLIN1)
217
# savePlot("GAM_ANUSPLIN1_prediction.emf", type="emf")        
218
# win.graph()
219
# vis.gam(GAM_PRISM1)
220
# savePlot("GAM_PRISM1_prediction.emf", type="emf")
221
# win.graph()
222
# vis.gam(GAM_ANUSPLIN1, view=c("lat","ELEV_SRTM"))
223
# #vis.gam(GAM_ANUSPLIN1, view=c("lat","ELEV_SRTM", theta=100,phi=200))
224
# savePlot("GAM_ANUSPLIN1_prediction2.emf", type="emf")
225 2a89fa5f Benoit Parmentier
#
226 25a68ae3 Benoit Parmentier
227 bc59c6bb Benoit Parmentier
#                 
228 2a89fa5f Benoit Parmentier
229 89c6eee2 Benoit Parmentier
230
231
232 e2d673e4 Benoit Parmentier