Feixiang Sun
Feixiang Sun

Reputation: 117

counstruct function to expression math formula in r

here I have a formula as in the graph,
enter image description here

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

Answers (1)

ekoam
ekoam

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

Related Questions