[R] vectorized dmvnorm

Ingmar Visser i.visser at uva.nl
Fri Mar 2 12:08:20 CET 2007


Dear List,
Is there an R-function that computes vectorized densities of the  
multivariate normal distribution, ie vectorized in x, mean and sigma?

That is, a function that takes eg:

x <- matrix(0,3,2)
y <- matrix(1:6,3)-3
sig <- array(c(1,0,0,1,1,0.1,0.1,1,1,0.3,0.3,1),c(2,2,3))

 > x
      [,1] [,2]
[1,]    0    0
[2,]    0    0
[3,]    0    0
 > y
      [,1] [,2]
[1,]   -2    1
[2,]   -1    2
[3,]    0    3
 > sig
, , 1

      [,1] [,2]
[1,]    1    0
[2,]    0    1

, , 2

      [,1] [,2]
[1,]  1.0  0.1
[2,]  0.1  1.0

, , 3

      [,1] [,2]
[1,]  1.0  0.3
[2,]  0.3  1.0

and then returns a vector with elements:

dmvnorm(x[i,],y[i,],sig[,,i])

And/or is there another efficient of computing these without using  
loops:
for(i in 1:3)  print(dmvnorm(x[i,],y[i,],sig[,,i]))

best, Ingmar Visser



More information about the R-help mailing list