I am implementing the following function in the R code:
So far I used:
sig.TOM <- function(adj, sig.adj) {
out <- matrix(nrow = nrow(adj), ncol = ncol(adj))
for (i in 1:nrow(adj)) {
for (j in 1:ncol(adj)) {
out[i,j] <- abs(adj[i, j] + sum(sig.adj[i, -c(i, j)]*sig.adj[-c(i, j), i]))/(
min(sum(sig.adj[-i, i]), sum(sig.adj[-j, j])) + 1 - abs(adj[i,j]))
}
}
return(out)
}
where ~a is the following mock matrix:
sig.adj <- structure(c(1, -0.418913311940584, 1, 0.947013383275973, -1,
-0.418913311940584, 1, -0.207962861914701, 0.584386281408348,
-0.687223049826016, 1, -0.207962861914701, 1, 0.763551721347657,
-0.0327147711077901, 0.947013383275973, 0.584386281408348, 0.763551721347657,
1, 0.284466543760789, -1, -0.687223049826016, -0.0327147711077901,
0.284466543760789, 1), .Dim = c(5L, 5L))
and adj <- abs(sig.adj)
, where adj in the formula is described as a and sig.adj as ~a.
But the result is not symmetric as expected so I must have implemented it wrong, I have doubts on the summation part.
How can that sum of products of values when the indices are not i or j be implemented?
The proposed solutions:
spec.mult1 <- function(A,B) {
rA <- nrow(A); cB <- ncol(B)
C <- A %*% B
for (i in 1:rA) for (j in 1:cB)
C[i,j] <- C[i,j] - A[i,i]*B[i,j] - A[i,j]*B[j,j] + ifelse(i==j, A[i,i]*B[j,j], 0)
C
}
spec.mult2 <- function(A) {
dA.A <- diag(A)*A
crossprod(A) - dA.A - t(dA.A) + diag(diag(A)^2)
}
spec.mult3 <- function(A,B) {
rA <- nrow(A); cB <- ncol(B)
C <- A %*% B
for (i in 1:rA) for (j in 1:cB)
C[i,j] <- C[i,j] - A[i,i]*B[i,j] - A[i,j]*B[j,j]
C
}
spec.mult4 <- function(A) {
dA.A <- diag(A)*A
crossprod(A) - dA.A - t(dA.A)
}
spec.mult5 <- function(sig.adj) {
nr <- nrow(sig.adj); nc <- ncol(sig.adj)
C <- matrix(NA, nr, nc)
for (i in 1:nr) for (j in 1:nc)
C[i,j] <- sum(sig.adj[i, -c(i, j)]*sig.adj[-c(i, j), j])
C
}
Comparing the results of each function :
all(res1 == res2)
[1] TRUE
> all(res1 == res3)
[1] FALSE
> all(res1 == res4)
[1] FALSE
> all(res1 == res5)
[1] FALSE
> all(res2 == res3)
[1] FALSE
> all(res2 == res4)
[1] FALSE
> all(res2 == res5)
[1] FALSE
> all(res3 == res4)
[1] TRUE
> all(res3 == res5)
[1] FALSE
> all(res4 == res5)
[1] FALSE
Results that, spec.mult1 == spec.mult2 and spec.mult3 == spec.mult4 but spec.mult5 (the one I understand, and hope it is correct) doesn't appear