Revision 6494f05a
Added by Benoit Parmentier over 11 years ago
climate/research/oregon/interpolation/IBS2013_figures_and_analyses_poster.R | ||
---|---|---|
1 |
######################################## IBS 2013 POSTER ####################################### |
|
2 |
############################ Scripts for figures and analyses for the the IBS poster ##################################### |
|
3 |
#This script creates the figures used in the IBS 2013 poster. |
|
4 |
#It uses inputs from interpolation objects created at earlier stages... # |
|
5 |
#AUTHOR: Benoit Parmentier # |
|
6 |
#DATE: 01/03/2013 # |
|
7 |
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#491-- # |
|
8 |
################################################################################################### |
|
9 |
|
|
10 |
###Loading R library and packages |
|
11 |
#library(gtools) # loading some useful tools |
|
12 |
library(mgcv) # GAM package by Wood 2006 (version 2012) |
|
13 |
library(sp) # Spatial pacakge with class definition by Bivand et al. 2008 |
|
14 |
library(spdep) # Spatial package with methods and spatial stat. by Bivand et al. 2012 |
|
15 |
library(rgdal) # GDAL wrapper for R, spatial utilities (Keitt et al. 2012) |
|
16 |
library(gstat) # Kriging and co-kriging by Pebesma et al. 2004 |
|
17 |
library(automap) # Automated Kriging based on gstat module by Hiemstra et al. 2008 |
|
18 |
library(spgwr) |
|
19 |
library(maptools) |
|
20 |
library(graphics) |
|
21 |
library(parallel) # Urbanek S. and Ripley B., package for multi cores & parralel processing |
|
22 |
library(raster) |
|
23 |
library(rasterVis) |
|
24 |
library(plotrix) # Draw circle on graph and additional plotting options |
|
25 |
library(reshape) # Data format and type transformation |
|
26 |
## Functions |
|
27 |
#loading R objects that might have similar names |
|
28 |
load_obj <- function(f) |
|
29 |
{ |
|
30 |
env <- new.env() |
|
31 |
nm <- load(f, env)[1] |
|
32 |
env[[nm]] |
|
33 |
} |
|
34 |
|
|
35 |
tb_metrics_fun<-function(list_obj,path_data,names_obj){ |
|
36 |
nel<-length(inlistobj) |
|
37 |
#method_mod <-vector("list",nel) #list of one row data.frame |
|
38 |
method_tb <-vector("list",nel) #list of one row data.frame |
|
39 |
for (k in 1:length(inlistobj)){ |
|
40 |
#obj_tmp<-load_obj(as.character(inlistobj[i])) |
|
41 |
#method_mod[[i]]<-obj_tmp |
|
42 |
#names(method_mod[[i]])<-obj_names[i] |
|
43 |
mod_tmp<-load_obj(as.character(inlistobj[k])) |
|
44 |
tb<-mod_tmp[[1]][[3]][0,] #copy of the data.frame structure that holds the acuary metrics |
|
45 |
#mod_tmp<-method_mod[[k]] |
|
46 |
for (i in 1:365){ # Assuming 365 days of prediction |
|
47 |
tmp<-mod_tmp[[i]][[3]] |
|
48 |
tb<-rbind(tb,tmp) |
|
49 |
} |
|
50 |
rm(mod_tmp) |
|
51 |
for(i in 4:(ncol(tb))){ # start of the for loop #1 |
|
52 |
tb[,i]<-as.numeric(as.character(tb[,i])) |
|
53 |
} |
|
54 |
method_tb[[k]]<-tb |
|
55 |
} |
|
56 |
names(method_tb)<-names_obj |
|
57 |
return(method_tb) |
|
58 |
} |
|
59 |
|
|
60 |
plot_model_boxplot_combined_fun<-function(tb_list,path_data,obj_names,mod_selected,out_prefix,layout_m){ |
|
61 |
|
|
62 |
method_stat<-vector("list",length(obj_names)) #This contains summary information based on accuracy metrics (MAE,RMSE) |
|
63 |
names_method<-obj_names |
|
64 |
metrics<-c("MAE","RMSE") |
|
65 |
tb_metric_list<-vector("list",length(metrics)) |
|
66 |
tb_metric_list_na<-vector("list",length(metrics)) |
|
67 |
mean_list<-vector("list",length(metrics)) |
|
68 |
sd_list<-vector("list",length(metrics)) |
|
69 |
na_mod_list<-vector("list",length(metrics)) |
|
70 |
|
|
71 |
for(i in 1:length(metrics)){ # Reorganizing information in terms of metrics |
|
72 |
#for(k in 1:length(tb_list)){ # start of the for main loop to all methods |
|
73 |
#tb<-tb_list[[k]] |
|
74 |
#metrics<-as.character(unique(tb$metric)) #Name of accuracy metrics (RMSE,MAE etc.) |
|
75 |
metric_name<-paste("tb_t_",metrics[i],sep="") |
|
76 |
png(paste("boxplot",metric_name,out_prefix,"_combined.png", sep="_"),height=480*layout_m[1],width=480*layout_m[2]) |
|
77 |
par(mfrow=layout_m) |
|
78 |
for(k in 1:length(tb_list)){ # start of the for main loop to all methods |
|
79 |
#}#for(i in 1:length(metrics)){ # Reorganizing information in terms of metrics |
|
80 |
tb<-tb_list[[k]] |
|
81 |
#metric_name<-paste("tb_t_",metrics[i],sep="") |
|
82 |
tb_metric<-subset(tb, metric==metrics[i]) |
|
83 |
assign(metric_name,tb_metric) |
|
84 |
tb_metric_list[[i]]<-tb_metric |
|
85 |
tb_processed<-tb_metric |
|
86 |
mod_pat<-glob2rx("mod*") |
|
87 |
var_pat<-grep(mod_pat,names(tb_processed),value=FALSE) # using grep with "value" extracts the matching names |
|
88 |
#mod_pat<-mod_selected |
|
89 |
#var_pat<-grep(mod_pat,names(tb_processed),value=FALSE) # using grep with "value" extracts the matching names |
|
90 |
na_mod<-colSums(!is.na(tb_processed[,var_pat])) |
|
91 |
for (j in 1:length(na_mod)){ |
|
92 |
if (na_mod[j]<183){ |
|
93 |
tmp_name<-names(na_mod)[j] |
|
94 |
pos<-match(tmp_name,names(tb_processed)) |
|
95 |
tb_processed<-tb_processed[,-pos] #Remove columns that have too many missing values!!! |
|
96 |
} |
|
97 |
} |
|
98 |
tb_metric_list_na[[i]]<-tb_processed |
|
99 |
mod_pat<-glob2rx("mod*") |
|
100 |
var_pat<-grep(mod_pat,names(tb_processed),value=FALSE) |
|
101 |
#Plotting box plots |
|
102 |
|
|
103 |
#png(paste("boxplot",metric_name,names_methods[k],out_prefix,".png", sep="_")) |
|
104 |
boxplot(tb_processed[,var_pat],main=names_methods[k], ylim=c(1,5), |
|
105 |
ylab= metrics[i], outline=FALSE) #ADD TITLE RELATED TO METHODS... |
|
106 |
|
|
107 |
#Add assessment of missing prediction over the year. |
|
108 |
mean_metric<-sapply(tb_processed[,var_pat],mean,na.rm=T) |
|
109 |
sd_metric<-sapply(tb_processed[,var_pat],sd,na.rm=T) |
|
110 |
mean_list[[i]]<-mean_metric |
|
111 |
sd_list[[i]]<-sd_metric |
|
112 |
na_mod_list[[i]]<-na_mod_list |
|
113 |
#Now calculate monthly averages and overall averages over full year |
|
114 |
method_stat<-list(mean_list,sd_list,na_mod_list) |
|
115 |
method_stat[[k]]<-list(mean_list,sd_list,na_mod_list) |
|
116 |
names(method_stat[[k]])<-c("mean_metrics","sd_metrics","na_metrics") |
|
117 |
names(mean_list)<-metrics |
|
118 |
method_mean[[k]]<-mean_list |
|
119 |
names_methods<-obj_names |
|
120 |
#names(method_stat)<-obj_names |
|
121 |
} |
|
122 |
dev.off() #Close file where figures are drawn |
|
123 |
} |
|
124 |
return(method_stat) |
|
125 |
} |
|
126 |
|
|
127 |
raster_plots_interpolation_fun<-function(file_pat1,file_pat2,mod_selected1,mod_selected2,titles,mask_rast, |
|
128 |
layout_m,out_suffix){ |
|
129 |
layout_m<-layout_plot |
|
130 |
lf_cai<-list.files(pattern=file_pat1) #Search for files in relation to fusion |
|
131 |
lf_fus<-list.files(pattern=file_pat2) #Search for files in relation to fusion |
|
132 |
|
|
133 |
r1<-stack(lf_cai[mod_selected1]) #CAI |
|
134 |
r2<-stack(lf_fus[mod_selected2])#FUS |
|
135 |
predictions<-stack(r1,r2) |
|
136 |
predictions<-mask(predictions,mask_rast) |
|
137 |
layerNames(predictions)<-unlist(titles) |
|
138 |
|
|
139 |
s.range <- c(min(minValue(predictions)), max(maxValue(predictions))) |
|
140 |
#s.range <- s.range+c(5,-5) |
|
141 |
col.breaks <- pretty(s.range, n=50) |
|
142 |
lab.breaks <- pretty(s.range, n=5) |
|
143 |
temp.colors <- colorRampPalette(c('blue', 'white', 'red')) |
|
144 |
X11(height=6,width=36) |
|
145 |
X11(height=6,width=18) |
|
146 |
#plot(predictions, breaks=col.breaks, col=rev(heat.colors(length(col.breaks)-1)), |
|
147 |
# axis=list(at=lab.breaks, labels=lab.breaks)) |
|
148 |
plot(predictions, breaks=col.breaks, col=temp.colors(length(col.breaks)-1), |
|
149 |
axis=list(at=lab.breaks, labels=lab.breaks)) |
|
150 |
#plot(reg_outline, add=TRUE) |
|
151 |
savePlot(paste("comparison_one_date_CAI_fusion_tmax_prediction_levelplot",date_selected,out_prefix,".png", sep=""),type="png") |
|
152 |
#png(paste("boxplot",metric_name,out_prefix,"_combined.png", sep="_"),height=480*layout_m[1],width=480*layout_m[2]) |
|
153 |
#par(mfrow=layout_m) |
|
154 |
layerNames(predictions)<-titles |
|
155 |
plot_name_pan<-unlist(titles) |
|
156 |
#plot_name_pan<-c('cai_kr (RMSE=2.29)','cai_mod7 (RMSE=2.38)','fss_kr (RMSE=2.29') |
|
157 |
png(paste("comparison_one_date_CAI_fusion_tmax_prediction_levelplot_",date_selected,out_prefix,".png", sep=""), |
|
158 |
height=480*layout_m[1],width=480*layout_m[2]) |
|
159 |
levelplot(predictions,main="Interpolated Surfaces Comparison", ylab=NULL,xlab=NULL,par.settings = list(axis.text = list(font = 2, cex = 1.3), |
|
160 |
par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5), |
|
161 |
names.attr=plot_name_pan,col.regions=temp.colors,at=seq(s.range[1],s.range[2],by=0.25)) |
|
162 |
#col.regions=temp.colors(25)) |
|
163 |
dev.off() |
|
164 |
#savePlot(paste("comparison_one_date_CAI_fusion_tmax_prediction_levelplot_",date_selected,out_prefix,".png", sep=""),type="png") |
|
165 |
return(predictions) |
|
166 |
} |
|
167 |
|
|
168 |
plot_transect_m2<-function(list_trans,r_stack,title_plot,disp=TRUE,m_layers){ |
|
169 |
#This function creates plot of transects for stack of raster images. |
|
170 |
#Arguments: |
|
171 |
#list_trans: list of files containing the transects lines in shapefile format |
|
172 |
#r_stack: raster stack containing the information to extect |
|
173 |
#title_plot: plot title |
|
174 |
#disp: display and save from X11 if TRUE or plot to png file if FALSE |
|
175 |
#m_layers: index for layerers containing alternate units to be drawned on a differnt scale |
|
176 |
#RETURN: |
|
177 |
#list containing transect information |
|
178 |
|
|
179 |
nb<-length(list_trans) |
|
180 |
t_col<-rainbow(nb) |
|
181 |
t_col<-c("red","green","black") |
|
182 |
lty_list<-c("dashed","solid","dotted") |
|
183 |
list_trans_data<-vector("list",nb) |
|
184 |
|
|
185 |
#For scale 1 |
|
186 |
for (i in 1:nb){ |
|
187 |
trans_file<-list_trans[[i]][1] |
|
188 |
filename<-sub(".shp","",trans_file) #Removing the extension from file. |
|
189 |
transect<-readOGR(".", filename) #reading shapefile |
|
190 |
trans_data<-extract(r_stack, transect) |
|
191 |
if (disp==FALSE){ |
|
192 |
png(file=paste(list_trans[[i]]),".png",sep="") |
|
193 |
} |
|
194 |
#Plot layer values for specific transect |
|
195 |
for (k in 1:ncol(trans_data[[1]])){ |
|
196 |
y<-trans_data[[1]][,k] |
|
197 |
x<-1:length(y) |
|
198 |
m<-match(k,m_layers) |
|
199 |
|
|
200 |
if (k==1 & is.na(m)){ |
|
201 |
plot(x,y,type="l",xlab="transect distance from coastal origin (km)", ylab=" maximum temperature (degree C)", |
|
202 |
,cex=1.2,col=t_col[k]) |
|
203 |
#axis(2) |
|
204 |
} |
|
205 |
if (k==1 & !is.na(m)){ |
|
206 |
plot(x,y,type="l",col=t_col[k],lty="dotted",axes=F) #plotting fusion profile |
|
207 |
#axis(4,xlab="",ylab="elevation(m)") |
|
208 |
axis(4,cex=1.2) |
|
209 |
} |
|
210 |
if (k!=1 & is.na(m)){ |
|
211 |
#par(new=TRUE) # new plot without erasing old |
|
212 |
lines(x,y,type="l",xlab="",ylab="",col=t_col[k],axes=F) #plotting fusion profile |
|
213 |
#axis(2,xlab="",ylab="tmax (in degree C)") |
|
214 |
} |
|
215 |
if (k!=1 & !is.na(m)){ |
|
216 |
par(new=TRUE) # key: ask for new plot without erasing old |
|
217 |
plot(x,y,type="l",col=t_col[k],xlab="",ylab="",lty="dotted",axes=F) #plotting fusion profile |
|
218 |
#axis(4,xlab="",ylab="elevation(m)") |
|
219 |
axis(4,cex=1.2) |
|
220 |
} |
|
221 |
} |
|
222 |
title(title_plot[i]) |
|
223 |
legend("topleft",legend=layerNames(r_stack)[1:2], |
|
224 |
cex=1.2, col=t_col,lty=1,bty="n") |
|
225 |
legend("topright",legend=layerNames(r_stack)[3], |
|
226 |
cex=1.2, col=t_col[3],lty="dotted",bty="n") |
|
227 |
if (disp==TRUE){ |
|
228 |
savePlot(file=paste(list_trans[[i]][2],".png",sep=""),type="png") |
|
229 |
} |
|
230 |
if (disp==FALSE){ |
|
231 |
dev.off() |
|
232 |
} |
|
233 |
list_trans_data[[i]]<-trans_data |
|
234 |
} |
|
235 |
names(list_trans_data)<-names(list_trans) |
|
236 |
return(list_trans_data) |
|
237 |
} |
|
238 |
|
|
239 |
stat_moran_std_raster_fun<-function(i){ |
|
240 |
list_var_stat<-vector("list",ncol(lf_list)) |
|
241 |
for (k in 1:length(lf_list)){ |
|
242 |
|
|
243 |
raster_pred<-raster(lf_list[i,k]) |
|
244 |
tmp_rast<-mask(raster_pred,mask_rast) |
|
245 |
#tmp_rast<-raster_pred |
|
246 |
raster_pred2<-tmp_rast |
|
247 |
|
|
248 |
t1<-cellStats(raster_pred,na.rm=TRUE,stat=sd) #Calculating the standard deviation for the |
|
249 |
m1<-Moran(raster_pred,w=3) #Calculating Moran's I with window of 3 an default Queen's case |
|
250 |
stat<-as.data.frame(t(c(m1,t1))) |
|
251 |
names(stat)<-c("moranI","std") |
|
252 |
list_var_stat[[k]]<-stat |
|
253 |
} |
|
254 |
dat_var_stat<-do.call(rbind,list_var_stat) |
|
255 |
dat_var_stat$lf_names<-names(lf_list) |
|
256 |
dat_var_stat$dates<-dates[i] |
|
257 |
return(dat_var_stat) |
|
258 |
} |
|
259 |
|
|
260 |
###Parameters and arguments |
|
261 |
|
|
262 |
infile1<- "ghcn_or_tmax_covariates_06262012_OR83M.shp" #GHCN shapefile containing variables for modeling 2010 |
|
263 |
#infile2<-"list_10_dates_04212012.txt" #List of 10 dates for the regression |
|
264 |
infile2<-"list_365_dates_04212012.txt" #list of dates |
|
265 |
infile3<-"LST_dates_var_names.txt" #LST dates name |
|
266 |
#infile4<-"models_interpolation_05142012.txt" #Interpolation model names |
|
267 |
infile5<-"mean_day244_rescaled.rst" #mean LST for day 244 |
|
268 |
inlistf<-"list_files_05032012.txt" #list of raster images containing the Covariates |
|
269 |
infile6<-"OR83M_state_outline.shp" |
|
270 |
#stat_loc<-read.table(paste(path,"/","location_study_area_OR_0602012.txt",sep=""),sep=",", header=TRUE) |
|
271 |
|
|
272 |
obj_list<-"list_obj_01012013.txt" #Results of fusion from the run on ATLAS |
|
273 |
#obj_list<-"list_obj_08262012.txt" #Results of fusion from the run on ATLAS |
|
274 |
path<-"/home/parmentier/Data/IPLANT_project/methods_interpolation_comparison_01012013" #Jupiter LOCATION on Atlas for kriging #Jupiter Location on XANDERS |
|
275 |
setwd(path) |
|
276 |
proj_str="+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"; |
|
277 |
#Number of kriging model |
|
278 |
out_prefix<-"methods_comp_AAG2013_04082013_" #User defined output prefix |
|
279 |
|
|
280 |
filename<-sub(".shp","",infile1) #Removing the extension from file. |
|
281 |
ghcn<-readOGR(".", filename) #reading shapefile |
|
282 |
|
|
283 |
### PREPARING RASTER COVARIATES STACK ####### |
|
284 |
|
|
285 |
#CRS<-proj4string(ghcn) #Storing projection information (ellipsoid, datum,etc.) |
|
286 |
lines<-read.table(paste(path,"/",inlistf,sep=""), sep="") #Column 1 contains the names of raster files |
|
287 |
inlistvar<-lines[,1] #column 3 the list of models to use...? |
|
288 |
|
|
289 |
inlistvar<-paste(path,"/",as.character(inlistvar),sep="") |
|
290 |
covar_names<-as.character(lines[,2]) #Column two contains short names for covaraites |
|
291 |
|
|
292 |
s_raster<- stack(inlistvar) #Creating a stack of raster images from the list of variables. |
|
293 |
layerNames(s_raster)<-covar_names #Assigning names to the raster layers |
|
294 |
projection(s_raster)<-proj_str |
|
295 |
|
|
296 |
#Create mask |
|
297 |
pos<-match("LC10",layerNames(s_raster)) |
|
298 |
LC10<-subset(s_raster,pos) |
|
299 |
LC10[is.na(LC10)]<-0 #Since NA values are 0, we assign all zero to NA |
|
300 |
mask_land<-LC10<100 |
|
301 |
mask_land_NA<-mask_land |
|
302 |
mask_land_NA[mask_land_NA==0]<-NA |
|
303 |
|
|
304 |
data_name<-"mask_land_OR" |
|
305 |
raster_name<-paste(data_name,".rst", sep="") |
|
306 |
writeRaster(mask_land, filename=raster_name,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI) |
|
307 |
#writeRaster(r2, filename=raster_name,overwrite=TRUE) #Writing the data in a raster file format...(IDRISI) |
|
308 |
|
|
309 |
pos<-match("ELEV_SRTM",layerNames(s_raster)) |
|
310 |
ELEV_SRTM<-raster(s_raster,pos) |
|
311 |
elev<-ELEV_SRTM |
|
312 |
elev[-0.050<elev]<-NA #Remove all negative elevation lower than 50 meters... |
|
313 |
|
|
314 |
mask_elev_NA<-elev |
|
315 |
|
|
316 |
pos<-match("mm_01",layerNames(s_raster)) |
|
317 |
mm_01<-subset(s_raster,pos) |
|
318 |
mm_01<-mm_01-273.15 |
|
319 |
mm_01<-mask(mm_01,mask_land_NA) |
|
320 |
#mention this is the last... files |
|
321 |
|
|
322 |
##################### METHODS COMPARISON ########################### |
|
323 |
|
|
324 |
#######FIGURE 1: Boxplot comparison |
|
325 |
|
|
326 |
lines<-read.table(paste(path,"/",obj_list,sep=""), sep=",") #Column 1 contains the names RData objects |
|
327 |
inlistobj<-lines[,1] |
|
328 |
tinlistobj<-paste(path,"/",as.character(inlistobj),sep="") |
|
329 |
obj_names<-as.character(lines[,2]) #Column two contains short names for obj. model |
|
330 |
|
|
331 |
tmp44<-tb_metrics_fun(as.character(inlistobj),path,obj_names) |
|
332 |
#Condensed, and added other comparison, monthly comparison...:ok |
|
333 |
|
|
334 |
tb_list<-tmp44 |
|
335 |
mod_selected<-"" |
|
336 |
layout_plot<-c(1,5) |
|
337 |
#out_prefix<- |
|
338 |
#mean_methods<-plot_model_boxplot_fun(tb_list,path,obj_names,mod_selected,out_prefix) |
|
339 |
mean_methods_2<-plot_model_boxplot_combined_fun(tb_list,path,obj_names,mod_selected,out_prefix,layout_m=layout_plot) |
|
340 |
|
|
341 |
#######FIGURE 2: Map-Spatial patterns of interpolated surfaces |
|
342 |
|
|
343 |
lf_raster_fus<-"_365d_GAM_fus5_all_lstd_12302012.rst" |
|
344 |
#lf_raster_fus<-"_365d_GAM_fusion_all_lstd_12272012.rst" |
|
345 |
|
|
346 |
lf_raster_cai<-"_365d_GAM_CAI4_all_12272012.rst" |
|
347 |
date_selected<-"20100901" |
|
348 |
#titles<-list(cai=c("cai_kr","cai_mod5","cai mod8"), |
|
349 |
# fusion=c("fusion_kr"," fusion_mod5"," fusion_mod8")) |
|
350 |
|
|
351 |
mask_rast<-mask_elev_NA |
|
352 |
mod_selected1<-c(1,2,3,4,5,6,7,8,9,10) |
|
353 |
mod_selected2<-c(1) |
|
354 |
#lf_raster_fus<-file_pat1 |
|
355 |
#lf_raster_cai<-file_pat2 |
|
356 |
file_pat1<-glob2rx(paste("*tmax_predicted*",date_selected,"*",lf_raster_cai,sep="")) #Search for files in relation to fusion |
|
357 |
file_pat2<-glob2rx(paste("*tmax_predicted*",date_selected,"*",lf_raster_fus,sep="")) #Search for files in relation to fusion |
|
358 |
lf_cai<-list.files(pattern=file_pat1) #Search for files in relation to fusion |
|
359 |
lf_fus<-list.files(pattern=file_pat2) #Search for files in relation to fusion |
|
360 |
titles<-list(cai=c("CAI_kr","CAI_mod6"), |
|
361 |
fusion=c("FSS_kr")) |
|
362 |
titles<-list(c("cai_kr","cai_mod1","cai_mod2","cai_mod3","cai_mod4","cai_mod5","cai_mod6","cai_mod7","cai_mod8","cai_mod9","fss_kr")) |
|
363 |
r1<-stack(lf_cai[mod_selected1]) #CAI |
|
364 |
r2<-stack(lf_fus[mod_selected2])#FUS |
|
365 |
|
|
366 |
predictions<-stack(r1,r2) |
|
367 |
predictions<-mask(predictions,mask_rast) |
|
368 |
layerNames(predictions)<-unlist(titles) |
|
369 |
plot(predictions) |
|
370 |
layout_plot<-c(1,3) |
|
371 |
rast_pred<-raster_plots_interpolation_fun(file_pat1,file_pat2, |
|
372 |
mod_selected1,mod_selected2,titles,mask_rast,layout_plot,out_prefix) |
|
373 |
|
|
374 |
#######FIGURE 3: Map of transects |
|
375 |
|
|
376 |
nb_transect<-4 |
|
377 |
list_transect2<-vector("list",nb_transect) |
|
378 |
layers_names<-layerNames(rast_pred2)<-c("FSS_kr","fss_mod1","elev") |
|
379 |
pos<-c(1,2) # postions in the layer prection |
|
380 |
list_transect2[[1]]<-c("t1_line.shp",paste("figure_3_tmax_elevation_transect1_OR_",date_selected, |
|
381 |
paste(layers_names,collapse="_"),out_prefix,sep="_")) |
|
382 |
list_transect2[[2]]<-c("t2_line.shp",paste("figure_3_tmax_elevation_transect2_OR_",date_selected, |
|
383 |
paste(layers_names,collapse="_"),out_prefix,sep="_")) |
|
384 |
list_transect2[[3]]<-c("t3_line.shp",paste("figure_3_tmax_elevation_transect3_OR_",date_selected, |
|
385 |
paste(layers_names,collapse="_"),out_prefix,sep="_")) |
|
386 |
list_transect2[[4]]<-c("t4_line.shp",paste("figure_3_tmax_elevation_transect4_OR_",date_selected, |
|
387 |
paste(layers_names,collapse="_"),out_prefix,sep="_")) |
|
388 |
|
|
389 |
names(list_transect2)<-c("transect_OR1","transect_OR2","transect_OR3","transect_OR4") |
|
390 |
|
|
391 |
#X11(width=9,height=9) |
|
392 |
#png(paste("fig3_elevation_transect1_path_CAI_fusion_",date_selected,out_prefix,".png", sep="")) |
|
393 |
#plot(elev) |
|
394 |
#k<-1 #transect to plot |
|
395 |
#trans_file<-list_transect2[[k]][[1]] |
|
396 |
#filename<-sub(".shp","",trans_file) #Removing the extension from file. |
|
397 |
#transect<-readOGR(".", filename) #reading shapefile |
|
398 |
#plot(transect,add=TRUE) |
|
399 |
#title("Transect Oregon") |
|
400 |
#dev.off() |
|
401 |
|
|
402 |
#######FIGURE 4: Spatial transects profiles |
|
403 |
|
|
404 |
rast_pred<-predictions |
|
405 |
rast_pred_selected<-subset(rast_pred,pos) #3 is referring to FSS, plot it first because it has the |
|
406 |
# the largest range. |
|
407 |
rast_pred2<-stack(rast_pred_selected,elev) |
|
408 |
layerNames(rast_pred2)<-layers_names |
|
409 |
title_plot2<-paste(names(list_transect2),date_selected,sep=" ") |
|
410 |
title_plot2<-paste(rep("Oregon transect on ",3), date_selected,sep="") |
|
411 |
#r_stack<-rast_pred |
|
412 |
|
|
413 |
X11(width=18,height=9) |
|
414 |
m_layers_sc<-c(3) |
|
415 |
#title_plot2 |
|
416 |
#rast_pred2 |
|
417 |
trans_data2<-plot_transect_m2(list_transect2,rast_pred2,title_plot2,disp=TRUE,m_layers_sc) |
|
418 |
dev.off() |
|
419 |
|
|
420 |
#######FIGURE 5: Moran's profile... |
|
421 |
|
|
422 |
list_var_stat<-vector("list", 365) |
|
423 |
lf_raster_fus<-"^fusion_tmax_predicted.*_365d_GAM_fusion_all_lstd_12272012.rst$" |
|
424 |
lf_raster_cai<-"^CAI_tmax_predicted.*_365d_GAM_CAI4_all_12272012.rst$" |
|
425 |
lf2<-list.files(pattern=lf_raster_fus) |
|
426 |
lf1<-list.files(pattern=lf_raster_cai) |
|
427 |
|
|
428 |
mask_rast<-mask_elev_NA |
|
429 |
|
|
430 |
#out_prefix<-"test_01022013" |
|
431 |
#lf1<-list.files(pattern=".*CAI.*.rst$") |
|
432 |
#lf2<-list.files(pattern=".*fus5.*.rst$") |
|
433 |
lf_list<-as.data.frame(cbind(lf1[1:365],lf2[1:365])) |
|
434 |
lf_list[,1]<-as.character(lf1[1:365]) |
|
435 |
lf_list[,2]<-as.character(lf2[1:365]) |
|
436 |
names(lf_list)<-c("cai","fus") |
|
437 |
dates<-1:365 |
|
438 |
|
|
439 |
#var_stat_rast<-lapply(1:nrow(lf_list),stat_moran_std_raster_fun) |
|
440 |
var_stat_rast<-mclapply(1:nrow(lf_list),stat_moran_std_raster_fun,mc.preschedule=FALSE,mc.cores = 9) |
|
441 |
#gam_fus_mod_s<-mclapply(1:length(ghcn.subsets), runGAMFusion,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement |
|
442 |
|
|
443 |
var_stat<-do.call(rbind,var_stat_rast) |
|
444 |
|
|
445 |
var_stat_cai<-subset(var_stat,lf_names=="cai") |
|
446 |
var_stat_fus<-subset(var_stat,lf_names=="fus") |
|
447 |
|
|
448 |
#### NOW plot the average statistic per date... |
|
449 |
|
|
450 |
x1<-var_stat_fus$dates |
|
451 |
y1<-var_stat_fus$moranI |
|
452 |
x2<-var_stat_cai$dates |
|
453 |
y2<-var_stat_cai$moranI |
|
454 |
|
|
455 |
x_range<-range(c(x1,x2)) |
|
456 |
y_range<-range(c(y1,y2)) |
|
457 |
|
|
458 |
plot(x1,y1,type="l",col="black",ylim=y_range) |
|
459 |
lines(x2,y2,type="l",col="red") |
|
460 |
png(paste("fig5_IBS_moranI_",out_prefix,".png",sep="")) |
|
461 |
plot(x1,y1,type="l",col="black",xlab="Day Of Year", |
|
462 |
ylab="Moran's I",ylim=y_range) |
|
463 |
lines(x2,y2,type="l",col="red") |
|
464 |
title("Moran's I for 365 dates in 2010") |
|
465 |
t_col<-c("black","red") |
|
466 |
legend("topleft",legend=c("FSS_kr","CAI_kr"), |
|
467 |
cex=1.2, col=t_col,lty=1,bty="n") |
|
468 |
dev.off() |
|
469 |
|
|
470 |
## NOW PLOT STANDARD DEVIATION |
|
471 |
|
|
472 |
x1<-var_stat_fus$dates |
|
473 |
y1<-var_stat_fus$std |
|
474 |
x2<-var_stat_cai$dates |
|
475 |
y2<-var_stat_cai$std |
|
476 |
|
|
477 |
x_range<-range(c(x1,x2)) |
|
478 |
y_range<-range(c(y1,y2)) |
|
479 |
|
|
480 |
png(paste("fig5_IBS_std_",out_prefix,".png",sep="")) |
|
481 |
plot(x1,y1,type="l",col="black",xlab="Day Of Year", |
|
482 |
ylab=" Standard Deviation (degree C)",ylim=y_range) |
|
483 |
lines(x2,y2,type="l",col="red") |
|
484 |
title("Standard Deviation for 365 dates in 2010") |
|
485 |
t_col<-c("black","red") |
|
486 |
legend("topleft",legend=c("FSS_kr","CAI_kr"), |
|
487 |
cex=1.2, col=t_col,lty=1,bty="n") |
|
488 |
dev.off() |
|
489 |
|
|
490 |
#######FIGURE 6: LAND COVER PROFILES |
|
491 |
|
|
492 |
#######FIGURE 7: MULTISAMPLING |
|
493 |
|
|
494 |
### PART I MULTISAMPLING COMPARISON #### |
|
495 |
|
|
496 |
sampling_CAI<-load_obj("results2_CAI_sampling_obj_09132012_365d_GAM_CAI2_multisampling2.RData") |
|
497 |
sampling_fus<-load_obj("results2_fusion_sampling_obj_10d_GAM_fusion_multisamp4_09192012.RData") |
|
498 |
fus_CAI_mod<-load_obj("results2_CAI_Assessment_measure_all_09132012_365d_GAM_CAI2_multisampling2.RData") |
|
499 |
gam_fus_mod1<-load_obj("results2_fusion_Assessment_measure_all_10d_GAM_fusion_multisamp4_09192012.RData") |
|
500 |
|
|
501 |
tb_diagnostic2<-sampling_CAI$tb #Extracting the accuracy metric information... |
|
502 |
tb_diagnostic<-sampling_fus$tb |
|
503 |
|
|
504 |
tb_diagnostic[["prop"]]<-as.factor(tb_diagnostic[["prop"]]) |
|
505 |
tb_diagnostic2[["prop"]]<-as.factor(tb_diagnostic2[["prop"]]) |
|
506 |
|
|
507 |
#Preparing the data for the plot |
|
508 |
#fus data |
|
509 |
t<-melt(tb_diagnostic, |
|
510 |
measure=c("mod1","mod2","mod3","mod4", "mod5", "mod6", "mod7", "mod8","mod9"), |
|
511 |
id=c("dates","metric","prop"), |
|
512 |
na.rm=F) |
|
513 |
avg_tb<-cast(t,metric+prop~variable,mean) |
|
514 |
sd_tb<-cast(t,metric+prop~variable,sd) |
|
515 |
n_tb<-cast(t,metric+prop~variable,length) |
|
516 |
avg_tb[["prop"]]<-as.numeric(as.character(avg_tb[["prop"]])) |
|
517 |
avg_RMSE<-subset(avg_tb,metric=="RMSE") |
|
518 |
|
|
519 |
#CAI data |
|
520 |
t2<-melt(tb_diagnostic2, |
|
521 |
measure=c("mod1","mod2","mod3","mod4", "mod5", "mod6", "mod7", "mod8","mod9"), |
|
522 |
id=c("dates","metric","prop"), |
|
523 |
na.rm=F) |
|
524 |
avg_tb2<-cast(t2,metric+prop~variable,mean) |
|
525 |
sd_tb2<-cast(t2,metric+prop~variable,sd) |
|
526 |
n_tb2<-cast(t2,metric+prop~variable,length) |
|
527 |
avg_tb2[["prop"]]<-as.numeric(as.character(avg_tb2[["prop"]])) |
|
528 |
avg_RMSE2<-subset(avg_tb2,metric=="RMSE") |
|
529 |
|
|
530 |
#Select only information related to FUSION |
|
531 |
|
|
532 |
x<-avg_RMSE[["prop"]] |
|
533 |
i=9 |
|
534 |
mod_name<-paste("mod",i,sep="") |
|
535 |
y<-avg_RMSE[[mod_name]] |
|
536 |
|
|
537 |
sd_tb_RMSE <- subset(sd_tb, metric=="RMSE") |
|
538 |
x_sd<-sd_tb_RMSE[["prop"]] |
|
539 |
i=9 |
|
540 |
mod_name<-paste("mod",i,sep="") |
|
541 |
y_sd<-sd_tb_RMSE[[mod_name]] |
|
542 |
|
|
543 |
#Select only information related to CAI |
|
544 |
|
|
545 |
x2<-avg_RMSE2[["prop"]] |
|
546 |
i=9 |
|
547 |
mod_name<-paste("mod",i,sep="") |
|
548 |
y2<-avg_RMSE2[[mod_name]] |
|
549 |
|
|
550 |
sd_tb_RMSE2 <- subset(sd_tb2, metric=="RMSE") |
|
551 |
x_sd2<-sd_tb_RMSE2[["prop"]] |
|
552 |
i=9 |
|
553 |
mod_name<-paste("mod",i,sep="") |
|
554 |
y_sd2<-sd_tb_RMSE2[[mod_name]] |
|
555 |
|
|
556 |
n=150 |
|
557 |
ciw <- qt(0.975, n) * y_sd / sqrt(n) |
|
558 |
ciw2 <- qt(0.975, n) * y_sd2 / sqrt(n) |
|
559 |
|
|
560 |
#Comparison of MAE for different proportions for FUSION and CAI using CI |
|
561 |
X11() |
|
562 |
plotCI(y=y, x=x, uiw=ciw, col="red", main=" FUS: RMSE proportion of validation hold out", barcol="blue", lwd=1, |
|
563 |
ylab="RMSE (C)", xlab="Proportions of validation hold out (in %)") |
|
564 |
lines(x,y,col="red") |
|
565 |
legend("bottomright",legend=c("fus"), cex=1.2, col=c("red"), |
|
566 |
lty=1, title="RMSE") |
|
567 |
savePlot(paste("Comparison_multisampling_fus_RMSE_CI",out_prefix,".png", sep=""), type="png") |
|
568 |
|
|
569 |
plotCI(y=y2, x=x2, uiw=ciw2, col="black", main=" CAI: RMSE proportion of validation hold out", barcol="blue", lwd=1, |
|
570 |
ylab="RMSE (C)", xlab="Proportions of validation hold out (in %)") |
|
571 |
lines(x2,y2,col="grey") |
|
572 |
legend("bottomright",legend=c("CAI"), cex=1.2, col=c("grey"), |
|
573 |
lty=1, title="RMSE") |
|
574 |
savePlot(paste("Comparison_multisampling_CAI_RMSE_CI",out_prefix,".png", sep=""), type="png") |
|
575 |
dev.off() |
|
576 |
|
|
577 |
#Comparison of MAE for different proportions for FUSION and CAI |
|
578 |
X11() |
|
579 |
plot(x,y,col="red",type="b", ylab="RMSE (C)", xlab="Proportions of validation hold out (in %)") |
|
580 |
lines(x2,y2,col="grey") |
|
581 |
points(x2,y2,col="grey") |
|
582 |
title("MAE in terms of proportions and random sampling") |
|
583 |
legend("bottomright",legend=c("fus","CAI"), cex=1.2, col=c("red","grey"), |
|
584 |
lty=1, title="RMSE") |
|
585 |
savePlot(paste("Comparison_multisampling_fus_CAI_RMSE_averages",out_prefix,".png", sep=""), type="png") |
|
586 |
dev.off() |
|
587 |
|
|
588 |
#######FIGURE 8: SPATIAL ACCURACY AND DISTANCE TO CLOSEST STATIONS |
|
589 |
|
|
590 |
|
|
591 |
#### END OF THE SCRIPT ######### |
Also available in: Unified diff
IBS figures code re-use