Project

General

Profile

« Previous | Next » 

Revision f1b347e2

Added by Benoit Parmentier over 11 years ago

paper 1 manuscript: analyses, figures and tables -daily temp OR predictions

View differences:

climate/research/oregon/interpolation/contribution_of_covariates_paper_interpolation.R
1
####################################  INTERPOLATION OF TEMPERATURES  #######################################
2
############################  Script for manuscript analyses,tables and figures #######################################
3
#This script reads information concerning the Oregon case study to adapt data for the revised 
4
# interpolation code.
5
#Figures and data for the contribution of covariate paper are also produced.
6
#AUTHOR: Benoit Parmentier                                                                      #
7
#DATE: 08/02/2013            
8
#Version: 1
9
#PROJECT: Environmental Layers project                                       #
10
#################################################################################################
11

  
12
###Loading R library and packages                                                      
13
library(gtools)                              # loading some useful tools 
14
library(mgcv)                                # GAM package by Simon Wood
15
library(sp)                                  # Spatial pacakge with class definition by Bivand et al.
16
library(spdep)                               # Spatial pacakge with methods and spatial stat. by Bivand et al.
17
library(rgdal)                               # GDAL wrapper for R, spatial utilities
18
library(gstat)                               # Kriging and co-kriging by Pebesma et al.
19
library(fields)                              # NCAR Spatial Interpolation methods such as kriging, splines
20
library(raster)                              # Hijmans et al. package for raster processing
21
library(gdata)                               # various tools with xls reading
22
library(rasterVis)
23
library(parallel)
24
library(maptools)
25
library(maps)
26
library(reshape)
27
library(plotrix)
28

  
29
#### FUNCTION USED IN SCRIPT
30

  
31
load_obj <- function(f)
32
{
33
  env <- new.env()
34
  nm <- load(f, env)[1]
35
  env[[nm]]
36
}
37

  
38
extract_list_from_list_obj<-function(obj_list,list_name){
39
  #Create a list of an object from a given list of object using a name prodived as input
40
  
41
  list_tmp<-vector("list",length(obj_list))
42
  for (i in 1:length(obj_list)){
43
    tmp<-obj_list[[i]][[list_name]] #double bracket to return data.frame
44
    list_tmp[[i]]<-tmp
45
  }
46
  return(list_tmp) #this is  a data.frame
47
}
48

  
49
calc_stat_from_raster_prediction_obj <-function(raster_prediction_obj,stat){
50
  tb <-raster_prediction_obj$tb_diagnostic_v  #Kriging methods
51
  
52
  t<-melt(tb,
53
          measure=c("mae","rmse","r","m50"), 
54
          id=c("pred_mod"),
55
          na.rm=T)
56
  
57
  stat_tb<-cast(t,pred_mod~variable,stat)
58
  return(stat_tb)
59
}
60

  
61
#### Parameters and constants  
62

  
63

  
64
script_path<-"/home/parmentier/Data/IPLANT_project/env_layers_scripts/"
65
#source(file.path(script_path,"interpolation_method_day_function_multisampling_06082013.R")) #Include GAM_day
66

  
67
#in_dir<- "/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/output_data_365d_GAM_fus_all_lst_05312013"
68
in_dir1 <-"/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/output_data_365d_gam_day_lst_comb3_07092013/"
69
in_dir2 <-"/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/output_data_365d_gam_day_lst_comb4_07152013/"
70

  
71
#kriging results:
72
in_dir3 <-"/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/output_data_365d_kriging_day_lst_comb3_07112013"
73
#gwr results:
74
in_dir4 <-"/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/output_data_365d_gwr_day_lst_comb3_part1_07122013"
75
#multisampling results (gam)
76
in_dir5<- "/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/output_data_365d_gam_day_mults15_lst_comb3_07232013"
77
#in_dir<-"/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/output_data_365d_gam_day_mult_lst_comb3_07202013"
78

  
79
in_dir <- in_dir1
80
out_dir<-"/home/parmentier/Data/IPLANT_project/paper_analyses_tables_fig_08032013"
81
setwd(out_dir)
82

  
83
y_var_name <- "dailyTmax"
84

  
85
out_prefix<-"analyses_08032013"
86

  
87
method_interpolation <- "gam_daily"
88
covar_obj_file1 <- "covar_obj__365d_gam_day_lst_comb3_07092013.RData"
89

  
90
#raster_prediciton object for baseline 1 () s(lat,lon) + s(elev)) and baseline 2 (slat,lon))
91
raster_obj_file_1 <- "raster_prediction_obj_gam_daily_dailyTmax_365d_gam_day_lst_comb3_07092013.RData" 
92
raster_obj_file_2 <- "raster_prediction_obj_gam_daily_dailyTmax_365d_gam_day_lst_comb4_07152013.RData"
93

  
94
raster_obj_file_3 <- "raster_prediction_obj_kriging_daily_dailyTmax_365d_kriging_day_lst_comb3_07112013.RData"
95
raster_obj_file_4 <- "raster_prediction_obj_gwr_daily_dailyTmax_365d_gwr_day_lst_comb3_part1_07122013.RData"
96
raster_obj_file_5 <- "raster_prediction_obj_gam_daily_dailyTmax_365d_gam_day_mult_lst_comb3_07202013.RData"
97

  
98
#Load objects containing training, testing, models objects 
99

  
100
covar_obj <-load_obj(file.path(in_dir1,covar_obj_file1))
101
infile_covariates <- covar_obj$infile_covariates
102
infile_reg_outline <- covar_obj$infile_reg_outline
103
covar_names<- covar_obj$covar_names
104
#####
105
s_raster <- brick(file.path(in_dir1,infile_covariates))
106
names(s_raster)<-covar_names
107

  
108
raster_prediction_obj_1 <-load_obj(file.path(in_dir1,raster_obj_file_1))
109
raster_prediction_obj_2 <-load_obj(file.path(in_dir2,raster_obj_file_2))
110
raster_prediction_obj_3 <-load_obj(file.path(in_dir3,raster_obj_file_3))
111
raster_prediction_obj_4 <-load_obj(file.path(in_dir4,raster_obj_file_4))
112

  
113
names(raster_prediction_obj_1) #list of two objects
114

  
115
### ACCURACY TABLE WITH BASELINES
116

  
117
#Check input covariates and model formula:
118
raster_prediction_obj_1$method_mod_obj[[1]]$formulas
119
raster_prediction_obj_2$method_mod_obj[[1]]$formulas
120

  
121
#baseline 1:
122

  
123
summary_metrics_v1<-raster_prediction_obj_1$summary_metrics_v
124
summary_metrics_v2<-raster_prediction_obj_2$summary_metrics_v
125

  
126
table_data1 <-summary_metrics_v1$avg[,c("mae","rmse","me","r")]
127
table_data2 <-summary_metrics_v2$avg[,c("mae","rmse","me","r")]
128

  
129
model_col<-c("Baseline","Northing","Easting","LST","DISTOC","Forest","CANHEIGHT","LST*Forest") # removed ,"LST*CANHEIGHT")
130
names_table_col<-c("DiffMAE","DiffRMSE","DiffME","Diffr","Model")
131

  
132
df1<- as.data.frame(sapply(table_data1,FUN=function(x) x-x[1]))
133
df1<- round(df1,digit=3) #roundto three digits teh differences
134
df1$Model <-model_col
135
names(df1)<- names_table_col
136
df1
137

  
138
model_col<-c("Baseline","Elevation","Northing","Easting","LST","DISTOC","Forest","CANHEIGHT") #,"LST*Forest") # removed ,"LST*CANHEIGHT")
139
df2<- as.data.frame(sapply(table_data2,FUN=function(x) x-x[1]))
140
df2<- round(df2,digit=3) #roundto three digits teh differences
141
df2$Model <-model_col
142
names(df2)<- names_table_col
143
df2
144

  
145
file_name<-paste("table3a_paper","_",out_prefix,".txt",sep="")
146
write.table(df1,file=file_name,sep=",")
147

  
148
file_name<-paste("table3b_paper","_",out_prefix,".txt",sep="")
149
write.table(df2,file=file_name,sep=",")
150

  
151
##Table 4: Interpolation methods comparison
152

  
153
#get sd for kriging, gam and gwr
154
tb1 <-raster_prediction_obj_1$tb_diagnostic_v  #Kriging methods
155
tb2 <-raster_prediction_obj_2$tb_diagnostic_v  #Kriging methods
156
tb3 <-raster_prediction_obj_3$tb_diagnostic_v  #Kriging methods
157
tb4 <-raster_prediction_obj_4$tb_diagnostic_v  #Kriging methods
158

  
159
names_mod<-c("mod1","mod2","mod3","mod4","mod5","mod6","mod7","mod8","mod9")
160

  
161

  
162
sd1 <- calc_stat_from_raster_prediction_obj(raster_prediction_obj_1,"sd")
163
sd2 <- calc_stat_from_raster_prediction_obj(raster_prediction_obj_2,"sd")
164
sd3 <- calc_stat_from_raster_prediction_obj(raster_prediction_obj_3,"sd")
165
sd4 <- calc_stat_from_raster_prediction_obj(raster_prediction_obj_4,"sd")
166

  
167
table_sd<-rbind(sd1[1,],sd3[1,])
168
table_sd<-rbind(table_sd,sd4[1,])
169

  
170
summary_metrics_v3<-raster_prediction_obj_3$summary_metrics_v  #Kriging methods
171
summary_metrics_v4<-raster_prediction_obj_4$summary_metrics_v  # GWR method
172

  
173
table_data3 <-summary_metrics_v3$avg[1,c("mae","rmse","me","r")] #first line mod1 (baseline)
174
table_data4 <-summary_metrics_v4$avg[1,c("mae","rmse","me","r")] #first line mod1 (baseline)
175
table_data1 <- table_data1[1,]
176

  
177
table<-rbind(table_data1,table_data3)
178
table<-rbind(table,table_data4)
179
table<- round(table,digit=3) #roundto three digits teh differences
180

  
181
model_col<-c("GAM","Kriging","GWR")
182
names_table_col<-c("MAE","RMSE","ME","R","Model")
183

  
184
table$Model <-model_col
185
names(table)<- names_table_col
186
table
187

  
188
file_name<-paste("table4_avg_paper","_",out_prefix,".txt",sep="")
189
write.table(table,file=file_name,sep=",")
190

  
191
file_name<-paste("table34_sd_paper","_",out_prefix,".txt",sep="")
192
write.table(table_sd,file=file_name,sep=",")
193

  
194
#for(i in nrow(table))
195
#mean_val<-table[i,j]
196
#sd_val<-table_sd[i,j]
197
#element<-paste(mean_val,"+-",sd_val,sep="")
198
#table__paper[i,j]<-element
199

  
200
#########################
201
####### Now create figures ###
202

  
203
#figure 1: study area
204
#figure 2: methodological worklfow
205
#figure 3:Figure 3. MAE/RMSE and distance to closest fitting station.
206
#Figure 4. RMSE and MAE, mulitisampling and hold out for FSS and GAM.
207
#Figure 5. Overtraining tendency
208

  
209
### Figure 1
210

  
211

  
212
### Figure 2
213

  
214
# ANALYSES 1: ACCURACY IN TERMS OF DISTANCE TO CLOSEST STATIONS...
215
#?? for all models gam or only interpolation methods??
216

  
217
tb1<- raster_prediction_obj_1$tb_diagnostic_v
218

  
219
names(raster_prediction_obj$validation_mod_obj[[1]])
220

  
221
list_data_s <- extract_list_from_list_obj(raster_prediction_obj$validation_mod_obj,"data_s")
222
list_data_v <- extract_list_from_list_obj(raster_prediction_obj$validation_mod_obj,"data_v")
223

  
224
names_mod <- paste("res_mod",1:9,sep="")

Also available in: Unified diff