-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowRankApproxSimScore.R
79 lines (58 loc) · 1.59 KB
/
lowRankApproxSimScore.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
lowRankApproxSimScore <- function(
numR,
numC,
trainIndexByRow,
simCC,
isLowRankApprox = TRUE,
isNormScoreMat = FALSE,
lowRankMethod = "SVD"
) {
##### INPUT
##
##
##
##
##### OUTPUT
## store neighbor scores for each row (either target or drug)
## it has the same shape with adjacency matrix, Y
neigScoreMat <- matrix(0, nrow = numR, ncol = numC)
for (i in 1:numR) {
## training index for current row (either target or drug)
idxTr <- trainIndexByRow[[i]]
## neighbor scores for current row
neigScore <- simCC[, idxTr]
if (is.matrix(neigScore)) {
neigScore <- rowSums(neigScore)
}
neigScoreMat[i, ] <- neigScore
}
## row-normalized score
if (isNormScoreMat) {
cat("perform row-wise nomalization of score matrix...\n")
neigScoreMat <- calcXnorm(neigScoreMat, "row")
}
## need low-rank approximation?
if (isLowRankApprox) {
if (lowRankMethod == "SVD") {
cat("SVD decomposition\n")
flush.console()
svdRank <- 100
cat("SVD rank =", svdRank, "\n")
svdRes <- irlba(neigScoreMat, nv = svdRank)
# str(svdRes)
U <- svdRes$u
S <- svdRes$d
V <- svdRes$v
U <- U %*% diag(S)
predScoreMat <- U %*% t(V)
} else {
stop("please specify low-rank method\n")
}
} else {
cat("without low-rank approximation\n")
flush.console()
predScoreMat <- neigScoreMat
}
# cat("min =", min(predScoreMat), "\n")
return(predScoreMat)
}