-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathA4_AlternativePC.R
39 lines (30 loc) · 1.16 KB
/
A4_AlternativePC.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
################################################################################
#
# Appendix 3.
# Alternative ways to construct composite indices of vulnerability
#
################################################################################
# Needs the metapred and metapred_obs object created
# Replace the "Compute PLS" (lines 20-30) part of the code in 23_Vulnerability.R
#---------------------
# PCA
#---------------------
# Unsupervised dimension reduction useful in any context
# Compute PCA
pcares <- prcomp(metapred_obs, center = TRUE, scale = TRUE)
# Extract PCs
comps <- scale(metapred, center = pcares$center, scale = pcares$scale) %*%
pcares$rotation
# Correlation with original metapredictors
coords <- t(apply(pcares$rotation, 1, "*", pcares$sdev))
#---------------------
# CCA
#---------------------
# Mostly useful when the response (ERF) is multidimensional
# Compute CCA
ccares <- cancor(metapred_obs, coefs_obs)
# Create new metavariables
comps <- scale(metapred, center = ccares$xcenter, scale = F) %*% ccares$xcoef
colnames(comps) <- sprintf("Comp%i", 1:ncol(metapred_obs))
# Correlation with original metapredictors
coords <- cor(metapred, comps)