最小二乘的 SparseQR

SparseQR for Least Squares

对于我正在构建的应用程序,我需要对大型数据集进行 运行 线性回归以获得残差。例如,一个数据集的维度超过 100 万 x 20k。对于较小的数据集,我使用 RcppArmadillo 包中的 fastLm - 目前非常适合这些数据集。随着时间的推移,这些数据集也将增长到超过 100 万行。

我的解决方案是使用稀疏矩阵和 Eigen。我找不到在 RcppEigen 中使用 SparseQR 的好例子。基于许多小时的阅读(例如 rcpp-gallery, Whosebug, rcpp-dev mailinglist, eigen docs, rcpp-gallery, Whosebug 以及我忘记但肯定已经阅读过的更多内容),我编写了以下代码;

(注意:我的第一个 C++ 程序 - 请保持友好 :) - 欢迎任何改进建议)

// [[Rcpp::depends(RcppEigen)]]
#include <RcppEigen.h>
using namespace Rcpp;
using namespace Eigen;

using Eigen::Map;
using Eigen::SparseMatrix;
using Eigen::MappedSparseMatrix;
using Eigen::VectorXd;
using Eigen::SimplicialCholesky;


// [[Rcpp::export]]
List sparseLm_eigen(const SEXP Xr, 
                    const NumericVector yr){

  typedef SparseMatrix<double>        sp_mat;
  typedef MappedSparseMatrix<double>  sp_matM;
  typedef Map<VectorXd>               vecM;
  typedef SimplicialCholesky<sp_mat>  solver;

  const sp_mat Xt(Rcpp::as<sp_matM>(Xr).adjoint());
  const VectorXd Xty(Xt * Rcpp::as<vecM>(yr));
  const solver Ch(Xt * Xt.adjoint());

  if(Ch.info() != Eigen::Success) return "failed";

  return List::create(Named("betahat") = Ch.solve(Xty));
}

这适用于例如;

library(Matrix)
library(speedglm)
Rcpp::sourceCpp("sparseLm_eigen.cpp")

data("data1")
data1$fat1 <- factor(data1$fat1)
mm <- model.matrix(formula("y ~ fat1 + x1 + x2"), dat = data1)

sp_mm <- as(mm, "dgCMatrix")
y <- data1$y

res1 <- sparseLm_eigen(sp_mm, y)$betahat
res2 <- unname(coefficients(lm.fit(mm, y)))

abs(res1 - res2)

然而,对于我的大型数据集,它失败了(正如我所预料的那样)。我最初的意图是使用 SparseQR 作为求解器,但我不知道如何实现它。

所以我的问题 - 谁能帮我用 RcppEigen 实现稀疏矩阵的 QR 分解?

如何使用 Eigen is a bit generic. This is mainly because the sparse solver classes are designed superbly well. They provide a guide explaining their sparse solver classes. Since the question focuses on SparseQR, the documentation indicates that there are two parameters required to initialize the solver: SparseMatrix class type and OrderingMethods class 编写稀疏求解器,它规定了支持的填充减少排序方法。

考虑到这一点,我们可以提出以下建议:

// [[Rcpp::depends(RcppEigen)]]
#include <RcppEigen.h>
#include <Eigen/SparseQR>

// [[Rcpp::export]]
Rcpp::List sparseLm_eigen(const Eigen::MappedSparseMatrix<double> A, 
                          const Eigen::Map<Eigen::VectorXd> b){

  Eigen::SparseQR <Eigen::MappedSparseMatrix<double>, Eigen::COLAMDOrdering<int> > solver;
  solver.compute(A);
  if(solver.info() != Eigen::Success) {
    // decomposition failed
    return Rcpp::List::create(Rcpp::Named("status") = false);
  }
  Eigen::VectorXd x = solver.solve(b);
  if(solver.info() != Eigen::Success) {
    // solving failed
    return Rcpp::List::create(Rcpp::Named("status") = false);
  }

  return Rcpp::List::create(Rcpp::Named("status") = true,
                            Rcpp::Named("betahat") = x);
}

注意:这里我们创建了一个列表,它总是传递一个命名的 status 变量,应该首先检查 。这表明收敛是否发生在两个区域:分解和求解。如果全部检查出来,那么我们传递 betahat 系数。


测试脚本:

library(Matrix)
library(speedglm)
Rcpp::sourceCpp("sparseLm_eigen.cpp")

data("data1")
data1$fat1 <- factor(data1$fat1)
mm <- model.matrix(formula("y ~ fat1 + x1 + x2"), dat = data1)

sp_mm <- as(mm, "dgCMatrix")
y <- data1$y

res1 <- sparseLm_eigen(sp_mm, y)
if(res1$status != TRUE){
    stop("convergence issue")
}
res1_coef = res1$betahat
res2_coef <- unname(coefficients(lm.fit(mm, y)))

cbind(res1_coef, res2_coef)

输出:

        res1_coef    res2_coef
[1,]  1.027742926  1.027742926
[2,]  0.142334262  0.142334262
[3,]  0.044327457  0.044327457
[4,]  0.338274783  0.338274783
[5,] -0.001740012 -0.001740012
[6,]  0.046558506  0.046558506