Project

General

Profile

« Previous | Next » 

Revision ab0cb59b

Added by Benoit Parmentier about 10 years ago

revisions 1 multi-timescale paper, adding function to plot average MAE per station

View differences:

climate/research/oregon/interpolation/multi_timescales_paper_interpolation_functions.R
5 5
#Functions used in the production of figures and data for the multi timescale paper are recorded.
6 6
#AUTHOR: Benoit Parmentier                                                                      #
7 7
#DATE CREATED: 11/25/2013            
8
#DATE MODIFIED: 05/05/2014            
8
#DATE MODIFIED: 08/12/2014            
9 9
#Version: 5
10 10
#PROJECT: Environmental Layers project                                       #
11 11
#################################################################################################
......
611 611
  return(stat_tb)
612 612
}
613 613

  
614
### Plotting and computing average MAE per station for different methods
615
plot_MAE_per_station_fun <- function(list_data_v,names_var,interp_method,var_background,stat_loc,out_suffix){
616
  #Function to create a series of residuals MAE plots...
617
  
618
  mae_fun<-function(x){mean(abs(x))} #Mean Absolute Error give a residuals vector
619
  sd_abs_fun<-function(x){sd(abs(x))} #sd Absolute Error give a residuals vector
620
  
621
  ### Start script ###
622
  
623
  data_v_test <- list_data_v[[1]]
624
  #Convert sp data.frame and combined them in one unique df, see function define earlier
625
  data_v_combined <-convert_spdf_to_df_from_list(list_data_v) #long rownames
626
  
627
  #names_var_all<-c("res_mod1","res_mod2","res_mod3","res_mod4","res_mod5","res_mod6","res_mod7")#,"res_mod8","res_mod9","res_mod10")
628
  names_var_all <- res_model_name <- paste("res",names_var,sep="_")
629

  
630
  t<-melt(data_v_combined,
631
        measure=names_var_all, 
632
        id=c("id"),
633
        na.rm=T)
634

  
635
  names(data_v_combined)
636
  mae_tb<-cast(t,id~variable,mae_fun) #join to station location...
637

  
638
  data_v_mae <-merge(mae_tb,stat_loc,by.x=c("id"),by.y=c("STAT_ID"))
639

  
640
  coords<- data_v_mae[c('longitude','latitude')]              #Define coordinates in a data frame
641
  CRS_interp<-proj4string(data_v_test)
642
  coordinates(data_v_mae)<-coords                      #Assign coordinates to the data frame
643
  proj4string(data_v_mae)<- proj4string(stat_loc)                #Assign coordinates reference system in PROJ4 format
644
  data_v_mae<-spTransform(data_v_mae,CRS(CRS_interp))     #Project from WGS84 to new coord. system
645

  
646
  list_p_mae <- vector("list", length(names_var_all))
647
  #names_var <- c("mod1","mod2","mod3","mod7")
648

  
649
  for (k in 1:length(names_var)){
650
    model_name <- names_var[k]
651
    res_model_name <- paste("res",model_name,sep="_")
652

  
653
    p1 <- levelplot(var_background,scales = list(draw = FALSE), colorkey = FALSE,par.settings = GrTheme)
654
    df_tmp=subset(data_v_mae,data_v_mae[[res_model_name]]!="NaN")
655
  
656
    p2 <- bubble(df_tmp,res_model_name, main=paste("Average MAE per station for ",model_name," ",interp_method, sep=""),
657
               na.rm=TRUE)
658
    p3 <- p2 + p1 + p2 #to force legend...
659
    list_p_mae[[k]] <- p3
660
  }
661
  
662
  data_mae_obj <- list(list_p_mae,data_v_mae)
663
  names(data_mae_obj) <- c("list_p_mae","data_v_mae")
664
  return(data_mae_obj)
665
}
666

  
614 667

  
615 668
################### END OF SCRIPT ###################
616 669

  

Also available in: Unified diff