-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputeSimFromY.R
68 lines (40 loc) · 1.17 KB
/
computeSimFromY.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
computeSimFromY <- function(
Y,
simMethod = "PPMI"
) {
##### INPUT
## Y: matrix, binary matrix with {0, 1} entries
## simMethod: character string, which method to be used
# source("calcXnorm.R")
if (simMethod == "PPMI") {
cat("PPMI...\n")
## Cij <- t(Y) %*% Y
Cij <- crossprod(Y)
## exchange 0 <=> 1
Y[Y == 1] <- 2
Y[Y == 0] <- 1
Y[Y == 2] <- 0
Cij <- Cij + crossprod(Y)
diag(Cij) <- 0
Pi <- colSums(Cij)
allSum <- sum(Pi)
P <- allSum * Cij / (Pi %*% t(Pi))
ppmi <- log2(P)
# ppmi <- log(P) ## natural logarithm
ppmi[ppmi < 0] <- 0
simCC <- ppmi
} else if (simMethod == "tanimoto") {
cat("Jaccard similarity...\n")
flush.console()
## same to calTanimotoSimMat()
simCC <- calJaccardMat(t(Y))
} else if (simMethod == "cosine") {
cat("cosine...\n")
flush.console()
## cosine based on col-col (col-wise)
simCC <- crossprod(Y) / tcrossprod(sqrt(colSums(Y ^ 2)))
} else {
stop("no such similarity!\n")
}
return(simCC)
}