Revision 3ab94e28
Added by Benoit Parmentier over 11 years ago
climate/research/oregon/interpolation/contribution_of_covariates_paper_interpolation_functions.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/15/2013 |
|
8 |
#Version: 2 |
|
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 |
library(plyr) |
|
29 |
|
|
30 |
#### FUNCTION USED IN SCRIPT |
|
31 |
|
|
32 |
function_analyses_paper <-"contribution_of_covariates_paper_interpolation_functions_08152013.R" |
|
33 |
|
|
34 |
load_obj <- function(f) |
|
35 |
{ |
|
36 |
env <- new.env() |
|
37 |
nm <- load(f, env)[1] |
|
38 |
env[[nm]] |
|
39 |
} |
|
40 |
|
|
41 |
extract_list_from_list_obj<-function(obj_list,list_name){ |
|
42 |
#Create a list of an object from a given list of object using a name prodived as input |
|
43 |
|
|
44 |
list_tmp<-vector("list",length(obj_list)) |
|
45 |
for (i in 1:length(obj_list)){ |
|
46 |
tmp<-obj_list[[i]][[list_name]] #double bracket to return data.frame |
|
47 |
list_tmp[[i]]<-tmp |
|
48 |
} |
|
49 |
return(list_tmp) #this is a data.frame |
|
50 |
} |
|
51 |
|
|
52 |
#This extract a data.frame object from raster prediction obj and combine them in one data.frame |
|
53 |
extract_from_list_obj<-function(obj_list,list_name){ |
|
54 |
#extract object from list of list. This useful for raster_prediction_obj |
|
55 |
library(plyr) |
|
56 |
|
|
57 |
list_tmp<-vector("list",length(obj_list)) |
|
58 |
for (i in 1:length(obj_list)){ |
|
59 |
tmp<-obj_list[[i]][[list_name]] #double bracket to return data.frame |
|
60 |
list_tmp[[i]]<-tmp |
|
61 |
} |
|
62 |
tb_list_tmp<-do.call(rbind.fill,list_tmp) #long rownames |
|
63 |
#tb_list_tmp<-do.call(rbind,list_tmp) #long rownames |
|
64 |
|
|
65 |
return(tb_list_tmp) #this is a data.frame |
|
66 |
} |
|
67 |
|
|
68 |
calc_stat_from_raster_prediction_obj <-function(raster_prediction_obj,stat){ |
|
69 |
tb <-raster_prediction_obj$tb_diagnostic_v #Kriging methods |
|
70 |
|
|
71 |
t<-melt(tb, |
|
72 |
measure=c("mae","rmse","r","me","m50"), |
|
73 |
id=c("pred_mod"), |
|
74 |
na.rm=T) |
|
75 |
|
|
76 |
stat_tb<-cast(t,pred_mod~variable,stat) |
|
77 |
return(stat_tb) |
|
78 |
} |
|
79 |
|
|
80 |
## Concatenate datal.frames with numbers... |
|
81 |
table_combined_symbol <-function(dfrm1,dfrm2,symbol_char){ |
|
82 |
#concatenate element in a table format combining elements from 2 tables/data.frames |
|
83 |
#For instance an element can become: 2.3±0.32 |
|
84 |
#Note that it is assumed that dfrm1 and dfrm2 have the same size/dimension!! |
|
85 |
|
|
86 |
rows<-nrow(dfrm1) |
|
87 |
cols<-ncol(dfrm1) |
|
88 |
#symbol_char <- "±" #set in arguments |
|
89 |
|
|
90 |
table_combined<-dfrm1 |
|
91 |
|
|
92 |
for (i in 1:rows){ |
|
93 |
for (j in 1:cols){ |
|
94 |
table_combined[i,j]<-paste(dfrm1[i,j],symbol_char,dfrm2[i,j],sep="") #on ± windows xp char "\261", what is it on ubuntu? |
|
95 |
} |
|
96 |
} |
|
97 |
return(table_combined) |
|
98 |
} |
|
99 |
|
|
100 |
## Produce data.frame with residuals for models and distance to closest fitting station |
|
101 |
calc_dist_ref_data_point <- function(i,list_param){ |
|
102 |
#This function creates a list of data.frame containing the distance to teh closest |
|
103 |
# reference point (e.g. fitting station) for a give data frame. |
|
104 |
#Inputs: |
|
105 |
#data_s: given data.frame from wich distance is computed |
|
106 |
#data_v: reference data.frame, destination, often the fitting points used in analyses |
|
107 |
#i: index variable to operate on list |
|
108 |
#names_var: |
|
109 |
#Outputs: |
|
110 |
#list_dstspat_er |
|
111 |
|
|
112 |
#Parsing input arguments |
|
113 |
data_s<-list_param$data_s[[i]] |
|
114 |
data_v<-list_param$data_v[[i]] |
|
115 |
|
|
116 |
names_var<-list_param$names_var |
|
117 |
|
|
118 |
###### |
|
119 |
|
|
120 |
names_var<-intersect(names_var,names(data_v)) #there may be missing columns |
|
121 |
#use columns that exists |
|
122 |
|
|
123 |
d_s_v<-matrix(0,nrow(data_v),nrow(data_s)) |
|
124 |
for(k in 1:nrow(data_s)){ |
|
125 |
pt<-data_s[k,] |
|
126 |
d_pt<-(spDistsN1(data_v,pt,longlat=FALSE))/1000 #Distance to station k in km |
|
127 |
d_s_v[,k]<-d_pt |
|
128 |
} |
|
129 |
|
|
130 |
#Create data.frame with position, ID, dst and residuals... |
|
131 |
|
|
132 |
pos<-vector("numeric",nrow(data_v)) |
|
133 |
y<-vector("numeric",nrow(data_v)) |
|
134 |
dst<-vector("numeric",nrow(data_v)) |
|
135 |
|
|
136 |
for (k in 1:nrow(data_v)){ |
|
137 |
pos[k]<-match(min(d_s_v[k,]),d_s_v[k,]) |
|
138 |
dst[k]<-min(d_s_v[k,]) |
|
139 |
} |
|
140 |
|
|
141 |
dstspat_er<-as.data.frame(cbind(v_id=as.vector(data_v$id),s_id=as.vector(data_s$id[pos]), |
|
142 |
pos=pos, lat=data_v$lat, lon=data_v$lon, x=data_v$x,y=data_v$y, |
|
143 |
dst=dst, |
|
144 |
as.data.frame(data_v[,names_var]))) |
|
145 |
|
|
146 |
return(dstspat_er) |
|
147 |
} |
|
148 |
|
|
149 |
### Main function to call to obtain distance to closest fitting stations for valiation dataset |
|
150 |
distance_to_closest_fitting_station<-function(raster_prediction_obj,names_mod,dist_classes=c(0)){ |
|
151 |
#This function computes the distance between training and testing points and returns and data frame |
|
152 |
#with distance,residuals, ID of training and testing points |
|
153 |
#Input: raster_prediction_obj, names of residuals models to return, distance classes |
|
154 |
#Output: data frame |
|
155 |
|
|
156 |
#Functions used in the script |
|
157 |
|
|
158 |
mae_fun<-function(x){mean(abs(x))} #Mean Absolute Error give a residuals vector |
|
159 |
sd_abs_fun<-function(x){sd(abs(x))} #sd Absolute Error give a residuals vector |
|
160 |
rmse_fun<-function(x){sqrt(mean(x^2))} #Mean Absolute Error give a residuals vector |
|
161 |
|
|
162 |
#calc_dist_ref_data_point : defined outside this function |
|
163 |
|
|
164 |
##BEGIN |
|
165 |
|
|
166 |
##### PART I: generate data.frame with residuals in term of distance to closest fitting station |
|
167 |
|
|
168 |
#return list of training and testing data frames |
|
169 |
list_data_s <- extract_list_from_list_obj(raster_prediction_obj$validation_mod_obj,"data_s") #training |
|
170 |
list_data_v <- extract_list_from_list_obj(raster_prediction_obj$validation_mod_obj,"data_v") #testing (validation) |
|
171 |
|
|
172 |
i<-1 |
|
173 |
names_var<-c(names_mod,"dates") |
|
174 |
list_param_dst<-list(i,list_data_s,list_data_v,names_mod) |
|
175 |
names(list_param_dst) <- c("index","data_s","data_v","names_var") |
|
176 |
|
|
177 |
#call function "calc_dist_ref_data_point" over 365 dates |
|
178 |
#note that this function depends on other functions !!! see script |
|
179 |
|
|
180 |
list_dstspat_er <-lapply(1:length(list_data_v),FUN=calc_dist_ref_data_point,list_param=list_param_dst) |
|
181 |
#now assemble in one data.frame |
|
182 |
dstspat_dat<-do.call(rbind.fill,list_dstspat_er) |
|
183 |
|
|
184 |
########### PART II: generate distance classes and summary statistics |
|
185 |
|
|
186 |
if (length(dist_classes)==1){ |
|
187 |
range_val<-range(dstspat_dat$dst) |
|
188 |
max_val<-round_any(range_val[2],10, f=ceiling) #round max value to nearest 10 (from plyr package) |
|
189 |
min_val<-0 |
|
190 |
limit_val<-seq(min_val,max_val, by=10) |
|
191 |
}else{ |
|
192 |
limit_val<-dist_classes |
|
193 |
} |
|
194 |
|
|
195 |
dstspat_dat$dst_cat1 <- cut(dstspat_dat$dst,include.lowest=TRUE,breaks=limit_val) |
|
196 |
|
|
197 |
names_var <- intersect(names_mod,names(dstspat_dat)) #some of the models may not have been predictd so subselect |
|
198 |
t<-melt(dstspat_dat, |
|
199 |
measure=names_var, |
|
200 |
id=c("dst_cat1"), |
|
201 |
na.rm=T) |
|
202 |
|
|
203 |
mae_tb <-cast(t,dst_cat1~variable,mae_fun) |
|
204 |
rmse_tb <-cast(t,dst_cat1~variable,rmse_fun) |
|
205 |
sd_abs_tb<-cast(t,dst_cat1~variable,sd_abs_fun) |
|
206 |
|
|
207 |
avg_tb<-cast(t,dst_cat1~variable,mean) |
|
208 |
sd_tb<-cast(t,dst_cat1~variable,sd) |
|
209 |
n_tb<-cast(t,dst_cat1~variable,length) |
|
210 |
#n_NA<-cast(t,dst_cat1~variable,is.na) |
|
211 |
|
|
212 |
#### prepare returning object |
|
213 |
dstspat_obj<-list(dstspat_dat,mae_tb,rmse_tb,sd_abs_tb,avg_tb,sd_tb,n_tb) |
|
214 |
names(dstspat_obj) <-c("dstspat_dat","mae_tb","rmse_tb","sd_abs_tb","avg_tb","sd_tb","n_tb") |
|
215 |
|
|
216 |
return(dstspat_obj) |
|
217 |
|
|
218 |
} |
|
219 |
|
|
220 |
# create plot of accuracy in term of distance to closest fitting station |
|
221 |
plot_dst_spat_fun<-function(stat_tb,names_var,cat_val){ |
|
222 |
|
|
223 |
range_y<-range(as.vector(unlist(stat_tb[,names_var])),na.rm=T) #flatten data.frame |
|
224 |
col_t<-rainbow(length(names_var)) |
|
225 |
pch_t<- 1:length(names_var) |
|
226 |
plot(stat_tb[,names_var[1]], ylim=range_y,pch=pch_t[1],col=col_t[1],type="b", |
|
227 |
yla="MAE (in degree C)",xlab="",xaxt="n") |
|
228 |
#points((stat_tb[,names_var[k]], ylim=range_y,pch=pch_t[1],col=col_t[1]),type="p") |
|
229 |
for (k in 2:length(names_var)){ |
|
230 |
lines(stat_tb[,names_var[k]], ylim=range_y,pch=pch_t[k],col=col_t[k],type="b", |
|
231 |
xlab="",axes=F) |
|
232 |
#points((stat_tb[,names_var[k]], ylim=range_y,pch=pch_t[k],col=col_t[k]),type="p") |
|
233 |
} |
|
234 |
legend("topleft",legend=names_var, |
|
235 |
cex=1.2, pch=pch_t,col=col_t,lty=1,bty="n") |
|
236 |
axis(1,at=1:length(stat_tb[,1]),labels=stat_tb[,1]) |
|
237 |
} |
|
238 |
|
|
239 |
plot_dst_MAE <-function(list_param){ |
|
240 |
# |
|
241 |
#list_dist_obj: list of dist object |
|
242 |
#col_t: list of color for each |
|
243 |
#pch_t: symbol for line |
|
244 |
#legend_text: text for line and symbol |
|
245 |
#mod_name: selected models |
|
246 |
# |
|
247 |
## BEGIN ## |
|
248 |
|
|
249 |
list_dist_obj<-list_param$list_dist_obj |
|
250 |
col_t<-list_param$col_t |
|
251 |
pch_t<- list_param$pch_t |
|
252 |
legend_text <- list_param$legend_text |
|
253 |
list_mod_name<-list_param$mod_name |
|
254 |
metric_name <-list_param$metric_name |
|
255 |
title_plot <- list_param$title_plot |
|
256 |
y_lab_text <- list_param$y_lab_text |
|
257 |
|
|
258 |
for (i in 1:length(list_dist_obj)){ |
|
259 |
|
|
260 |
l<-list_dist_obj[[i]] |
|
261 |
metric_tb<-l[[metric_name]] |
|
262 |
n_tb<-l$n_tb |
|
263 |
sd_abs_tb<-l$sd_abs_tb |
|
264 |
mod_name<-list_mod_name[i] |
|
265 |
#xlab_text<-"distance to fitting station (km) " |
|
266 |
|
|
267 |
n <- unlist(n_tb[,c(mod_name)]) |
|
268 |
y <- unlist(metric_tb[,c(mod_name)]) |
|
269 |
|
|
270 |
#x<- 1:length(y) |
|
271 |
x <- x_tick_labels |
|
272 |
y_sd <- unlist(sd_abs_tb[,c(mod_name)]) |
|
273 |
|
|
274 |
ciw <-y_sd |
|
275 |
#ciw2 <- qt(0.975, n) * y_sd2 / sqrt(n) |
|
276 |
ciw <- qt(0.975, n) * y_sd / sqrt(n) |
|
277 |
|
|
278 |
if(i==1){ |
|
279 |
plotCI(y=y, x=x, uiw=ciw, col=col_t[i], barcol="blue", lwd=1, |
|
280 |
ylab=y_lab_text, xlab="") |
|
281 |
lines(y~x, pch=pch_t[i],col=col_t[i],type="b") |
|
282 |
|
|
283 |
}else{ |
|
284 |
lines(y~x, pch=pch_t[i],col=col_t[i],type="b") |
|
285 |
} |
|
286 |
|
|
287 |
} |
|
288 |
legend("topleft",legend=legend_text, |
|
289 |
cex=1.2, pch=pch_t,col=col_t,lty=1,bty="n") |
|
290 |
#axis(1,at=1:length(stat_tb[,1]),labels=stat_tb[,1]) |
|
291 |
title(title_plot) |
|
292 |
#paste(" Comparison of MAE in ",mod_name,sep="") |
|
293 |
|
|
294 |
} |
|
295 |
|
|
296 |
calc_stat_prop_tb <-function(names_mod,raster_prediction_obj,testing=TRUE){ |
|
297 |
|
|
298 |
#add for testing?? |
|
299 |
if (testing==TRUE){ |
|
300 |
tb <-raster_prediction_obj$tb_diagnostic_v #use testing accuracy information |
|
301 |
}else{ |
|
302 |
tb <-raster_prediction_obj$tb_diagnostic_s #use training accuracy information |
|
303 |
} |
|
304 |
|
|
305 |
t<-melt(subset(tb,pred_mod==names_mod), |
|
306 |
measure=c("mae","rmse","r","me","m50"), |
|
307 |
id=c("pred_mod","prop"), |
|
308 |
na.rm=T) |
|
309 |
|
|
310 |
avg_tb<-cast(t,pred_mod+prop~variable,mean) |
|
311 |
sd_tb<-cast(t,pred_mod+prop~variable,sd) |
|
312 |
n_tb<-cast(t,pred_mod+prop~variable,length) |
|
313 |
#n_NA<-cast(t,dst_cat1~variable,is.na) |
|
314 |
|
|
315 |
#### prepare returning object |
|
316 |
prop_obj<-list(tb,avg_tb,sd_tb,n_tb) |
|
317 |
names(prop_obj) <-c("tb","avg_tb","sd_tb","n_tb") |
|
318 |
|
|
319 |
return(prop_obj) |
|
320 |
} |
|
321 |
|
|
322 |
#ploting |
|
323 |
plot_prop_metrics <-function(list_param){ |
|
324 |
# |
|
325 |
#list_dist_obj: list of dist object |
|
326 |
#col_t: list of color for each |
|
327 |
#pch_t: symbol for line |
|
328 |
#legend_text: text for line and symbol |
|
329 |
#mod_name: selected models |
|
330 |
# |
|
331 |
## BEGIN ## |
|
332 |
|
|
333 |
list_obj<-list_param$list_prop_obj |
|
334 |
col_t <-list_param$col_t |
|
335 |
pch_t <- list_param$pch_t |
|
336 |
legend_text <- list_param$legend_text |
|
337 |
list_mod_name<-list_param$mod_name |
|
338 |
metric_name<-list_param$metric_name |
|
339 |
|
|
340 |
for (i in 1:length(list_obj)){ |
|
341 |
|
|
342 |
l<-list_obj[[i]] |
|
343 |
mod_name<-list_mod_name[i] |
|
344 |
avg_tb<-subset(l$avg_tb,pred_mod==mod_name,select=metric_name) #selecte relevant accuarcy metric |
|
345 |
n_tb<-subset(l$n_tb,pred_mod==mod_name,select=metric_name) |
|
346 |
sd_tb<-subset(l$sd_tb,pred_mod==mod_name,select=metric_name) #l$sd_abs_tb[,metric_name] |
|
347 |
|
|
348 |
xlab_text<-"holdout proportion" |
|
349 |
|
|
350 |
no <- unlist(as.data.frame(n_tb)) |
|
351 |
y <- unlist(as.data.frame(avg_tb)) |
|
352 |
|
|
353 |
x<- l$avg_tb$prop |
|
354 |
y_sd <- unlist(as.data.frame(sd_tb)) #sd_tb |
|
355 |
|
|
356 |
ciw <-y_sd |
|
357 |
#ciw2 <- qt(0.975, n) * y_sd2 / sqrt(n) |
|
358 |
|
|
359 |
#plotCI(y=y, x=x, uiw=ciw, col="red", main=paste(" MAE for ",mod_name,sep=""), barcol="blue", lwd=1, |
|
360 |
# ylab="RMSE (C)", xlab=xlab_text) |
|
361 |
|
|
362 |
ciw <- qt(0.975, no) * y_sd / sqrt(no) |
|
363 |
|
|
364 |
if(i==1){ |
|
365 |
plotCI(y=y, x=x, uiw=ciw, col=col_t[i], main=paste(" Comparison of ",metric_name," in ",mod_name,sep=""), barcol="blue", lwd=1, |
|
366 |
ylab="RMSE (C)", xlab=xlab_text) |
|
367 |
lines(y~x, col=col_t[i]) |
|
368 |
|
|
369 |
}else{ |
|
370 |
lines(y~x, col=col_t[i]) |
|
371 |
} |
|
372 |
|
|
373 |
} |
|
374 |
legend("topleft",legend=legend_text, |
|
375 |
cex=1.2, pch=pch_t,col=col_t,lty=1,bty="n") |
|
376 |
#axis(1,at=1:length(stat_tb[,1]),labels=stat_tb[,1]) |
|
377 |
|
|
378 |
} |
|
379 |
|
|
380 |
plot_dst_spat_fun<-function(stat_tb,names_var,cat_val){ |
|
381 |
|
|
382 |
range_y<-range(as.vector(unlist(stat_tb[,names_var])),na.rm=T) #flatten data.frame |
|
383 |
col_t<-rainbow(length(names_var)) |
|
384 |
pch_t<- 1:length(names_var) |
|
385 |
plot(stat_tb[,names_var[1]], ylim=range_y,pch=pch_t[1],col=col_t[1],type="b", |
|
386 |
yla="MAE (in degree C)",xlab="",xaxt="n") |
|
387 |
#points((stat_tb[,names_var[k]], ylim=range_y,pch=pch_t[1],col=col_t[1]),type="p") |
|
388 |
for (k in 2:length(names_var)){ |
|
389 |
lines(stat_tb[,names_var[k]], ylim=range_y,pch=pch_t[k],col=col_t[k],type="b", |
|
390 |
xlab="",axes=F) |
|
391 |
#points((stat_tb[,names_var[k]], ylim=range_y,pch=pch_t[k],col=col_t[k]),type="p") |
|
392 |
} |
|
393 |
legend("topleft",legend=names_var, |
|
394 |
cex=1.2, pch=pch_t,col=col_t,lty=1,bty="n") |
|
395 |
axis(1,at=1:length(stat_tb[,1]),labels=stat_tb[,1]) |
|
396 |
} |
|
397 |
|
|
398 |
plot_prop_metrics <-function(list_param){ |
|
399 |
# |
|
400 |
#list_dist_obj: list of dist object |
|
401 |
#col_t: list of color for each |
|
402 |
#pch_t: symbol for line |
|
403 |
#legend_text: text for line and symbol |
|
404 |
#mod_name: selected models |
|
405 |
# |
|
406 |
## BEGIN ## |
|
407 |
|
|
408 |
list_obj<-list_param$list_prop_obj |
|
409 |
col_t <-list_param$col_t |
|
410 |
pch_t <- list_param$pch_t |
|
411 |
legend_text <- list_param$legend_text |
|
412 |
list_mod_name<-list_param$mod_name |
|
413 |
metric_name<-list_param$metric_name |
|
414 |
|
|
415 |
for (i in 1:length(list_obj)){ |
|
416 |
|
|
417 |
l<-list_obj[[i]] |
|
418 |
mod_name<-list_mod_name[i] |
|
419 |
avg_tb<-subset(l$avg_tb,pred_mod==mod_name,select=metric_name) #selecte relevant accuarcy metric |
|
420 |
n_tb<-subset(l$n_tb,pred_mod==mod_name,select=metric_name) |
|
421 |
sd_tb<-subset(l$sd_tb,pred_mod==mod_name,select=metric_name) #l$sd_abs_tb[,metric_name] |
|
422 |
|
|
423 |
#xlab_text<-"holdout proportion" |
|
424 |
|
|
425 |
no <- unlist(as.data.frame(n_tb)) |
|
426 |
y <- unlist(as.data.frame(avg_tb)) |
|
427 |
|
|
428 |
x<- l$avg_tb$prop |
|
429 |
y_sd <- unlist(as.data.frame(sd_tb)) #sd_tb |
|
430 |
|
|
431 |
ciw <-y_sd |
|
432 |
#ciw2 <- qt(0.975, n) * y_sd2 / sqrt(n) |
|
433 |
ciw <- qt(0.975, no) * y_sd / sqrt(no) |
|
434 |
|
|
435 |
if(i==1){ |
|
436 |
plotCI(y=y, x=x, uiw=ciw, col=col_t[i], barcol="blue", lwd=1, |
|
437 |
ylab="", xlab="") |
|
438 |
lines(y~x, col=col_t[i],pch=pch_t[i],type="b") |
|
439 |
}else{ |
|
440 |
lines(y~x, col=col_t[i],pch=pch_t[i],type="b") |
|
441 |
} |
|
442 |
|
|
443 |
} |
|
444 |
legend("topleft",legend=legend_text, |
|
445 |
cex=1.2, pch=pch_t,col=col_t,lty=1,bty="n") |
|
446 |
#axis(1,at=1:length(stat_tb[,1]),labels=stat_tb[,1]) |
|
447 |
|
|
448 |
} |
|
449 |
|
|
450 |
#Calculate the difference between training and testing in two different data.frames. Columns to substract are provided. |
|
451 |
diff_df<-function(tb_s,tb_v,list_metric_names){ |
|
452 |
tb_diff<-vector("list", length(list_metric_names)) |
|
453 |
for (i in 1:length(list_metric_names)){ |
|
454 |
metric_name<-list_metric_names[i] |
|
455 |
tb_diff[[i]] <-tb_s[,c(metric_name)] - tb_v[,c(metric_name)] |
|
456 |
} |
|
457 |
names(tb_diff)<-list_metric_names |
|
458 |
tb_diff<-as.data.frame(do.call(cbind,tb_diff)) |
|
459 |
return(tb_diff) |
|
460 |
} |
|
461 |
|
|
462 |
create_s_and_p_table_term_models <-function(i,list_myModels){ |
|
463 |
#Purpose: |
|
464 |
#Function to extract smooth term table,parameter table and AIC from a list of models. |
|
465 |
#Originally created to processed GAM models run over a full year. |
|
466 |
#Inputs: |
|
467 |
#1) list_myModels: list of fitted GAM models |
|
468 |
#2) i: index of list to run with lapply or mcapply |
|
469 |
#Outputs: list of |
|
470 |
#1)s.table.term |
|
471 |
#2)p.table.term |
|
472 |
#3)AIC list |
|
473 |
#Authors: Benoit Parmentier |
|
474 |
#date: 08/142013 |
|
475 |
|
|
476 |
### Functions used in the scritp: |
|
477 |
|
|
478 |
#Remove models that were not fitted from the list |
|
479 |
#All modesl that are "try-error" are removed |
|
480 |
remove_errors_list<-function(list_items){ |
|
481 |
#This function removes "error" items in a list |
|
482 |
list_tmp<-list_items |
|
483 |
for(i in 1:length(list_items)){ |
|
484 |
|
|
485 |
if(inherits(list_items[[i]],"try-error")){ |
|
486 |
list_tmp[[i]]<-0 |
|
487 |
}else{ |
|
488 |
list_tmp[[i]]<-1 |
|
489 |
} |
|
490 |
} |
|
491 |
cnames<-names(list_tmp[list_tmp>0]) |
|
492 |
x<-list_items[match(cnames,names(list_items))] |
|
493 |
return(x) |
|
494 |
} |
|
495 |
|
|
496 |
#turn term from list into data.frame |
|
497 |
name_col<-function(i,x){ |
|
498 |
x_mat<-x[[i]] |
|
499 |
x_df<-as.data.frame(x_mat) |
|
500 |
x_df$mod_name<-rep(names(x)[i],nrow(x_df)) |
|
501 |
x_df$term_name <-row.names(x_df) |
|
502 |
return(x_df) |
|
503 |
} |
|
504 |
|
|
505 |
##BEGIN ### |
|
506 |
|
|
507 |
myModels <- list_myModels[[i]] |
|
508 |
myModels<-remove_errors_list(myModels) |
|
509 |
#could add AIC, GCV to the table as well as ME, RMSE...+dates... |
|
510 |
|
|
511 |
summary_list <- lapply(myModels, summary) |
|
512 |
s.table_list <- lapply(summary_list, `[[`, 's.table') |
|
513 |
p.table_list <- lapply(summary_list, `[[`, 'p.table') |
|
514 |
AIC_list <- lapply(myModels, AIC) |
|
515 |
|
|
516 |
#now put in one table |
|
517 |
|
|
518 |
s.table_list2<-lapply(1:length(myModels),name_col,s.table_list) |
|
519 |
p.table_list2<-lapply(1:length(myModels),name_col,p.table_list) |
|
520 |
s.table_term <-do.call(rbind,s.table_list2) |
|
521 |
p.table_term <-do.call(rbind,p.table_list2) |
|
522 |
|
|
523 |
#Now get AIC |
|
524 |
AIC_list2<-lapply(1:length(myModels),name_col,AIC_list) |
|
525 |
AIC_models <- do.call(rbind,AIC_list2) |
|
526 |
names(AIC_models)[1]<-"AIC" |
|
527 |
|
|
528 |
#Set up return object |
|
529 |
|
|
530 |
s_p_table_term_obj<-list(s.table_term,p.table_term,AIC_models) |
|
531 |
names(s_p_table_term_obj) <-c("s.table_term","p.table_term","AIC_models") |
|
532 |
return(s_p_table_term_obj) |
|
533 |
|
|
534 |
} |
|
535 |
|
|
536 |
convert_spdf_to_df_from_list <-function(obj_list,list_name){ |
|
537 |
#extract object from list of list. This useful for raster_prediction_obj |
|
538 |
#output: data.frame formed by rbinding sp data.frame in the list |
|
539 |
library(plyr) |
|
540 |
|
|
541 |
list_tmp<-vector("list",length(obj_list)) |
|
542 |
for (i in 1:length(obj_list)){ |
|
543 |
#tmp<-obj_list[[i]][[list_name]] #double bracket to return data.frame |
|
544 |
list_tmp[[i]]<-as.data.frame(obj_list[[i]]) |
|
545 |
} |
|
546 |
tb_list_tmp<-do.call(rbind.fill,list_tmp) #long rownames |
|
547 |
#tb_list_tmp<-do.call(rbind,list_tmp) #long rownames |
|
548 |
|
|
549 |
return(tb_list_tmp) #this is a data.frame |
|
550 |
} |
|
551 |
|
|
552 |
|
|
553 |
################### END OF SCRIPT ################### |
|
554 |
|
|
555 |
|
Also available in: Unified diff
initial commit contribution of covariates functions for paper