Revision 1f24e304
Added by Benoit Parmentier over 11 years ago
climate/research/oregon/interpolation/contribution_of_covariates_paper_interpolation.R | ||
---|---|---|
4 | 4 |
# interpolation code. |
5 | 5 |
#Figures and data for the contribution of covariate paper are also produced. |
6 | 6 |
#AUTHOR: Benoit Parmentier # |
7 |
#DATE: 08/05/2013
|
|
7 |
#DATE: 08/13/2013
|
|
8 | 8 |
#Version: 1 |
9 | 9 |
#PROJECT: Environmental Layers project # |
10 | 10 |
################################################################################################# |
... | ... | |
49 | 49 |
|
50 | 50 |
#This extract a data.frame object from raster prediction obj and combine them in one data.frame |
51 | 51 |
extract_from_list_obj<-function(obj_list,list_name){ |
52 |
#extract object from list of list. This useful for raster_prediction_obj |
|
53 |
library(ply) |
|
54 |
|
|
52 | 55 |
list_tmp<-vector("list",length(obj_list)) |
53 | 56 |
for (i in 1:length(obj_list)){ |
54 | 57 |
tmp<-obj_list[[i]][[list_name]] #double bracket to return data.frame |
55 | 58 |
list_tmp[[i]]<-tmp |
56 | 59 |
} |
57 |
tb_list_tmp<-do.call(rbind,list_tmp) #long rownames |
|
60 |
tb_list_tmp<-do.call(rbind.fill,list_tmp) #long rownames |
|
61 |
#tb_list_tmp<-do.call(rbind,list_tmp) #long rownames |
|
62 |
|
|
58 | 63 |
return(tb_list_tmp) #this is a data.frame |
59 | 64 |
} |
60 | 65 |
|
... | ... | |
345 | 350 |
|
346 | 351 |
} |
347 | 352 |
|
353 |
plot_dst_spat_fun<-function(stat_tb,names_var,cat_val){ |
|
354 |
|
|
355 |
range_y<-range(as.vector(unlist(stat_tb[,names_var])),na.rm=T) #flatten data.frame |
|
356 |
col_t<-rainbow(length(names_var)) |
|
357 |
pch_t<- 1:length(names_var) |
|
358 |
plot(stat_tb[,names_var[1]], ylim=range_y,pch=pch_t[1],col=col_t[1],type="b", |
|
359 |
yla="MAE (in degree C)",xlab="",xaxt="n") |
|
360 |
#points((stat_tb[,names_var[k]], ylim=range_y,pch=pch_t[1],col=col_t[1]),type="p") |
|
361 |
for (k in 2:length(names_var)){ |
|
362 |
lines(stat_tb[,names_var[k]], ylim=range_y,pch=pch_t[k],col=col_t[k],type="b", |
|
363 |
xlab="",axes=F) |
|
364 |
#points((stat_tb[,names_var[k]], ylim=range_y,pch=pch_t[k],col=col_t[k]),type="p") |
|
365 |
} |
|
366 |
legend("topleft",legend=names_var, |
|
367 |
cex=1.2, pch=pch_t,col=col_t,lty=1,bty="n") |
|
368 |
axis(1,at=1:length(stat_tb[,1]),labels=stat_tb[,1]) |
|
369 |
} |
|
370 |
|
|
371 |
plot_prop_metrics <-function(list_param){ |
|
372 |
# |
|
373 |
#list_dist_obj: list of dist object |
|
374 |
#col_t: list of color for each |
|
375 |
#pch_t: symbol for line |
|
376 |
#legend_text: text for line and symbol |
|
377 |
#mod_name: selected models |
|
378 |
# |
|
379 |
## BEGIN ## |
|
380 |
|
|
381 |
list_obj<-list_param$list_prop_obj |
|
382 |
col_t <-list_param$col_t |
|
383 |
pch_t <- list_param$pch_t |
|
384 |
legend_text <- list_param$legend_text |
|
385 |
list_mod_name<-list_param$mod_name |
|
386 |
metric_name<-list_param$metric_name |
|
387 |
|
|
388 |
for (i in 1:length(list_obj)){ |
|
389 |
|
|
390 |
l<-list_obj[[i]] |
|
391 |
mod_name<-list_mod_name[i] |
|
392 |
avg_tb<-subset(l$avg_tb,pred_mod==mod_name,select=metric_name) #selecte relevant accuarcy metric |
|
393 |
n_tb<-subset(l$n_tb,pred_mod==mod_name,select=metric_name) |
|
394 |
sd_tb<-subset(l$sd_tb,pred_mod==mod_name,select=metric_name) #l$sd_abs_tb[,metric_name] |
|
395 |
|
|
396 |
xlab_text<-"holdout proportion" |
|
397 |
|
|
398 |
no <- unlist(as.data.frame(n_tb)) |
|
399 |
y <- unlist(as.data.frame(avg_tb)) |
|
400 |
|
|
401 |
x<- l$avg_tb$prop |
|
402 |
y_sd <- unlist(as.data.frame(sd_tb)) #sd_tb |
|
403 |
|
|
404 |
ciw <-y_sd |
|
405 |
#ciw2 <- qt(0.975, n) * y_sd2 / sqrt(n) |
|
406 |
|
|
407 |
#plotCI(y=y, x=x, uiw=ciw, col="red", main=paste(" MAE for ",mod_name,sep=""), barcol="blue", lwd=1, |
|
408 |
# ylab="RMSE (C)", xlab=xlab_text) |
|
409 |
|
|
410 |
ciw <- qt(0.975, no) * y_sd / sqrt(no) |
|
411 |
|
|
412 |
if(i==1){ |
|
413 |
plotCI(y=y, x=x, uiw=ciw, col=col_t[i], main=paste(" Comparison of ",metric_name," in ",mod_name,sep=""), barcol="blue", lwd=1, |
|
414 |
ylab="RMSE (C)", xlab=xlab_text) |
|
415 |
lines(y~x, col=col_t[i]) |
|
416 |
|
|
417 |
}else{ |
|
418 |
lines(y~x, col=col_t[i]) |
|
419 |
} |
|
420 |
|
|
421 |
} |
|
422 |
legend("topleft",legend=legend_text, |
|
423 |
cex=1.2, pch=pch_t,col=col_t,lty=1,bty="n") |
|
424 |
#axis(1,at=1:length(stat_tb[,1]),labels=stat_tb[,1]) |
|
425 |
|
|
426 |
} |
|
427 |
|
|
428 |
create_s_and_p_table_term_models <-function(i,list_myModels){ |
|
429 |
#Purpose: |
|
430 |
#Function to extract smooth term table,parameter table and AIC from a list of models. |
|
431 |
#Originally created to processed GAM models run over a full year. |
|
432 |
#Inputs: |
|
433 |
#1) list_myModels: list of fitted GAM models |
|
434 |
#2) i: index of list to run with lapply or mcapply |
|
435 |
#Outputs: list of |
|
436 |
#1)s.table.term |
|
437 |
#2)p.table.term |
|
438 |
#3)AIC list |
|
439 |
#Authors: Benoit Parmentier |
|
440 |
#date: 08/142013 |
|
441 |
|
|
442 |
### Functions used in the scritp: |
|
443 |
|
|
444 |
#Remove models that were not fitted from the list |
|
445 |
#All modesl that are "try-error" are removed |
|
446 |
remove_errors_list<-function(list_items){ |
|
447 |
#This function removes "error" items in a list |
|
448 |
list_tmp<-list_items |
|
449 |
for(i in 1:length(list_items)){ |
|
450 |
|
|
451 |
if(inherits(list_items[[i]],"try-error")){ |
|
452 |
list_tmp[[i]]<-0 |
|
453 |
}else{ |
|
454 |
list_tmp[[i]]<-1 |
|
455 |
} |
|
456 |
} |
|
457 |
cnames<-names(list_tmp[list_tmp>0]) |
|
458 |
x<-list_items[match(cnames,names(list_items))] |
|
459 |
return(x) |
|
460 |
} |
|
461 |
|
|
462 |
#turn term from list into data.frame |
|
463 |
name_col<-function(i,x){ |
|
464 |
x_mat<-x[[i]] |
|
465 |
x_df<-as.data.frame(x_mat) |
|
466 |
x_df$mod_name<-rep(names(x)[i],nrow(x_df)) |
|
467 |
x_df$term_name <-row.names(x_df) |
|
468 |
return(x_df) |
|
469 |
} |
|
470 |
|
|
471 |
##BEGIN ### |
|
472 |
|
|
473 |
myModels <- list_myModels[[i]] |
|
474 |
myModels<-remove_errors_list(myModels) |
|
475 |
#could add AIC, GCV to the table as well as ME, RMSE...+dates... |
|
476 |
|
|
477 |
summary_list <- lapply(myModels, summary) |
|
478 |
s.table_list <- lapply(summary_list, `[[`, 's.table') |
|
479 |
p.table_list <- lapply(summary_list, `[[`, 'p.table') |
|
480 |
AIC_list <- lapply(myModels, AIC) |
|
481 |
|
|
482 |
#now put in one table |
|
483 |
|
|
484 |
s.table_list2<-lapply(1:length(myModels),name_col,s.table_list) |
|
485 |
p.table_list2<-lapply(1:length(myModels),name_col,p.table_list) |
|
486 |
s.table_term <-do.call(rbind,s.table_list2) |
|
487 |
p.table_term <-do.call(rbind,p.table_list2) |
|
488 |
|
|
489 |
#Now get AIC |
|
490 |
AIC_list2<-lapply(1:length(myModels),name_col,AIC_list) |
|
491 |
AIC_models <- do.call(rbind,AIC_list2) |
|
492 |
names(AIC_models)[1]<-"AIC" |
|
493 |
|
|
494 |
#Set up return object |
|
495 |
|
|
496 |
s_p_table_term_obj<-list(s.table_term,p.table_term,AIC_models) |
|
497 |
names(s_p_table_term_obj) <-c("s.table_term","p.table_term","AIC_models") |
|
498 |
return(s_p_table_term_obj) |
|
499 |
|
|
500 |
} |
|
501 |
|
|
502 |
convert_spdf_to_df_from_list <-function(obj_list,list_name){ |
|
503 |
#extract object from list of list. This useful for raster_prediction_obj |
|
504 |
#output: data.frame formed by rbinding sp data.frame in the list |
|
505 |
library(plyr) |
|
506 |
|
|
507 |
list_tmp<-vector("list",length(obj_list)) |
|
508 |
for (i in 1:length(obj_list)){ |
|
509 |
#tmp<-obj_list[[i]][[list_name]] #double bracket to return data.frame |
|
510 |
list_tmp[[i]]<-as.data.frame(obj_list[[i]]) |
|
511 |
} |
|
512 |
tb_list_tmp<-do.call(rbind.fill,list_tmp) #long rownames |
|
513 |
#tb_list_tmp<-do.call(rbind,list_tmp) #long rownames |
|
514 |
|
|
515 |
return(tb_list_tmp) #this is a data.frame |
|
516 |
} |
|
517 |
|
|
348 | 518 |
############################## |
349 | 519 |
#### Parameters and constants |
350 | 520 |
|
... | ... | |
374 | 544 |
|
375 | 545 |
method_interpolation <- "gam_daily" |
376 | 546 |
covar_obj_file1 <- "covar_obj__365d_gam_day_lst_comb3_07092013.RData" |
547 |
met_obj_file_1 <- "met_stations_outfiles_obj_gam_daily__365d_gam_day_lst_comb3_07092013.RData" |
|
377 | 548 |
|
378 | 549 |
#raster_prediciton object for baseline 1 () s(lat,lon) + s(elev)) and baseline 2 (slat,lon)) |
379 | 550 |
raster_obj_file_1 <- "raster_prediction_obj_gam_daily_dailyTmax_365d_gam_day_lst_comb3_07092013.RData" |
... | ... | |
501 | 672 |
#figure 3:Figure 3. MAE/RMSE and distance to closest fitting station. |
502 | 673 |
#Figure 4. RMSE and MAE, mulitisampling and hold out for FSS and GAM. |
503 | 674 |
#Figure 5. Overtraining tendency |
675 |
#Figure 6: Spatial pattern of prediction for one day |
|
504 | 676 |
|
505 | 677 |
### Figure 1 |
506 | 678 |
|
... | ... | |
630 | 802 |
return(tb_diff) |
631 | 803 |
} |
632 | 804 |
|
633 |
|
|
634 | 805 |
#debug(diff_df) |
635 | 806 |
diff_tb1 <-diff_df(tb1_s[tb1_s$pred_mod=="mod1",],tb1[tb1$pred_mod=="mod1",],c("mae","rmse")) #select differences for mod1 |
636 | 807 |
diff_tb3 <-diff_df(tb3_s[tb3_s$pred_mod=="mod1",],tb3[tb3$pred_mod=="mod1",],c("mae","rmse")) |
... | ... | |
655 | 826 |
x2<-tb1[tb1$pred_mod=="mod1",c("mae","date")] |
656 | 827 |
arrange(x2,desc(mae)) |
657 | 828 |
|
658 |
|
|
659 |
pmax(x2$mae,x2$date) |
|
660 |
|
|
661 | 829 |
kriging=tb3[tb3$pred_mod=="mod1",c("mae")], |
662 | 830 |
gwr=tb4[tb4$pred_mod=="mod1",c("mae")]) |
663 | 831 |
|
... | ... | |
682 | 850 |
median(x3[x3$month=="03",c("mae")],na.rm=T) |
683 | 851 |
mean(x3[x3$month=="03",c("mae")],na.rm=T) |
684 | 852 |
|
685 |
plot_dst_spat_fun<-function(stat_tb,names_var,cat_val){ |
|
686 |
|
|
687 |
range_y<-range(as.vector(unlist(stat_tb[,names_var])),na.rm=T) #flatten data.frame |
|
688 |
col_t<-rainbow(length(names_var)) |
|
689 |
pch_t<- 1:length(names_var) |
|
690 |
plot(stat_tb[,names_var[1]], ylim=range_y,pch=pch_t[1],col=col_t[1],type="b", |
|
691 |
yla="MAE (in degree C)",xlab="",xaxt="n") |
|
692 |
#points((stat_tb[,names_var[k]], ylim=range_y,pch=pch_t[1],col=col_t[1]),type="p") |
|
693 |
for (k in 2:length(names_var)){ |
|
694 |
lines(stat_tb[,names_var[k]], ylim=range_y,pch=pch_t[k],col=col_t[k],type="b", |
|
695 |
xlab="",axes=F) |
|
696 |
#points((stat_tb[,names_var[k]], ylim=range_y,pch=pch_t[k],col=col_t[k]),type="p") |
|
697 |
} |
|
698 |
legend("topleft",legend=names_var, |
|
699 |
cex=1.2, pch=pch_t,col=col_t,lty=1,bty="n") |
|
700 |
axis(1,at=1:length(stat_tb[,1]),labels=stat_tb[,1]) |
|
701 |
} |
|
702 | 853 |
|
703 |
plot_prop_metrics <-function(list_param){ |
|
704 |
# |
|
705 |
#list_dist_obj: list of dist object |
|
706 |
#col_t: list of color for each |
|
707 |
#pch_t: symbol for line |
|
708 |
#legend_text: text for line and symbol |
|
709 |
#mod_name: selected models |
|
710 |
# |
|
711 |
## BEGIN ## |
|
712 |
|
|
713 |
list_obj<-list_param$list_prop_obj |
|
714 |
col_t <-list_param$col_t |
|
715 |
pch_t <- list_param$pch_t |
|
716 |
legend_text <- list_param$legend_text |
|
717 |
list_mod_name<-list_param$mod_name |
|
718 |
metric_name<-list_param$metric_name |
|
719 |
|
|
720 |
for (i in 1:length(list_obj)){ |
|
721 |
|
|
722 |
l<-list_obj[[i]] |
|
723 |
mod_name<-list_mod_name[i] |
|
724 |
avg_tb<-subset(l$avg_tb,pred_mod==mod_name,select=metric_name) #selecte relevant accuarcy metric |
|
725 |
n_tb<-subset(l$n_tb,pred_mod==mod_name,select=metric_name) |
|
726 |
sd_tb<-subset(l$sd_tb,pred_mod==mod_name,select=metric_name) #l$sd_abs_tb[,metric_name] |
|
727 |
|
|
728 |
xlab_text<-"holdout proportion" |
|
729 |
|
|
730 |
no <- unlist(as.data.frame(n_tb)) |
|
731 |
y <- unlist(as.data.frame(avg_tb)) |
|
732 |
|
|
733 |
x<- l$avg_tb$prop |
|
734 |
y_sd <- unlist(as.data.frame(sd_tb)) #sd_tb |
|
735 |
|
|
736 |
ciw <-y_sd |
|
737 |
#ciw2 <- qt(0.975, n) * y_sd2 / sqrt(n) |
|
738 |
|
|
739 |
#plotCI(y=y, x=x, uiw=ciw, col="red", main=paste(" MAE for ",mod_name,sep=""), barcol="blue", lwd=1, |
|
740 |
# ylab="RMSE (C)", xlab=xlab_text) |
|
741 |
|
|
742 |
ciw <- qt(0.975, no) * y_sd / sqrt(no) |
|
743 |
|
|
744 |
if(i==1){ |
|
745 |
plotCI(y=y, x=x, uiw=ciw, col=col_t[i], main=paste(" Comparison of ",metric_name," in ",mod_name,sep=""), barcol="blue", lwd=1, |
|
746 |
ylab="RMSE (C)", xlab=xlab_text) |
|
747 |
lines(y~x, col=col_t[i]) |
|
748 |
|
|
749 |
}else{ |
|
750 |
lines(y~x, col=col_t[i]) |
|
751 |
} |
|
752 |
|
|
753 |
} |
|
754 |
legend("topleft",legend=legend_text, |
|
755 |
cex=1.2, pch=pch_t,col=col_t,lty=1,bty="n") |
|
756 |
#axis(1,at=1:length(stat_tb[,1]),labels=stat_tb[,1]) |
|
757 |
|
|
854 |
####### FIGURE 6 ###### |
|
855 |
|
|
856 |
y_var_name <-"dailyTmax" |
|
857 |
index<-244 #index corresponding to January 1 |
|
858 |
|
|
859 |
lf1 <- raster_prediction_obj_1$method_mod_obj[[index]][[y_var_name]] |
|
860 |
lf3 <- raster_prediction_obj_3$method_mod_obj[[index]][[y_var_name]] |
|
861 |
lf4 <- raster_prediction_obj_4$method_mod_obj[[index]][[y_var_name]] |
|
862 |
|
|
863 |
date_selected <- "20109101" |
|
864 |
methods_names <-c("gam","kriging","gwr") |
|
865 |
names_layers<-methods_names |
|
866 |
lf <-list(lf1$mod1,lf3$mod1,lf4$mod1) |
|
867 |
#lf <-lf[[1]] |
|
868 |
|
|
869 |
pred_temp_s <-stack(lf) |
|
870 |
#predictions<-mask(predictions,mask_rast) |
|
871 |
names(pred_temp_s)<-names_layers |
|
872 |
s.range <- c(min(minValue(pred_temp_s)), max(maxValue(pred_temp_s))) |
|
873 |
#s.range <- s.range+c(5,-5) |
|
874 |
col.breaks <- pretty(s.range, n=200) |
|
875 |
lab.breaks <- pretty(s.range, n=100) |
|
876 |
temp.colors <- colorRampPalette(c('blue', 'white', 'red')) |
|
877 |
max_val<-s.range[2] |
|
878 |
min_val <-s.range[1] |
|
879 |
#max_val<- -10 |
|
880 |
min_val <- 0 |
|
881 |
layout_m<-c(1,3) #one row two columns |
|
882 |
|
|
883 |
png(paste("spatial_pattern_tmax_prediction_levelplot_",date_selected,out_prefix,".png", sep=""), |
|
884 |
height=480*layout_m[1],width=480*layout_m[2]) |
|
885 |
|
|
886 |
levelplot(pred_temp_s,main="Interpolated Surfaces Method Comparison", ylab=NULL,xlab=NULL, |
|
887 |
par.settings = list(axis.text = list(font = 2, cex = 1.3),layout=layout_m, |
|
888 |
par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5), |
|
889 |
names.attr=names_layers,col.regions=temp.colors,at=seq(max_val,min_val,by=0.01)) |
|
890 |
#col.regions=temp.colors(25)) |
|
891 |
dev.off() |
|
892 |
|
|
893 |
## FIGURE COMPARISON OF MODELS COVARRIATES |
|
894 |
|
|
895 |
lf1 <- raster_prediction_obj_1$method_mod_obj[[index]][[y_var_name]] |
|
896 |
lf1 #contains the models for gam |
|
897 |
|
|
898 |
pred_temp_s <-stack(lf1$mod1,lf1$mod4) |
|
899 |
date_selected <- "20109101" |
|
900 |
#names_layers <-c("mod1=s(lat,long)+s(elev)","mod4=s(lat,long)+s(LST)","diff=mod1-mod4") |
|
901 |
names_layers <-c("mod1=s(lat,long)+s(elev)","mod4=s(lat,long)+s(LST)") |
|
902 |
names(pred_temp_s)<-names_layers |
|
903 |
s.range <- c(min(minValue(pred_temp_s)), max(maxValue(pred_temp_s))) |
|
904 |
#s.range <- s.range+c(5,-5) |
|
905 |
col.breaks <- pretty(s.range, n=200) |
|
906 |
lab.breaks <- pretty(s.range, n=100) |
|
907 |
temp.colors <- colorRampPalette(c('blue', 'white', 'red')) |
|
908 |
max_val<-s.range[2] |
|
909 |
min_val <-s.range[1] |
|
910 |
#max_val<- -10 |
|
911 |
min_val <- 0 |
|
912 |
layout_m<-c(1,2) #one row two columns |
|
913 |
|
|
914 |
png(paste("spatial_pattern_tmax_prediction_models_gam_levelplot_",date_selected,out_prefix,".png", sep=""), |
|
915 |
height=480*layout_m[1],width=480*layout_m[2]) |
|
916 |
|
|
917 |
levelplot(pred_temp_s,main="Interpolated Surfaces Model Comparison", ylab=NULL,xlab=NULL, |
|
918 |
par.settings = list(axis.text = list(font = 2, cex = 1.3),layout=layout_m, |
|
919 |
par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5), |
|
920 |
names.attr=names_layers,col.regions=temp.colors,at=seq(max_val,min_val,by=0.01)) |
|
921 |
#col.regions=temp.colors(25)) |
|
922 |
dev.off() |
|
923 |
|
|
924 |
diff<-raster(lf1$mod1)-raster(lf1$mod4) |
|
925 |
names_layers <- c("difference=mod1-mod4") |
|
926 |
names(diff) <- names_layers |
|
927 |
plot(diff,col=temp.colors(100),main=names_layers) |
|
928 |
#levelplot(diff,main="Interpolated Surfaces Model Comparison", ylab=NULL,xlab=NULL, |
|
929 |
# par.settings = list(axis.text = list(font = 2, cex = 1.3),layout=c(1,1), |
|
930 |
# par.main.text=list(font=2,cex=2),strip.background=list(col="white")),par.strip.text=list(font=2,cex=1.5), |
|
931 |
# names.attr=names_layers,col.regions=temp.colors) |
|
932 |
|
|
933 |
######## NOW GET A ACCUURAY BY STATIONS |
|
934 |
|
|
935 |
list_data_v<-extract_list_from_list_obj(raster_prediction_obj_1$validation_mod_obj,"data_v") |
|
936 |
data_v_test <- list_data_v[[1]] |
|
937 |
|
|
938 |
#Convert sp data.frame and combined them in one unique df, see function define earlier |
|
939 |
data_v_combined <-convert_spdf_to_df_from_list(list_data_v) #long rownames |
|
940 |
names_var<-c("res_mod1","res_mod2","res_mod3","res_mod4","res_mod5","res_mod6","res_mod7","res_mod8") |
|
941 |
t<-melt(data_v_combined, |
|
942 |
measure=names_var, |
|
943 |
id=c("id"), |
|
944 |
na.rm=T) |
|
945 |
|
|
946 |
mae_fun<-function(x){mean(abs(x))} #Mean Absolute Error give a residuals vector |
|
947 |
sd_abs_fun<-function(x){sd(abs(x))} #sd Absolute Error give a residuals vector |
|
948 |
|
|
949 |
mae_tb<-cast(t,id~variable,mae_fun) #join to station location... |
|
950 |
|
|
951 |
sd_abs_tb<-cast(t,dst_cat1~variable,sd_abs_fun) |
|
952 |
|
|
953 |
#avg_tb<-cast(t,dst_cat1~variable,mean) |
|
954 |
#sd_tb<-cast(t,dst_cat1~variable,sd) |
|
955 |
#n_tb<-cast(t,dst_cat1~variable,length) |
|
956 |
|
|
957 |
met_obj <-load_obj(file.path(in_dir1,met_obj_file_1)) |
|
958 |
stat_loc<-readOGR(dsn=dirname(met_obj$loc_stations),layer=sub(".shp","",basename(met_obj$loc_stations))) |
|
959 |
|
|
960 |
data_v_mae <-merge(mae_tb,stat_loc,by.x=c("id"),by.y=c("STAT_ID")) |
|
961 |
hist(data_v_mae$res_mod1) |
|
962 |
mean(data_v_mae$res_mod1) |
|
963 |
|
|
964 |
coords<- data_v_mae[c('longitude','latitude')] #Define coordinates in a data frame |
|
965 |
CRS_interp<-proj4string(data_v_test) |
|
966 |
coordinates(data_v_mae)<-coords #Assign coordinates to the data frame |
|
967 |
proj4string(data_v_mae)<- proj4string(stat_loc) #Assign coordinates reference system in PROJ4 format |
|
968 |
data_v_mae<-spTransform(data_v_mae,CRS(CRS_interp)) #Project from WGS84 to new coord. system |
|
969 |
|
|
970 |
p<-bubble(data_v_mae,"res_mod1",maxsize=4,col=c("red"),fill=FALSE) |
|
971 |
#p<-bubble(data_v_mae,"res_mod1",maxsize=4,col=c("red"),fill=FALSE,key.entries=c(1,1.5,2,2.5,3,3.5,4,4.5)) |
|
972 |
|
|
973 |
p |
|
974 |
|
|
975 |
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 |
|
976 |
reg_outline <- readOGR(dsn=dirname(infile_reg_outline),layer=sub(".shp","",basename(infile_reg_outline))) |
|
977 |
|
|
978 |
p + layer(sp.polygons(reg_outline,lwd=0.9,col="darkgray")) |
|
979 |
|
|
980 |
p4<-bubble(data_v_mae,"res_mod4",maxsize=4,col=c("red"),fill=FALSE) |
|
981 |
p4 + layer(sp.polygons(reg_outline,lwd=0.9,col="darkgray")) |
|
982 |
|
|
983 |
col_t <- colorRampPalette(c('blue', 'white', 'red')) |
|
984 |
|
|
985 |
p_elev <-levelplot(subset(s_raster,"elev_s"),margin=FALSE) |
|
986 |
p4 <-bubble(data_v_mae[data_v_mae$res_mod4>2.134,],"res_mod4",maxsize=4,col=c("blue"),fill=FALSE) |
|
987 |
p_elev + p4 + layer(sp.polygons(reg_outline,lwd=0.9,col="green")) |
|
988 |
title("mod4") |
|
989 |
|
|
990 |
p_elev <-levelplot(subset(s_raster,"elev_s")) |
|
991 |
p1 <-bubble(data_v_mae[data_v_mae$res_mod1>2.109,],"res_mod1",maxsize=4,col=c("blue"),fill=FALSE) |
|
992 |
p_elev + p1 + layer(sp.polygons(reg_outline,lwd=0.9,col="green")) |
|
993 |
#bubble(data_v_mae,"res_mod1") |
|
994 |
#p<-spplot(data_v_mae,"res_mod1",maxsize=4,col=c("red")) |
|
995 |
#p |
|
996 |
#stations that are outliers in one but not the other... |
|
997 |
id_setdiff<-setdiff(data_v_mae[data_v_mae$res_mod1>2.109,]$id,data_v_mae[data_v_mae$res_mod4>2.134,]$id) |
|
998 |
|
|
999 |
data_id_setdiff <- data_v_mae[data_v_mae$id %in% id_setdiff,] |
|
1000 |
|
|
1001 |
p_elev +layer(sp.polygons(reg_outline,lwd=0.9,col="green")) + layer(sp.points(data_id_setdiff,pch=4,cex=2,col="pink")) |
|
1002 |
|
|
1003 |
#### ls() |
|
1004 |
|
|
1005 |
#Now get p values and other things... |
|
1006 |
|
|
1007 |
###baseline 2: s(lat,lon) + s(elev) |
|
1008 |
|
|
1009 |
tb1_s |
|
1010 |
names_var <- c("mae","rmse","me","r") |
|
1011 |
#id_var <- |
|
1012 |
t<-melt(tb1_s, |
|
1013 |
measure=names_var, |
|
1014 |
id=c("pred_mod"), |
|
1015 |
na.rm=T) |
|
1016 |
|
|
1017 |
summary_metrics_s1$avg <-cast(t,pred_mod~variable,mean) |
|
1018 |
#sd_abs_tb<-cast(t,dst_cat1~variable,sd_abs_fun) |
|
1019 |
|
|
1020 |
#summary_metrics_s1<-raster_prediction_obj_1$summary_metrics_s |
|
1021 |
#summary_metrics_s2<-raster_prediction_obj_2$summary_metrics_v |
|
1022 |
|
|
1023 |
table_data1 <-summary_metrics_s1$avg[,c("mae","rmse","me","r")] |
|
1024 |
#table_data2 <-summary_metrics_v2$avg[,c("mae","rmse","me","r")] |
|
1025 |
|
|
1026 |
model_col<-c("Baseline2","Northness","Eastness","LST","DISTOC","Forest","CANHEIGHT","LST*Forest") # removed ,"LST*CANHEIGHT") |
|
1027 |
names_table_col<-c("DiffMAE","DiffRMSE","DiffME","Diffr","Model") |
|
1028 |
|
|
1029 |
df1<- as.data.frame(sapply(table_data1,FUN=function(x) x-x[1])) |
|
1030 |
df1<- round(df1,digit=3) #roundto three digits teh differences |
|
1031 |
df1$Model <-model_col |
|
1032 |
names(df1)<- names_table_col |
|
1033 |
df1 |
|
1034 |
|
|
1035 |
list_myModels <- extract_list_from_list_obj(raster_prediction_obj_1$method_mod_obj,"mod") |
|
1036 |
|
|
1037 |
#for (i in 1:length(list_myModels)){ |
|
1038 |
# i<-1 |
|
1039 |
|
|
1040 |
|
|
1041 |
list_models_info <-lapply(1:length(list_myModels),FUN=create_s_and_p_table_term_models,list_myModels) |
|
1042 |
#raster_prediction_obj_1$method_mod_obj[[i]]$sampling_dat$date |
|
1043 |
dates<-(extract_from_list_obj(raster_prediction_obj_1$method_mod_obj,"sampling_dat"))$date #get vector of dates |
|
1044 |
names(list_models_info)<-dates |
|
1045 |
|
|
1046 |
#Add dates to the data.frame?? |
|
1047 |
|
|
1048 |
s.table_term_tb <-extract_from_list_obj(list_models_info,"s.table_term") |
|
1049 |
s.table_term_tb_t <-extract_list_from_list_obj(list_models_info,"s.table_term") |
|
1050 |
|
|
1051 |
max_val<-round_any(range_val[2],10, f=ceiling) #round max value to nearest 10 (from plyr package) |
|
1052 |
min_val<-0 |
|
1053 |
limit_val<-seq(min_val,max_val, by=10) |
|
1054 |
}else{ |
|
1055 |
limit_val<-dist_classes |
|
758 | 1056 |
} |
1057 |
threshold_val<-c(0.01,0.05,0.1) |
|
1058 |
s.table_term_tb$p_val_rec1 <- s.table_term_tb[["p-value"]] < threshold_val[1] |
|
1059 |
s.table_term_tb$p_val_rec2 <- s.table_term_tb[["p-value"]] < threshold_val[2] |
|
1060 |
s.table_term_tb$p_val_rec3 <- s.table_term_tb[["p-value"]] < threshold_val[3] |
|
1061 |
|
|
1062 |
#dstspat_dat$dst_cat1 <- cut(dstspat_dat$dst,include.lowest=TRUE,breaks=limit_val) |
|
1063 |
|
|
1064 |
#test<-do.call(rbind,s.table_term_tb_t) |
|
1065 |
#cut |
|
1066 |
s.table_term_tb |
|
1067 |
names_var <- c("p-value") |
|
1068 |
#id_var <- |
|
1069 |
t<-melt(s.table_term_tb, |
|
1070 |
measure=names_var, |
|
1071 |
id=c("mod_name","term_name"), |
|
1072 |
na.rm=T) |
|
1073 |
|
|
1074 |
summary_s.table_term <- cast(t,term_name+mod_name~variable,median) |
|
1075 |
summary_s.table_term |
|
1076 |
|
|
1077 |
names_var <- c("p_val_rec1","p_val_rec2","p_val_rec3") |
|
1078 |
t2<-melt(s.table_term_tb, |
|
1079 |
measure=names_var, |
|
1080 |
id=c("mod_name","term_name"), |
|
1081 |
na.rm=T) |
|
1082 |
|
|
1083 |
summary_s.table_term2 <- cast(t2,term_name+mod_name~variable,sum) |
|
1084 |
summary_s.table_term2 |
|
1085 |
|
|
1086 |
#Now combine tables and drop duplicate columns the combined table can be modified for the paper... |
|
1087 |
s.table_summary_tb <- cbind(summary_s.table_term,summary_s.table_term2[,-c("term_name","mod_name")]) |
|
1088 |
|
|
759 | 1089 |
|
760 | 1090 |
|
761 | 1091 |
################### END OF SCRIPT ################### |
Also available in: Unified diff
contribution of covariates paper script major modification analyses and figures