Project

General

Profile

« Previous | Next » 

Revision 70704c05

Added by Benoit Parmentier about 11 years ago

multi timescale paper script, correlogram for methods and procedures, updated figures

View differences:

climate/research/oregon/interpolation/multi_timescale_paper_interpolation.R
198 198
####### Now create figures #############
199 199

  
200 200
#figure 1: study area
201
#figure 2: methodological worklfow
202
#figure 3: daily mean compared to monthly mean
203
#Figure 4. RMSE and MAE, mulitisampling and hold out for FSS and GAM.
204
#Figure 5. Overtraining tendency
205
#Figure 6: Spatial pattern of prediction for one day (maps)
206
#Figure 7: Spatial transects for one day (maps)
207
#Figure 8: Spatial lag profiles and stations data  
208
#Figure 9: Image differencing and land cover                               
201
#figure 2: methodological worklfow: outside R
202
#figure 3: LST climatology production: daily mean compared to monthly mean: 
203
#Figure 4. RMSE and MAE, monthly hold out for FSS and GAM methods
204
#Figure 5. Overtraining tendency, monhtly hold out
205
#Figure 6: Spatial pattern of prediction for one day (3 maps)
206
#Figure 7: Spatial transects for one day (transect map + transect profiles)
207
#Figure 8: Image differencing and land cover: spatial patterns     
208
#Figure 9: Spatial lag profiles and stations data  
209 209

  
210 210
################################################
211 211
######### Figure 1: Oregon study area, add labeling names to  Willamette Valley an Mountain Ranges?
......
312 312
list_tb <- c(tb_s_list,tb_v_list,tb_ms_list,tb_mv_list) #combined in one list
313 313
ac_metric <- "rmse"
314 314
#Quick function to explore accuracy make this a function to create solo figure...and run only a subset...
315
plot_accuracy_by_holdout_fun <-function(list_tb,ac_metric){
316
  #
317
  for(i in 1:length(list_tb)){
318
    #i <- i+1
319
    tb <-list_tb[[i]]
320
    plot_name <- names(list_tb)[i]
321
    pat_str <- "tb_m"
322
    if(substr(plot_name,start=1,stop=4)== pat_str){
323
      names_id <- c("pred_mod","prop")
324
      plot_formula <- paste(ac_metric,"~prop",sep="",collapse="") 
325
    }else{
326
      names_id <- c("pred_mod","prop_month")
327
      plot_formula <- paste(ac_metric,"~prop_month",collapse="")
328
    }
329
    names_mod <-unique(tb$pred_mod)
330
    prop_obj <- calc_stat_prop_tb_diagnostic(names_mod,names_id,tb)
331
    avg_tb <- prop_obj$avg_tb
332
  
333
    layout_m<-c(1,1) #one row two columns
334
    par(mfrow=layout_m)
335
    
336
    png(paste("Figure__accuracy_",ac_metric,"_prop_month_",plot_name,"_",out_prefix,".png", sep=""),
337
      height=480*layout_m[1],width=480*layout_m[2])
338
    
339
    p<- xyplot(as.formula(plot_formula),group=pred_mod,type="b",
340
          data=avg_tb,
341
          main=paste(ac_metric,plot_name,sep=" "),
342
          pch=1:length(avg_tb$pred_mod),
343
          par.settings=list(superpose.symbol = list(
344
          pch=1:length(avg_tb$pred_mod))),
345
          auto.key=list(columns=5))
346
    print(p)
347
  
348
    dev.off()
349
  }
350
  #end of function
351
}
352 315

  
316
list_plots <- plot_accuracy_by_holdout_fun(list_tb,ac_metric)
317
names(list_tb)
318
tb_v_list 
353 319
#For paper...
354 320
#Combine figures... tb_v for GWR, Kriging and GAM for both FSS and CAI
321
#grid.arrange(p1,p2, ncol=2)
322

  
323
layout_m<-c(2,3) #one row two columns
324
#par(mfrow=layout_m)
325
    
326
##add option for plot title? 
327
png(paste("Figure4__accuracy_",ac_metric,"_prop_month","_",out_prefix,".png", sep=""),
328
    height=480*layout_m[1],width=480*layout_m[2])
