Revision b5ca4884
Added by Benoit Parmentier about 11 years ago
climate/research/oregon/interpolation/GAM_fusion_function_multisampling_validation_metrics.R | ||
---|---|---|
13 | 13 |
#5)stack of covariates: not needed at this this stage |
14 | 14 |
#6)dst: data at the monthly time scale |
15 | 15 |
|
16 |
#Function used in the script |
|
16 |
#Functions used in the script |
|
17 |
calc_val_metrics<-function(x,y){ |
|
18 |
#This functions calculates accurayc metrics on given two vectors. |
|
19 |
#Arguments: list of fitted models, raster stack of covariates |
|
20 |
#Output: spatial grid data frame of the subset of tiles |
|
21 |
#s_sgdf<-as(r_stack,"SpatialGridDataFrame") #Conversion to spatial grid data frame |
|
22 |
|
|
23 |
residuals<-x-y |
|
24 |
mae<-mean(abs(residuals),na.rm=T) |
|
25 |
rmse<-sqrt(mean((residuals)^2,na.rm=T)) |
|
26 |
me<-mean(residuals,na.rm=T) |
|
27 |
r<-cor(x,y,use="complete") |
|
28 |
m50<-median(residuals,na.rm=T) |
|
29 |
metrics_dat<-as.data.frame(cbind(mae,rmse,me,r,m50)) |
|
30 |
names(metrics_dat)<-c("mae","rmse","me","r","m50") |
|
31 |
metrics_obj<-list(metrics_dat,as.data.frame(residuals)) |
|
32 |
names(metrics_obj)<-c("metrics_dat","residuals") |
|
33 |
return(metrics_obj) |
|
34 |
} |
|
35 |
|
|
36 |
calc_val_metrics_rast <-function(df,y_ref,pred_names){ |
|
37 |
#Input parameters: |
|
38 |
#1) df: data frame containing the observed and predicted variables (data_s or data_v) |
|
39 |
#2) y_ref: observed variable correspond to y_var_name?? |
|
40 |
#3) pred_names: models run containig predicted values |
|
41 |
|
|
42 |
# library |
|
43 |
library(maptools) |
|
44 |
|
|
45 |
## START SCRIPT |
|
46 |
|
|
47 |
list_metrics<-vector("list",length(pred_names)) |
|
48 |
list_residuals<-vector("list",length(pred_names)) |
|
49 |
names(list_metrics)<-pred_names |
|
50 |
names(list_residuals)<-pred_names |
|
51 |
for (j in 1:length(pred_names)){ |
|
52 |
pred_var<-pred_names[j] |
|
53 |
metrics<-calc_val_metrics(df[[pred_var]],df[[y_ref]]) |
|
54 |
list_metrics[[j]]<-metrics[[1]] |
|
55 |
list_residuals[[j]]<-metrics[[2]] |
|
56 |
} |
|
57 |
metrics_df<-do.call(rbind,list_metrics) |
|
58 |
metrics_df$pred_mod <- pred_names #adding name column |
|
59 |
residuals_df<-do.call(cbind,list_residuals) #creating data frame for residuals |
|
60 |
names(residuals_df)<-paste("res",pred_names,sep="_") |
|
61 |
|
|
62 |
accuracy_obj<-list(metrics_df,residuals_df) #output object |
|
63 |
names(accuracy_obj)<-c("metrics","residuals") |
|
64 |
return(accuracy_obj) |
|
65 |
} |
|
17 | 66 |
|
67 |
### Main function to compute training and testing accuracy statistics |
|
18 | 68 |
calculate_accuracy_metrics<-function(i,list_param){ |
19 | 69 |
library(plyr) |
20 | 70 |
### Caculate accuracy metrics |
21 |
calc_val_metrics<-function(x,y){ |
|
22 |
#This functions calculates accurayc metrics on given two vectors. |
|
23 |
#Arguments: list of fitted models, raster stack of covariates |
|
24 |
#Output: spatial grid data frame of the subset of tiles |
|
25 |
#s_sgdf<-as(r_stack,"SpatialGridDataFrame") #Conversion to spatial grid data frame |
|
26 |
|
|
27 |
residuals<-x-y |
|
28 |
mae<-mean(abs(residuals),na.rm=T) |
|
29 |
rmse<-sqrt(mean((residuals)^2,na.rm=T)) |
|
30 |
me<-mean(residuals,na.rm=T) |
|
31 |
r<-cor(x,y,use="complete") |
|
32 |
m50<-median(residuals,na.rm=T) |
|
33 |
metrics_dat<-as.data.frame(cbind(mae,rmse,me,r,m50)) |
|
34 |
names(metrics_dat)<-c("mae","rmse","me","r","m50") |
|
35 |
metrics_obj<-list(metrics_dat,as.data.frame(residuals)) |
|
36 |
names(metrics_obj)<-c("metrics_dat","residuals") |
|
37 |
return(metrics_obj) |
|
38 |
} |
|
39 |
|
|
40 |
calc_val_metrics_rast <-function(df,y_ref,pred_names){ |
|
41 |
#Input parameters: |
|
42 |
#1) df: data frame containing the observed and predicted variables (data_s or data_v) |
|
43 |
#2) y_ref: observed variable correspond to y_var_name?? |
|
44 |
#3) pred_names: models run containig predicted values |
|
45 |
|
|
46 |
# library |
|
47 |
library(maptools) |
|
48 |
|
|
49 |
## START SCRIPT |
|
50 |
|
|
51 |
list_metrics<-vector("list",length(pred_names)) |
|
52 |
list_residuals<-vector("list",length(pred_names)) |
|
53 |
names(list_metrics)<-pred_names |
|
54 |
names(list_residuals)<-pred_names |
|
55 |
for (j in 1:length(pred_names)){ |
|
56 |
pred_var<-pred_names[j] |
|
57 |
metrics<-calc_val_metrics(df[[pred_var]],df[[y_ref]]) |
|
58 |
list_metrics[[j]]<-metrics[[1]] |
|
59 |
list_residuals[[j]]<-metrics[[2]] |
|
60 |
} |
|
61 |
metrics_df<-do.call(rbind,list_metrics) |
|
62 |
metrics_df$pred_mod <- pred_names #adding name column |
|
63 |
residuals_df<-do.call(cbind,list_residuals) #creating data frame for residuals |
|
64 |
names(residuals_df)<-paste("res",pred_names,sep="_") |
|
65 |
|
|
66 |
accuracy_obj<-list(metrics_df,residuals_df) #output object |
|
67 |
names(accuracy_obj)<-c("metrics","residuals") |
|
68 |
return(accuracy_obj) |
|
69 |
} |
|
70 | 71 |
|
71 | 72 |
############### BEGIN SCRIPT ########### |
72 | 73 |
|
73 | 74 |
#PARSING INPUT PARAMETERS |
74 | 75 |
out_path <- list_param$out_path |
75 | 76 |
day_list <- list_param$rast_day_year_list[[i]] #this is the list of raster files, may be daily or monthly predictions |
77 |
if(class(day_list[[1]])=="list"){ |
|
78 |
day_list<-unlist(day_list) |
|
79 |
} |
|
76 | 80 |
names_mod <- names(day_list) #names of the predicted variables |
77 | 81 |
|
78 | 82 |
y_ref <- list_param$y_ref #This is the reference variable from which resituals and accuracy metrics are created |
Also available in: Unified diff
validation script check and slight modifications to handle modifications in daily deviations