-
Notifications
You must be signed in to change notification settings - Fork 0
/
61_plotting_data_frst_nofrst_inone.R
executable file
·379 lines (338 loc) · 18 KB
/
61_plotting_data_frst_nofrst_inone.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# Description:
# Author: Alice Ziegler
# Date: 2018-12-07 14:45:46
# to do:
rm(list=ls())
########################################################################################
###Presettings
########################################################################################
#####
###load packages
#####
library(CAST)
library(caret)
source("000_setup.R")
#####
###read files
#####
set_lst <- lapply(set, function(o){
set_moddir <- mod_dir_lst[grepl(paste0("_", o, "_"), mod_dir_lst)]
file <- tryCatch(
readRDS(file = paste0(outpath, set_dir, set_moddir, "/", "50_master_lst_all_mods_",o, ".rds")),
error = function(e)file <- NA)
return(file)
})
names(set_lst) <- set
set_lst <- set_lst[!is.na(set_lst)]
#####
###create mixed dataset
#####
###mix responses
mix_lst_resp <- lapply(names(set_lst$allplts$resp), function(i){
mix_df_resp<- rbind(set_lst$nofrst$resp[[i]], set_lst$frst$resp[[i]])
})
names(mix_lst_resp) <- names(set_lst$allplts$resp)
###mix meta
mix_meta_nofrst <- set_lst$nofrst$meta
mix_meta_frst <- set_lst$frst$meta
mix_meta_frst$cvindex_run <- mix_meta_frst$cvindex_run + 5 ###hard coded
col_nm <- lapply(colnames(mix_meta_frst)[grepl("outerrun",
colnames(mix_meta_frst))],
function(i){
elem1 <- strsplit(i, "_")[[1]][[1]]
elem2 <- strsplit(i, "_")[[1]][[2]]
num <- as.numeric(strsplit(i, "_")[[1]][[3]]) + 20
col_nm <- paste0(elem1, "_", elem2, "_", num)
})
colnames(mix_meta_frst)[grepl("outerrun", colnames(mix_meta_frst))] <- col_nm
mix_meta_nofrst[setdiff(colnames(mix_meta_frst),
colnames(mix_meta_nofrst))] <- NA
mix_meta_frst[setdiff(colnames(mix_meta_nofrst),
colnames(mix_meta_frst))] <- NA
mix_df_meta <- rbind(mix_meta_nofrst, mix_meta_frst)
mix_lst <- list(meta = mix_df_meta, resp = mix_lst_resp)
########################################################################################
########################################################################################
########################################################################################
###Do it (Don't change anything past this point except you know what you are doing!) ###
########################################################################################
########################################################################################
########################################################################################
########################################################################################
###validation data
########################################################################################
if(grepl("cv_index", cv)){
runs <- sort(unique(mix_lst$meta$cvindex_run))
}else{
runs <- seq(sum(grepl("outerrun", colnames(mix_lst$meta))))
}
for (k in names(mix_lst$resp)){ # k <- "SRmammals" k <- "SRpredator"
# print(k)
val_df_all_lst <- lapply (runs, function(outs){ #outs <- 1
#####
###out rows for thisrun (has to be chosen first, as depending on cv20/cv-index,
###the rows are chosen by one column, or by several.)
#####
if(grepl("cv_index", cv)){
outrows <- which(mix_lst$meta$cvindex_run == outs)
}else{
cv_nm <- colnames(mix_lst$meta)[grepl("outerrun", colnames(mix_lst$meta))][outs]
outrows <- which(mix_lst$meta[cv_nm] == 1)
}
#####
###RMSE
#####
RMSE_elevSR <- caret::RMSE(pred = mix_lst$resp[[k]]$pred_elevSR[outrows],
obs = mix_lst$resp[[k]]$SR[outrows], na.rm = T)
RMSE_lidarSR <- caret::RMSE(pred = mix_lst$resp[[k]]$pred_lidarSR[outrows],
obs = mix_lst$resp[[k]]$SR[outrows], na.rm = T)
RMSE_lidarRES <- caret::RMSE(pred = mix_lst$resp[[k]]$pred_lidarRES[outrows],
obs = mix_lst$resp[[k]]$calc_elevRES[outrows], na.rm = T)
RMSE_sumSR <- caret::RMSE(pred = mix_lst$resp[[k]]$pred_sumSR[outrows],
obs = mix_lst$resp[[k]]$SR[outrows], na.rm = T)
RMSE_lidarelevSR <- caret::RMSE(pred = mix_lst$resp[[k]]$pred_lidarelevSR[outrows],
obs = mix_lst$resp[[k]]$SR[outrows], na.rm = T)
#####
###RMSE/sd
#####
sd <- sd(mix_lst$resp[[k]]$SR, na.rm = T)
RMSEsd_elevSR <- RMSE_elevSR/sd
RMSEsd_lidarSR<- RMSE_lidarSR/sd
# residuen fehler soll auf sd absoluten werten gerechnet werden
# es ist egal ob residuen + oder - x sind
RMSEsd_lidarRES <- RMSE_lidarRES/sd(abs(mix_lst$resp[[k]]$calc_elevRES), na.rm = T)
RMSEsd_sumSR <- RMSE_sumSR/sd
RMSEsd_lidarelevSR <- RMSE_lidarelevSR/sd
#####
###RMSE/median
#####
mdn <- median(mix_lst$resp[[k]]$SR, na.rm = T)
RMSEmdn_elevSR <- RMSE_elevSR/mdn
RMSEmdn_lidarSR<- RMSE_lidarSR/mdn
# residuen fehler soll auf mdn absoluten werten gerechnet werden
# es ist egal ob residuen + oder - x sind
RMSEmdn_lidarRES <- RMSE_lidarRES/median(abs(mix_lst$resp[[k]]$calc_elevRES), na.rm = T)
RMSEmdn_sumSR <- RMSE_sumSR/mdn
RMSEmdn_lidarelevSR <- RMSE_lidarelevSR/mdn
armean <- mean(mix_lst$resp[[k]]$SR, na.rm = T)
#####
###check variation of ncomp
#####
#####
###new list element with validation
#####
val_df <- data.frame(run = outs,
RMSE_elevSR = RMSE_elevSR,
RMSE_lidarSR = RMSE_lidarSR,
RMSE_lidarRES = RMSE_lidarRES,
RMSE_sumSR = RMSE_sumSR,
RMSE_lidarelevSR = RMSE_lidarelevSR,
RMSEsd_elevSR = RMSEsd_elevSR,
RMSEsd_lidarSR = RMSEsd_lidarSR,
RMSEsd_lidarRES = RMSEsd_lidarRES,
RMSEsd_sumSR = RMSEsd_sumSR,
RMSEsd_lidarelevSR = RMSEsd_lidarelevSR,
sd = sd,
RMSEmdn_elevSR = RMSEmdn_elevSR,
RMSEmdn_lidarSR = RMSEmdn_lidarSR,
RMSEmdn_lidarRES = RMSEmdn_lidarRES,
RMSEmdn_sumSR = RMSEmdn_sumSR,
RMSEmdn_lidarelevSR = RMSEmdn_lidarelevSR,
mdn = mdn,
armean = armean
)
})
val_df_all <- do.call(rbind, val_df_all_lst)
#####
###add columns with statistical information about the RMSE and RMSEsd errors
#####
for (p in colnames(val_df_all)[!colnames(val_df_all) %in%
c("run", "sd", "mdn", "armean")]){ #p <- "RMSEsd_lidarRES"
val_df_all$sd_tmp <- sd(val_df_all[[p]], na.rm = T)
colnames(val_df_all)[colnames(val_df_all) == "sd_tmp"] <- paste0(p, "_sd")
val_df_all$mdn_tmp <- median(val_df_all[[p]], na.rm = T)
colnames(val_df_all)[colnames(val_df_all) == "mdn_tmp"] <- paste0(p, "_mdn")
qntls <- quantile(val_df_all[[p]], probs = c(0.25, 0.75), na.rm = T)
val_df_all$q25_tmp <- qntls[[1]]
val_df_all$q75_tmp <- qntls[[2]]
val_df_all$IQR_tmp <- val_df_all$q75_tmp - val_df_all$q25_tmp
colnames(val_df_all)[colnames(val_df_all) == "q25_tmp"] <- paste0(p, "_q25")
colnames(val_df_all)[colnames(val_df_all) == "q75_tmp"] <- paste0(p, "_q75")
colnames(val_df_all)[colnames(val_df_all) == "IQR_tmp"] <- paste0(p, "_IQR")
}
val_df_all$RMSE_IQR_min <- min(val_df_all[,grepl(pattern = "q25", colnames(val_df_all))&
grepl(pattern = "RMSE_", colnames(val_df_all))])
val_df_all$RMSE_IQR_max <- max(val_df_all[,grepl(pattern = "q75", colnames(val_df_all)) &
grepl(pattern = "RMSE_", colnames(val_df_all))])
val_df_all$RMSEsd_IQR_min <- min(val_df_all[,grepl(pattern = "q25", colnames(val_df_all))&
grepl(pattern = "RMSEsd_", colnames(val_df_all))])
val_df_all$RMSEsd_IQR_max <- max(val_df_all[,grepl(pattern = "q75", colnames(val_df_all)) &
grepl(pattern = "RMSEsd_", colnames(val_df_all))])
val_df_all$RMSEmdn_IQR_min <- min(val_df_all[,grepl(pattern = "q25", colnames(val_df_all))&
grepl(pattern = "RMSEmdn_", colnames(val_df_all))])
val_df_all$RMSEmdn_IQR_max <- max(val_df_all[,grepl(pattern = "q75", colnames(val_df_all)) &
grepl(pattern = "RMSEmdn_", colnames(val_df_all))])
#####
###add ranking of performances by model
#####
###rank by median - RMSEsd
mod_df_mdn_tmp <- val_df_all[,c("RMSEsd_elevSR_mdn",
"RMSEsd_lidarSR_mdn",
"RMSEsd_sumSR_mdn",
"RMSEsd_lidarelevSR_mdn",
"RMSEsd_lidarRES_mdn")]
mod_df_mdn_tmp_t <- t(mod_df_mdn_tmp[!duplicated(mod_df_mdn_tmp),])
mod_df_mdn_tmp_srt <- mod_df_mdn_tmp_t[order(mod_df_mdn_tmp_t[,1]),]
val_df_all$RMSEsd_elevSR_mdn_rank <- which(names(mod_df_mdn_tmp_srt) == "RMSEsd_elevSR_mdn")
val_df_all$RMSEsd_sumSR_mdn_rank <- which(names(mod_df_mdn_tmp_srt) == "RMSEsd_sumSR_mdn")
val_df_all$RMSEsd_lidarSR_mdn_rank <- which(names(mod_df_mdn_tmp_srt) == "RMSEsd_lidarSR_mdn")
val_df_all$RMSEsd_lidarelevSR_mdn_rank <- which(names(mod_df_mdn_tmp_srt) == "RMSEsd_lidarelevSR_mdn")
val_df_all$RMSEsd_lidarRES_mdn_rank <- which(names(mod_df_mdn_tmp_srt) == "RMSEsd_lidarRES_mdn")
###rank by median - RMSEmdn
mod_df_mdn_tmp <- val_df_all[,grepl(pattern = "_mdn$", colnames(val_df_all)) &
grepl(pattern = "RMSEmdn_", colnames(val_df_all))]
mod_df_mdn_tmp_t <- t(mod_df_mdn_tmp[!duplicated(mod_df_mdn_tmp),])
mod_df_mdn_tmp_srt <- mod_df_mdn_tmp_t[order(mod_df_mdn_tmp_t[,1]),]
val_df_all$RMSEmdn_elevSR_mdn_rank <- which(names(mod_df_mdn_tmp_srt) == "RMSEmdn_elevSR_mdn")
val_df_all$RMSEmdn_sumSR_mdn_rank <- which(names(mod_df_mdn_tmp_srt) == "RMSEmdn_sumSR_mdn")
val_df_all$RMSEmdn_lidarSR_mdn_rank <- which(names(mod_df_mdn_tmp_srt) == "RMSEmdn_lidarSR_mdn")
val_df_all$RMSEmdn_lidarelevSR_mdn_rank <- which(names(mod_df_mdn_tmp_srt) == "RMSEmdn_lidarelevSR_mdn")
val_df_all$RMSEmdn_lidarRES_mdn_rank <- which(names(mod_df_mdn_tmp_srt) == "RMSEmdn_lidarRES_mdn")
#####
###column with different model constellations
#####
#best model by mdn RMSEsd
val_df_all$constll1_RMSEsd_mdn[val_df_all$RMSEsd_elevSR_mdn_rank == 1] <- 1
val_df_all$constll1_RMSEsd_mdn[val_df_all$RMSEsd_sumSR_mdn_rank == 1] <- 2
val_df_all$constll1_RMSEsd_mdn[val_df_all$RMSEsd_lidarSR_mdn_rank == 1] <- 3
val_df_all$constll1_RMSEsd_mdn[val_df_all$RMSEsd_lidarelevSR_mdn_rank == 1] <- 4
val_df_all$constll1_RMSEsd_mdn[val_df_all$RMSEsd_lidarRES_mdn_rank == 1] <- 5
#best model by median RMSemdn
val_df_all$constll1_RMSEmdn_mdn[val_df_all$RMSEmdn_elevSR_mdn_rank == 1] <- 1
val_df_all$constll1_RMSEmdn_mdn[val_df_all$RMSEmdn_sumSR_mdn_rank == 1] <- 2
val_df_all$constll1_RMSEmdn_mdn[val_df_all$RMSEmdn_lidarSR_mdn_rank == 1] <- 3
val_df_all$constll1_RMSEmdn_mdn[val_df_all$RMSEmdn_lidarelevSR_mdn_rank == 1] <- 4
val_df_all$constll1_RMSEmdn_mdn[val_df_all$RMSEmdn_lidarRES_mdn_rank == 1] <- 5
###second best, ... could follow
###rank by IQR - RMSEsd
mod_df_IQR_tmp <- val_df_all[,grepl(pattern = "IQR$", colnames(val_df_all)) &
grepl(pattern = "RMSEsd_", colnames(val_df_all))]
mod_df_IQR_tmp_t <- t(mod_df_IQR_tmp[!duplicated(mod_df_IQR_tmp),])
mod_df_IQR_tmp_srt <- mod_df_IQR_tmp_t[order(mod_df_IQR_tmp_t[,1]),]
val_df_all$RMSEsd_elevSR_IQR_rank <- which(names(mod_df_IQR_tmp_srt) == "RMSEsd_elevSR_IQR")
val_df_all$RMSEsd_sumSR_IQR_rank <- which(names(mod_df_IQR_tmp_srt) == "RMSEsd_sumSR_IQR")
val_df_all$RMSEsd_lidarSR_IQR_rank <- which(names(mod_df_IQR_tmp_srt) == "RMSEsd_lidarSR_IQR")
val_df_all$RMSEsd_lidarelevSR_IQR_rank <- which(names(mod_df_IQR_tmp_srt) == "RMSEsd_lidarelevSR_IQR")
val_df_all$RMSEsd_lidarRES_IQR_rank <- which(names(mod_df_IQR_tmp_srt) == "RMSEsd_lidarRES_IQR")
###rank by IQR - RMSEmdn
mod_df_IQR_tmp <- val_df_all[,grepl(pattern = "IQR$", colnames(val_df_all)) &
grepl(pattern = "RMSEmdn_", colnames(val_df_all))]
mod_df_IQR_tmp_t <- t(mod_df_IQR_tmp[!duplicated(mod_df_IQR_tmp),])
mod_df_IQR_tmp_srt <- mod_df_IQR_tmp_t[order(mod_df_IQR_tmp_t[,1]),]
val_df_all$RMSEmdn_elevSR_IQR_rank <- which(names(mod_df_IQR_tmp_srt) == "RMSEmdn_elevSR_IQR")
val_df_all$RMSEmdn_sumSR_IQR_rank <- which(names(mod_df_IQR_tmp_srt) == "RMSEmdn_sumSR_IQR")
val_df_all$RMSEmdn_lidarSR_IQR_rank <- which(names(mod_df_IQR_tmp_srt) == "RMSEmdn_lidarSR_IQR")
val_df_all$RMSEmdn_lidarelevSR_IQR_rank <- which(names(mod_df_IQR_tmp_srt) == "RMSEmdn_lidarelevSR_IQR")
val_df_all$RMSEmdn_lidarRES_IQR_rank <- which(names(mod_df_IQR_tmp_srt) == "RMSEmdn_lidarRES_IQR")
mix_lst$val[[k]] <- val_df_all
}
if (file.exists(paste0(outpath, set_dir, "mix/"))==F){
dir.create(file.path(paste0(outpath, set_dir, "mix/")), recursive = T)
}
saveRDS(mix_lst, file = paste0(outpath, set_dir, "mix/", "61_master_lst_val.rds"))
########################################################################################
###var Imp
########################################################################################
pot_dirs <- list.dirs(path = paste0(outpath, set_dir), recursive = F, full.names = F)
for(k in names(mix_lst$resp)){ # k <- "SRmammals"
# print(k)
for (outs in runs){ #outs <- 19
if (outs <= max(runs)/2){
landuse <- "nofrst"
outs_corr <- outs
} else{
landuse <- "frst"
outs_corr <- outs - 20
}
dir_landuse <- pot_dirs[grepl(paste0("_", landuse, "_"), pot_dirs)]
# print(outs)
#####
###split for outer loop (independet cv)
###and inner index selection for model
#####
if(grepl("cv_index", cv)){
###index-cv
plt_in <- mix_lst$meta$plotID[-which(mix_lst$meta$cvindex_run == outs)]
plt_out <- mix_lst$meta$plotID[which(mix_lst$meta$cvindex_run == outs)]
tbl_in <- mix_lst[[k]][which(mix_lst[[k]]$plotID %in% plt_in),]
}else{
###cv-x
cv_nm <- colnames(mix_lst$meta)[grepl("outerrun", colnames(mix_lst$meta))][outs]
plt_in <- mix_lst$meta$plotID[mix_lst$meta[cv_nm] == 0]
plt_out <- mix_lst$meta$plotID[mix_lst$meta[cv_nm] == 1]
tbl_in <- mix_lst$resp[[k]][which(mix_lst$resp[[k]]$plotID %in% plt_in),]
}
for (m in resp_set){ # m <- "lidarSR"
# print(m)
m_name <- paste0("pred_", m)
# if(length(unique(tbl_in[,m])) > 1){ #check if tbl_in has only 0 zB: SRlycopodiopsida/nofrst/outs = 1
if(length(unique(tbl_in[,m_name])) > 1){ #check if tbl_in has only 0 zB: SRlycopodiopsida/nofrst/outs = 1
#####
###read actual model
#####
mod <- tryCatch(
readRDS(file = paste0(inpath, set_dir, dir_landuse, "/mod_run_", outs_corr, "_", k, "_", m, ".rds")),
error = function(e)mod <- NA)
var_imp <- tryCatch(
data.frame(sel_vars = mod$selectedvars,
varimp = varImp(mod)$importance),
error = function(e)var_imp <- NA)
# print(m)
if (!is.na(var_imp)){
mix_lst$varimp[[k]][[m_name]][[landuse]][[outs_corr]] <- var_imp
}else{
mix_lst$varimp[[k]][[m_name]][[landuse]][[outs_corr]] <- data.frame(sel_vars = NA, Overall = NA)
}
}else{
mix_lst$varimp[[k]][[m_name]][[landuse]][[outs_corr]] <- data.frame(sel_vars = NA, Overall = NA)
}
}
} #end for n
#######################
###varsel plots
#######################
# for (k in mix_lst$varimp){
# print(k)
# loop model for SR and resid
for (m in resp_set){ # m <- "lidarSR"
m_name <- paste0("pred_", m)
a <- do.call(c, mix_lst$varimp[[k]][[m_name]])
selvars_allruns <- do.call(rbind, a)#mix_lst$varimp[[k]][[m_name]])
# if (!is.na(sum(selvars_allruns$Overall))){
if ((sum(selvars_allruns$Overall, na.rm = T)!= 0)){
frq <- as.data.frame(table(selvars_allruns$sel_vars))
}else{
frq <- data.frame(resp = NA, frq = NA)
}
colnames(frq) <- c("pred", "freq")
# colnames(frq) <- c(paste0(k,"_", m), "freq")
mix_lst$varsel[[k]][[m_name]] <- frq
}
# }
}##k names resp
saveRDS(mix_lst, file = paste0(outpath, set_dir, "mix/", "61_master_lst_varimp_", ".rds"))
########################################################################################
###descriptive stuff for general overview of responses (in frst, nofrst, all, sr, median, sd, mean)
########################################################################################
resp_overview <- data.frame(resp = names(mix_lst$resp))
for (k in resp_overview$resp){
resp_overview$tmp[which(resp_overview$resp == k)] <- mix_lst$val[[k]]$armean
resp_overview$mdn[which(resp_overview$resp == k)] <- mix_lst$val[[k]]$mdn
resp_overview$sd[which(resp_overview$resp == k)] <- mix_lst$val[[k]]$sd
}
colnames(resp_overview)[colnames(resp_overview) == "tmp"] <- "mean_mix"
colnames(resp_overview)[colnames(resp_overview) == "mdn"] <- "median_mix"
colnames(resp_overview)[colnames(resp_overview) == "sd"] <- "sd_mix"
saveRDS(resp_overview, file = paste0(outpath, set_dir, "mix/", "61_resp_overview_descriptive.rds"))
write.csv(resp_overview, file = paste0(outpath, set_dir, "mix/", "61_resp_overview_descriptive.csv"))
#resp_overview <- read.csv(file = paste0(outpath, set_dir, "mix/", "61_resp_overview_descriptive.csv"))