329
p1<-list_plots[[7]]
330
p2<-list_plots[[8]]
331
p3<-list_plots[[9]]
332
p4<-list_plots[[10]]
333
p5<-list_plots[[11]]
334
p6<-list_plots[[12]]
335

  
336
grid.arrange(p1,p2,p3,p4,p5,p6,ncol=3)
337
dev.off()
355 338

  
356 339
################################################
357 340
######### Figure 5. RMSE multi-timescale mulitple hold out Overtraining tendency
......
359 342
#For paper...
360 343
#Combine figures... tb_v for GWR, Kriging and GAM for both FSS and CAI
361 344

  
362
##### Calculate differences
345
metric_names <- c("mae","rmse","me","r")
346
list_metric_names <- vector("list", length=6) #list(metric_names)
347
list_metric_names[[1]] <- metric_names
348
list_metric_names <-lapply(1:6,FUN=function(i,list_metric_names,metric_names){list_metric_names[[i]]<-metric_names},
349
       list_metric_names,metric_names)
350
list_diff <-mapply(tb_s_list,FUN=diff_df,tb_v_list,list_metric_names)                           
351

  
352
#list_diff <-mapply(tb_s_list,FUN=diff_df,tb_v_list,metric_names) # run all at once fix it...                          
353

  
354
##### Calculate differences: change all the following...shorten
363 355

  
364 356
metric_names <- c("mae","rmse","me","r")
365 357
diff_kriging_CAI <- diff_df(list_tb[["tb_s_kriging_CAI"]],list_tb[["tb_v_kriging_CAI"]],metric_names)
366 358
diff_gam_CAI <- diff_df(list_tb[["tb_s_gam_CAI"]][tb_s_gam_CAI$pred_mod!="mod_kr"],list_tb[["tb_v_gam_CAI"]],metric_names)
367 359
diff_gwr_CAI <- diff_df(tb_s_gwr_CAI,tb_v_gwr_CAI,metric_names)
368 360

  
361
# mapply()
369 362
layout_m<-c(1,1) #one row two columns
370 363
par(mfrow=layout_m)
371 364

  
......
454 447
names_layers <-c("mod1 = lat*long","mod2 = lat*long + LST","mod3 = lat*long + elev","mod4 = lat*long + N_w*E_w",
455 448
                 "mod5 = lat*long + elev + DISTOC","mod6 = lat*long + elev + LST","mod7 = lat*long + elev + LST*FOREST")
456 449
nb_fig<- c("7a","7b","7c")
450
list_plots_spt <- vector("list",length=length(nb_fig))
457 451
for (i in 1:length(lf)){
458 452
  pred_temp_s <-stack(lf[[i]])
459 453

  
......
481 475
  #col.regions=temp.colors(25))
482 476
  print(p)
483 477
  dev.off()
478
  list_plots_spt[[i]] <-p
484 479
}
485 480

  
481
layout_m<-c(2,4) # works if set to this?? ok set the resolution...
482
#layout_m<-c(2*3,4) # works if set to this?? ok set the resolution...
483

  
484
png(paste("Figure7_","_spatial_pattern_tmax_prediction_models_gam_levelplot_",date_selected,out_prefix,".png", sep=""),
485
    height=480*layout_m[1],width=480*layout_m[2])
486
    #height=480*6,width=480*4)
