繁体   English   中英

带有 List 参数的 Rcpp 函数返回一个矩阵

[英]Rcpp function with List arguments returning a matrix

我在中有一段运行得很慢的代码,所以我希望在重写该函数,但是,这是我第一次尝试使用 Rcpp,我无法编译或运行代码。

在 R 中,我试图重写的部分是

  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
    })

此代码应返回一个嵌套矩阵的IST。 该列表的长度应该是numstudies并且它的每个元素应该是一个维度为n[v]ql列的 这些矩阵的每一行都应该从多元正态分布中抽取,但​​是每行的抽取将由不同的均值向量finalmu_b2_l和不同的协方差矩阵finalcov_b2_l finalmu_b2_lfinalcov_b2_l都是嵌套列表,长度为numstudies ,其中的每个元素都是一个长度为n[v]的列表。

到目前为止,我已经尝试编写一段代码,它只会绘制所需的矩阵之一,但希望对其进行扩展以尽快返回矩阵列表。 代码如下:

#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;
}

任何时候我尝试编译代码(通过使用函数sourceCpp获取代码所在的 .cpp 文件),我都会收到一条错误消息:

“const::mat&”类型的引用初始化无效

,我将其理解为创建draws矩阵或填充它的问题?

关于-的任何建议、指示或指导

  1. 为什么这段代码没有编译,我应该在 Rcpp 中做什么,和/或
  2. 如何扩展它以返回矩阵列表而不仅仅是单个矩阵将不胜感激。

编辑

原始 r 代码包含嵌套在 lapply 函数中的 mapply 函数。 我想要一个列表长度为 numstudies 的输出,其中的每个元素都是一个矩阵。 此列表中每个矩阵的每一行都是来自不同多元正态分布的相关性(因此在每个列表元素中,对于矩阵的每一行,都有一个唯一的均值向量和一个唯一的协方差矩阵,控制我想要绘制的多元正态分布从)。 我写了一个嵌套在 lapply 中的 mapply 以自动给出我想要的输出格式,同时允许从每个矩阵行的不同分布中进行绘制。

编辑2

将迭代更改为从 0 而不是 1 运行后,编译以下代码:

#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;
}

编辑3

代码当前编译,但不绘制样本。 相反,我收到以下错误消息:

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

我已经准备了一个我希望这个基本函数做什么的例子(它只是从不同的多元正态分布中采样一个实现矩阵。

数据:

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代码

  drawsampletest<-draw_b(n=n,
                           ql=ql,
                           mu=mu_example,
                           cov=cov_example)

注册码

#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;
}

当然,一旦这工作 - 我仍然需要扩展它以绘制矩阵列表而不是单个矩阵

这是一个基本设置,应该可以满足您的需求:

#include <RcppDist.h>
#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppDist,RcppArmadillo)]]

// [[Rcpp::export]]
arma::mat draw_b(const int ql,
                 const Rcpp::List& mu,
                 const Rcpp::List& cov) {
    int n = mu.size();
    arma::mat draws = arma::zeros(n, ql);
    for ( arma::uword iter = 0; iter < n; iter++ ) {
        draws.row(iter) = rmvnorm(1, mu[iter], cov[iter]);
    }
    return draws;
}

// [[Rcpp::export]]
Rcpp::List get_list_of_draws(Rcpp::List mu, Rcpp::List Sigma, int ql) {
    int numstudies = mu.size();
    Rcpp::List res(numstudies);
    for ( int iter = 0; iter < numstudies; ++iter ) {
        Rcpp::List mu_temp = mu[iter];
        Rcpp::List cov_temp = Sigma[iter];
        res[iter] = draw_b(ql, mu_temp, cov_temp);
    }
    return res;
}

它似乎按预期工作:

res <- get_list_of_draws(mu_example, cov_example, ql)
str(res)
# List of 1
#  $ : num [1:10, 1:2] -0.0156 -0.4717 -0.8134 0.5489 0.1215 ...

(请注意,当我设置mu_examplecov_example我将它们包装在list()正如您所说,它们应该是列表列表。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM