-
Notifications
You must be signed in to change notification settings - Fork 3
/
5i_GTD_Prj03v4_Analysis_PairwiseCorrelations.R
executable file
·181 lines (143 loc) · 6.36 KB
/
5i_GTD_Prj03v4_Analysis_PairwiseCorrelations.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
################### CALCULATE PAIRWISE CORRELATION ##################
#
# Daniel Schlaepfer, 2015-2016
#
# Calculate pairwise correlation coefficients for response variables within temperate dryland areas for
# all study_areas_and_shifts x RCP combinations
# - requests 'get_precalculations' from '5a1_GTD_*.R'
# - requests 'get_studyareaextents' from '5a1_GTD_*.R'
#
###################################################################
#-------------------------------
#---GLOBAL SETTINGS
comp <- "dropbox"
do_cors <- TRUE
redo_cors <- FALSE
#-------------------------------
#---R packages
pkg_reqd <- c("reshape2", "pcaPP")
has_loaded <- sapply(pkg_reqd,
function(lib) require(lib, character.only = TRUE, quietly = FALSE))
stopifnot(has_loaded)
#---Load data and misc. functions
if (comp %in% c("err", "eleos")) {
dir.gtd <- "/PATH_TO_PROJECT/Product_PowellCenter/6_Projects_Year1"
} else if (comp == "dropbox") {
dir.gtd <- "/PATH_TO_PROJECT/Product_PowellCenter/6_Projects_Year1"
}
dir.prj <- file.path(dir.gtd, "Prj03_GlobalVulnerability", "4_Analysis", "4_Analysis_v4")
get_precalculations <- TRUE
get_studyareaextents <- TRUE
source(file.path(dir.prj, "5a1_GTD_Prj03v4_Helper.R"))
stopifnot(done_precalculations, done_studyareaextents)
#---Directories
dir.create(dir.fig_SAM <- file.path(dir.prj, "6_Results", "8_CorrelationTables_Pairwise_v4"), showWarnings = FALSE)
#-------------------------------
#------WITHIN TEXT RESULTS
#-------------------------------
#------TABLES AND FIGURES
if (do_cors) {
res_names <- sets_names_extracted2[-3]
var_names <- list(var_climate, var_response_sel, var_definition, var_soils, var_veg)[-3]
var_labels <- list(label_climate, label_response_sel, label_definition, label_soils, label_veg)[-3]
do_tabulate_correlation_response <- function(dat, dat_current = NULL, varnames, varlabels, method = c("spearman", "pearson", "kendall", "calc_fast_tau"), dir_out, ftag) {
#str(dat_current): num [1:20021, 1:nvars] 1002 NA NA 987 NA ...
# - attr(*, "dimnames") = List of 2
# ..$ : NULL
# ..$ : chr [1:nvars] vars
#str(dat): num [1:2, 1:3, 1:20021, 1:nvars] NA NA NA NA NA NA NA NA NA NA ...
# - attr(*, "dimnames") = List of 4
# ..$ : chr [1:2] "RCP45" "RCP85"
# ..$ : chr [1:3] "1" "8" "16"
# ..$ : NULL
# ..$ : chr [1:nvars] vars
method <- match.arg(method)
ftag <- clean_name(ftag)
ftemp <- file.path(dir_out, fname <- paste0("Table_PairwiseCorrelation_", ftag, "_", method, ".csv"))
if (redo_cors || !file.exists(ftemp)) {
#---Individual scenarios
res <- array(NA, dim = c(1 + length(reqRCPs), 1 + length(reqGCMs), length(varnames), length(varnames)),
dimnames = list(c(currentSc, reqRCPs), c(currentSc, reqGCMs), varlabels, varlabels))
#Current
if (method %in% c("pearson", "kendall", "spearman")) {
res[currentSc, currentSc, , ] <- cor(dat_current, use = "pairwise.complete.obs", method = method)
} else if (method == "calc_fast_tau") {
for (iv1 in seq_along(varnames)) for (iv2 in seq_along(varnames)) {
ivar1 <- dimnames(dat_current)[[2]][grep(varnames[iv1], dimnames(dat_current)[[2]])]
ivar2 <- dimnames(dat_current)[[2]][grep(varnames[iv2], dimnames(dat_current)[[2]])]
res[currentSc, currentSc, iv1, iv2] <- calc_fast_tau(dat_current[, ivar1], dat_current[, ivar2])
}
}
#GCMs
for (ircp in seq_along(reqRCPs)) for (igcm in seq_along(reqGCMs)) {
if (method %in% c("pearson", "kendall", "spearman")) {
res[1 + ircp, 1 + igcm, , ] <- cor(dat[ircp, igcm, , ], use = "pairwise.complete.obs", method = method)
} else if (method == "calc_fast_tau") {
for (iv1 in seq_along(varnames)) for (iv2 in seq_along(varnames)) {
ivar1 <- dimnames(dat)[[4]][grep(varnames[iv1], dimnames(dat)[[4]])]
ivar2 <- dimnames(dat)[[4]][grep(varnames[iv2], dimnames(dat)[[4]])]
res[1 + ircp, 1 + igcm, iv1, iv2] <- calc_fast_tau(dat[ircp, igcm, , ivar1], dat[ircp, igcm, , ivar2])
}
}
}
#---Aggregated to ranks
res2 <- array(NA, dim = c(1 + length(reqRCPs), 1 + length(ranks), length(varnames), length(varnames)),
dimnames = list(c(currentSc, reqRCPs), c(currentSc, paste0("Rank", ranks)), varlabels, varlabels))
#Current
res2[currentSc, currentSc, , ] <- res[currentSc, currentSc, , ]
#Ranks
for (ircp in seq_along(reqRCPs)) for (iv1 in seq_along(varnames)) for (iv2 in seq_along(varnames)) {
res2[1 + ircp, -1, iv1, iv2] <- calc_ensemble4(res[1 + ircp, -1, iv1, iv2])
}
#---Prepare table
temp <- melt(res2)
temp <- temp[!is.na(temp[, "value"]), ]
temp <- temp[!(temp[, "Var3"] == temp[, "Var4"]), ]
resT <- dcast(temp, Var1 + Var3 + Var2 ~ Var4)
#---Format table
resOut <- resT
for (iv in 1:ncol(resOut)) {
if (inherits(resOut[, iv], "numeric")) {
resOut[, iv] <- as.character(round(resOut[, iv], 2))
resOut[, iv] <- ifelse(is.na(resOut[, iv]), "", resOut[, iv])
resOut[(resOut$Var2 == "Rank16"), iv] <- ifelse(resOut[(resOut$Var2 == "Rank8"), iv] == "", "", paste0("(", resOut[(resOut$Var2 == "Rank1"), iv], ", ", resOut[(resOut$Var2 == "Rank16"), iv], ")"))
}
if (is.factor(resOut[, iv])) resOut[, iv] <- as.character(resOut[, iv])
}
resOut <- resOut[!(resOut$Var2 == "Rank1"), ]
resOut[(resOut$Var2 == "Rank8"), "Var2"] <- "mid"
resOut[(resOut$Var2 == "Rank16"), "Var2"] <- "(lo, hi)"
write.csv(resOut, file = ftemp)
}
invisible(NULL)
}
print("DrylandResponse: pairwise-corrrelation tables")
for (id in seq_along(res_names)) {
dir.temp <- dir.fig_SAM
# get data objects
temp <- fix_var_dim(get(res_names[id]))
vars <- temp$vars
data <- temp$data[, -1, -1, , ]
data_current <- temp$data[, currentSc, currentSc, , ]
if (length(dim(data_current)) <= 2) {
dimns <- dimnames(data_current)
dim(data_current) <- c(dim(data_current), 1)
dimnames(data_current) <- modifyList(dimns, list(vars = vars))
}
rm(temp)
# select variables and cells
ivars <- sapply(var_names[[id]], function(x) grep(x, vars)[1])
stopifnot(!is.na(ivars))
data <- data["MetDef_Any17Cond", , , , ivars]
data_current <- data_current["MetDef_ThisCond", , ivars]
print(paste(Sys.time(), res_names[id]))
for (method in c("spearman", "calc_fast_tau")) {
do_tabulate_correlation_response(dat = data,
dat_current = data_current,
varnames = var_names[[id]],
varlabels = var_labels[[id]],
method = method,
dir_out = dir.temp, ftag = res_names[id])
}
}
}