Reputation: 117
here I have a formula as in the graph,
S is a matrix, as below
A B C D
A 0 3 10 5
B 3 0 12 8
C 10 12 0 11
D 5 8 11 0
N is a vector, as below:
A B C D
60 80 90 100
How to construct a function to express the left part of numerator in R? (No need to consider t.)
My dataset of S is about 600*600.
Upvotes: 0
Views: 34
Reputation: 8844
Try this
AR <- function(S, N) {
P <- sum(N)
out <- (t(N) %*% S %*% N + 2 * P) / (P * (P - 1))
dim(out) <- NULL
out
}
Output
> S # the diagonal of S MUST be all zeros.
A B C D
A 0 3 10 5
B 3 0 12 8
C 10 12 0 11
D 5 8 11 0
> N
A B C D
60 80 90 100
> AR(S, N)
[1] 6.413005
Upvotes: 1