Here is one way to do it (the way I'm doing it currently)...

dmixnorm <- function(x,mu,sigma,prob) { ## x can be a vector
   colSums(prob*dnorm(matrix(x,length(mu),length(x),byrow=TRUE),mu,sigma))
}

d <- 0
for(i in iter)
     d <- d + dmixnorm(x, mcmc$mu[i,], mcmc$sigma[i,], mcmc$prob[i,])
d/length(iter)

my mcmc$mu, etc are yours simMeans, simVars and simWeights

This does not take that long time and uses little memory.

The trick is that for-loops are not always that bad (i.e., they are not 
always the bottle neck)

Gardar

At 06:09 PM 10/16/2003 -0500, Giovanni Petris wrote:

>Hello,
>
>I am doing mcmc=10000 simulations from a posterior distribution of the 
>parameters
>of a mixture of K=6 normal densities.
>I have mcmc by K matrices simMeans, simVars and simWeights containing
>the simulation output: one row for each simulation, one column for
>each normal component of the mixture.
>One thing I would like to do is a plot of the posterior predictive
>density. In order to do that I define a vector x of points at which I
>want to evaluate this density. And then I use the following commands:
>
>pred <- colMeans(apply(structure(dnorm(rep(x,each=mcmc*K), mean=simMeans, 
>sd=sqrt(simVars))*
> 
>rep(simWeights,length(x)),dim=c(mcmc,K,length(x))),c(1,3),sum))
>lines(x,pred)
>
>Everything works, but it is very slow. Does anybody have suggestions
>to do the same thing in a more efficient way?
>
>Thanks in advance,
>Giovanni Petris
>
>--
>
>  __________________________________________________
>[                                                  ]
>[ Giovanni Petris                 [EMAIL PROTECTED] ]
>[ Department of Mathematical Sciences              ]
>[ University of Arkansas - Fayetteville, AR 72701  ]
>[ Ph: (479) 575-6324, 575-8630 (fax)               ]
>[ http://definetti.uark.edu/~gpetris/              ]
>[__________________________________________________]
>
>______________________________________________
>[EMAIL PROTECTED] mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help

___________________________________________________________________
Gardar Johannesson
Lawrence Livermore National Laboratory
7000 East Avenue, L-229
Livermore, CA 94550

[EMAIL PROTECTED]
Tel: (925) 422-3901,  Fax: (925) 422-4141
        [[alternative HTML version deleted]]

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

Reply via email to