Revision 9ed7cd0e
Added by Benoit Parmentier about 10 years ago
climate/research/oregon/interpolation/multi_timescale_paper_interpolation.R | ||
---|---|---|
7 | 7 |
#Analyses, figures, tables and data for the paper are also produced in the script. |
8 | 8 |
#AUTHOR: Benoit Parmentier |
9 | 9 |
#CREATED ON: 10/31/2013 |
10 |
#MODIFIED ON: 08/11/2014
|
|
10 |
#MODIFIED ON: 08/12/2014
|
|
11 | 11 |
#Version: 6 |
12 | 12 |
#PROJECT: Environmental Layers project |
13 | 13 |
################################################################################################# |
... | ... | |
41 | 41 |
|
42 | 42 |
#### FUNCTION USED IN SCRIPT |
43 | 43 |
|
44 |
function_analyses_paper1 <-"contribution_of_covariates_paper_interpolation_functions_10222013.R" #first interp paper
|
|
45 |
function_analyses_paper2 <-"multi_timescales_paper_interpolation_functions_05052014.R"
|
|
44 |
function_analyses_paper1 <-"contribution_of_covariates_paper_interpolation_functions_07182014.R" #first interp paper
|
|
45 |
function_analyses_paper2 <-"multi_timescales_paper_interpolation_functions_08122014.R"
|
|
46 | 46 |
|
47 | 47 |
############################## |
48 | 48 |
#### Parameters and constants |
... | ... | |
1199 | 1199 |
trans_data3 <- plot_transect_m2(list_transect3,rast_pred3,title_plot2,disp=FALSE,m_layers_sc) |
1200 | 1200 |
|
1201 | 1201 |
|
1202 |
|
|
1203 |
#### DATA INFORMATION ON STATIONS |
|
1202 |
### REVISIONS 1 PAPER ##### |
|
1204 | 1203 |
|
1204 |
## DATA INFORMATION ON STATIONS |
|
1205 | 1205 |
|
1206 | 1206 |
#Stations located in Oregon |
1207 | 1207 |
loc_spdf <- readOGR(dsn=dirname(met_stations_obj$loc_stations), |
... | ... | |
1285 | 1285 |
file_name<-paste("table8__paper","_",out_prefix,".txt",sep="") |
1286 | 1286 |
write.table(table8_paper,file=file_name,sep=",",row.names=F) |
1287 | 1287 |
|
1288 |
|
|
1288 |
######## RESIDUALS ANALYSES ###### |
|
1289 | 1289 |
############# |
1290 |
list_data_s <-extract_list_from_list_obj(raster_prediction_obj_2$validation_mod_obj,"data_s") |
|
1291 |
list_data_v <-extract_list_from_list_obj(raster_prediction_obj_2$validation_mod_obj,"data_v") |
|
1292 |
names_mod <- names(raster_prediction_obj_2$method_mod_obj[[1]][[y_var_name]]) #names of models to plot |
|
1293 | 1290 |
|
1294 |
#number of observations per day |
|
1295 |
year_nbv <- sapply(list_data_v,FUN=length) |
|
1296 |
year_nbs <- sapply(list_data_s,FUN=length) |
|
1297 |
nb_df <- data.frame(nv=year_nbv,ns=year_nbs) |
|
1298 |
nb_df$n_tot <- year_nbv + year_nbs |
|
1299 |
range(nb_df$n_tot) |
|
1291 |
#names(list_raster_obj_files)<- c("gam_daily","kriging_daily","gwr_daily_a","gwr_daily_b", |
|
1292 |
# "gam_CAI","kriging_CAI","gwr_CAI", |
|
1293 |
# "gam_fss","kriging_fss","gwr_fss") |
|
1300 | 1294 |
|
1301 |
data_v_test <- list_data_v[[1]] |
|
1295 |
list_data_v <-lapply(list_raster_obj_files[5:7],FUN=function(x){x<-load_obj(x);extract_list_from_list_obj(x$validation_mod_obj,"data_v")}) |
|
1296 |
#list_data_s <-lapply(list_raster_obj_files[5:7],FUN=function(x){x<-load_obj(x);extract_list_from_list_obj(x$validation_mod_obj,"data_s")}) |
|
1302 | 1297 |
|
1303 |
#Convert sp data.frame and combined them in one unique df, see function define earlier |
|
1304 |
data_v_combined <-convert_spdf_to_df_from_list(list_data_v) #long rownames |
|
1305 |
names_var<-c("res_mod1","res_mod2","res_mod3","res_mod4","res_mod5","res_mod6","res_mod7","res_mod8","res_mod9","res_mod10") |
|
1298 |
met_obj <-load_obj(file.path(in_dir1,met_obj_file_1)) |
|
1299 |
stat_loc<-readOGR(dsn=in_dir1,layer=sub(".shp","",basename(met_obj$loc_stations))) |
|
1300 |
|
|
1301 |
elev <- subset(s_raster,"elev_s") #background var |
|
1302 |
var_background <- elev #raster image used as background image |
|
1303 |
names_var <- c("mod1","mod2","mod3","mod4","mod5","mod6","mod7") |
|
1304 |
|
|
1305 |
interp_method <- "gam_CAI" |
|
1306 |
list_data_v <- list_data_v_all$gam_CAI |
|
1307 |
data_mae_gam_CAI_obj <- plot_MAE_per_station_fun(list_data_v,names_var,interp_method,var_background,stat_loc,out_suffix) |
|
1306 | 1308 |
|
1307 |
t<-melt(data_v_combined, |
|
1308 |
measure=names_var, |
|
1309 |
interp_method <- "kriging_CAI" |
|
1310 |
list_data_v <- list_data_v_all$kriging_CAI |
|
1311 |
data_mae_kriging_CAI_obj <- plot_MAE_per_station_fun(list_data_v,names_var,interp_method,var_background,stat_loc,out_suffix) |
|
1312 |
|
|
1313 |
interp_method <- "gwr_CAI" |
|
1314 |
list_data_v <- list_data_v_all$gwr_CAI |
|
1315 |
data_mae_gwr_CAI_obj <- plot_MAE_per_station_fun(list_data_v,names_var,interp_method,var_background,stat_loc,out_suffix) |
|
1316 |
|
|
1317 |
plot_MAE_per_station_fun <- function(list_data_v,names_var,interp_method,var_background,stat_loc,out_suffix){ |
|
1318 |
#Function to create a series of residuals MAE plots... |
|
1319 |
|
|
1320 |
mae_fun<-function(x){mean(abs(x))} #Mean Absolute Error give a residuals vector |
|
1321 |
sd_abs_fun<-function(x){sd(abs(x))} #sd Absolute Error give a residuals vector |
|
1322 |
|
|
1323 |
### Start script ### |
|
1324 |
|
|
1325 |
data_v_test <- list_data_v[[1]] |
|
1326 |
#Convert sp data.frame and combined them in one unique df, see function define earlier |
|
1327 |
data_v_combined <-convert_spdf_to_df_from_list(list_data_v) #long rownames |
|
1328 |
|
|
1329 |
#names_var_all<-c("res_mod1","res_mod2","res_mod3","res_mod4","res_mod5","res_mod6","res_mod7")#,"res_mod8","res_mod9","res_mod10") |
|
1330 |
names_var_all <- res_model_name <- paste("res",names_var,sep="_") |
|
1331 |
|
|
1332 |
t<-melt(data_v_combined, |
|
1333 |
measure=names_var_all, |
|
1309 | 1334 |
id=c("id"), |
1310 | 1335 |
na.rm=T) |
1311 | 1336 |
|
1312 |
#hist(data_v_combined)
|
|
1313 |
names(data_v_combined)
|
|
1337 |
names(data_v_combined)
|
|
1338 |
mae_tb<-cast(t,id~variable,mae_fun) #join to station location...
|
|
1314 | 1339 |
|
1315 |
mae_fun<-function(x){mean(abs(x))} #Mean Absolute Error give a residuals vector |
|
1316 |
sd_abs_fun<-function(x){sd(abs(x))} #sd Absolute Error give a residuals vector |
|
1340 |
data_v_mae <-merge(mae_tb,stat_loc,by.x=c("id"),by.y=c("STAT_ID")) |
|
1317 | 1341 |
|
1318 |
mae_tb<-cast(t,id~variable,mae_fun) #join to station location... |
|
1342 |
coords<- data_v_mae[c('longitude','latitude')] #Define coordinates in a data frame |
|
1343 |
CRS_interp<-proj4string(data_v_test) |
|
1344 |
coordinates(data_v_mae)<-coords #Assign coordinates to the data frame |
|
1345 |
proj4string(data_v_mae)<- proj4string(stat_loc) #Assign coordinates reference system in PROJ4 format |
|
1346 |
data_v_mae<-spTransform(data_v_mae,CRS(CRS_interp)) #Project from WGS84 to new coord. system |
|
1319 | 1347 |
|
1320 |
met_obj <-load_obj(file.path(in_dir1,met_obj_file_1))
|
|
1321 |
stat_loc<-readOGR(dsn=in_dir1,layer=sub(".shp","",basename(met_obj$loc_stations)))
|
|
1348 |
list_p_mae <- vector("list", length(names_var_all))
|
|
1349 |
#names_var <- c("mod1","mod2","mod3","mod7")
|
|
1322 | 1350 |
|
1323 |
data_v_mae <-merge(mae_tb,stat_loc,by.x=c("id"),by.y=c("STAT_ID"))
|
|
1324 |
hist(data_v_mae$res_mod1)
|
|
1325 |
mean(data_v_mae$res_mod1)
|
|
1351 |
for (k in 1:length(names_var)){
|
|
1352 |
model_name <- names_var[k]
|
|
1353 |
res_model_name <- paste("res",model_name,sep="_")
|
|
1326 | 1354 |
|
1327 |
coords<- data_v_mae[c('longitude','latitude')] #Define coordinates in a data frame |
|
1328 |
CRS_interp<-proj4string(data_v_test) |
|
1329 |
coordinates(data_v_mae)<-coords #Assign coordinates to the data frame |
|
1330 |
proj4string(data_v_mae)<- proj4string(stat_loc) #Assign coordinates reference system in PROJ4 format |
|
1331 |
data_v_mae<-spTransform(data_v_mae,CRS(CRS_interp)) #Project from WGS84 to new coord. system |
|
1332 |
elev <- subset(s_raster,"elev_s") |
|
1333 |
list_p_mae <- vector("list", 3) |
|
1334 |
names_var <- c("mod1","mod2","mod5") |
|
1355 |
p1 <- levelplot(var_background,scales = list(draw = FALSE), colorkey = FALSE,par.settings = GrTheme) |
|
1356 |
df_tmp=subset(data_v_mae,data_v_mae[[res_model_name]]!="NaN") |
|
1357 |
|
|
1358 |
p2 <- bubble(df_tmp,res_model_name, main=paste("Average MAE per station for ",model_name," ",interp_method, sep=""), |
|
1359 |
na.rm=TRUE) |
|
1360 |
p3 <- p2 + p1 + p2 #to force legend... |
|
1361 |
list_p_mae[[k]] <- p3 |
|
1362 |
} |
|
1363 |
|
|
1364 |
data_mae_obj <- list(list_p_mae,data_v_mae) |
|
1365 |
names(data_mae_obj) <- c("list_p_mae","data_v_mae") |
|
1366 |
return(data_mae_obj) |
|
1367 |
} |
|
1368 |
|
|
1369 |
list_p_mae <- data_mae_gam_CAI_obj$list_p_mae |
|
1370 |
interp_method <- "gam_CAI" |
|
1371 |
|
|
1372 |
layout_m<-c(1,4) # works if set to this?? ok set the resolution... |
|
1373 |
|
|
1374 |
png(paste("Figure10_paper_","average_MAE_",interp_method,out_prefix,".png", sep=""), |
|
1375 |
height=480*layout_m[1],width=480*layout_m[2]) |
|
1376 |
|
|
1377 |
grid.arrange(list_p_mae[[1]],list_p_mae[[2]],list_p_mae[[3]],list_p_mae[[7]], ncol=4) |
|
1378 |
|
|
1379 |
dev.off() |
|
1335 | 1380 |
|
1381 |
list_p_mae <- data_mae_kriging_CAI_obj$list_p_mae |
|
1336 | 1382 |
|
1383 |
layout_m<-c(1,4) # works if set to this?? ok set the resolution... |
|
1384 |
interp_method <- "kriging_CAI" |
|
1385 |
|
|
1386 |
png(paste("Figure10_paper_","average_MAE_",interp_method,out_prefix,".png", sep=""), |
|
1387 |
height=480*layout_m[1],width=480*layout_m[2]) |
|
1388 |
|
|
1389 |
grid.arrange(list_p_mae[[1]],list_p_mae[[2]],list_p_mae[[3]],list_p_mae[[7]], ncol=4) |
|
1390 |
|
|
1391 |
dev.off() |
|
1392 |
|
|
1393 |
list_p_mae <- data_mae_gwr_CAI_obj$list_p_mae |
|
1394 |
layout_m<-c(1,4) # works if set to this?? ok set the resolution... |
|
1395 |
interp_method <- "gwr_CAI" |
|
1396 |
|
|
1397 |
png(paste("Figure10_paper_","average_MAE_",interp_method,out_prefix,".png", sep=""), |
|
1398 |
height=480*layout_m[1],width=480*layout_m[2]) |
|
1399 |
|
|
1400 |
grid.arrange(list_p_mae[[1]],list_p_mae[[2]],list_p_mae[[3]],list_p_mae[[7]], ncol=4) |
|
1401 |
|
|
1402 |
dev.off() |
|
1403 |
|
|
1404 |
# boxplot compare lat*lon, lat*lon+elev , lat*lon+LST for all three methods? |
|
1405 |
|
|
1406 |
# Do MAE per stations for the best model for kriging, GWR and GAM/. |
|
1407 |
names(data_mae_gam_CAI_obj) |
|
1408 |
data_v_mae_gam_CAI <- data_mae_gam_CAI_obj$data_v_mae |
|
1409 |
data_v_mae_kriging_CAI <- data_mae_kriging_CAI_obj$data_v_mae |
|
1410 |
data_v_mae_gwr_CAI <- data_mae_gwr_CAI_obj$data_v_mae |
|
1411 |
|
|
1412 |
## Histogram plots: Show histo for mod1,mod2,mod3 and mod7 for gam, kriging and gwr |
|
1413 |
|
|
1414 |
#Combine fig?? |
|
1415 |
|
|
1416 |
#hist(data_v_mae_gam_CAI$res_mod1,breaks=c(1,2,3)) |
|
1417 |
hist(data_v_mae_gam_CAI$res_mod1) #,breaks=c(1,2,3)) |
|
1418 |
hist(data_v_mae_kriging_CAI$res_mod1) |
|
1419 |
hist(data_v_mae_gwr_CAI$res_mod1) |
|
1420 |
|
|
1421 |
hist(data_v_mae_gam_CAI$res_mod2) #,breaks=c(1,2,3)) |
|
1422 |
hist(data_v_mae_kriging_CAI$res_mod2) |
|
1423 |
hist(data_v_mae_gwr_CAI$res_mod2) |
|
1424 |
|
|
1425 |
hist(data_v_mae_gam_CAI$res_mod3) #,breaks=c(1,2,3)) |
|
1426 |
hist(data_v_mae_kriging_CAI$res_mod3) |
|
1427 |
hist(data_v_mae_gwr_CAI$res_mod3) |
|
1428 |
|
|
1429 |
hist(data_v_mae_gam_CAI$res_mod7) #,breaks=c(1,2,3)) |
|
1430 |
hist(data_v_mae_kriging_CAI$res_mod7) |
|
1431 |
hist(data_v_mae_gwr_CAI$res_mod7) |
|
1432 |
|
|
1433 |
### Figure 13: Analysing residuals and relationship to elevation for mod1, mod2 and mod4 ###### |
|
1434 |
|
|
1435 |
#raster_predicton object for baseline 1 () s(lat,lon) + s(elev)) and baseline 2 (slat,lon)) |
|
1436 |
#list_data_v <- lapply(1:365,function(i){raster_prediction_obj_2$validation_mod_obj[[i]]$data_v}) #testing with residuals |
|
1437 |
#l_formulas<-(extract_from_list_obj(raster_prediction_obj_2$method_mod_obj,"formulas")) #get vector of dates |
|
1438 |
|
|
1439 |
test <- do.call(rbind,list_data_v$gam_CAI) |
|
1440 |
data_v_ag <-test |
|
1441 |
cor(test$res_mod1,test$elev) |
|
1442 |
cor(test$res_mod2,test$elev,use="complete.obs") #decrease in corellation when using elev |
|
1443 |
cor(test$res_mod1,test$LST,,use="complete.obs") |
|
1444 |
cor(test$res_mod3,test$LST,use="complete.obs") #decrease in correlation when using LST |
|
1445 |
|
|
1446 |
brks<- c(0,500,1000,1500,2000) |
|
1447 |
lab_brks<-1:4 |
|
1448 |
elev_rcstat<-cut(data_v_ag$elev,breaks=brks,labels=lab_brks,right=F) |
|
1449 |
|
|
1450 |
#Now set up plotting device |
|
1451 |
|
|
1452 |
layout_m <- c(1,3) # works if set to this?? ok set the resolution... |
|
1453 |
png(paste("Figure_13_paper_","residuals_MAE_",out_prefix,".png", sep=""), |
|
1454 |
height=480*layout_m[1],width=480*layout_m[2]) |
|
1455 |
#height=480*6,width=480*4) |
|
1456 |
|
|
1457 |
p_bw1<-bwplot(data_v_ag$res_mod1~elev_rcstat,do.out=F,ylim=c(-15,15), |
|
1458 |
ylab=list(label="Residuals (deg C)",cex=1.5), |
|
1459 |
xlab=list(label="Elevation classes (meter)",cex=1.5), |
|
1460 |
main=list(label="Residuals vs elev for mod1=lat*lon",cex=1.8), |
|
1461 |
scales = list(x = list(at = c(1, 2, 3, 4), #provide tick location and labels |
|
1462 |
labels = c("0-500","500-1000","1000-1500","1500-2000"))), |
|
1463 |
par.settings = list(axis.text = list(font = 2, cex = 1.3), #control the font size!! |
|
1464 |
par.main.text=list(font=2,cex=2),strip.background=list(col="white")), |
|
1465 |
par.strip.text=list(font=2,cex=1.5) |
|
1466 |
) |
|
1467 |
|
|
1468 |
p_bw1<-bwplot(data_v_ag$res_mod1~data_v_ag$LST,do.out=F,ylim=c(-15,15), |
|
1469 |
ylab=list(label="Residuals (deg C)",cex=1.5), |
|
1470 |
xlab=list(label="Elevation classes (meter)",cex=1.5), |
|
1471 |
main=list(label="Residuals vs elev for mod1=lat*lon",cex=1.8), |
|
1472 |
scales = list(x = list(at = c(1, 2, 3, 4), #provide tick location and labels |
|
1473 |
labels = c("0-500","500-1000","1000-1500","1500-2000"))), |
|
1474 |
par.settings = list(axis.text = list(font = 2, cex = 1.3), #control the font size!! |
|
1475 |
par.main.text=list(font=2,cex=2),strip.background=list(col="white")), |
|
1476 |
par.strip.text=list(font=2,cex=1.5) |
|
1477 |
) |
|
1478 |
|
|
1479 |
p_bw2 <- bwplot(data_v_ag$res_mod2~elev_rcstat,do.out=F,ylim=c(-15,15), |
|
1480 |
ylab=list(label="Residuals (deg C)",cex=1.5), |
|
1481 |
xlab=list(label="Elevation classes (meter)",cex=1.5), |
|
1482 |
main=list(label="Residuals vs elev for mod5=lat*lon+LST",cex=1.8), |
|
1483 |
scales = list(x = list(at = c(1, 2, 3, 4), |
|
1484 |
labels = c("0-500","500-1000","1000-1500","1500-2000"))), |
|
1485 |
par.settings = list(axis.text = list(font = 2, cex = 1.3), #control the font size!! |
|
1486 |
par.main.text=list(font=2,cex=2),strip.background=list(col="white")), |
|
1487 |
par.strip.text=list(font=2,cex=1.5) |
|
1488 |
) |
|
1489 |
|
|
1490 |
p_bw3 <- bwplot(data_v_ag$res_mod7~elev_rcstat,do.out=F,ylim=c(-15,15), |
|
1491 |
ylab=list(label="Residuals (deg C)",cex=1.5), |
|
1492 |
xlab=list(label="Elevation classes (meter)",cex=1.5), |
|
1493 |
main=list(label="Residuals vs elev for mod2=lat*lon+elev",cex=1.8), |
|
1494 |
scales = list(x = list(at = c(1, 2, 3, 4), |
|
1495 |
labels = c("0-500","500-1000","1000-1500","1500-2000"))), |
|
1496 |
par.settings = list(axis.text = list(font = 2, cex = 1.3), #control the font size!! |
|
1497 |
par.main.text=list(font=2,cex=2),strip.background=list(col="white")), |
|
1498 |
par.strip.text=list(font=2,cex=1.5) |
|
1499 |
) |
|
1500 |
grid.arrange(p_bw1,p_bw2,p_bw3,ncol=3) |
|
1501 |
dev.off() |
|
1337 | 1502 |
|
1338 | 1503 |
|
1339 | 1504 |
|
1340 | 1505 |
###################### END OF SCRIPT ####################### |
1341 | 1506 |
|
1342 |
# #LAND COVER INFORMATION |
|
1507 |
# #LAND COVER ########################################### |
|
1508 |
|
|
1509 |
#INFORMATION |
|
1343 | 1510 |
|
1344 | 1511 |
# LC1: Evergreen/deciduous needleleaf trees |
1345 | 1512 |
# LC2: Evergreen broadleaf trees |
Also available in: Unified diff
revisions2 muli-timescale paper adding residuals analysis