487

  
488
p1<-list_plots_spt[[1]]
489
p2<-list_plots_spt[[2]]
490
p3<-list_plots_spt[[3]]
491

  
492
grid.arrange(p1,p2,p3,ncol=1)
493
dev.off()
494

  
486 495
################################################
487 496
#Figure 7: Spatial transects for one day (maps)
488 497

  
......
557 566

  
558 567
################################################
559 568

  
560
#Figure 9: Image differencing and land cover  
561
#Do for january and September...
562
png(paste("Fig9_image_difference_",date_selected,out_prefix,".png", sep=""),
569
#Figure 8: Spatial pattern: Image differencing and land cover  
570
#Do for january and September...?
571

  
572
################################################
573
#Figure 9: Spatial lag profiles and stations data  
574

  
575
y_var_name <-"dailyTmax"
576
index<-244 #index corresponding to Sept 1 #For now create Moran's I for only one date...
577

  
578
lf_moran_list<-lapply(list_raster_obj_files[c("gam_daily","gam_CAI","gam_fss")],
579
                               FUN=function(x){x<-load_obj(x);x$method_mod_obj[[index]][[y_var_name]]})                           
580
#lf1 <- raster_prediction_obj_1$method_mod_obj[[index]][[y_var_name]] #select relevant raster images for the given dates
581

  
582
date_selected <- "20109101"
583
#methods_names <-c("gam","kriging","gwr")
584
methods_names <-c("gam_daily","gam_CAI","gam_FSS")
585

  
586
names_layers<-methods_names
587
#lf <- (list(lf1,lf4[1:7],lf7[1:7]))
588
lf<-list(lf_moran_list[[1]],lf_moran_list[[2]][1:7],lf_moran_list[[3]][1:7])
589

  
590
names_layers <-c("mod1 = lat*long","mod2 = lat*long + LST","mod3 = lat*long + elev","mod4 = lat*long + N_w*E_w",
591
                 "mod5 = lat*long + elev + DISTOC","mod6 = lat*long + elev + LST","mod7 = lat*long + elev + LST*FOREST")
592

  
593
list_filters<-lapply(1:10,FUN=autocor_filter_fun,f_type="queen") #generate lag 10 filters
594
#moran_list <- lapply(list_filters,FUN=Moran,x=r)
595
list_moran_df <- vector("list",length=length(lf))
596
for (j in 1:length(lf)){
597
  r_stack <- stack(lf[[j]])
598
  list_param_moran <- list(list_filters=list_filters,r_stack=r_stack) #prepare parameters list for function
599
  #moran_r <-moran_multiple_fun(1,list_param=list_param_moran)
600
  nlayers(r_stack) 
601
  moran_I_df <-mclapply(1:nlayers(r_stack), list_param=list_param_moran, FUN=moran_multiple_fun,mc.preschedule=FALSE,mc.cores = 10) #This is the end bracket from mclapply(...) statement
602

  
603
  moran_df <- do.call(cbind,moran_I_df) #bind Moran's I value 10*nlayers data.frame
604
  moran_df$lag <-1:nrow(moran_df)
605
  
606
  list_moran_df[[j]] <- moran_df
607

  
608
}
609

  
610
names(list_moran_df)<-c("gam_daily","gam_CAI","gam_fss")
611
list_dd <- vector("list",length=length(list_moran_df))
612

  
613
for(j in 1:length(lf)){
614
  method_name <- names(list_moran_df)[j]
615
  mydata <- list_moran_df[[j]]
616
  dd <- do.call(make.groups, mydata[,-ncol(mydata)]) 
617
  dd$lag <- mydata$lag
618
  dd$method_v <- method_name
619
  list_dd[[j]] <- dd
620
}
621

  
622
dd_combined<- do.call(rbind,list_dd)
623

  
624
layout_m<-c(4,3) #one row two columns
625

  
626
png(paste("Figure_9_spatial_correlogram_tmax_prediction_models_gam_levelplot_",date_selected,out_prefix,".png", sep=""),
563 627
    height=480*layout_m[1],width=480*layout_m[2])
564 628

  
565
  pred_temp <-subset(rast_pred,c(1,2,6)) #3 
629
p<-xyplot(data ~ lag | which , data=dd_combined,group=method_v,type="b",
630
          pch=1:3,auto.key=list(columns=3,cex=1.5,font=2),
631
          par.settings = list(
632
          superpose.symbol = list(pch=1:3,col=1:3,pch.cex=1.4),
633
          axis.text = list(font = 2, cex = 1.3),layout=layout_m,
634
                              par.main.text=list(font=2,cex=2),strip.background=list(col="white")),
635
                              par.strip.text=list(font=2,cex=1.5),
636
          strip=strip.custom(factor.levels=names_layers),
637
          xlab=list(label="Spatial lag neighbor", cex=2,font=2),
638
          ylab=list(label="Moran's I", cex=2, font=2))
639

  
640

  
641
#p<-xyplot(data ~ lag | which , dd_combined,group=method_v,type="b",
642
#          par.settings = list(axis.text = list(font = 2, cex = 1.3),layout=layout_m,
643
#                              par.main.text=list(font=2,cex=2),strip.background=list(col="white")),
644
#                              par.strip.text=list(font=2,cex=1.5),
645
#          strip=strip.custom(factor.levels=names_layers),
646
#          xlab=list(label="Spatial lag neighbor", cex=2,font=2),
647
#          ylab=list(label="Moran's I", cex=2, font=2))
648

  
649
print(p)
650

  
566 651

  
567
  p <- levelplot(pred_temp_s,main=methods_names[i], ylab=NULL,xlab=NULL,
568
          par.settings = list(axis.text = list(font = 2, cex = 1.3),layout=layout_m,
569
                              par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5),
570
          names.attr=names_layers,col.regions=temp.colors,at=seq(min_val,max_val,by=0.25))
571
  #col.regions=temp.colors(25))
572
  print(p)
573 652
dev.off()
574 653

  
575 654
###################### END OF SCRIPT #######################

Also available in: Unified diff