[Rcpp-devel] How to use R function mvrnorm in c++?

Douglas Bates bates at stat.wisc.edu
Mon Oct 8 23:53:25 CEST 2012


On Mon, Oct 8, 2012 at 4:23 PM, Victor <lijilong1979 at 163.com> wrote:
> Dear all,
>
> I want to use R function mvrnorm (package MASS) in my c++ scripts. Below is
> the R code:
>
>     library(MASS)
>
>     x<-matrix(c(1.5,0,0,0,1.5,0,0,0,1.5),3,3)
>
>     mvrnorm(1,c(15,33,26),x)
>
>
> I will appreciate you very much if you give me some help and advice! Thank
> you so much!

Read the source code for the mvrnorm function.  You will find that it
is implemented by taking an eigenvalue-eigenvector decomposition of
the Sigma matrix.  If your Sigma matrix is fixed, then simply take the
decomposition in R and pass the components.  If it varies then use
RcppEigen or RcppArmadillo to obtain the decomposition.

This is not difficult but also not trivial.  You will need to do some
reading and experimenting.  Ask yourself if the amount of time you
will save in execution of the function warrants the time you will need
to spend to learn the necessary programming.


More information about the Rcpp-devel mailing list