Project

General

Profile

« Previous | Next » 

Revision 4d4c2e20

Added by Benoit Parmentier over 10 years ago

contributions of covariates and methods paper: revisions- major clean up of code and production of figures for submission of revisions

View differences:

climate/research/oregon/interpolation/contribution_of_covariates_paper_interpolation.R
39 39

  
40 40
#### FUNCTION USED IN SCRIPT
41 41

  
42
function_analyses_paper <-"contribution_of_covariates_paper_interpolation_functions_05212014.R"
42
function_analyses_paper <-"contribution_of_covariates_paper_interpolation_functions_07182014.R"
43 43

  
44 44
##############################
45 45
#### Parameters and constants  
......
64 64

  
65 65

  
66 66
infile_reg_outline <- "/data/project/layers/commons/data_workflow/inputs/region_outlines_ref_files/OR83M_state_outline.shp"  #input region outline defined by polygon: Oregon
67
infile_reg_raster <- "/data/project/layers/commons/data_workflow/inputs/region_outlines_ref_files/OR83M_state_outline.rst"  #input region outline defined by polygon: Oregon
68

  
67 69
met_stations_outfiles_obj_file<-"/data/project/layers/commons/data_workflow/output_data_365d_gam_fus_lst_test_run_07172013/met_stations_outfiles_obj_gam_fusion__365d_gam_fus_lst_test_run_07172013.RData"
68 70
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
69 71
y_var_name <- "dailyTmax"
70 72
out_prefix<-"_07182014"
71 73
out_dir<-"/home/parmentier/Data/IPLANT_project/paper_contribution_covar_analyses_tables_fig"
72
setwd(out_dir)
74
#setwd(out_dir)
73 75

  
74 76
#out_dir<-"/home/parmentier/Data/IPLANT_project/paper_multitime_scale__analyses_tables"
75 77
create_out_dir_param = TRUE
......
242 244
#figure 2: methodological worklfow (generated outside R)
243 245
#figure 3: LST daily and monthly climatology
244 246
#figure 4: MAE/RMSE and distance to closest fitting station.
245
#Figure 5. RMSE and MAE, mulitisampling and hold out for FSS and GAM.
246
#Figure 6. Overtraining tendency
247
#Figure 5: RMSE and MAE, mulitisampling and hold out for FSS and GAM.
248
#Figure 6: Overtraining tendency
247 249
#Figure 7a: Spatial pattern of prediction for one day
248 250
#figure 7b: Spatial autocorrelation profile : Moran's vs lag distance
249
#Figure 8: Figure 8. (a) Monthly MAE averages for the three interpolation methods: GAM, GWR and Kriging.(b) Monthly MAE boxplot for GAM.
250
#Figure 9: difference image
251
#Figure 8: Prediction difference image for Sept 1, 2010 for mod1 and mod4
252
#Figure 9: Semi variograms for January 1 2010 and September 1 2010.
253
#Figure 10: Summary of variograms model parameters for mod1=lat*lon+elev over 2010
254
#Figure 11: Testing Residuals per model for GAM method for baseline 2 for September 1, 2010.
255
#Figure 12: Average testing MAE in 2010 per station for GAM method for baseline 2 models 
256
#Figure 13: Boxplots of testing residuals and elevation classes over the year 2010 for GAM 
257
#Figure 14: (a) Monthly MAE averages for the three interpolation methods: GAM, GWR and Kriging.(b) Monthly MAE boxplot for GAM.
258
#Figure 15: Correlation between LST-tmax, elevation-tmax in relation to LST significance and monthly model accuracy 
251 259

  
252 260
### Figure 1: Oregon study area
253
#3 parameters from output
254
#infile_monthly<-list_outfiles$monthly_covar_ghcn_data #outile4 from database_covar script
255
#infile_daily<-list_outfiles$daily_covar_ghcn_data  #outfile3 from database_covar script
256
#infile_locs<- list_outfiles$loc_stations_ghcn #outfile2? from database covar script
257
#
258
#
259
#ghcn_day_dat <- readOGR(dsn=dirname(met_stations_obj$daily_covar_ghcn_data),
260
#                    sub(".shp","",basename(met_stations_obj$daily_covar_ghcn_data)))
261
#ghcn_dayq_dat <- readOGR(dsn=dirname(met_stations_obj$daily_query_ghcn_data),
262
#                        sub(".shp","",basename(met_stations_obj$daily_query_ghcn_data)))
263

  
264 261

  
265 262
ghcn_dat <- readOGR(dsn=dirname(met_stations_obj$monthly_covar_ghcn_data),
266 263
        sub(".shp","",basename(met_stations_obj$monthly_covar_ghcn_data)))
