-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path23_Vulnerability.R
More file actions
48 lines (37 loc) · 1.38 KB
/
Copy path23_Vulnerability.R
File metadata and controls
48 lines (37 loc) · 1.38 KB
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
################################################################################
#
# RiskExtrapolation
#
# Composite indices of vulnerability
#
################################################################################
#---------------------
# Reduce dimension
#---------------------
#----- Compute PLS
# Create formula
coefvars <- grep("coef", names(metadf), value = T) |> sort()
plsform <- sprintf("cbind(%s) ~ %s", paste(coefvars, collapse = ", "),
paste(metaprednames, collapse = " + "))
# Compute PLSR (basic PLS computed as a linear model)
plsres <- plsr(as.formula(plsform), data = metadf, scale = T)
# Extract scores for all locations and age
comps <- predict(plsres, newdata = metadf, type = "scores")
colnames(comps) <- sprintf("Comp%i", 1:ncol(comps))
metadf <- bind_cols(metadf, comps, .name_repair = "check_unique")
# Correlation with original metapredictors
coords <- plsres$loadings
#------------------
# Plot correlation with initial variables
#------------------
# Select right coordinates
coord_plot <- coords[, 1:npc, drop = F]
rownames(coord_plot) <- names(metaprednames)
# Color scale
pal <- scico(200, palette = "cork")
# Plot coordinates
corrplot(t(coord_plot), method = "square", is.corr = T, col.lim = c(-1, 1),
tl.srt = 45, tl.col = "black", cl.cex = .7, cl.align.text = "l",
col = pal, cl.length = 5)
# Save
dev.print(pdf, file = "figures/Fig2_compcor.pdf")