I have a section of code in r that is running quite slowly, and so I'm hoping to rewrite the function in rcpp, however, this is my first attempt using Rcpp, and I cannot get the code to compile or run.
In R, the section I am trying to rewrite is
drawsamples<-lapply(1:numstudies,function(v){
temp<-t(mapply(rmvn,n=1,mu=finalmu_b2_l[[v]],sigma=finalcov_b2_l[[v]]))
rownames(temp)<-id[[v]]
temp
})
This code should return a nested list of matrices. The list should be of length numstudies and each element of it should be a matrix of dimension n[v] rows by ql columns. Each row of these matrices should be draws from a multivariate normal distribution, however the draws for each row will be controlled by a different mean vector finalmu_b2_l and a different covariance matrix finalcov_b2_l. Both finalmu_b2_l and finalcov_b2_l are nested lists, top level of length numstudies, each element of which is a list of length n[v].
So far, I have tried to write a piece of rcpp code that will just draw one of the matrices needed, but want to extend this to return a list of matrices as soon as possible. The code is below:
#include <RcppDist.h>
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::depends(RcppDist,RcppArmadillo)]]
// [[Rcpp::export]]
arma::mat draw_b(const int n,
const int ql,
const Rcpp::List mu,
const Rcpp::List cov) {
arma::mat draws = arma::zeros(n,ql);
for (int iter = 1; iter <= n; iter++) {
draws.row(iter) = rmvnorm(1, mu[iter], cov[iter]);
}
return draws;
}
Any time I try to compile the code (by sourcing the .cpp file the code is held in, using function sourceCpp), I get an error reading:
invalid initialization of reference of type 'const::mat&'
, which I understand as an issue either with creating the draws matrix or with filling it?
Any advice, pointers or guidance on -
- why this code is not compiling, what I should be doing in Rcpp instead, and/or
- how to expand this to return a list of matrices rather than just a single matrix would be greatly appreciated.
EDIT
The original r code contains a mapply function nested within a lapply function. I wanted to have an output that is a list length numstudies, each element of which is a matrix. Each row of each matrix within this list is a relisation from a different multivariate normal distribution (so within each list element, for each row of the matrix, there is a unique mean vector and a unique covariance matrix controlling the multivariate normal I want to draw from). I'd written as a mapply nested within a lapply to automatically give the output format I wanted, whilst allowing draws from different distributions for each matrix row.
EDIT2
After changing the iteration to run from 0 instead of 1, the following code compiles:
#include <RcppDist.h>
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::depends(RcppDist,RcppArmadillo)]]
//' @keywords internal
// [[Rcpp::export]]
arma::mat draw_b(const int & n,
const int & ql,
const Rcpp::List & mu,
const Rcpp::List & cov) {
arma::mat draws = arma::zeros(n,ql);
for (int iter = 0; iter <= n; iter++ ) {
draws.row(iter) = rmvnorm(1, mu_temp, cov_temp);
}
return draws;
}
EDIT3
The code currently compiles, but does not draw samples. Instead I get the following error message:
error: Mat::init(): requested size is not compatible with column vector layout
Error in draw_b(n = n, ql = ql, mu = mu_example, cov = cov_example) :
Mat::init(): requested size is not compatible with column vector layout
I've prepared an example of what I would like this basic function to do (that just samples a matrix of realisations from different multivariate normal distributions.
Data:
list(n = 10, ql = 2, mu_example = list(c(0.342909965003207, -0.788070875792469
), c(-0.00499810116271365, 0.0114865660452949), c(-0.149753928200309,
0.344162379034614), c(0.335176829763227, -0.770298692761465),
c(0.254206123984596, -0.584212951520601), c(0.379893097582703,
-0.873064992779416), c(0.137231089484867, -0.315382566602526
), c(0.405123380985852, -0.931048876501857), c(-0.00505917031396947,
0.0116269143128456), c(-0.0743318653279181, 0.170828451158346
)), cov_example = list(structure(c(0.199912910315971, -0.459437048770092,
-0.459437048770092, 4.49223135519527), .Dim = c(2L, 2L)), structure(c(0.199912910315971,
-0.459437048770092, -0.459437048770092, 4.49223135519527), .Dim = c(2L,
2L)), structure(c(0.199912910315971, -0.459437048770092, -0.459437048770092,
4.49223135519527), .Dim = c(2L, 2L)), structure(c(0.199912910315971,
-0.459437048770092, -0.459437048770092, 4.49223135519527), .Dim = c(2L,
2L)), structure(c(0.199912910315971, -0.459437048770092, -0.459437048770092,
4.49223135519527), .Dim = c(2L, 2L)), structure(c(0.199912910315971,
-0.459437048770092, -0.459437048770092, 4.49223135519527), .Dim = c(2L,
2L)), structure(c(0.199912910315971, -0.459437048770092, -0.459437048770092,
4.49223135519527), .Dim = c(2L, 2L)), structure(c(0.199912910315971,
-0.459437048770092, -0.459437048770092, 4.49223135519527), .Dim = c(2L,
2L)), structure(c(0.199912910315971, -0.459437048770092, -0.459437048770092,
4.49223135519527), .Dim = c(2L, 2L)), structure(c(0.199912910315971,
-0.459437048770092, -0.459437048770092, 4.49223135519527), .Dim = c(2L,
2L))))
R code
drawsampletest<-draw_b(n=n,
ql=ql,
mu=mu_example,
cov=cov_example)
Rcpp code
#include <RcppDist.h>
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::depends(RcppDist,RcppArmadillo)]]
//' @keywords internal
// [[Rcpp::export]]
arma::mat draw_b(const int & n,
const int & ql,
const Rcpp::List & mu,
const Rcpp::List & cov) {
arma::mat draws = arma::zeros(n,ql);
for (int iter = 0; iter <= n; iter++ ) {
arma::rowvec mu_temp = mu[iter];
arma::mat cov_temp = cov[iter];
draws.row(iter) = rmvnorm(1, mu_temp, cov_temp);
}
return draws;
}
Of course once this is working- I still need to extend this to draw a list of matrices rather than a single matrix
mapply()call within thelapply()call? I think this question could benefit from a full minimal reproducible example. For tips on making one in the context ofR, see How to make a great R reproducible example. In particular, adding your data (i.e.numstudies,finalmu_b2_l, andfinalcov_b2_l) by editing your question to include the output of the R commanddput()and adding some expected output would probably help.R,Rcpp, andRcppDistversions (such as viasessionInfo())