1 |
e7bf2d1b
|
Benoit Parmentier
|
################## MULTI SAMPLING GAM FUSION METHOD ASSESSMENT ####################################
|
2 |
|
|
############################ Merging LST and station data ##########################################
|
3 |
fb039a6b
|
Benoit Parmentier
|
#This script interpolates tmax values using MODIS LST and GHCND station data
|
4 |
|
|
#interpolation area. It requires the text file of stations and a shape file of the study area.
|
5 |
|
|
#Note that the projection for both GHCND and study area is lonlat WGS84.
|
6 |
|
|
#Options to run this program are:
|
7 |
|
|
#1) Multisampling: vary the porportions of hold out and use random samples for each run
|
8 |
|
|
#2)Constant sampling: use the same sample over the runs
|
9 |
|
|
#3)over dates: run over for example 365 dates without mulitsampling
|
10 |
|
|
#4)use seed number: use seed if random samples must be repeatable
|
11 |
|
|
#5)GAM fusion: possibilty of running GAM+FUSION or GAM separately
|
12 |
|
|
#AUTHOR: Benoit Parmentier
|
13 |
0766370d
|
Benoit Parmentier
|
#DATE: 12/27/2012
|
14 |
fb039a6b
|
Benoit Parmentier
|
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--
|
15 |
e7bf2d1b
|
Benoit Parmentier
|
###################################################################################################
|
16 |
|
|
|
17 |
|
|
###Loading R library and packages
|
18 |
|
|
library(gtools) # loading some useful tools
|
19 |
|
|
library(mgcv) # GAM package by Simon Wood
|
20 |
|
|
library(sp) # Spatial pacakge with class definition by Bivand et al.
|
21 |
|
|
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al.
|
22 |
|
|
library(rgdal) # GDAL wrapper for R, spatial utilities
|
23 |
|
|
library(gstat) # Kriging and co-kriging by Pebesma et al.
|
24 |
|
|
library(fields) # NCAR Spatial Interpolation methods such as kriging, splines
|
25 |
|
|
library(raster) # Hijmans et al. package for raster processing
|
26 |
|
|
library(rasterVis)
|
27 |
|
|
library(parallel) # Urbanek S. and Ripley B., package for multi cores & parralel processing
|
28 |
fb039a6b
|
Benoit Parmentier
|
library(reshape)
|
29 |
|
|
library(plotrix)
|
30 |
e7bf2d1b
|
Benoit Parmentier
|
### Parameters and argument
|
31 |
|
|
|
32 |
|
|
infile1<- "ghcn_or_tmax_covariates_06262012_OR83M.shp" #GHCN shapefile containing variables for modeling 2010
|
33 |
fb039a6b
|
Benoit Parmentier
|
infile2<-"list_365_dates_04212012.txt"
|
34 |
e7bf2d1b
|
Benoit Parmentier
|
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 |
|
|
#infile6<-"lst_climatology.txt"
|
38 |
|
|
infile6<-"LST_files_monthly_climatology.txt"
|
39 |
|
|
inlistf<-"list_files_05032012.txt" #Stack of images containing the Covariates
|
40 |
|
|
|
41 |
fb039a6b
|
Benoit Parmentier
|
path<-"/home/parmentier/Data/IPLANT_project/data_Oregon_stations_10242012_GAM"
|
42 |
e7bf2d1b
|
Benoit Parmentier
|
|
43 |
|
|
setwd(path)
|
44 |
|
|
#Station location of the study area
|
45 |
|
|
stat_loc<-read.table(paste(path,"/","location_study_area_OR_0602012.txt",sep=""),sep=",", header=TRUE)
|
46 |
|
|
#GHCN Database for 1980-2010 for study area (OR)
|
47 |
|
|
data3<-read.table(paste(path,"/","ghcn_data_TMAXy1980_2010_OR_0602012.txt",sep=""),sep=",", header=TRUE)
|
48 |
|
|
|
49 |
0766370d
|
Benoit Parmentier
|
nmodels<-9 #number of models running
|
50 |
e7bf2d1b
|
Benoit Parmentier
|
y_var_name<-"dailyTmax"
|
51 |
|
|
predval<-1
|
52 |
fb039a6b
|
Benoit Parmentier
|
prop<-0.3 #Proportion of testing retained for validation
|
53 |
e7bf2d1b
|
Benoit Parmentier
|
#prop<-0.25
|
54 |
fb039a6b
|
Benoit Parmentier
|
seed_number<- 100 #if seed zero then no seed? #Seed number for random sampling
|
55 |
6b22c61f
|
Benoit Parmentier
|
out_prefix<-"_365d_GAM_fus5_all_lstd_12302012" #User defined output prefix
|
56 |
0766370d
|
Benoit Parmentier
|
#out_prefix<-"_365d_GAM_12272012" #User defined output prefix
|
57 |
e7bf2d1b
|
Benoit Parmentier
|
|
58 |
|
|
bias_val<-0 #if value 1 then training data is used in the bias surface rather than the all monthly stations
|
59 |
fb039a6b
|
Benoit Parmentier
|
bias_prediction<-1 #if value 1 then use GAM for the BIAS prediction otherwise GAM direct repdiction for y_var (daily tmax)
|
60 |
|
|
nb_sample<-1 #number of time random sampling must be repeated for every hold out proportion
|
61 |
|
|
prop_min<-0.3 #if prop_min=prop_max and step=0 then predicitons are done for the number of dates...
|
62 |
|
|
prop_max<-0.3
|
63 |
|
|
step<-0
|
64 |
0766370d
|
Benoit Parmentier
|
constant<-0 #if value 1 then use the same samples as date one for the all set of dates
|
65 |
fb039a6b
|
Benoit Parmentier
|
#projection used in the interpolation of the study area
|
66 |
|
|
CRS_interp<-"+proj=lcc +lat_1=43 +lat_2=45.5 +lat_0=41.75 +lon_0=-120.5 +x_0=400000 +y_0=0 +ellps=GRS80 +units=m +no_defs";
|
67 |
|
|
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
|
68 |
|
|
|
69 |
6b22c61f
|
Benoit Parmentier
|
source("GAM_fusion_function_multisampling_12302012.R")
|
70 |
fb039a6b
|
Benoit Parmentier
|
|
71 |
|
|
###################### START OF THE SCRIPT ########################
|
72 |
e7bf2d1b
|
Benoit Parmentier
|
|
73 |
|
|
###Reading the station data and setting up for models' comparison
|
74 |
|
|
filename<-sub(".shp","",infile1) #Removing the extension from file.
|
75 |
|
|
ghcn<-readOGR(".", filename) #reading shapefile
|
76 |
|
|
|
77 |
|
|
CRS<-proj4string(ghcn) #Storing projection information (ellipsoid, datum,etc.)
|
78 |
|
|
|
79 |
|
|
mean_LST<- readGDAL(infile5) #Reading the whole raster in memory. This provides a grid for kriging
|
80 |
|
|
proj4string(mean_LST)<-CRS #Assigning coordinate information to prediction grid.
|
81 |
|
|
|
82 |
|
|
ghcn <- transform(ghcn,Northness = cos(ASPECT*pi/180)) #Adding a variable to the dataframe
|
83 |
|
|
ghcn <- transform(ghcn,Eastness = sin(ASPECT*pi/180)) #adding variable to the dataframe.
|
84 |
|
|
ghcn <- transform(ghcn,Northness_w = sin(slope*pi/180)*cos(ASPECT*pi/180)) #Adding a variable to the dataframe
|
85 |
|
|
ghcn <- transform(ghcn,Eastness_w = sin(slope*pi/180)*sin(ASPECT*pi/180)) #adding variable to the dataframe.
|
86 |
|
|
|
87 |
|
|
#Remove NA for LC and CANHEIGHT
|
88 |
|
|
ghcn$LC1[is.na(ghcn$LC1)]<-0
|
89 |
|
|
ghcn$LC3[is.na(ghcn$LC3)]<-0
|
90 |
|
|
ghcn$CANHEIGHT[is.na(ghcn$CANHEIGHT)]<-0
|
91 |
0766370d
|
Benoit Parmentier
|
ghcn$LC4[is.na(ghcn$LC4)]<-0
|
92 |
|
|
ghcn$LC6[is.na(ghcn$LC6)]<-0
|
93 |
e7bf2d1b
|
Benoit Parmentier
|
|
94 |
|
|
dates <-readLines(paste(path,"/",infile2, sep=""))
|
95 |
|
|
LST_dates <-readLines(paste(path,"/",infile3, sep=""))
|
96 |
|
|
models <-readLines(paste(path,"/",infile4, sep=""))
|
97 |
|
|
|
98 |
|
|
##Extracting the variables values from the raster files
|
99 |
|
|
|
100 |
|
|
lines<-read.table(paste(path,"/",inlistf,sep=""), sep=" ") #Column 1 contains the names of raster files
|
101 |
|
|
inlistvar<-lines[,1]
|
102 |
|
|
inlistvar<-paste(path,"/",as.character(inlistvar),sep="")
|
103 |
|
|
covar_names<-as.character(lines[,2]) #Column two contains short names for covaraites
|
104 |
|
|
|
105 |
|
|
s_raster<- stack(inlistvar) #Creating a stack of raster images from the list of variables.
|
106 |
|
|
layerNames(s_raster)<-covar_names #Assigning names to the raster layers
|
107 |
|
|
projection(s_raster)<-CRS
|
108 |
|
|
|
109 |
|
|
#stat_val<- extract(s_raster, ghcn3) #Extracting values from the raster stack for every point location in coords data frame.
|
110 |
|
|
pos<-match("ASPECT",layerNames(s_raster)) #Find column with name "value"
|
111 |
|
|
r1<-raster(s_raster,layer=pos) #Select layer from stack
|
112 |
|
|
pos<-match("slope",layerNames(s_raster)) #Find column with name "value"
|
113 |
|
|
r2<-raster(s_raster,layer=pos) #Select layer from stack
|
114 |
|
|
N<-cos(r1*pi/180)
|
115 |
|
|
E<-sin(r1*pi/180)
|
116 |
|
|
Nw<-sin(r2*pi/180)*cos(r1*pi/180) #Adding a variable to the dataframe
|
117 |
|
|
Ew<-sin(r2*pi/180)*sin(r1*pi/180) #Adding variable to the dataframe.
|
118 |
|
|
|
119 |
|
|
pos<-match("LC1",layerNames(s_raster)) #Find column with name "value"
|
120 |
|
|
LC1<-raster(s_raster,layer=pos) #Select layer from stack
|
121 |
|
|
s_raster<-dropLayer(s_raster,pos)
|
122 |
|
|
LC1[is.na(LC1)]<-0
|
123 |
fb039a6b
|
Benoit Parmentier
|
|
124 |
e7bf2d1b
|
Benoit Parmentier
|
pos<-match("LC3",layerNames(s_raster)) #Find column with name "value"
|
125 |
|
|
LC3<-raster(s_raster,layer=pos) #Select layer from stack
|
126 |
|
|
s_raster<-dropLayer(s_raster,pos)
|
127 |
|
|
LC3[is.na(LC3)]<-0
|
128 |
fb039a6b
|
Benoit Parmentier
|
|
129 |
|
|
pos<-match("LC4",layerNames(s_raster)) #Find column with name "value"
|
130 |
|
|
LC4<-raster(s_raster,layer=pos) #Select layer from stack
|
131 |
|
|
s_raster<-dropLayer(s_raster,pos)
|
132 |
|
|
LC4[is.na(LC4)]<-0
|
133 |
|
|
|
134 |
|
|
pos<-match("LC6",layerNames(s_raster)) #Find column with name "value"
|
135 |
|
|
LC6<-raster(s_raster,layer=pos) #Select layer from stack
|
136 |
|
|
s_raster<-dropLayer(s_raster,pos)
|
137 |
|
|
LC6[is.na(LC6)]<-0
|
138 |
|
|
|
139 |
|
|
LC_s<-stack(LC1,LC3,LC4,LC6)
|
140 |
|
|
layerNames(LC_s)<-c("LC1_forest","LC3_grass","LC4_crop","LC6_urban")
|
141 |
|
|
plot(LC_s)
|
142 |
|
|
|
143 |
e7bf2d1b
|
Benoit Parmentier
|
pos<-match("CANHEIGHT",layerNames(s_raster)) #Find column with name "value"
|
144 |
|
|
CANHEIGHT<-raster(s_raster,layer=pos) #Select layer from stack
|
145 |
|
|
s_raster<-dropLayer(s_raster,pos)
|
146 |
|
|
CANHEIGHT[is.na(CANHEIGHT)]<-0
|
147 |
0766370d
|
Benoit Parmentier
|
pos<-match("ELEV_SRTM",layerNames(s_raster)) #Find column with name "ELEV_SRTM"
|
148 |
|
|
ELEV_SRTM<-raster(s_raster,layer=pos) #Select layer from stack on 10/30
|
149 |
|
|
s_raster<-dropLayer(s_raster,pos)
|
150 |
|
|
ELEV_SRTM[ELEV_SRTM <0]<-NA
|
151 |
e7bf2d1b
|
Benoit Parmentier
|
|
152 |
|
|
xy<-coordinates(r1) #get x and y projected coordinates...
|
153 |
|
|
xy_latlon<-project(xy, CRS, inv=TRUE) # find lat long for projected coordinats (or pixels...)
|
154 |
|
|
lon<-raster(xy_latlon) #Transform a matrix into a raster object ncol=ncol(r1), nrow=nrow(r1))
|
155 |
|
|
ncol(lon)<-ncol(r1)
|
156 |
|
|
nrow(lon)<-nrow(r1)
|
157 |
|
|
extent(lon)<-extent(r1)
|
158 |
|
|
projection(lon)<-CRS #At this stage this is still an empty raster with 536 nrow and 745 ncell
|
159 |
|
|
lat<-lon
|
160 |
|
|
values(lon)<-xy_latlon[,1]
|
161 |
|
|
values(lat)<-xy_latlon[,2]
|
162 |
|
|
|
163 |
0766370d
|
Benoit Parmentier
|
r<-stack(N,E,Nw,Ew,lon,lat,LC1,LC3,LC4,LC6, CANHEIGHT,ELEV_SRTM)
|
164 |
|
|
rnames<-c("Northness","Eastness","Northness_w","Eastness_w", "lon","lat","LC1","LC3","LC4","LC6","CANHEIGHT","ELEV_SRTM")
|
165 |
e7bf2d1b
|
Benoit Parmentier
|
layerNames(r)<-rnames
|
166 |
|
|
s_raster<-addLayer(s_raster, r)
|
167 |
|
|
|
168 |
|
|
#s_sgdf<-as(s_raster,"SpatialGridDataFrame") #Conversion to spatial grid data frame
|
169 |
|
|
|
170 |
|
|
####### Preparing LST stack of climatology...
|
171 |
|
|
|
172 |
|
|
#l=list.files(pattern="mean_month.*rescaled.rst")
|
173 |
|
|
l <-readLines(paste(path,"/",infile6, sep=""))
|
174 |
|
|
molst<-stack(l) #Creating a raster stack...
|
175 |
|
|
#setwd(old)
|
176 |
|
|
molst<-molst-273.16 #K->C #LST stack of monthly average...
|
177 |
|
|
idx <- seq(as.Date('2010-01-15'), as.Date('2010-12-15'), 'month')
|
178 |
|
|
molst <- setZ(molst, idx)
|
179 |
|
|
layerNames(molst) <- month.abb
|
180 |
|
|
|
181 |
|
|
###### Preparing tables for model assessment: specific diagnostic/metrics
|
182 |
|
|
|
183 |
|
|
#Model assessment: specific diagnostics/metrics
|
184 |
|
|
results_AIC<- matrix(1,1,nmodels+3)
|
185 |
|
|
results_GCV<- matrix(1,1,nmodels+3)
|
186 |
|
|
results_DEV<- matrix(1,1,nmodels+3)
|
187 |
|
|
#results_RMSE_f<- matrix(1,length(models)+3)
|
188 |
|
|
|
189 |
|
|
#Model assessment: general diagnostic/metrics
|
190 |
|
|
results_RMSE <- matrix(1,1,nmodels+4)
|
191 |
|
|
results_MAE <- matrix(1,1,nmodels+4)
|
192 |
|
|
results_ME <- matrix(1,1,nmodels+4) #There are 8+1 models
|
193 |
|
|
results_R2 <- matrix(1,1,nmodels+4) #Coef. of determination for the validation dataset
|
194 |
|
|
|
195 |
|
|
results_RMSE_f<- matrix(1,1,nmodels+4) #RMSE fit, RMSE for the training dataset
|
196 |
|
|
results_MAE_f <- matrix(1,1,nmodels+4)
|
197 |
|
|
|
198 |
fb039a6b
|
Benoit Parmentier
|
######## Preparing monthly averages from the ProstGres database and extracting covarvariates from stack
|
199 |
e7bf2d1b
|
Benoit Parmentier
|
|
200 |
|
|
# do this work outside of (before) this function
|
201 |
|
|
# to avoid making a copy of the data frame inside the function call
|
202 |
|
|
date1<-ISOdate(data3$year,data3$month,data3$day) #Creating a date object from 3 separate column
|
203 |
|
|
date2<-as.POSIXlt(as.Date(date1))
|
204 |
|
|
data3$date<-date2
|
205 |
|
|
d<-subset(data3,year>=2000 & mflag=="0" ) #Selecting dataset 2000-2010 with good quality: 193 stations
|
206 |
|
|
#May need some screeing??? i.e. range of temp and elevation...
|
207 |
|
|
d1<-aggregate(value~station+month, data=d, mean) #Calculate monthly mean for every station in OR
|
208 |
|
|
id<-as.data.frame(unique(d1$station)) #Unique station in OR for year 2000-2010: 193 but 7 loss of monthly avg
|
209 |
|
|
|
210 |
|
|
dst<-merge(d1, stat_loc, by.x="station", by.y="STAT_ID") #Inner join all columns are retained
|
211 |
|
|
|
212 |
|
|
#This allows to change only one name of the data.frame
|
213 |
|
|
pos<-match("value",names(dst)) #Find column with name "value"
|
214 |
|
|
names(dst)[pos]<-c("TMax")
|
215 |
|
|
dst$TMax<-dst$TMax/10 #TMax is the average max temp for monthy data
|
216 |
|
|
#dstjan=dst[dst$month==9,] #dst contains the monthly averages for tmax for every station over 2000-2010
|
217 |
|
|
|
218 |
fb039a6b
|
Benoit Parmentier
|
#Extracting covariates from stack
|
219 |
|
|
coords<- dst[c('lon','lat')] #Define coordinates in a data frame
|
220 |
|
|
coordinates(dst)<-coords #Assign coordinates to the data frame
|
221 |
|
|
proj4string(dst)<-CRS_locs_WGS84 #Assign coordinates reference system in PROJ4 format
|
222 |
|
|
dst_month<-spTransform(dst,CRS(CRS_interp)) #Project from WGS84 to new coord. system
|
223 |
|
|
|
224 |
|
|
stations_val<-extract(s_raster,dst_month) #extraction of the infomration at station location
|
225 |
|
|
stations_val<-as.data.frame(stations_val)
|
226 |
|
|
dst_extract<-cbind(dst_month,stations_val)
|
227 |
|
|
dst<-dst_extract
|
228 |
0766370d
|
Benoit Parmentier
|
#Now clean and screen monthly values
|
229 |
|
|
dst_all<-dst
|
230 |
|
|
dst<-subset(dst,dst$TMax>-15 & dst$TMax<40)
|
231 |
|
|
dst<-subset(dst,dst$ELEV_SRTM>0) #This will drop two stations...or 24 rows
|
232 |
fb039a6b
|
Benoit Parmentier
|
|
233 |
e7bf2d1b
|
Benoit Parmentier
|
######### Preparing daily values for training and testing
|
234 |
|
|
|
235 |
|
|
#Screening for bad values: value is tmax in this case
|
236 |
|
|
#ghcn$value<-as.numeric(ghcn$value)
|
237 |
|
|
ghcn_all<-ghcn
|
238 |
|
|
ghcn_test<-subset(ghcn,ghcn$value>-150 & ghcn$value<400)
|
239 |
|
|
ghcn_test2<-subset(ghcn_test,ghcn_test$ELEV_SRTM>0)
|
240 |
|
|
ghcn<-ghcn_test2
|
241 |
|
|
#coords<- ghcn[,c('x_OR83M','y_OR83M')]
|
242 |
|
|
|
243 |
|
|
##Sampling: training and testing sites.
|
244 |
|
|
|
245 |
fb039a6b
|
Benoit Parmentier
|
if (seed_number>0) {
|
246 |
|
|
set.seed(seed_number) #Using a seed number allow results based on random number to be compared...
|
247 |
|
|
}
|
248 |
e7bf2d1b
|
Benoit Parmentier
|
nel<-length(dates)
|
249 |
|
|
dates_list<-vector("list",nel) #list of one row data.frame
|
250 |
|
|
|
251 |
fb039a6b
|
Benoit Parmentier
|
prop_range<-(seq(from=prop_min,to=prop_max,by=step))*100 #range of proportion to run
|
252 |
|
|
sn<-length(dates)*nb_sample*length(prop_range) #Number of samples to run
|
253 |
e7bf2d1b
|
Benoit Parmentier
|
|
254 |
|
|
for(i in 1:length(dates)){
|
255 |
|
|
d_tmp<-rep(dates[i],nb_sample*length(prop_range)) #repeating same date
|
256 |
|
|
s_nb<-rep(1:nb_sample,length(prop_range)) #number of random sample per proportion
|
257 |
|
|
prop_tmp<-sort(rep(prop_range, nb_sample))
|
258 |
|
|
tab_run_tmp<-cbind(d_tmp,s_nb,prop_tmp)
|
259 |
|
|
dates_list[[i]]<-tab_run_tmp
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
sampling_dat<-as.data.frame(do.call(rbind,dates_list))
|
263 |
|
|
names(sampling_dat)<-c("date","run_samp","prop")
|
264 |
|
|
|
265 |
|
|
for(i in 2:3){ # start of the for loop #1
|
266 |
|
|
sampling_dat[,i]<-as.numeric(as.character(sampling_dat[,i]))
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
sampling_dat$date<- as.character(sampling_dat[,1])
|
270 |
|
|
#ghcn.subsets <-lapply(dates, function(d) subset(ghcn, date==d)) #this creates a list of 10 or 365 subsets dataset based on dates
|
271 |
|
|
ghcn.subsets <-lapply(as.character(sampling_dat$date), function(d) subset(ghcn, date==d)) #this creates a list of 10 or 365 subsets dataset based on dates
|
272 |
|
|
|
273 |
fb039a6b
|
Benoit Parmentier
|
## adding choice of constant sample
|
274 |
|
|
if (seed_number>0) {
|
275 |
|
|
set.seed(seed_number) #Using a seed number allow results based on random number to be compared...
|
276 |
|
|
}
|
277 |
|
|
|
278 |
|
|
sampling<-vector("list",length(ghcn.subsets))
|
279 |
|
|
sampling_station_id<-vector("list",length(ghcn.subsets))
|
280 |
e7bf2d1b
|
Benoit Parmentier
|
for(i in 1:length(ghcn.subsets)){
|
281 |
|
|
n<-nrow(ghcn.subsets[[i]])
|
282 |
|
|
prop<-(sampling_dat$prop[i])/100
|
283 |
|
|
ns<-n-round(n*prop) #Create a sample from the data frame with 70% of the rows
|
284 |
|
|
nv<-n-ns #create a sample for validation with prop of the rows
|
285 |
|
|
ind.training <- sample(nrow(ghcn.subsets[[i]]), size=ns, replace=FALSE) #This selects the index position for 70% of the rows taken randomly
|
286 |
|
|
ind.testing <- setdiff(1:nrow(ghcn.subsets[[i]]), ind.training)
|
287 |
fb039a6b
|
Benoit Parmentier
|
#Find the corresponding
|
288 |
|
|
data_sampled<-ghcn.subsets[[i]][ind.training,] #selected the randomly sampled stations
|
289 |
|
|
station_id.training<-data_sampled$station #selected id for the randomly sampled stations (115)
|
290 |
|
|
#Save the information
|
291 |
e7bf2d1b
|
Benoit Parmentier
|
sampling[[i]]<-ind.training
|
292 |
fb039a6b
|
Benoit Parmentier
|
sampling_station_id[[i]]<- station_id.training
|
293 |
|
|
}
|
294 |
|
|
## Use same samples across the year...
|
295 |
|
|
if (constant==1){
|
296 |
|
|
sampled<-sampling[[1]]
|
297 |
|
|
data_sampled<-ghcn.subsets[[1]][sampled,] #selected the randomly sampled stations
|
298 |
|
|
station_sampled<-data_sampled$station #selected id for the randomly sampled stations (115)
|
299 |
|
|
list_const_sampling<-vector("list",sn)
|
300 |
|
|
list_const_sampling_station_id<-vector("list",sn)
|
301 |
|
|
for(i in 1:sn){
|
302 |
|
|
station_id.training<-intersect(station_sampled,ghcn.subsets[[i]]$station)
|
303 |
|
|
ind.training<-match(station_id.training,ghcn.subsets[[i]]$station)
|
304 |
|
|
list_const_sampling[[i]]<-ind.training
|
305 |
|
|
list_const_sampling_station_id[[i]]<-station_id.training
|
306 |
|
|
}
|
307 |
|
|
sampling<-list_const_sampling
|
308 |
|
|
sampling_station_id<-list_const_sampling_station_id
|
309 |
e7bf2d1b
|
Benoit Parmentier
|
}
|
310 |
|
|
|
311 |
|
|
######## Prediction for the range of dates and sampling data
|
312 |
|
|
|
313 |
|
|
#gam_fus_mod<-mclapply(1:length(dates), runGAMFusion,mc.preschedule=FALSE,mc.cores = 8) #This is the end bracket from mclapply(...) statement
|
314 |
fb039a6b
|
Benoit Parmentier
|
#gam_fus_mod_s<-mclapply(1:1, runGAMFusion,mc.preschedule=FALSE,mc.cores = 1) #This is the end bracket from mclapply(...) statement
|
315 |
0766370d
|
Benoit Parmentier
|
gam_fus_mod_s<-mclapply(1:length(ghcn.subsets), runGAMFusion,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
|
316 |
fb039a6b
|
Benoit Parmentier
|
#gam_fus_mod2<-mclapply(4:4, runGAMFusion,mc.preschedule=FALSE,mc.cores = 1) #This is the end bracket from mclapply(...) statement
|
317 |
e7bf2d1b
|
Benoit Parmentier
|
|
318 |
fb039a6b
|
Benoit Parmentier
|
save(gam_fus_mod_s,file= paste(path,"/","results2_fusion_Assessment_measure_all",out_prefix,".RData",sep=""))
|
319 |
e7bf2d1b
|
Benoit Parmentier
|
|
320 |
|
|
## Plotting and saving diagnostic measures
|
321 |
|
|
|
322 |
|
|
tb<-gam_fus_mod_s[[1]][[3]][0,] #empty data frame with metric table structure that can be used in rbinding...
|
323 |
|
|
tb_tmp<-gam_fus_mod_s #copy
|
324 |
|
|
|
325 |
|
|
for (i in 1:length(tb_tmp)){
|
326 |
|
|
tmp<-tb_tmp[[i]][[3]]
|
327 |
|
|
tb<-rbind(tb,tmp)
|
328 |
|
|
}
|
329 |
|
|
rm(tb_tmp)
|
330 |
|
|
|
331 |
|
|
for(i in 4:ncol(tb)){ # start of the for loop #1
|
332 |
|
|
tb[,i]<-as.numeric(as.character(tb[,i]))
|
333 |
|
|
}
|
334 |
|
|
|
335 |
fb039a6b
|
Benoit Parmentier
|
metrics<-as.character(unique(tb$metric)) #Name of accuracy metrics (RMSE,MAE etc.)
|
336 |
e7bf2d1b
|
Benoit Parmentier
|
tb_metric_list<-vector("list",length(metrics))
|
337 |
|
|
|
338 |
fb039a6b
|
Benoit Parmentier
|
for(i in 1:length(metrics)){ # Reorganizing information in terms of metrics
|
339 |
e7bf2d1b
|
Benoit Parmentier
|
metric_name<-paste("tb_",metrics[i],sep="")
|
340 |
|
|
tb_metric<-subset(tb, metric==metrics[i])
|
341 |
|
|
tb_metric<-cbind(tb_metric,sampling_dat[,2:3])
|
342 |
|
|
assign(metric_name,tb_metric)
|
343 |
|
|
tb_metric_list[[i]]<-tb_metric
|
344 |
|
|
}
|
345 |
|
|
|
346 |
|
|
tb_diagnostic<-do.call(rbind,tb_metric_list)
|
347 |
0766370d
|
Benoit Parmentier
|
tb_diagnostic[["prop"]]<-as.factor(tb_diagnostic[["prop"]])
|
348 |
|
|
|
349 |
|
|
mod_pat<-glob2rx("mod*")
|
350 |
|
|
mod_var<-grep(mod_pat,names(tb_diagnostic),value=TRUE) # using grep with "value" extracts the matching names
|
351 |
|
|
|
352 |
|
|
t<-melt(tb_diagnostic,
|
353 |
|
|
measure=mod_var,
|
354 |
|
|
id=c("dates","metric","prop"),
|
355 |
|
|
na.rm=F)
|
356 |
|
|
avg_tb<-cast(t,metric+prop~variable,mean)
|
357 |
|
|
median_tb<-cast(t,metric+prop~variable,median)
|
358 |
|
|
avg_tb[["prop"]]<-as.numeric(as.character(avg_tb[["prop"]]))
|
359 |
|
|
avg_RMSE<-subset(avg_tb,metric=="RMSE")
|
360 |
|
|
|
361 |
fb039a6b
|
Benoit Parmentier
|
sampling_obj<-list(sampling_dat=sampling_dat,training=sampling, training_id=sampling_station_id, tb=tb_diagnostic)
|
362 |
e7bf2d1b
|
Benoit Parmentier
|
|
363 |
0766370d
|
Benoit Parmentier
|
write.table(avg_tb, file= paste(path,"/","results2_fusion_Assessment_measure_avg_",out_prefix,".txt",sep=""), sep=",")
|
364 |
|
|
write.table(median_tb, file= paste(path,"/","results2_fusion_Assessment_measure_median_",out_prefix,".txt",sep=""), sep=",")
|
365 |
e7bf2d1b
|
Benoit Parmentier
|
write.table(tb_diagnostic, file= paste(path,"/","results2_fusion_Assessment_measure",out_prefix,".txt",sep=""), sep=",")
|
366 |
|
|
write.table(tb, file= paste(path,"/","results2_fusion_Assessment_measure_all",out_prefix,".txt",sep=""), sep=",")
|
367 |
|
|
|
368 |
0766370d
|
Benoit Parmentier
|
save(sampling_obj, file= paste(path,"/","results2_fusion_sampling_obj",out_prefix,".RData",sep=""))
|
369 |
|
|
#save(gam_fus_mod_s,file= paste(path,"/","results2_fusion_Assessment_measure_all",out_prefix,".RData",sep=""))
|
370 |
|
|
gam_fus_mod_obj<-list(gam_fus_mod=gam_fus_mod_s,sampling_obj=sampling_obj)
|
371 |
|
|
save(gam_fus_mod_obj,file= paste(path,"/","results_mod_obj_",out_prefix,".RData",sep=""))
|
372 |
e7bf2d1b
|
Benoit Parmentier
|
|
373 |
|
|
#### END OF SCRIPT
|