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: 05/05/2014
|
11 |
|
#Version: 5
|
|
10 |
#MODIFIED ON: 08/11/2014
|
|
11 |
#Version: 6
|
12 |
12 |
#PROJECT: Environmental Layers project
|
13 |
13 |
#################################################################################################
|
14 |
14 |
|
... | ... | |
111 |
111 |
"gam_fss","kriging_fss","gwr_fss")
|
112 |
112 |
|
113 |
113 |
y_var_name <- "dailyTmax"
|
114 |
|
out_prefix<-"analyses_05062014"
|
|
114 |
out_prefix<-"analyses_08112014"
|
115 |
115 |
out_dir<-"/home/parmentier/Data/IPLANT_project/paper_multitime_scale__analyses_tables"
|
116 |
116 |
create_out_dir_param = TRUE
|
117 |
117 |
|
... | ... | |
1200 |
1200 |
|
1201 |
1201 |
|
1202 |
1202 |
|
1203 |
|
#### DATA MONTH
|
1204 |
|
|
|
1203 |
#### DATA INFORMATION ON STATIONS
|
|
1204 |
|
|
1205 |
|
|
1206 |
#Stations located in Oregon
|
|
1207 |
loc_spdf <- readOGR(dsn=dirname(met_stations_obj$loc_stations),
|
|
1208 |
sub(".shp","",basename(met_stations_obj$loc_stations)))
|
|
1209 |
dim(loc_spdf) # 1093 stations located in OR
|
|
1210 |
|
|
1211 |
### Stations with covaraites values appended
|
|
1212 |
ghcn_day_dat <- readOGR(dsn=dirname(met_stations_obj$daily_covar_ghcn_data),
|
|
1213 |
sub(".shp","",basename(met_stations_obj$daily_covar_ghcn_data)))
|
|
1214 |
|
|
1215 |
loc_2010_spdf<-subset(loc_spdf,loc_spdf$STAT_ID%in%ghcn_day_dat$station) #stations in 2010
|
|
1216 |
loc_2010_spdf <- spTransform(loc_2010_spdf, CRS=CRS(proj4string(ghcn_day_dat)))
|
|
1217 |
dim(loc_2010_spdf) #164 stations in 2010
|
|
1218 |
|
|
1219 |
loc_knn1 <- knearneigh(coordinates(loc_2010_spdf), k=1) #lag1
|
|
1220 |
class(loc_knn1) #knn object
|
|
1221 |
loc_nb1 <- knn2nb(loc_knn1) #nb object
|
|
1222 |
dsts_nb1 <-unlist(nbdists(loc_nb1,coordinates(loc_2010_spdf))) #vector with first nearest neighbour distance for each station
|
|
1223 |
|
|
1224 |
median(dsts_nb1) #median first nearest neighbour distance: 20023.03 m
|
|
1225 |
mean(dsts_nb1) #mean first nearest neighbour distance : 22480.91 meters
|
|
1226 |
min(dsts_nb1)
|
|
1227 |
max(dsts_nb1)
|
|
1228 |
sd(dsts_nb1) #
|
|
1229 |
hist(dsts_nb1)
|
|
1230 |
|
|
1231 |
p <- histogram(dsts_nb1)
|
|
1232 |
|
|
1233 |
hist(ghcnd_day_dat$elev_s)
|
|
1234 |
|
|
1235 |
mean(ghcn_day_dat)
|
|
1236 |
unique(ghcn_day_dat$date) #
|
|
1237 |
tb_n_day <- as.data.frame(table(ghcn_day_dat$date)) #find the number of observation per day
|
|
1238 |
hist(table(ghcn_day_dat$date))
|
|
1239 |
range(tb_n_day$Freq) #range 134 to 159
|
|
1240 |
median(tb_n_day$Freq) #range 134 to 159
|
|
1241 |
|
|
1242 |
|
|
1243 |
## EXAMINE MONTHLY DAta
|
|
1244 |
(met_stations_obj$monthly_covar_ghcn_data)
|
|
1245 |
ghcn_month_dat <- readOGR(dsn=dirname(met_stations_obj$monthly_covar_ghcn_data),
|
|
1246 |
sub(".shp","",basename(met_stations_obj$monthly_covar_ghcn_data)))
|
|
1247 |
|
|
1248 |
tb_n_month <- as.data.frame(table(ghcn_month_dat$month))
|
|
1249 |
hist(table(ghcn_month_dat$month))
|
|
1250 |
range(tb_n_month$Freq) #range 134 to 159
|
|
1251 |
median(tb_n_month$Freq) #range 134 to 159
|
|
1252 |
|
|
1253 |
list_table_stat <- list("vector",length=2)
|
|
1254 |
#df_in <- list("vector",length=2)
|
|
1255 |
df_in <- list(ghcn_month_dat,ghcn_day_dat)
|
|
1256 |
for(i in 1:2){
|
|
1257 |
df_stat <- as.data.frame(df_in[[i]])[,c("elev_s", "lat", "lon", "E_w", "N_w", "DISTOC", "LC1")]
|
|
1258 |
table8a <- as.data.frame(cbind(sapply(df_stat,min,na.rm=T),sapply(df_stat,max,na.rm=T),sapply(df_stat,mean,na.rm=T),sapply(df_stat,median,na.rm=T)))
|
|
1259 |
names(table8a) <- c("min", "max", "mean", "median")
|
|
1260 |
|
|
1261 |
df_stat <- as.data.frame(df_in[[i]])[,c("mm_01","mm_02","mm_03","mm_04","mm_05","mm_06","mm_07","mm_08","mm_09","mm_10","mm_11","mm_12")]# #, "Tmax", TMax")]
|
|
1262 |
#df_stat <- as.matrix(as.data.frame(ghcn_month_dat)[,c("mm_01","mm_02","mm_03","mm_04","mm_05","mm_06","mm_07","mm_08","mm_09","mm_10","mm_11","mm_12")])# #, "Tmax", TMax")])
|
|
1263 |
#df_sat <- as.vector(df_stat)
|
|
1264 |
table8b <- cbind(sapply(df_stat,min,na.rm=T),sapply(df_stat,max,na.rm=T),sapply(df_stat,mean,na.rm=T),sapply(df_stat,median,na.rm=T))
|
|
1265 |
df_stat <- as.data.frame(table8b)
|
|
1266 |
#table8b <- cbind(sapply(df_stat,min,na.rm=T),sapply(df_stat,max,na.rm=T),sapply(df_stat,mean,na.rm=T),sapply(df_stat,median,na.rm=T))
|
|
1267 |
table8b <- as.data.frame(cbind(min(df_stat[,1]),max(df_stat[,2]),mean(df_stat[,3]),median(df_stat[,4])))
|
|
1268 |
row.names(table8b) <-"LST"
|
|
1269 |
names(table8b) <- c("min", "max", "mean", "median")
|
|
1270 |
table_stat <- rbind(table8a,table8b)
|
|
1271 |
#names(table_stat) <- c("min", "max", "mean", "median")
|
|
1272 |
table_stat$covariates <- row.names(table_stat)
|
|
1273 |
table_stat <- table_stat[c(5,1,2,3,4)]
|
|
1274 |
list_table_stat[[i]] <- table_stat
|
|
1275 |
}
|
|
1276 |
names(list_table_stat) <- c("month","day")
|
|
1277 |
#Very similar results so use only daily is sufficient
|
|
1278 |
|
|
1279 |
table8_paper <- round(list_table_stat$day[,2:5],digit=3) #roundto three digits teh differences
|
|
1280 |
table8_paper$covariates <- list_table_stat$day[,1]
|
|
1281 |
table8_paper <- table8_paper[c(5,1,2,3,4)]
|
|
1282 |
|
|
1283 |
#Now write out table 8
|
|
1284 |
|
|
1285 |
file_name<-paste("table8__paper","_",out_prefix,".txt",sep="")
|
|
1286 |
write.table(table8_paper,file=file_name,sep=",",row.names=F)
|
|
1287 |
|
|
1288 |
|
|
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 |
|
|
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)
|
|
1300 |
|
|
1301 |
data_v_test <- list_data_v[[1]]
|
|
1302 |
|
|
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")
|
|
1306 |
|
|
1307 |
t<-melt(data_v_combined,
|
|
1308 |
measure=names_var,
|
|
1309 |
id=c("id"),
|
|
1310 |
na.rm=T)
|
|
1311 |
|
|
1312 |
#hist(data_v_combined)
|
|
1313 |
names(data_v_combined)
|
|
1314 |
|
|
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
|
|
1317 |
|
|
1318 |
mae_tb<-cast(t,id~variable,mae_fun) #join to station location...
|
|
1319 |
|
|
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)))
|
|
1322 |
|
|
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)
|
|
1326 |
|
|
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")
|
|
1335 |
|
|
1336 |
|
|
1337 |
|
|
1338 |
|
1205 |
1339 |
|
1206 |
1340 |
###################### END OF SCRIPT #######################
|
1207 |
1341 |
|
revisions 1, multi timescale paper adding table with summary month accuracy comparison