-
Notifications
You must be signed in to change notification settings - Fork 10
/
FINDALR.R
59 lines (48 loc) · 2.2 KB
/
FINDALR.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
FINDALR <- function(data, weight = FALSE) {
### -------------------------------------------------------------------
### function to identify the best reference for a set of ALRs
### various statistics are computed for each reference to assist
### the choice of best reference
### data is a normalized data matrix
### equal weighting is default for the logratio geometry here
### row (sample) weighting not implemented in this version
if(sum(data[1,]!=1)) data <- data/rowSums(data)
### first compute the exact logratio geometry
data.lra <- LRA(data, weight=weight)
data.lra.rpc <- data.lra$rowpcoord
tot.var <- sum(data.lra$sv^2)
### loop on all the potential references, computing Procrustes correlation
### of each set of ALRs with the exact geometry
procrust.cor <- rep(0, ncol(data))
dim <- min(nrow(data), ncol(data)) - 1
for(j in 1:ncol(data)) {
# ALR transformation
alr <- ALR(data, denom=j, weight=weight)
# ALR geometry using PCA 'by hand' using SVD, without or with weighting
if(!weight) {
alr.svd <- svd(sqrt(1/nrow(alr$LR)) * sweep(alr$LR, 2, colMeans(alr$LR)) * sqrt(1/ncol(alr$LR)))
alr.rpc <- sqrt(nrow(alr$LR)) * alr.svd$u %*% diag(alr.svd$d)
}
if(weight) {
c <- colMeans(data)
cc <- c*c[j]
cc <- cc[-j]
alr.svd <- svd(sqrt(1/nrow(alr$LR)) * sweep(alr$LR, 2, colMeans(alr$LR)) %*% diag(sqrt(cc)))
alr.rpc <- sqrt(nrow(alr$LR)) * alr.svd$u %*% diag(alr.svd$d)
}
procrust.cor[j] <- protest(alr.rpc[,1:dim],data.lra.rpc, permutations=0)$t0
}
### the variances of the log-transformed parts
var.log <- as.numeric(apply(log(data), 2, var))
### highest Procrustes correlation
procrust.max <- max(procrust.cor)
### which reference gives maximum Procrustes
procrust.ref <- which(procrust.cor==procrust.max)
### lowest log variance
var.min <- min(var.log)
### which reference gives lowest log variance
var.ref <- which(var.log==var.min)
return(list(tot.var=tot.var, procrust.cor=procrust.cor,
procrust.max=procrust.max, procrust.ref=procrust.ref,
var.log=var.log, var.min=var.min, var.ref=var.ref))
}