[R] vectorized dmvnorm

Adelchi Azzalini azzalini at stat.unipd.it
Fri Mar 2 13:02:35 CET 2007


On Fri, 2 Mar 2007 12:08:20 +0100, Ingmar Visser wrote:

IV> Dear List,
IV> Is there an R-function that computes vectorized densities of the  
IV> multivariate normal distribution, ie vectorized in x, mean and sigma?
IV> 
IV> That is, a function that takes eg:
IV> 
IV> x <- matrix(0,3,2)
IV> y <- matrix(1:6,3)-3
IV> sig <- array(c(1,0,0,1,1,0.1,0.1,1,1,0.3,0.3,1),c(2,2,3))
IV> 
IV>  > x
IV>       [,1] [,2]
IV> [1,]    0    0
IV> [2,]    0    0
IV> [3,]    0    0
IV>  > y
IV>       [,1] [,2]
IV> [1,]   -2    1
IV> [2,]   -1    2
IV> [3,]    0    3
IV>  > sig
IV> , , 1
IV> 
IV>       [,1] [,2]
IV> [1,]    1    0
IV> [2,]    0    1
IV> 
IV> , , 2
IV> 
IV>       [,1] [,2]
IV> [1,]  1.0  0.1
IV> [2,]  0.1  1.0
IV> 
IV> , , 3
IV> 
IV>       [,1] [,2]
IV> [1,]  1.0  0.3
IV> [2,]  0.3  1.0
IV> 
IV> and then returns a vector with elements:
IV> 
IV> dmvnorm(x[i,],y[i,],sig[,,i])
IV> 
IV> And/or is there another efficient of computing these without using  
IV> loops:
IV> for(i in 1:3)  print(dmvnorm(x[i,],y[i,],sig[,,i]))
IV> 

a partial answer:
dmnorm() in package mnormt is vectorized for x, not for mean and covariance;
dmvnorm() in package mvtnorm works similarly

-- 
Adelchi Azzalini  <azzalini a stat.unipd.it>
Dipart.Scienze Statistiche, Università di Padova, Italia
tel. +39 049 8274147,  http://azzalini.stat.unipd.it/



More information about the R-help mailing list