gpt4 book ai didi

c++ - 带有列表参数的RCPP函数返回矩阵

转载 作者:行者123 更新时间:2023-12-02 10:04:24 27 4
gpt4 key购买 nike

我在中有一段代码运行得很慢,因此我希望在中重写该函数,但是,这是我第一次尝试使用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行的 。这些矩阵的每一行都应来自多元正态分布,但是每一行的绘制将由不同的均值 vector 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文件),都会收到错误消息:

invalid initialization of reference of type 'const::mat&'



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

关于-的任何建议,指示或指导-
  • 为什么不编译此代码,我应该在Rcpp中做什么,和/或
  • 如何扩展它以返回矩阵列表,而不只是一个矩阵。

  • 编辑

    原始的r代码包含嵌套在lapply函数中的mapply函数。我想要一个输出,它是一个列表长度的数字,每个元素都是一个矩阵。此列表中每个矩阵的每一行都是来自不同多元正态分布的关联(因此,在每个列表元素中,对于矩阵的每一行,都有一个唯一的均值 vector 和一个唯一的协方差矩阵来控制我要绘制的多元正态从)。我将其编写为嵌套在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)

    RCPP代码
    #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()中,正如您所说的,它们应该是列表列表。)

    关于c++ - 带有列表参数的RCPP函数返回矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60986805/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com