使用`sourceCpp`编译`fastLm`

时间:2012-12-03 06:14:42

标签: r rcpp

我开始使用Rcpp,并希望使用fastLm函数作为示例(也因为它对以后的潜在工作很有用)。我知道fastLmRcppArmadillo包的一部分,但我想使用sourceCpp进行编译。代码可以在here找到,也在下面。 我遇到的第一个问题是,在安装并加载sourceCpp("fastLm.cpp")Rcpp后,我无法在R中运行RcppArmadillo。我得到了这个错误error: RcppArmadillo.h: No such file or directory,然后是所有类型的东西,我想从那里得到。

第二个问题是我认为我需要更改fastLm.cpp中的某些内容。我的变化也在下面,但我确信缺少或错误。我添加了#include <Rcpp.h>using namespace Rcpp;以及// [[Rcpp::export]]将函数导出到R,我将参数从SEXP更改为NumericVectorNumericMatrix。我不明白为什么不起作用,返回值可能有类似的调整?

fastLm.cpp

#include <RcppArmadillo.h>

extern "C" SEXP fastLm(SEXP ys, SEXP Xs) {

    Rcpp::NumericVector yr(ys);                 // creates Rcpp vector from SEXP
    Rcpp::NumericMatrix Xr(Xs);                 // creates Rcpp matrix from SEXP
    int n = Xr.nrow(), k = Xr.ncol();

    arma::mat X(Xr.begin(), n, k, false);       // reuses memory and avoids extra copy
    arma::colvec y(yr.begin(), yr.size(), false);

    arma::colvec coef = arma::solve(X, y);      // fit model y ~ X
    arma::colvec resid = y - X*coef;            // residuals

    double sig2 = arma::as_scalar( arma::trans(resid)*resid/(n-k) );
                                                // std.error of estimate
    arma::colvec stderrest = arma::sqrt( sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );

    return Rcpp::List::create(
        Rcpp::Named("coefficients") = coef,
        Rcpp::Named("stderr")       = stderrest
    ) ;

}

fastLm.cpp已更改

#include <Rcpp.h>
#include <RcppArmadillo.h>
using namespace Rcpp;

// [[Rcpp::export]]
extern "C" SEXP fastLm(NumericVector yr, NumericMatrix Xr) {

    int n = Xr.nrow(), k = Xr.ncol();

    arma::mat X(Xr.begin(), n, k, false);       // reuses memory and avoids extra copy
    arma::colvec y(yr.begin(), yr.size(), false);

    arma::colvec coef = arma::solve(X, y);      // fit model y ~ X
    arma::colvec resid = y - X*coef;            // residuals

    double sig2 = arma::as_scalar( arma::trans(resid)*resid/(n-k) );
                                                // std.error of estimate
    arma::colvec stderrest = arma::sqrt( sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );

    return Rcpp::List::create(
        Rcpp::Named("coefficients") = coef,
        Rcpp::Named("stderr")       = stderrest
    ) ;

}

1 个答案:

答案 0 :(得分:9)

您需要使用RcppArmadillo伪属性指示对Rcpp::depends的依赖性。这将负责查找RcppArmadillo标题并链接到blaslapack等...

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;

// [[Rcpp::export]]
List fastLm(NumericVector yr, NumericMatrix Xr) {

    int n = Xr.nrow(), k = Xr.ncol();

    arma::mat X(Xr.begin(), n, k, false);       // reuses memory and avoids extra copy
    arma::colvec y(yr.begin(), yr.size(), false);

    arma::colvec coef = arma::solve(X, y);      // fit model y ~ X
    arma::colvec resid = y - X*coef;            // residuals

    double sig2 = arma::as_scalar( arma::trans(resid)*resid/(n-k) );
                                                // std.error of estimate
    arma::colvec stderrest = arma::sqrt( sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );

    return Rcpp::List::create(
        Rcpp::Named("coefficients") = coef,
        Rcpp::Named("stderr")       = stderrest
    ) ;

}

此外,使用#include <RcppArmadillo.h>而非#include <Rcpp.h>非常重要。 RcppArmadillo.h负责在适当的时间包含Rcpp.h ,包含文件的顺序在这里非常重要。

此外,您可以返回List并放弃extern "C"

相关问题