在Rcpp中使用fisher.test中的big.matrix列

时间:2018-04-22 13:00:22

标签: r rcpp r-bigmemory

我有一个非常大的二进制big.matrix,也是一个类赋值向量(长度与big.matrix的行数相同)。

我希望能够循环遍历big.matrix的每一列,并为每个fisher.test输出p值。

使用普通的矩阵对象,我可以执行以下操作,但将我的big.matrix转换为矩阵需要超过5 GB的ram。

      p.value <-
        unlist(lapply(lapply(
          as.data.table(binarymatrix),
          fisher.test,
          y = class
        ), function(x)
          x$p.value))

如何在不转换为矩阵对象的情况下执行此操作?据我了解,访问big.matrix的元素需要C ++代码,但我对此并不熟悉。

这里显示了如何在Rcpp Rcpp: Is there an implementation fisher.test() in Rcpp中进行fisher.test,但我不知道如何将矩阵的每一列输入到此中。

示例big.matrix看起来像

library(bigmemory)
matrix <- matrix(sample(0:1, 100 * 10000, replace = TRUE), 100 , 10000)
bigmatrix <- as.big.matrix(matrix)

我的类变量看起来像:

class <- sample( LETTERS[1:2], 100, replace=TRUE)

谢谢!

编辑:

这是我现在拥有的Rcpp代码。如果有人能帮我解决问题,我会非常感激。

// [[Rcpp::depends(RcppEigen, RcppArmadillo, bigmemory, BH)]]
#include <RcppArmadillo.h>
#include <RcppEigen.h>
#include <bigmemory/BigMatrix.h>
#include <bigmemory/MatrixAccessor.hpp>

using namespace Rcpp;
using namespace arma;
using namespace Eigen;
using namespace std;

// [[Rcpp::plugins(cpp11)]]

// [[Rcpp::export]]
ListOf<IntegerVector> AccessVector(SEXP pBigMat, int j, vector<int> status) {
  XPtr<BigMatrix> xpMat(pBigMat);
  MatrixAccessor<int> macc(*xpMat);

  int n = xpMat->nrow();

  // Bigmemory
  cout << "Bigmemory:"; 
  for (int i = 0; i < n; i++) {
    cout << macc[j][i] << ' ';
  }
  cout << endl;    

  // STD VECTOR
  vector<int> stdvec(macc[j], macc[j] + n); 

  // Obtain environment containing function
  Rcpp::Environment base("package:stats"); 

  // Make function callable from C++
  Rcpp::Function fisher_test = base["fisher.test"];    

  // Call the function and receive its list output
  Rcpp::List test_out = fisher_test(Rcpp::_["x"] = stdvec, Rcpp::_["y"] = status);

  // Return test object in list structure
  return test_out;
}

理想情况下,我希望能够循环遍历C ++本身的每一列,并将p值输出到R。

0 个答案:

没有答案