Project

General

Profile

« Previous | Next » 

Revision 08721acf

Added by Benoit Parmentier about 11 years ago

changes in validation script to deal with hold out proportion at monthly time scale

View differences:

climate/research/oregon/interpolation/GAM_fusion_function_multisampling_validation_metrics.R
72 72
  
73 73
  #PARSING INPUT PARAMETERS
74 74
  out_path <- list_param$out_path
75
  day_list<- list_param$rast_day_year_list[[i]]
75
  day_list <- list_param$rast_day_year_list[[i]]
76 76
  #day_list <-rast_day_yearlist[[i]] #list of prediction for the current date...
77
  names_mod<-names(day_list)
78
  method_mod_obj<-list_param$method_mod_obj
79
  #Change to results_mod_obj[[i]]$data_s to make it less specific
80
  data_v <- method_mod_obj[[i]]$data_v
81
  data_s <- method_mod_obj[[i]]$data_s
77
  names_mod <- names(day_list)
78
  method_mod_obj <- list_param$method_mod_obj
79

  
82 80
  y_var_name <- list_param$y_var_name #missing--debugging
81
  multi_time_scale <- list_param$multi_time_scale
82
  
83
  if(multi_time_scale==TRUE){
84
    data_v <- method_mod_obj[[i]]$data_month_v
85
    data_s <- method_mod_obj[[i]]$data_month_s
86
    daily_dev_sampling_dat <- method_mod_obj[[i]]$daily_dev_sampling_dat
87
    sampling_dat_day <- method_mod_obj[[i]]$daily_dev_sampling_dat
88
  }else{
89
    #Change to results_mod_obj[[i]]$data_s to make it less specific
90
    data_v <- method_mod_obj[[i]]$data_v
91
    data_s <- method_mod_obj[[i]]$data_s
92
    sampling_dat_day <- (method_mod_obj[[i]])$sampling_dat
93
  }
83 94
  
84 95
  ## Now create the stack
85 96
  
86 97
  rast_day_mod <- stack(day_list)
87 98
  names(rast_day_mod) <- names(day_list)
88
  extract_data_v<-extract(rast_day_mod,data_v,df=TRUE)
89
  extract_data_s<-extract(rast_day_mod,data_s,df=TRUE)
90
  
91
  data_v <-spCbind(data_v,extract_data_v) #should match IDs before joining for good practice
92
  data_s <-spCbind(data_s,extract_data_s)
93
  
99
  #Change to handle cases in which data_v is NULL!!!
100
    
94 101
  ns<-nrow(data_s) # some loss of data might have happened because of the averaging...
95 102
  nv<-nrow(data_v)
96 103
  
97
  sampling_dat_day<-(method_mod_obj[[i]])$sampling_dat
98
   
99
  metrics_v_obj<-calc_val_metrics_rast(data_v,y_var_name,names_mod)
100
  metrics_s_obj<-calc_val_metrics_rast(data_s,y_var_name,names_mod)
101
  
104

  
102 105
  #add sampling dat info...
103 106
  N=length(names_mod)
104
  run_info<-cbind(sampling_dat_day,n=nv)
105
  run_info[rep(seq_len(nrow(run_info)), each=N),] #repeating same row n times
106
  metrics_v_df<-cbind(metrics_v_obj$metrics,run_info)
107
  metrics_v_df["var_interp"]<-rep(y_var_name,times=nrow(metrics_v_df)) 
108
  #Name of the variable interpolated, useful for cross-comparison between methods at later stages
109 107
  
110
  run_info<-cbind(sampling_dat_day,n=ns)
108
  #Handle case of 0% hold out, monhtly or daily
109
  if (nv > 0){
110
    run_info<-cbind(sampling_dat_day,n=nv)
111
    run_info[rep(seq_len(nrow(run_info)), each=N),] #repeating same row n times
112
    
113
    extract_data_v<-extract(rast_day_mod,data_v,df=TRUE)
114
    data_v <-spCbind(data_v,extract_data_v) #should match IDs before joining for good practice    
115
    metrics_v_obj<-calc_val_metrics_rast(data_v,y_var_name,names_mod)
116
    metrics_v_df<-cbind(metrics_v_obj$metrics,run_info)
117
    metrics_v_df["var_interp"]<-rep(y_var_name,times=nrow(metrics_v_df)) 
118
    #Name of the variable interpolated, useful for cross-comparison between methods at later stages
119
    data_v<-spCbind(data_v,metrics_v_obj$residuals)
120
    
121
  }
122
  
123
  extract_data_s<-extract(rast_day_mod,data_s,df=TRUE)  
124
  data_s <-spCbind(data_s,extract_data_s)
125

  
126
  metrics_s_obj <- calc_val_metrics_rast(data_s,y_var_name,names_mod)  
127
  run_info <- cbind(sampling_dat_day,n=ns)
111 128
  run_info[rep(seq_len(nrow(run_info)), each=N),]
112
  metrics_s_df<-cbind(metrics_s_obj$metrics,run_info)
113
  metrics_s_df["var_interp"]<-rep(y_var_name,times=nrow(metrics_s_df)) 
129
  metrics_s_df <- cbind(metrics_s_obj$metrics,run_info)
130
  metrics_s_df["var_interp"] <- rep(y_var_name,times=nrow(metrics_s_df)) 
114 131
  #Name of the variable interpolated, useful for cross-comparison between methods at later stages
115 132
  
116
  data_v<-spCbind(data_v,metrics_v_obj$residuals)
117
  data_s<-spCbind(data_s,metrics_s_obj$residuals)
118
    
119
  validation_obj<-list(metrics_s_df,metrics_v_df,data_s,data_v)
120
  names(validation_obj)<-c("metrics_s","metrics_v","data_s","data_v")
133
  data_s <- spCbind(data_s,metrics_s_obj$residuals)
134
  
135
  #prepare output object
136
  
137
  if (nv > 0){
138
    validation_obj<-list(metrics_s_df,metrics_v_df,data_s,data_v)
139
    names(validation_obj)<-c("metrics_s","metrics_v","data_s","data_v")
140
  }else{
141
    validation_obj<-list(metrics_s_df,data_s)
142
    names(validation_obj)<-c("metrics_s","data_s")
143
  }
121 144
  
122 145
  return(validation_obj)
123 146

  
......
134 157
  return(tb_list_tmp) #this is  a data.frame
135 158
}
136 159

  
160
#### Function to create a list from a object made up of a list with names e.g. method_mod_obj or clim_method_mod_obj
161
extract_list_from_list_obj<-function(obj_list,list_name){
162
  #Create a list of an object from a given list of object using a name prodived as input
163
  
164
  list_tmp<-vector("list",length(obj_list))
165
  for (i in 1:length(obj_list)){
166
    tmp<-obj_list[[i]][[list_name]] #double bracket to return data.frame
167
    list_tmp[[i]]<-tmp
168
  }
169
  return(list_tmp) #this is  a data.frame
170
}
171

  
137 172
#### Function to plot boxplot from data.frame table of accuracy metrics
138 173

  
139 174
boxplot_from_tb <-function(tb_diagnostic,metric_names,out_prefix,out_path){

Also available in: Unified diff