......
270 267
interp_area <- readOGR(dsn=dirname(infile_reg_outline),sub(".shp","",basename(infile_reg_outline)))
271 268
interp_area_WGS84 <-spTransform(interp_area,CRS_locs_WGS84)         # Project from WGS84 to new coord. system
272 269

  
273
usa_map <- getData('GADM', country='USA', level=1) #Get US map
270
usa_map <- getData('GADM', country='USA'), level=1) #Get US map
274 271
usa_map_2 <- usa_map[usa_map$NAME_1!="Alaska",] #remove Alaska
275 272
usa_map_2 <- usa_map_2[usa_map_2$NAME_1!="Hawaii",] #remove Hawai 
276 273
usa_map_OR <- usa_map_2[usa_map_2$NAME_1=="Oregon",] #get OR
......
341 338
title("Mean for month of January")
342 339
dev.off()
343 340

  
344
path_in_tmp <- "/home/parmentier/Data/IPLANT_project/region_outlines_ref_files"
345
interp_area_tmp <- readOGR(dsn=path_in_tmp,"OR83M_state_outline")
346
proj4string(interp_area_tmp ) <- proj4string(interp_area)
347
projection(lst_md) <- proj4string(interp_area)
348
r_region_ref <- rasterize(x=interp_area_tmp,y=lst_md,field="State_ID_s",fun=max)
341
## Calucate the proprotion of missing pixel for January 1 mean climatotology image
342
r_reg <- raster(infile_reg_raster)
343
projection(r_reg) <- proj4string(interp_area)
344
freq(r_reg)
345
r_reg[r_reg==0] <- NA
346
r_rec = lst_md > (-9999)
347
lst_md_xtb <- crosstab(r_reg,r_rec, useNA='always',long=T)
348
missing_prop <- lst_md_xtb[3,3]/freq(r_reg,value=1)
349
missing_prop #51.3% of the study area (within Oregon state) that is missing!!!
349 350

  
350 351
################################################
351 352
################### Figure 4. MAE/RMSE and distance to closest fitting station.
......
395 396
res_pix<-480
396 397
col_mfrow<-2
397 398
row_mfrow<-1
398
png_file_name<- paste("Figure_3_accuracy_and_distance_to_closest_fitting_station_",out_prefix,".png", sep="")
399
png_file_name<- paste("Figure_4_accuracy_and_distance_to_closest_fitting_station_",out_prefix,".png", sep="")
399 400
png(filename=file.path(out_dir,png_file_name),
400 401
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
401 402
par(mfrow=c(row_mfrow,col_mfrow))
402 403

  
403
#Figure 3a
404
#Figure 4a
404 405
list_param_plot<-list(list_dist_obj,col_t,pch_t,legend_text,mod_name,x_tick_labels,metric_name,
405 406
                      title_plot,y_lab_text,add_CI)
406 407
names(list_param_plot)<-c("list_dist_obj","col_t","pch_t","legend_text","mod_name","x_tick_labels","metric_name",
......
409 410
plot_dst_MAE(list_param_plot)
410 411
title(xlab="Distance to closest fitting station (km)")
411 412

  
412
#Figure 3b: histogram
413
#Figure 4b: histogram
413 414
barplot(l1$n_tb$res_mod1,names.arg=limit_val,
414 415
        ylab="Number of observations",
415 416
        xlab="Distance to closest fitting station (km)")
......
418 419
dev.off()
419 420

  
420 421
####################################################
421
#########Figure 4. RMSE and MAE, mulitisampling and hold out for single time scale methods.
422
#########Figure 5. RMSE and MAE, mulitisampling and hold out for single time scale methods.
422 423

  
423 424
#Using baseline 2: lat,lon and elev
424 425

  
......
454 455
mod_compk1 <-kruskal.test(prop_tb$rmse ~ as.factor(prop_tb$method_interp)+as.factor(prop_tb$prop)) #Kruskal Wallis test
455 456
mod_prop <-lm(prop_tb$rmse ~ as.factor(prop_tb$method_interp)+as.factor(prop_tb$prop)) #This is significant!!
456 457

  
457
## plot setting for figure 4
458
## plot setting for figure 5
458 459

  
459 460
col_t<-c("red","blue","black")
460 461
pch_t<- 1:length(col_t)
......
464 465
CI_bar <- c(TRUE,TRUE,TRUE)
465 466
#add_CI <- c(TRUE,FALSE,FALSE)
466 467

  
467
##### plot figure 4 for paper
468
##### plot figure 5 for paper
468 469
####
469 470

  
470 471
res_pix<-480
471 472
col_mfrow<-2
472 473
row_mfrow<-1
473
png_file_name<- paste("Figure_4_proportion_of_holdout_and_accuracy_",out_prefix,".png", sep="")
474
png_file_name<- paste("Figure_5_proportion_of_holdout_and_accuracy_",out_prefix,".png", sep="")
474 475
png(filename=file.path(out_dir,png_file_name),
475 476
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
476 477
par(mfrow=c(row_mfrow,col_mfrow))
......
485 486
      xlab="Hold out validation/testing proportion",
486 487
      ylab="MAE (°C)")
487 488

  
488
#now figure 4b
489
#now figure 5b
489 490
metric_name<-"rmse"
490 491
list_param_plot<-list(list_prop_obj,col_t,pch_t,legend_text,mod_name,metric_name,add_CI,CI_bar)
491 492
names(list_param_plot)<-c("list_prop_obj","col_t","pch_t","legend_text","mod_name","metric_name","add_CI","CI_bar")
......
497 498
dev.off()
498 499

  
499 500
####################################################
500
#########Figure 5. Overtraining tendency
501
#########Figure 6. Overtraining tendency
501 502

  
502 503
#read in relevant data:
503 504
## Calculate average difference for RMSE for all three methods
......
589 590
plot(prop_obj_gam_s$avg_tb$rmse ~ prop_obj_gam_s$avg_tb$prop, type="b",)
590 591

  
591 592
###############
592
#### plot figure 5
593
#### plot figure 6
593 594
#set up the output file to plot
594 595
res_pix<-480
595 596
col_mfrow<-2
596 597
row_mfrow<-1
597
png_file_name<- paste("Figure_5_overtraining_tendency_and_holdout_proportion_",out_prefix,".png", sep="")
598
png_file_name<- paste("Figure_6_overtraining_tendency_and_holdout_proportion_",out_prefix,".png", sep="")
598 599
png(filename=file.path(out_dir,png_file_name),
599 600
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
600 601
par(mfrow=c(row_mfrow,col_mfrow))
......
603 604
pch_t<- 1:length(col_t)
604 605
##Make this a function???
605 606
y_range<-range(prop_obj_kriging$avg_tb$rmse,prop_obj_kriging_s$avg_tb$rmse)
606
#y_range<-range(prop_obj_gam$avg_tb$rmse,prop_obj_gam_s$avg_tb$rmse)
607
#plot(prop_obj_gam$avg_tb$rmse ~ prop_obj_gam$avg_tb$prop, ylab="",xlab="",type="b",col=c("red"),pch=pch_t[1],ylim=y_range,lty=2)
608
#lines(prop_obj_gam_s$avg_tb$rmse ~ prop_obj_gam_s$avg_tb$prop, ylab="",xlab="",type="b",pch=pch_t[1],ylim=y_range,col=c("red"))
609
#lines(prop_obj_gwr_s$avg_tb$rmse ~ prop_obj_gwr_s$avg_tb$prop, ylab="",xlab="",type="b",ylim=y_range,pch=pch_t[3],col=c("black"))
610
#lines(prop_obj_gwr$avg_tb$rmse ~ prop_obj_gam$avg_tb$prop, ylab="",xlab="",type="b",ylim=y_range,pch=pch_t[3],,col=c("black"),lty=2)
611
#lines(prop_obj_kriging$avg_tb$rmse ~ prop_obj_kriging$avg_tb$prop,ylab="",xlab="", type="b",ylim=y_range,pch=pch_t[2],,col=c("blue"),lty=2)
612
#lines(prop_obj_kriging_s$avg_tb$rmse ~ prop_obj_kriging_s$avg_tb$prop,ylab="",xlab="",type="b",ylim=y_range,pch=pch_t[2],col=c("blue"))
613 607

  
614 608
plot_ac_holdout_prop<- function(l_prop,l_col_t,l_pch_t,add_CI,y_range){
615 609
  
......
662 656

  
663 657
dev.off()
664 658

  
665
############### STUDY TIME AND accuracy
666
#########Figure 6: Monthly RMSE averages for the three interpolation methods: GAM, GWR and Kriging.
659
############### STUDY TIME AND accuracy: comparing methods over monthly averages
660
#########Figure 14: Monthly RMSE averages for the three interpolation methods: GAM, GWR and Kriging.
667 661

  
668 662
mae_tmp<- data.frame(gam=tb1[tb1$pred_mod=="mod1",c("mae")],
669 663
                     kriging=tb3[tb3$pred_mod=="mod1",c("mae")],
......
708 702
                      gwr=tb4[tb4$pred_mod=="mod1",c(metric_name,"month")])
709 703
y_range<-range(unlist(month_data_list))
710 704

  
711

  
712
#boxplot(test[[metric_ac]]~test[[c("month")]],outline=FALSE,horizontal=FALSE,cex=0.5,
713
#        ylab=paste(metric_ac,"in degree C",sep=" "),,axisnames=FALSE,axes=FALSE)
714
#boxplot(test[[metric_ac]]~test[[c("month")]],outline=FALSE,horizontal=FALSE,cex=0.5,
715
#        ylab=paste(metric_ac,"in degree C",sep=" "))
716
#axis(1, labels = FALSE)
717
## Create some text labels
718
#labels <- month.abb # abbreviated names for each month
719
## Plot x axis labels at default tick marks
720
#text(1:length(labels), par("usr")[3] - 0.25, srt = 45, adj = 1,
721
#     labels = labels, xpd = TRUE)
722
#axis(2)
723
#box()
724

  
725
#Now plot figure 6
705
#Now plot figure 14
726 706
res_pix<-480
727 707
col_mfrow<-2
728 708
#row_mfrow<-2
729 709
row_mfrow<-1
730 710

  
731
png_file_name<- paste("Figure_6_monthly_accuracy_",out_prefix,".png", sep="")
711
png_file_name<- paste("Figure_14_monthly_accuracy_",out_prefix,".png", sep="")
732 712
png(filename=file.path(out_dir,png_file_name),
733 713
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
734 714
par(mfrow=c(row_mfrow,col_mfrow))
......
752 732
#Second plot
753 733
ylab_text<-"MAE (°C)"
754 734
xlab_text<-"Month"
755
#y_range<-range(month_data_list$gam$mae,month_data_list$kriging$mae,month_data_list$gwr$mae)
756
#y_range<-range(month_data_list$gam$mae)
757 735
boxplot(mae~month,data=month_data_list$gam,main="Monthly MAE boxplot", xlab=xlab_text,ylab=ylab_text,outline=FALSE)
758
#boxplot(mae~month,data=month_data_list$kriging,ylim=y_range,main="Kriging",ylab=ylab_text,outline=FALSE)
759
#boxplot(mae~month,data=month_data_list$gwr,ylim=y_range,main="GWR",ylab=ylab_text,outline=FALSE)
760

  
761 736
dev.off()
762 737

  
763 738
#Now generate table 5
......
826 801
min_val <- 0
827 802
layout_m<-c(1,3) #one row two columns
828 803

  
829
#png(paste("Figure7__spatial_pattern_tmax_prediction_levelplot_",date_selected,out_prefix,".png", sep=""),
804
#png(paste("Figure7a__spatial_pattern_tmax_prediction_levelplot_",date_selected,out_prefix,".png", sep=""),
830 805
#    height=480*layout_m[1],width=480*layout_m[2])
831 806

  
832 807
p<-levelplot(pred_temp_s,main="Interpolated Surfaces Method Comparison", ylab=NULL,xlab=NULL,
......
837 812
#col.regions=temp.colors(25))
838 813
#dev.off()
839 814

  
840
## FIGURE COMPARISON OF  MODELS COVARRIATES: Figure 7...
815
## FIGURE COMPARISON OF  MODELS COVARIATES: Figure 7...
841 816

  
842 817
lf2 <- raster_prediction_obj_2$method_mod_obj[[index]][[y_var_name]]
843 818
lf2 #contains the models for gam
......
863 838
#min_val <- 0
864 839
layout_m<-c(4,3) #one row two columns
865 840

  
866
png(paste("Figure_7_spatial_pattern_tmax_prediction_models_baseline1_gam_levelplot_",date_selected,out_prefix,".png", sep=""),
841
png(paste("Figure_7a_spatial_pattern_tmax_prediction_models_baseline1_gam_levelplot_",date_selected,out_prefix,".png", sep=""),
867 842
    height=480*layout_m[1],width=480*layout_m[2])
868 843

  
869 844
p<- levelplot(pred_temp_s,main="Interpolated Surfaces Model Comparison baseline 1", ylab=NULL,xlab=NULL,
......
988 963
###baseline 2: s(lat,lon) + s(elev)
989 964
      
990 965
l_obj<-vector("list",length=2)
991
l_obj[[1]]<-raster_prediction_obj_1
966
l_obj[[1]]<-raster_prediction_obj_1 #baseline 1: s(lat,lon) + s(elev_s)
992 967
l_obj[[2]]<-raster_prediction_obj_2
993 968
l_table <- vector("list",length=length(l_obj))   
994 969
l_s_table_term_tb <- vector("list",length(l_obj))
970

  
971
(l_obj[[1]]$method_mod_obj[[1]]$formulas)
995 972
for (k in 1:length(l_obj)){
996 973
  raster_prediction_obj<- l_obj[[k]]
997 974
  #extract models for every day
......
1010 987
  s.table_term_tb <- do.call(rbind.fill,s.table_term_list) 
1011 988
  #Adding month to df
1012 989
  #s.table_term_tb <- add_month_tag(s.table_term_tb)
1013
  s.table_term_tb$month <- add_month_tag(s.table_term_tb,"rownames")
1014
  
990
  #s.table_term_tb$month <- add_month_tag(s.table_term_tb,"rownames")
991
  col_date<-strptime(s.table_term_tb[["rownames"]], "%Y%m%d")   # interpolation date being processed
992
  s.table_term_tb$month <-strftime(col_date, "%m")          # current month of the date being processed
993

  
1015 994
  threshold_val<-c(0.01,0.05,0.1)
1016 995
  s.table_term_tb$p_val_rec1 <- s.table_term_tb[["p-value"]] < threshold_val[1]
1017 996
  s.table_term_tb$p_val_rec2 <- s.table_term_tb[["p-value"]] < threshold_val[2]
......
1097 1076
tb_mod4_LST_rec3 <- aggregate(s_table_LST_mod4$p_val_rec3~s_table_term_mod4$month,FUN=mean)
1098 1077
plot(tb_mod4_LST_rec3,type="l",ylim=c(0.2,1))
1099 1078
s_table_elev_mod4 <- subset(s.table_term_tb,mod_name=="mod4" & term_name=="s(elev_s)")
1100
tb_mod4_elev_rec3 <- aggregate(tb_mod4_elev_rec3$p_val_rec2 ~ tb_mod4_elev_rec3$month,FUN=mean)
1079
#tb_mod4_elev_rec3 <- aggregate(tb_mod4_elev_rec3$p_val_rec2 ~ tb_mod4_elev_rec3$month,FUN=mean)
1101 1080
plot(tb_mod4_elev_rec3)
1102 1081
lines(tb_mod4_elev_rec3)
1103 1082
test1 <- subset(s.table_term_tb,mod_name=="mod1" & term_name=="s(elev_s)")
......
1143 1122

  
1144 1123
#### Now plot figure for paper:
1145 1124

  
1146

  
1147
res_pix<-480
1148
col_mfrow<-3
1149
row_mfrow<-1
1150
png_file_name<- paste("Figure_17_paper_","LST_elev_",out_prefix,".png", sep="")
1151
png(filename=file.path(out_dir,png_file_name),
1152
   width=col_mfrow*res_pix,height=row_mfrow*res_pix)
1153
#png(filename=file.path(out_dir,png_file_name),
1154
    #width=col_mfrow*res_pix,height=row_mfrow*res_pix)
1155
par(mfrow=c(row_mfrow,col_mfrow))
1156

  
1157
plot(tb_mod4_LST_rec3,type="l",ylab="Proportion of significant LST term",
1158
     xlab="Month")
1159
title("Proportion of significant LST term in mod4",cex=1.5)
1160

  
1161
plot(rmse_dif,type="l",ylab="ΔRMSE between mod1 and mod4",
1162
     xlab="Month")
1163
abline(h=0,lty="dashed")
1164
title("Monthly ΔRMSE betwen mod1 and mod4",cex=1.5)
1165

  
1166
plot(df_cor$LST_tmax,ylim=c(-1,1),col="red",type="l",
1167
     ylab="Pearson Correlation",xlab="Month")
1168
legend("topright",legend=c("Elev-tmax","LST-tmax"),col=c("red","blue"),cex=1,bty="n")
1169

  
1170
lines(df_cor$elev_tmax,ylim=c(-1,1),col="blue")
1171
title("Monthly correlation for LST-tmax and elev-tmax",cex=1.5)
1172

  
1173
dev.off()
1174

  
1175 1125
names(tb_mod4_LST_rec3) <- c("month","p_val_rec3")
1176
tb_mod4_LST_rec3$month <- c("month","p_val_rec3")
1126
tb_mod4_LST_rec3$month <- 1:12
1177 1127

  
1178 1128
res_pix<-480
1179 1129
layout_m <- c(1,3) # works if set to this?? ok set the resolution...      
1180 1130
#date_selected <- c("20100101","20100901")
1181
png(paste("Figure_17_paper_","LST_elev_",out_prefix,".png", sep=""),
1131
png(paste("Figure_15_paper_","LST_elev_",out_prefix,".png", sep=""),
1182 1132
    height=res_pix*layout_m[1],width=res_pix*layout_m[2])
1183 1133
    #height=480*6,width=480*4)
1184 1134
     
1185
p_prop <- xyplot(p_val_rec3 ~ as.numeric(month),data=tb_mod4_LST_rec3,
1135
p_prop <- xyplot(p_val_rec3 ~ month,data=tb_mod4_LST_rec3,
1186 1136
          col=c("black"),
1187 1137
          type="b",
1188 1138
          ylab=list(label="\u0394RMSE between mod1 and mod4",cex=1.5),
1189 1139
          xlab=list(label="Month",cex=1.5),
1190 1140
          main=list(label="Proportion of significant LST term in mod4",cex=1.8))
1141
p_prop <- update(p_prop,par.settings = list(axis.text = list(font = 2, cex = 1.3),
1142
                par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5))
1191 1143

  
1192 1144
p_dif <- xyplot(rmse_dif ~ 1:12,
1193 1145
          col=c("black"),
1194 1146
          type="b",
1195 1147
          ylab=list(label="\u0394RMSE between mod1 and mod4",cex=1.5),
1196 1148
          xlab=list(label="Month",cex=1.5),
1197
          main=list(label="Monthly \u0394RMSE betwen mod1 and mod4",cex=1.8))
1149
          main=list(label="Monthly \u0394RMSE betwen mod1 and mod4",cex=1.8),
1150
          par.settings = list(axis.text = list(font = 2, cex = 1.3),
1151
          par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5))
1198 1152
p_dif <- update(p_dif, panel = function(...) {
1199 1153
            panel.abline(h = 0, lty = 2, col = "gray")
1200 1154
            panel.xyplot(...)
......
1217 1171
            panel.abline(h = 0, lty = 2, col = "gray")
1218 1172
            panel.xyplot(...)
1219 1173
        })
1220
          #par.settings = list(axis.text = list(font = 2, cex = 1.3),layout=layout_m,
1221
                              #par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5),
1174
#increasing font size and making it bold
1175
p_cor <- update(p_cor,par.settings = list(axis.text = list(font = 2, cex = 1.3),
1176
                par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5))
1222 1177

  
1223 1178
#grid.arrange(p1,p2,p3,ncol=1)
1224 1179
grid.arrange(p_prop,p_dif,p_cor,ncol=3)
1225 1180

  
1226 1181
dev.off()
1227 1182

  
1228

  
1229 1183
########################
1230 1184
### Prepare table 7: correlation matrix between covariates      
1231 1185

  
......
1257 1211
#Plot a sample variogram, and possibly a fitted model
1258 1212
#model 1 lat,lon and elev
1259 1213
layout_m <- c(1,2) # works if set to this?? ok set the resolution...
1260
      
1214
res_pix <- 480      
1261 1215
date_selected <- c("20100101","20100901")
1262 1216
png(paste("Figure9_paper_","_variogram_",date_selected[1],"_",date_selected[2],"_",out_prefix,".png", sep=""),
1263
    height=960*layout_m[1],width=960*layout_m[2])
1217
    height=res_pix*layout_m[1],width=res_pix*layout_m[2])
1264 1218
    #height=480*6,width=480*4)
1265 1219

  
1266 1220
#p3 <- list_plots_spt[[3]]
......
1268 1222
         ylim=c(0,9),
1269 1223
         ylab=list(label="Semivariance",cex=1.5),
1270 1224
         xlab=list(label="Distance (meter)",cex=1.5),
1271
         main=list(label="Mod1 January 1, 2010",cex=1.8))
1225
         main=list(label="Mod1 January 1, 2010",cex=1.8),
1226
         par.settings = list(axis.text = list(font = 2, cex = 1.3), #control the font size!!
1227
                par.main.text=list(font=2,cex=2),strip.background=list(col="white")),
1228
                par.strip.text=list(font=2,cex=1.5)
1229
         )
1272 1230
#plot(p1)
1273 1231
p241<-plot(raster_prediction_obj_3$method_mod_obj[[241]]$mod[[1]]$exp_var,raster_prediction_obj_3$method_mod_obj[[241]]$mod[[1]]$var_model,
1274 1232
         ylim=c(0,9),
1275 1233
         ylab=list(label="Semivariance",cex=1.5),
1276 1234
         xlab=list(label="Distance (meter)",cex=1.5),
1277
         main=list(label="Mod1 September 1, 2010",cex=1.8))
1278
#plot(p241)
1279

  
1280
#grid.arrange(p1,p2,p3,ncol=1)
1235
         main=list(label="Mod1 September 1, 2010",cex=1.8),
1236
         par.settings = list(axis.text = list(font = 2, cex = 1.3),
1237
                par.main.text=list(font=2,cex=2),strip.background=list(col="white")),
1238
                par.strip.text=list(font=2,cex=1.5)
1239
         )
1281 1240
grid.arrange(p1,p241,ncol=2)
1282 1241

  
1283 1242
dev.off()
1284 1243
#Combine both plot?     + plot info on sill, nugget and range? and most frequent model selected
1285 1244

  
1245
############################
1246
#### Figure 10: Summarize variograms parameters over 365 days
1247

  
1286 1248
list_kg_var_model <- lapply(1:365,function(i){raster_prediction_obj_3$method_mod_obj[[i]]$mod[[1]]$var_model})
1287 1249
list_kg_var_model <- lapply(1:365,function(i){raster_prediction_obj_3$method_mod_obj[[i]]$mod[[1]]$var_model})
1288

  
1289 1250
list_kg_var_model
1290 1251
   
1291 1252
test <- do.call(rbind,list_kg_var_model)
......
1293 1254
tt2 <- subset(test,model=="Nug")      
1294 1255
tb_variogram["Nug"] <- (tt2$psill)
1295 1256

  
1296
add_month_tag<-function(tb){
1297
  date<-strptime(tb$date, "%Y%m%d")   # interpolation date being processed
1298
  month<-strftime(date, "%m")          # current month of the date being processed
1299
}
1300

  
1301 1257
dates<-(extract_from_list_obj(raster_prediction_obj_1$method_mod_obj,"sampling_dat"))$date #get vector of dates
1302 1258
tb_variogram["date"] <- dates
1303 1259
tb_variogram$month <- add_month_tag(tb_variogram)
......
1305 1261

  
1306 1262
layout_m <- c(2,2) # works if set to this?? ok set the resolution...      
1307 1263
#date_selected <- c("20100101","20100901")
1308
png(paste("Figure14_paper_","_variogram_",out_prefix,".png", sep=""),
1264
png(paste("Figure10_paper_","_variogram_",out_prefix,".png", sep=""),
1309 1265
    height=480*layout_m[1],width=480*layout_m[2])
1310 1266
    #height=480*6,width=480*4)
1311 1267
     
......
1333 1289
dev.off()
1334 1290

  
1335 1291
###########################################
1336
### Figure 10: map of residuals...for a specific date...
1292
### Figure 11: map of residuals...for a specific date...
1293

  
1337 1294
index <- 244
1338 1295
names_mod <- names(raster_prediction_obj_2$method_mod_obj[[index]][[y_var_name]]) #names of models to plot
1339 1296
#in_dir2 <-"/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/output_data_365d_gam_day_lst_comb4_08152013"
......
1361 1318
  #p1 <- levelplot(elev,scales = list(draw = FALSE), colorkey = FALSE,col.regions=rev(terrain.colors(255)),contour=T)
1362 1319
  #add legend..par.settings = GrTheme
1363 1320
  cx <- ((data_v[[res_model_name]])*2)
1364
  p1 <- levelplot(elev,scales = list(draw = FALSE), colorkey = FALSE,par.settings = GrTheme)
1321
  p1 <- levelplot(elev,#margin=F,
1322
                  scales = list(draw = FALSE), colorkey = FALSE,par.settings = GrTheme)
1365 1323

  
1366 1324
  #p2 <- spplot(data_v,res_model_name, cex=1 * cx,main=paste("Residuals for ",res_model_name," ",datelabel,sep=""),
1367 1325
  #             col.regions=matlab.like(25))
1368
  p2 <- bubble(data_v,res_model_name, main=paste("Residuals for ",res_model_name," ",datelabel,sep=""),
1369
               col=matlab.like(25))  
1326
  p2 <- bubble(data_v,res_model_name, 
1327
               main=paste("Residuals for ",res_model_name," ",datelabel,sep=""))#,
1328
               #col=matlab.like(5))  
1370 1329
  p3 <- p2 + p1 + p2 #to force legend...
1371 1330
  #p1 <- spplot(interp_area)
1372 1331
  #p3 <- p1+p2 #to force legend...
......
1379 1338
  list_p[[k]] <- p3
1380 1339
}
1381 1340

  
1382
layout_m<-c(1,3) # works if set to this?? ok set the resolution...
1383
png(paste("Figure13_paper_","_residuals_",date_selected,"_",out_prefix,".png", sep=""),
1341
layout_m<-c(4,3) # works if set to this?? ok set the resolution...
1342
png(paste("Figure_11_paper_","_residuals_",date_selected,"_",out_prefix,".png", sep=""),
1384 1343
    height=480*layout_m[1],width=480*layout_m[2])
1385 1344

  
1386
grid.arrange(list_p_mae[[1]],list_p_mae[[2]],list_p_mae[[3]],ncol=3)
1345
grid.arrange(list_p[[1]],list_p[[2]],list_p[[3]],
1346
             list_p[[4]],list_p[[5]],list_p[[6]],
1347
             list_p[[7]],list_p[[8]],list_p[[9]],
1348
             list_p[[10]],ncol=3)
1387 1349
      
1388 1350
dev.off()   
1389 1351

  
1390
######## NOW GET A ACCURACY BY STATIONS
1352
layout_m<-c(1,3) # works if set to this?? ok set the resolution...
1353
png(paste("Figure_11a_paper_","_residuals_",date_selected,"_",out_prefix,".png", sep=""),
1354
    height=480*layout_m[1],width=480*layout_m[2])
1355

  
1356
grid.arrange(list_p[[1]],list_p[[2]],list_p[[3]],
1357
             ncol=3)  
1358
dev.off() 
1359
layout_m<-c(1,3) # works if set to this?? ok set the resolution...
1360
png(paste("Figure_11b_paper_","_residuals_",date_selected,"_",out_prefix,".png", sep=""),
1361
    height=480*layout_m[1],width=480*layout_m[2])
1362

  
1363
grid.arrange(list_p[[4]],list_p[[5]],list_p[[6]],
1364
             ncol=3)  
1365
dev.off() 
1366
layout_m<-c(1,3) # works if set to this?? ok set the resolution...
1367
png(paste("Figure_11c_paper_","_residuals_",date_selected,"_",out_prefix,".png", sep=""),
1368
    height=480*layout_m[1],width=480*layout_m[2])
1369

  
1370
grid.arrange(list_p[[7]],list_p[[8]],list_p[[9]],
1371
             ncol=3)  
1372
dev.off() 
1373
layout_m<-c(1,3) # works if set to this?? ok set the resolution...
1374
png(paste("Figure_11d_paper_","_residuals_",date_selected,"_",out_prefix,".png", sep=""),
1375
    height=480*layout_m[1],width=480*layout_m[2])
1376

  
1377
grid.arrange(list_p[[10]],
1378
             ncol=3)  
1379
dev.off() 
1380
###########################################
1381
### Figure 12: map of MAE by stations over 365 days to summarize residuals information
1382

  
1383
###First get accuracy by stations
1391 1384
l_formulas<-(extract_from_list_obj(raster_prediction_obj_2$method_mod_obj,"formulas")) #get vector of dates
1392 1385
#                            y_var ~ s(lat,lon)
1393 1386
#                y_var ~ s(lat,lon) + s(elev_s)
......
1464 1457
}
1465 1458

  
1466 1459
layout_m<-c(1,3) # works if set to this?? ok set the resolution...
1467
png(paste("Figure15_paper_","average_MAE_",date_selected,"_",out_prefix,".png", sep=""),
1460
png(paste("Figure12_paper_","average_MAE_",date_selected,"_",out_prefix,".png", sep=""),
1468 1461
    height=480*layout_m[1],width=480*layout_m[2])
1469 1462

  
1470 1463
grid.arrange(list_p_mae[[1]],list_p_mae[[2]],list_p_mae[[3]],ncol=3)
1471 1464
      
1472 1465
dev.off()   
1473 1466

  
1474
#infile_reg_outline <- "/data/project/layers/commons/data_workflow/inputs/region_outlines_ref_files/OR83M_state_outline.shp"  #input region outline defined by polygon: Oregon
1475
#reg_outline <- readOGR(dsn=dirname(infile_reg_outline),layer=sub(".shp","",basename(infile_reg_outline)))
1476

  
1477
########### Figure 16: residuals and elev ######
1467
###########################################
1468
### Figure 13: Analysing residuals and relationship to elevation for mod1, mod2 and mod4 ######
1478 1469

  
1479
#raster_prediciton object for baseline 1 () s(lat,lon) + s(elev)) and baseline 2 (slat,lon))      
1470
#raster_predicton object for baseline 1 () s(lat,lon) + s(elev)) and baseline 2 (slat,lon))      
1480 1471
list_data_v <- lapply(1:365,function(i){raster_prediction_obj_2$validation_mod_obj[[i]]$data_v}) #testing with residuals
1481 1472
l_formulas<-(extract_from_list_obj(raster_prediction_obj_2$method_mod_obj,"formulas")) #get vector of dates
1482 1473

  
1483 1474
test <- do.call(rbind,list_data_v)        
1484 1475
data_v_ag <-test                
1485
plot(res_mod1~elev,data=test)                    
1486
plot(res_mod2~elev,data=test)                    
1487 1476
cor(test$res_mod1,test$elev)
1488 1477
cor(test$res_mod2,test$elev,use="complete.obs") #decrease in corellation when using elev
1489 1478
cor(test$res_mod1,test$LST,,use="complete.obs")
1490 1479
cor(test$res_mod5,test$LST,use="complete.obs") #decrease in correlation when using LST
1491

  
1492
plot(res_mod1~LST,data=test)                    
1493
plot(res_mod5~LST,data=test)                    
1494 1480
                     
1495 1481
brks<-c(0,500,1000,1500,2000)
1496 1482
lab_brks<-1:4
1497 1483
elev_rcstat<-cut(data_v_ag$elev,breaks=brks,labels=lab_brks,right=F)
1498 1484

  
1499 1485
#Now set up plotting device
1500
res_pix<-480
1501
col_mfrow<-3
1502
row_mfrow<-1
1503
png_file_name<- paste("Figure_16_paper_","residuals_MAE_",out_prefix,".png", sep="")
1504
png(filename=file.path(out_dir,png_file_name),
1505
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
1506
par(mfrow=c(row_mfrow,col_mfrow))
1507

  
1508
boxplot(data_v_ag$res_mod1~elev_rcstat,ylim=c(-15,15),ylab="Residuals (deg C)",main="Residuals vs elevation classes for mod1=lat*lon",
1509
        xlab="Elevation classes (meter)",names=c("0-500","500-1000","1000-1500","1500-2000"))           
1510
boxplot(data_v_ag$res_mod5~elev_rcstat,ylim=c(-15,15),ylab="Residuals (deg C)",main="Residuals vs elevation classes for mod5=lat*lon+LST",
1511
        xlab="Elevation classes (meter)",names=c("0-500","500-1000","1000-1500","1500-2000"))           
1512
boxplot(data_v_ag$res_mod2~elev_rcstat,ylim=c(-15,15),ylab="Residuals (deg C)",main="Residuals vs elevation classes for mod2=lat*lon+elev",
1513
        xlab="Elevation classes (meter)",names=c("0-500","500-1000","1000-1500","1500-2000"))           
1514
dev.off()
1515 1486

  
1516 1487
layout_m <- c(1,3) # works if set to this?? ok set the resolution...      
1517
png(paste("Figure_16_paper_","residuals_MAE_",out_prefix,".png", sep=""),
1488
png(paste("Figure_13_paper_","residuals_MAE_",out_prefix,".png", sep=""),
1518 1489
    height=480*layout_m[1],width=480*layout_m[2])
1519 1490
    #height=480*6,width=480*4)
1520 1491

  
1521 1492
p_bw1<-bwplot(data_v_ag$res_mod1~elev_rcstat,do.out=F,ylim=c(-15,15),
1522 1493
         ylab=list(label="Residuals (deg C)",cex=1.5),
1523 1494
         xlab=list(label="Elevation classes (meter)",cex=1.5),
1524
         main=list(label="Residuals vs elevation for mod1=lat*lon",cex=1.8),
1525
         scales = list(x = list(at = c(1, 2, 3, 4), 
1526
                               labels = c("0-500","500-1000","1000-1500","1500-2000"))))
1495
         main=list(label="Residuals vs elev for mod1=lat*lon",cex=1.8),
1496
         scales = list(x = list(at = c(1, 2, 3, 4), #provide tick location and labels
1497
                               labels = c("0-500","500-1000","1000-1500","1500-2000"))),
1498
                       par.settings = list(axis.text = list(font = 2, cex = 1.3), #control the font size!!
1499
        par.main.text=list(font=2,cex=2),strip.background=list(col="white")),
1500
        par.strip.text=list(font=2,cex=1.5)
1501
        )
1527 1502

  
1528 1503
p_bw2 <- bwplot(data_v_ag$res_mod5~elev_rcstat,do.out=F,ylim=c(-15,15),
1529 1504
         ylab=list(label="Residuals (deg C)",cex=1.5),
1530 1505
         xlab=list(label="Elevation classes (meter)",cex=1.5),
1531
         main=list(label="Residuals vs elevation for mod5=lat*lon+LST",cex=1.8),
1506
         main=list(label="Residuals vs elev for mod5=lat*lon+LST",cex=1.8),
1532 1507
         scales = list(x = list(at = c(1, 2, 3, 4), 
1533
                               labels = c("0-500","500-1000","1000-1500","1500-2000"))))
1508
                               labels = c("0-500","500-1000","1000-1500","1500-2000"))),
1509
         par.settings = list(axis.text = list(font = 2, cex = 1.3), #control the font size!!
1510
         par.main.text=list(font=2,cex=2),strip.background=list(col="white")),
1511
         par.strip.text=list(font=2,cex=1.5)
1512
         )
1534 1513

  
1535
p_bw3 <- bwplot(data_v_ag$res_mod5~elev_rcstat,do.out=F,ylim=c(-15,15),
1514
p_bw3 <- bwplot(data_v_ag$res_mod2~elev_rcstat,do.out=F,ylim=c(-15,15),
1536 1515
         ylab=list(label="Residuals (deg C)",cex=1.5),
1537 1516
         xlab=list(label="Elevation classes (meter)",cex=1.5),
1538
         main=list(label="Residuals vs elevation for mod5=lat*lon+LST",cex=1.8),
1517
         main=list(label="Residuals vs elev for mod2=lat*lon+elev",cex=1.8),
1539 1518
         scales = list(x = list(at = c(1, 2, 3, 4), 
1540
                               labels = c("0-500","500-1000","1000-1500","1500-2000"))))
1519
                               labels = c("0-500","500-1000","1000-1500","1500-2000"))),
1520
         par.settings = list(axis.text = list(font = 2, cex = 1.3), #control the font size!!
1521
                par.main.text=list(font=2,cex=2),strip.background=list(col="white")),
1522
                par.strip.text=list(font=2,cex=1.5)
1523
                )
1541 1524
grid.arrange(p_bw1,p_bw2,p_bw3,ncol=3)
1542 1525
dev.off()
1543

  
1544
#layout_m <- c(1,3) # works if set to this?? ok set the resolution...      
1545
#png(paste("Figure16_paper_","residuals_MAE",out_prefix,".png", sep=""),
1546
#    height=480*layout_m[1],width=480*layout_m[2])
1547
#    #height=480*6,width=480*4)
1548
     
1549
#p_bw1<- bwplot(data_v_ag$res_mod1~elev_rcstat,ylim=c(-15,15),do.out=F,
1550
#         ylab=list(label="Residuals for Mod1 (deg C)",cex=1.5),
1551
#         xlab=list(label="Elevation class",cex=1.5),
1552
#         main=list(label="Mod1 range",cex=1.8))
1553
#p_bw2<-bwplot(tb_variogram$Nug~tb_variogram$month,do.out=F,ylim=c(0,12),
1554
#         ylab=list(label="Nugget of variograms",cex=1.5),
1555
#         xlab=list(label="Month",cex=1.5),
1556
#         main=list(label="Mod1 Nugget",cex=1.8))
1557
#p_bw3<-bwplot(tb_variogram$psill~tb_variogram$month,do.out=F,ylim=c(0,30),
1558
#         ylab=list(label="Sill of variograms",cex=1.5),
1559
#         xlab=list(label="Month",cex=1.5),
1560
#         main=list(label="Mod1 sill",cex=1.8))
1561
#grid.arrange(p1,p2,p3,ncol=1)
1562
#grid.arrange(p_hist,p_bw1,p_bw2,p_bw3,ncol=2)
1563

  
1564
#dev.off()
1565

  
1566
#brks<-c(0,20,40,60,80,100)
1567
#lab_brks<-1:5
1568
#rcstat<-cut(data_v_ag$LC1,breaks=brks,labels=lab_brks,right=F)
1569
#plot(data_v_ag$res_mod5~rcstat,ylim=c(-5,5))                      
1570
#plot(data_v_ag$res_mod5~rcstat,ylim=c(-5,5))
1571 1526
                      
1572 1527
###################### END OF SCRIPT #######################

Also available in: Unified diff