Revision fff944c5
Added by Benoit Parmentier about 11 years ago
climate/research/oregon/interpolation/contribution_of_covariates_paper_interpolation.R | ||
---|---|---|
46 | 46 |
source(file.path(script_path,function_analyses_paper)) #source all functions used in this script. |
47 | 47 |
|
48 | 48 |
in_dir1 <-"/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/output_data_365d_gam_day_lst_comb3_08132013" |
49 |
in_dir2 <-"/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/output_data_365d_gam_day_lst_comb4_07152013/" |
|
50 |
|
|
49 |
in_dir2 <-"/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/output_data_365d_gam_day_lst_comb4_08152013" |
|
51 | 50 |
#kriging results: |
52 | 51 |
in_dir3 <-"/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/output_data_365d_kriging_day_lst_comb3_07112013" |
53 | 52 |
#gwr results: |
... | ... | |
67 | 66 |
method_interpolation <- "gam_daily" |
68 | 67 |
covar_obj_file_1 <- "covar_obj__365d_gam_day_lst_comb3_08132013.RData" |
69 | 68 |
met_obj_file_1 <- "met_stations_outfiles_obj_gam_daily__365d_gam_day_lst_comb3_08132013.RData" |
69 |
#met_stations_outfiles_obj_gam_daily__365d_gam_day_lst_comb3_08132013.RData |
|
70 | 70 |
|
71 | 71 |
#raster_prediciton object for baseline 1 () s(lat,lon) + s(elev)) and baseline 2 (slat,lon)) |
72 | 72 |
raster_obj_file_1 <- "raster_prediction_obj_gam_daily_dailyTmax_365d_gam_day_lst_comb3_08132013.RData" |
73 |
raster_obj_file_2 <- "raster_prediction_obj_gam_daily_dailyTmax_365d_gam_day_lst_comb4_07152013.RData"
|
|
73 |
raster_obj_file_2 <- "raster_prediction_obj_gam_daily_dailyTmax_365d_gam_day_lst_comb4_08152013.RData"
|
|
74 | 74 |
|
75 | 75 |
raster_obj_file_3 <- "raster_prediction_obj_kriging_daily_dailyTmax_365d_kriging_day_lst_comb3_07112013.RData" |
76 | 76 |
raster_obj_file_4 <- "raster_prediction_obj_gwr_daily_dailyTmax_365d_gwr_day_lst_comb3_part1_07122013.RData" |
... | ... | |
117 | 117 |
table_data2 <-summary_metrics_v2$avg[,c("mae","rmse","me","r")] |
118 | 118 |
|
119 | 119 |
###Table 3a, baseline 1: s(lat,lon) |
120 |
|
|
121 |
model_col<-c("Baseline1","Elevation","Northing","Easting","LST","DISTOC","Forest","CANHEIGHT") #,"LST*Forest","LST*CANHEIGHT") #
|
|
120 |
#Chnage here !!! need to reorder rows based on mod first |
|
121 |
model_col<-c("Baseline1","Elevation","Northing","Easting","LST","DISTOC","Forest","CANHEIGHT","LST*Forest","LST*CANHEIGHT") # |
|
122 | 122 |
df3a<- as.data.frame(sapply(table_data2,FUN=function(x) x-x[1])) |
123 | 123 |
df3a<- round(df3a,digit=3) #roundto three digits teh differences |
124 | 124 |
df3a$Model <-model_col |
... | ... | |
128 | 128 |
###Table 3b, baseline 2: s(lat,lon) + s(elev) |
129 | 129 |
|
130 | 130 |
model_col<-c("Baseline2","Northness","Eastness","LST","DISTOC","Forest","CANHEIGHT","LST*Forest","LST*CANHEIGHT") |
131 |
names_table_col<-c("DiffMAE","DiffRMSE","DiffME","Diffr","Model")
|
|
131 |
names_table_col<-c("ΔMAE","ΔRMSE","ΔME","Δr","Model")
|
|
132 | 132 |
|
133 | 133 |
df3b <- as.data.frame(sapply(table_data1,FUN=function(x) x-x[1])) #difference between baseline (line 1) and other models |
134 | 134 |
df3b <- round(df3b,digit=3) #roundto three digits the differences |
... | ... | |
136 | 136 |
names(df3b)<- names_table_col |
137 | 137 |
print(df3b) #Part b of table 3 |
138 | 138 |
|
139 |
sd2_v <-calc_stat_from_raster_prediction_obj(raster_prediction_obj_2,"sd",training=FALSE) |
|
140 |
|
|
139 | 141 |
#Testing siginificance between models |
140 | 142 |
|
141 | 143 |
mod_compk1 <-kruskal.test(tb1$rmse ~ as.factor(tb1$pred_mod)) #Kruskal Wallis test |
... | ... | |
489 | 491 |
#Now plot figure 6 |
490 | 492 |
res_pix<-480 |
491 | 493 |
col_mfrow<-2 |
492 |
row_mfrow<-2 |
|
494 |
#row_mfrow<-2 |
|
495 |
row_mfrow<-1 |
|
496 |
|
|
493 | 497 |
png_file_name<- paste("Figure_6_monthly_accuracy_",out_prefix,".png", sep="") |
494 | 498 |
png(filename=file.path(out_dir,png_file_name), |
495 | 499 |
width=col_mfrow*res_pix,height=row_mfrow*res_pix) |
... | ... | |
507 | 511 |
|
508 | 512 |
ylab_text<-"MAE (C)" |
509 | 513 |
xlab_text<-"Month" |
510 |
y_range<-range(month_data_list$gam$mae,month_data_list$kriging$mae,month_data_list$gwr$mae) |
|
511 |
boxplot(mae~month,data=month_data_list$gam,ylim=y_range,main="GAM",ylab=ylab_text,outline=FALSE) |
|
512 |
boxplot(mae~month,data=month_data_list$kriging,ylim=y_range,main="Kriging",ylab=ylab_text,outline=FALSE) |
|
513 |
boxplot(mae~month,data=month_data_list$gwr,ylim=y_range,main="GWR",ylab=ylab_text,outline=FALSE) |
|
514 |
#y_range<-range(month_data_list$gam$mae,month_data_list$kriging$mae,month_data_list$gwr$mae) |
|
515 |
#y_range<-range(month_data_list$gam$mae) |
|
516 |
boxplot(mae~month,data=month_data_list$gam,main="GAM",ylab=ylab_text,outline=FALSE) |
|
517 |
#boxplot(mae~month,data=month_data_list$kriging,ylim=y_range,main="Kriging",ylab=ylab_text,outline=FALSE) |
|
518 |
#boxplot(mae~month,data=month_data_list$gwr,ylim=y_range,main="GWR",ylab=ylab_text,outline=FALSE) |
|
514 | 519 |
|
515 | 520 |
dev.off() |
516 | 521 |
|
517 |
plot(x3[month=="01",c("mae")])) |
|
518 |
median(x3[x3$month=="03",c("mae")],na.rm=T) |
|
519 |
mean(x3[x3$month=="03",c("mae")],na.rm=T) |
|
520 |
|
|
521 |
boxplot(x) |
|
522 |
#Now generate table 5 |
|
522 | 523 |
|
523 |
#Now generate table
|
|
524 |
test<-boxplot(mae~month,data=month_data_list$gam,main="GAM",ylab=ylab_text,outline=FALSE)
|
|
524 | 525 |
|
525 | 526 |
length(tb1_month$mae) |
526 | 527 |
names(tb1_month) |
527 | 528 |
|
529 |
#Calculate standard deviation for each metric |
|
530 |
sd1 <- calc_stat_month_from_raster_prediction_obj(raster_prediction_obj_1,"sd") # see function script |
|
531 |
sd2 <- calc_stat_month_from_raster_prediction_obj(raster_prediction_obj_2,"sd") # standard deviation for baseline 2 |
|
532 |
sd3 <- calc_stat_month_from_raster_prediction_obj(raster_prediction_obj_3,"sd") # kriging |
|
533 |
sd4 <- calc_stat_month_from_raster_prediction_obj(raster_prediction_obj_4,"sd") #gwr |
|
534 |
|
|
535 |
avg_v1 <- calc_stat_month_from_raster_prediction_obj(raster_prediction_obj_1,"mean") # see function script |
|
536 |
avg_v2 <- calc_stat_month_from_raster_prediction_obj(raster_prediction_obj_2,"mean") # standard deviation for baseline 2 |
|
537 |
avg_v3 <- calc_stat_month_from_raster_prediction_obj(raster_prediction_obj_3,"mean") # kriging |
|
538 |
avg_v4 <- calc_stat_month_from_raster_prediction_obj(raster_prediction_obj_4,"mean") #gwr |
|
539 |
|
|
540 |
#Combined sd in one table for mod1 (baseline 2) |
|
541 |
table_sd <- do.call(cbind,list(gam=sd1[sd1$pred_mod=="mod1",c("rmse")], |
|
542 |
kriging=sd3[sd3$pred_mod=="mod1",c("rmse")], |
|
543 |
gwr=sd4[sd4$pred_mod=="mod1",c("rmse")])) #table containing the sd for the three mdethods for baseline 2 |
|
544 |
table_sd <- as.data.frame(round(table_sd,digit=3)) #round to three digits the differences |
|
545 |
|
|
546 |
#Combined mean in one table for mod1 (baseline 2) |
|
547 |
table_avg <- do.call(cbind,list(gam=avg_v1[avg_v1$pred_mod=="mod1",c("rmse")], |
|
548 |
kriging=avg_v3[sd3$pred_mod=="mod1",c("rmse")], |
|
549 |
gwr=avg_v4[avg_v4$pred_mod=="mod1",c("rmse")])) #table containing the sd for the three mdethods for baseline 2 |
|
550 |
table_avg <- as.data.frame(round(table_avg,digit=3)) #round to three digits the differences |
|
551 |
|
|
552 |
#combined tables with accuracy metrics and their standard deviations |
|
553 |
table5_paper <-table_combined_symbol(table_avg,table_sd,"±") |
|
554 |
table5_paper$month <- month.abb |
|
555 |
|
|
556 |
file_name<-paste("table5_comparisons_monthly_averages_interpolation_methods_paper","_",out_prefix,".txt",sep="") |
|
557 |
write.table(as.data.frame(table5_paper),file=file_name,sep=",") |
|
558 |
|
|
528 | 559 |
####### FIGURE 7: Spatial pattern ###### |
529 | 560 |
|
530 | 561 |
y_var_name <-"dailyTmax" |
531 | 562 |
index<-244 #index corresponding to January 1 |
532 | 563 |
|
533 |
lf1 <- raster_prediction_obj_1$method_mod_obj[[index]][[y_var_name]] |
|
564 |
lf1 <- raster_prediction_obj_1$method_mod_obj[[index]][[y_var_name]] #select relevant raster images for the given dates
|
|
534 | 565 |
lf3 <- raster_prediction_obj_3$method_mod_obj[[index]][[y_var_name]] |
535 | 566 |
lf4 <- raster_prediction_obj_4$method_mod_obj[[index]][[y_var_name]] |
536 | 567 |
|
... | ... | |
566 | 597 |
|
567 | 598 |
## FIGURE COMPARISON OF MODELS COVARRIATES |
568 | 599 |
|
600 |
lf2 <- raster_prediction_obj_2$method_mod_obj[[index]][[y_var_name]] |
|
601 |
lf2 #contains the models for gam |
|
602 |
|
|
603 |
pred_temp_s <-stack(lf2) |
|
604 |
date_selected <- "20109101" |
|
605 |
#names_layers <-c("mod1=s(lat,long)+s(elev)","mod4=s(lat,long)+s(LST)","diff=mod1-mod4") |
|
606 |
names_layers <-c("mod1 = s(lat,long)","mod2 = s(lat,long)+s(elev)","mod3 = s(lat,long)+s(N_w)","mod4 = s(lat,long)+s(E_w)", |
|
607 |
"mod5 = s(lat,long)+s(LST)","mod6 = s(lat,long)+s(DISTOC)","mod7 = s(lat,long)+s(LC1)", |
|
608 |
"mod8 = s(lat,long)+s(LC1,LST)","mod9 = s(lat,long)+s(CANHGHT)","mod10 = s(lat,long)+s(LST,CANHGHT)") |
|
609 |
|
|
610 |
#names_layers<-names(pred_temp_s) |
|
611 |
#names(pred_temp_s)<-names_layers |
|
612 |
|
|
613 |
s.range <- c(min(minValue(pred_temp_s)), max(maxValue(pred_temp_s))) |
|
614 |
#s.range <- s.range+c(5,-5) |
|
615 |
col.breaks <- pretty(s.range, n=200) |
|
616 |
lab.breaks <- pretty(s.range, n=100) |
|
617 |
temp.colors <- colorRampPalette(c('blue', 'white', 'red')) |
|
618 |
max_val<-s.range[2] |
|
619 |
min_val <-s.range[1] |
|
620 |
#max_val<- -10 |
|
621 |
#min_val <- 0 |
|
622 |
layout_m<-c(4,3) #one row two columns |
|
623 |
|
|
624 |
png(paste("Figure_7_spatial_pattern_tmax_prediction_models_baseline1_gam_levelplot_",date_selected,out_prefix,".png", sep=""), |
|
625 |
height=480*layout_m[1],width=480*layout_m[2]) |
|
626 |
|
|
627 |
levelplot(pred_temp_s,main="Interpolated Surfaces Model Comparison baseline 1", ylab=NULL,xlab=NULL, |
|
628 |
par.settings = list(axis.text = list(font = 2, cex = 1.3),layout=layout_m, |
|
629 |
par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5), |
|
630 |
names.attr=names_layers,col.regions=temp.colors,at=seq(max_val,min_val,by=0.01)) |
|
631 |
#col.regions=temp.colors(25)) |
|
632 |
dev.off() |
|
633 |
|
|
634 |
|
|
635 |
################ #FIGURE 8 |
|
569 | 636 |
lf1 <- raster_prediction_obj_1$method_mod_obj[[index]][[y_var_name]] |
570 | 637 |
lf1 #contains the models for gam |
571 | 638 |
|
572 | 639 |
pred_temp_s <-stack(lf1$mod1,lf1$mod4) |
573 | 640 |
date_selected <- "20109101" |
574 | 641 |
#names_layers <-c("mod1=s(lat,long)+s(elev)","mod4=s(lat,long)+s(LST)","diff=mod1-mod4") |
575 |
names_layers <-c("mod1=s(lat,long)+s(elev)","mod4=s(lat,long)+s(LST)")
|
|
642 |
names_layers <-c("mod1 = s(lat,long)+s(elev)","mod4 = s(lat,long)+s(LST)")
|
|
576 | 643 |
names(pred_temp_s)<-names_layers |
577 | 644 |
s.range <- c(min(minValue(pred_temp_s)), max(maxValue(pred_temp_s))) |
578 | 645 |
#s.range <- s.range+c(5,-5) |
... | ... | |
585 | 652 |
min_val <- 0 |
586 | 653 |
layout_m<-c(1,2) #one row two columns |
587 | 654 |
|
588 |
png(paste("spatial_pattern_tmax_prediction_models_gam_levelplot_",date_selected,out_prefix,".png", sep=""), |
|
655 |
png(paste("Figure_8a_spatial_pattern_tmax_prediction_models_gam_levelplot_",date_selected,out_prefix,".png", sep=""),
|
|
589 | 656 |
height=480*layout_m[1],width=480*layout_m[2]) |
590 | 657 |
|
591 | 658 |
levelplot(pred_temp_s,main="Interpolated Surfaces Model Comparison", ylab=NULL,xlab=NULL, |
... | ... | |
593 | 660 |
par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5), |
594 | 661 |
names.attr=names_layers,col.regions=temp.colors,at=seq(max_val,min_val,by=0.01)) |
595 | 662 |
#col.regions=temp.colors(25)) |
663 |
|
|
664 |
#col.regions=temp.colors(25)) |
|
596 | 665 |
dev.off() |
597 | 666 |
|
667 |
|
|
598 | 668 |
diff<-raster(lf1$mod1)-raster(lf1$mod4) |
599 | 669 |
names_layers <- c("difference=mod1-mod4") |
600 | 670 |
names(diff) <- names_layers |
671 |
|
|
672 |
|
|
673 |
png(paste("Figure_8b_spatial_pattern_tmax_prediction_models_gam_levelplot_",date_selected,out_prefix,".png", sep=""), |
|
674 |
height=480*layout_m[1],width=480*layout_m[2]) |
|
675 |
|
|
601 | 676 |
plot(diff,col=temp.colors(100),main=names_layers) |
602 | 677 |
#levelplot(diff,main="Interpolated Surfaces Model Comparison", ylab=NULL,xlab=NULL, |
603 | 678 |
# par.settings = list(axis.text = list(font = 2, cex = 1.3),layout=c(1,1), |
604 | 679 |
# par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5), |
605 | 680 |
# names.attr=names_layers,col.regions=temp.colors) |
681 |
dev.off() |
|
606 | 682 |
|
607 |
######## NOW GET A ACCUURAY BY STATIONS
|
|
683 |
######## NOW GET A ACCURACY BY STATIONS
|
|
608 | 684 |
|
609 | 685 |
list_data_v<-extract_list_from_list_obj(raster_prediction_obj_1$validation_mod_obj,"data_v") |
610 | 686 |
data_v_test <- list_data_v[[1]] |
... | ... | |
612 | 688 |
#Convert sp data.frame and combined them in one unique df, see function define earlier |
613 | 689 |
data_v_combined <-convert_spdf_to_df_from_list(list_data_v) #long rownames |
614 | 690 |
names_var<-c("res_mod1","res_mod2","res_mod3","res_mod4","res_mod5","res_mod6","res_mod7","res_mod8") |
691 |
|
|
692 |
limit_val<- c(-30,-2.57,0,2.57,30) |
|
693 |
data_v_combined$res_mod1_rc1 <- cut(data_v_combined$res_mod1,include.lowest=TRUE,breaks=limit_val) |
|
694 |
data_v_combined$res_mod1_rc1 |
|
695 |
|
|
696 |
t<-melt(data_v_combined, |
|
697 |
measure=names_var, |
|
698 |
id=c("res_mod1_rc1","id"), |
|
699 |
na.rm=T) |
|
700 |
|
|
701 |
n_tb<-cast(t,res_mod1_rc1+id~variable,length) |
|
702 |
n_tb_tot <-cast(t,id~variable,length) #number of times the stations was used for validation |
|
703 |
|
|
704 |
merge(n_tb$id |
|
705 |
dim(n_tb) |
|
706 |
#mae_tb <-cast(t,dst_cat1~variable,mae_fun) |
|
707 |
#rmse_tb <-cast(t,dst_cat1~variable,rmse_fun) |
|
708 |
#sd_abs_tb<-cast(t,dst_cat1~variable,sd_abs_fun) |
|
709 |
|
|
710 |
#avg_tb<-cast(t,dst_cat1~variable,mean) |
|
711 |
#sd_tb<-cast(t,dst_cat1~variable,sd) |
|
712 |
|
|
615 | 713 |
t<-melt(data_v_combined, |
616 | 714 |
measure=names_var, |
617 | 715 |
id=c("id"), |
618 | 716 |
na.rm=T) |
619 | 717 |
|
718 |
hist(data_v_combined) |
|
719 |
names(data_v_combined) |
|
720 |
|
|
620 | 721 |
mae_fun<-function(x){mean(abs(x))} #Mean Absolute Error give a residuals vector |
621 | 722 |
sd_abs_fun<-function(x){sd(abs(x))} #sd Absolute Error give a residuals vector |
622 | 723 |
|
... | ... | |
629 | 730 |
#n_tb<-cast(t,dst_cat1~variable,length) |
630 | 731 |
|
631 | 732 |
met_obj <-load_obj(file.path(in_dir1,met_obj_file_1)) |
632 |
stat_loc<-readOGR(dsn=dirname(met_obj$loc_stations),layer=sub(".shp","",basename(met_obj$loc_stations)))
|
|
733 |
stat_loc<-readOGR(dsn=in_dir1,layer=sub(".shp","",basename(met_obj$loc_stations)))
|
|
633 | 734 |
|
634 | 735 |
data_v_mae <-merge(mae_tb,stat_loc,by.x=c("id"),by.y=c("STAT_ID")) |
635 | 736 |
hist(data_v_mae$res_mod1) |
... | ... | |
674 | 775 |
|
675 | 776 |
p_elev +layer(sp.polygons(reg_outline,lwd=0.9,col="green")) + layer(sp.points(data_id_setdiff,pch=4,cex=2,col="pink")) |
676 | 777 |
|
677 |
#### ls() |
|
678 |
|
|
679 |
#Now get p values and other things... |
|
680 |
|
|
778 |
############################### |
|
779 |
########## Prepare table 6 |
|
780 |
# Now get p values and other things... |
|
681 | 781 |
###baseline 2: s(lat,lon) + s(elev) |
782 |
|
|
783 |
l_obj<-vector("list",length=2) |
|
784 |
l_obj[[1]]<-raster_prediction_obj_1 |
|
785 |
l_obj[[2]]<-raster_prediction_obj_2 |
|
786 |
l_table <- vector("list",length=length(l_obj)) |
|
787 |
for (k in 1:length(l_obj)){ |
|
788 |
raster_prediction_obj<- l_obj[[k]] |
|
789 |
list_myModels <- extract_list_from_list_obj(raster_prediction_obj$method_mod_obj,"mod") |
|
790 |
|
|
791 |
list_models_info <-lapply(1:length(list_myModels),FUN=create_s_and_p_table_term_models,list_myModels) |
|
792 |
dates<-(extract_from_list_obj(raster_prediction_obj_1$method_mod_obj,"sampling_dat"))$date #get vector of dates |
|
793 |
names(list_models_info)<-dates #adding names to the list |
|
794 |
|
|
795 |
#Prepare and process p. value information regarding models: count number of times values were above a threshold. |
|
796 |
s.table_term_tb <-extract_from_list_obj(list_models_info,"s.table_term") |
|
797 |
|
|
798 |
threshold_val<-c(0.01,0.05,0.1) |
|
799 |
s.table_term_tb$p_val_rec1 <- s.table_term_tb[["p-value"]] < threshold_val[1] |
|
800 |
s.table_term_tb$p_val_rec2 <- s.table_term_tb[["p-value"]] < threshold_val[2] |
|
801 |
s.table_term_tb$p_val_rec3 <- s.table_term_tb[["p-value"]] < threshold_val[3] |
|
802 |
|
|
803 |
names_var <- c("p_val_rec1","p_val_rec2","p_val_rec3") |
|
804 |
t2<-melt(s.table_term_tb, |
|
805 |
measure=names_var, |
|
806 |
id=c("mod_name","term_name"), |
|
807 |
na.rm=T) |
|
808 |
|
|
809 |
summary_s.table_term2 <- cast(t2,term_name+mod_name~variable,sum) |
|
810 |
|
|
811 |
#Now add AIC |
|
812 |
AIC_models_tb <-extract_from_list_obj(list_models_info,"AIC_models") |
|
813 |
AIC_models_tb |
|
814 |
names_var <- c("AIC") |
|
815 |
#id_var <- |
|
816 |
t3<-melt(AIC_models_tb, |
|
817 |
measure=names_var, |
|
818 |
id=c("mod_name","term_name"), |
|
819 |
na.rm=T) |
|
820 |
|
|
821 |
summary_AIC <- cast(t3,term_name+mod_name~variable,median) |
|
822 |
summary_AIC$AIC <- round(summary_AIC[,c("AIC")],digit=2) #roundto three digits teh differences |
|
823 |
|
|
824 |
#Now combine tables and drop duplicate columns the combined table can be modified for the paper... |
|
825 |
avg_s <- calc_stat_from_raster_prediction_obj(raster_prediction_obj,"mean",training=TRUE) |
|
826 |
avg_v <- calc_stat_from_raster_prediction_obj(raster_prediction_obj,"mean",training=FALSE) |
|
827 |
avg <- cbind(avg_s[,c("pred_mod","mae")],avg_v[,c("mae")]) |
|
828 |
names(avg)<-c("pred_mod","mae_s","mae_v") |
|
829 |
avg[,c("mae_s","mae_v")] <- round(avg[,c("mae_s","mae_v")],digit=2) |
|
830 |
table <- merge(summary_AIC,avg,by.x="mod_name",by.y="pred_mod") |
|
831 |
|
|
832 |
tables_AIC_ac_p_val <-list(table,summary_s.table_term2) |
|
833 |
names(tables_AIC_ac_p_val) <-c("table","s.table_p_val_term") |
|
834 |
l_table[[k]] <- tables_AIC_ac_p_val |
|
835 |
} |
|
682 | 836 |
|
683 |
tb1_s |
|
684 |
names_var <- c("mae","rmse","me","r") |
|
685 |
#id_var <- |
|
686 |
t<-melt(tb1_s, |
|
687 |
measure=names_var, |
|
688 |
id=c("pred_mod"), |
|
689 |
na.rm=T) |
|
690 |
|
|
691 |
summary_metrics_s1$avg <-cast(t,pred_mod~variable,mean) |
|
692 |
#sd_abs_tb<-cast(t,dst_cat1~variable,sd_abs_fun) |
|
693 |
|
|
694 |
#summary_metrics_s1<-raster_prediction_obj_1$summary_metrics_s |
|
695 |
#summary_metrics_s2<-raster_prediction_obj_2$summary_metrics_v |
|
696 |
|
|
697 |
table_data1 <-summary_metrics_s1$avg[,c("mae","rmse","me","r")] |
|
698 |
#table_data2 <-summary_metrics_v2$avg[,c("mae","rmse","me","r")] |
|
699 |
|
|
700 |
model_col<-c("Baseline2","Northness","Eastness","LST","DISTOC","Forest","CANHEIGHT","LST*Forest") # removed ,"LST*CANHEIGHT") |
|
701 |
names_table_col<-c("DiffMAE","DiffRMSE","DiffME","Diffr","Model") |
|
702 |
|
|
703 |
df1<- as.data.frame(sapply(table_data1,FUN=function(x) x-x[1])) |
|
704 |
df1<- round(df1,digit=3) #roundto three digits teh differences |
|
705 |
df1$Model <-model_col |
|
706 |
names(df1)<- names_table_col |
|
707 |
df1 |
|
708 |
|
|
709 |
list_myModels <- extract_list_from_list_obj(raster_prediction_obj_1$method_mod_obj,"mod") |
|
710 |
|
|
711 |
#for (i in 1:length(list_myModels)){ |
|
712 |
# i<-1 |
|
713 |
|
|
714 |
list_models_info <-lapply(1:length(list_myModels),FUN=create_s_and_p_table_term_models,list_myModels) |
|
715 |
#raster_prediction_obj_1$method_mod_obj[[i]]$sampling_dat$date |
|
716 |
dates<-(extract_from_list_obj(raster_prediction_obj_1$method_mod_obj,"sampling_dat"))$date #get vector of dates |
|
717 |
names(list_models_info)<-dates |
|
718 |
|
|
719 |
#Add dates to the data.frame?? -->later |
|
720 |
|
|
721 |
s.table_term_tb <-extract_from_list_obj(list_models_info,"s.table_term") |
|
722 |
#s.table_term_tb_t <-extract_list_from_list_obj(list_models_info,"s.table_term") #add dates to summary later |
|
723 |
AIC_models_tb <-extract_from_list_obj(list_models_info,"AIC_models") |
|
724 |
|
|
725 |
threshold_val<-c(0.01,0.05,0.1) |
|
726 |
s.table_term_tb$p_val_rec1 <- s.table_term_tb[["p-value"]] < threshold_val[1] |
|
727 |
s.table_term_tb$p_val_rec2 <- s.table_term_tb[["p-value"]] < threshold_val[2] |
|
728 |
s.table_term_tb$p_val_rec3 <- s.table_term_tb[["p-value"]] < threshold_val[3] |
|
729 |
|
|
730 |
#test<-do.call(rbind,s.table_term_tb_t) |
|
731 |
|
|
732 |
s.table_term_tb |
|
733 |
names_var <- c("p-value") |
|
734 |
#id_var <- |
|
735 |
t<-melt(s.table_term_tb, |
|
736 |
measure=names_var, |
|
737 |
id=c("mod_name","term_name"), |
|
738 |
na.rm=T) |
|
739 |
|
|
740 |
summary_s.table_term <- cast(t,term_name+mod_name~variable,median) |
|
741 |
summary_s.table_term |
|
742 |
|
|
743 |
names_var <- c("p_val_rec1","p_val_rec2","p_val_rec3") |
|
744 |
t2<-melt(s.table_term_tb, |
|
745 |
measure=names_var, |
|
746 |
id=c("mod_name","term_name"), |
|
747 |
na.rm=T) |
|
748 |
|
|
749 |
summary_s.table_term2 <- cast(t2,term_name+mod_name~variable,sum) |
|
750 |
summary_s.table_term2 |
|
751 |
|
|
752 |
#Now combine tables and drop duplicate columns the combined table can be modified for the paper... |
|
753 |
s.table_summary_tb <- cbind(summary_s.table_term,summary_s.table_term2[,]) #-c("term_name","mod_name")]) |
|
754 |
|
|
755 |
AIC_models_tb |
|
756 |
names_var <- c("AIC") |
|
757 |
#id_var <- |
|
758 |
t3<-melt(AIC_models_tb, |
|
759 |
measure=names_var, |
|
760 |
id=c("mod_name","term_name"), |
|
761 |
na.rm=T) |
|
762 |
|
|
763 |
summary_AIC <- cast(t3,term_name+mod_name~variable,median) |
|
764 |
summary_AIC |
|
765 |
|
|
766 |
|
|
837 |
## Now prepare table |
|
838 |
s.table_p_val_term <- l_table[[1]]$s.table_p_val_term[-c(10:26),] |
|
839 |
s.table_p_val_term <- s.table_p_val_term[order(s.table_p_val_term$mod_name),] |
|
840 |
#summary_s.table_term2 <- summary_s.table_term2[-c(8,10),] |
|
841 |
table <- l_table[[1]]$table |
|
842 |
|
|
843 |
table6a <- merge(s.table_p_val_term,table,by="mod_name") |
|
844 |
table6a <- table6a[,-match(c("term_name.y"),names(table6a))] |
|
845 |
#model_col<-c("Baseline2","Northness","Eastness","LST","DISTOC","Forest","CANHEIGHT","LST*Forest") # removed ,"LST*CANHEIGHT") |
|
846 |
#names_table_col<-c("DiffMAE","DiffRMSE","DiffME","Diffr","Model") |
|
847 |
|
|
848 |
s.table_p_val_term <- l_table[[2]]$s.table_p_val_term[-c(11:19),] |
|
849 |
s.table_p_val_term <- s.table_p_val_term[order(s.table_p_val_term$mod_name),] |
|
850 |
#summary_s.table_term2 <- summary_s.table_term2[-c(8,10),] |
|
851 |
tableb <- l_table[[2]]$table |
|
852 |
|
|
853 |
table6b <- merge(s.table_p_val_term,tableb,by="mod_name") |
|
854 |
table6b <- table6b[,-match(c("term_name.y"),names(table6b))] |
|
855 |
#model_col<-c("Baseline2","Northness","Eastness","LST","DISTOC","Forest","CANHEIGHT","LST*Forest") # removed ,"LST*CANHEIGHT") |
|
856 |
#names_table_col<-c("DiffMAE","DiffRMSE","DiffME","Diffr","Model") |
|
857 |
|
|
858 |
#table6b[order(table6b$mod_name),] |
|
859 |
|
|
767 | 860 |
#Now write out table... |
768 | 861 |
|
769 |
table<-rbind(table_data1,table_data3) |
|
770 |
table<-rbind(table,table_data4) |
|
771 |
table<- round(table,digit=3) #roundto three digits teh differences |
|
772 |
|
|
773 |
model_col<-c("GAM","Kriging","GWR") |
|
774 |
names_table_col<-c("MAE","RMSE","ME","R","Model") |
|
775 |
|
|
776 |
table$Model <-model_col |
|
777 |
names(table)<- names_table_col |
|
778 |
table |
|
779 |
|
|
780 |
file_name<-paste("table4_avg_paper","_",out_prefix,".txt",sep="") |
|
781 |
write.table(table,file=file_name,sep=",") |
|
782 |
|
|
783 |
file_name<-paste("table4_sd_paper","_",out_prefix,".txt",sep="") |
|
784 |
write.table(table_sd,file=file_name,sep=",") |
|
862 |
file_name<-paste("table6a_paper","_",out_prefix,".txt",sep="") |
|
863 |
write.table(table6a,file=file_name,sep=",") |
|
864 |
|
|
865 |
file_name<-paste("table6b_paper","_",out_prefix,".txt",sep="") |
|
866 |
write.table(table6b,file=file_name,sep=",") |
|
867 |
|
|
868 |
######################## |
|
869 |
### Prepare table 7: correlation matrix between covariates |
|
870 |
|
|
871 |
names(s_raster) |
|
872 |
|
|
873 |
list_formulas<-raster_prediction_obj_2$method_mod_obj[[1]]$formulas |
|
874 |
list_formulas <- lapply(list_formulas,FUN=as.formula) |
|
875 |
covar_names_model <- unique(unlist(lapply(list_formulas,FUN=all.vars)))[-1] |
|
876 |
covar_names_model <- c(covar_names_model[-6],c("mm_01","mm_02","mm_03","mm_04","mm_05","mm_06", |
|
877 |
"mm_07","mm_08","mm_09","mm_10","mm_11","mm_12")) |
|
878 |
covar_raster <- subset(s_raster,covar_names_model) |
|
879 |
|
|
880 |
names(covar_raster) <- covar_names_model |
|
881 |
corr_layers_covar <-layerStats(covar_raster,"pearson",na.rm=TRUE) |
|
882 |
corr_mat <-round(corr_layers_covar[[1]], digit=2) |
|
883 |
|
|
884 |
file_name<-paste("table7_paper","_",out_prefix,".txt",sep="") |
|
885 |
write.table(corr_mat,file=file_name,sep=",") |
|
886 |
|
|
887 |
#met_obj <-load_obj(file.path(in_dir1,met_obj_file_1)) |
|
888 |
#stat_loc<-readOGR(dsn=in_dir1,layer=sub(".shp","",basename(met_obj$loc_stations))) |
|
889 |
#dim(stat_loc) |
|
785 | 890 |
|
786 | 891 |
###################### END OF SCRIPT ####################### |
787 | 892 |
|
Also available in: Unified diff
contribution covariates fig and tables for manuscript