[R] Improving efficiency in "outer"-like calculation

Giovanni Petris GPetris at uark.edu
Fri Oct 17 01:09:55 CEST 2003


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                 GPetris at uark.edu ]
[ Department of Mathematical Sciences              ]
[ University of Arkansas - Fayetteville, AR 72701  ]
[ Ph: (479) 575-6324, 575-8630 (fax)               ]
[ http://definetti.uark.edu/~gpetris/              ]
[__________________________________________________]




More information about the R-help mailing list