使用`sourceCpp`编译`fastLm`

use*_*795 5 r rcpp

我开始玩,Rcpp并希望使用该fastLm函数作为一个例子(也因为它对以后的潜在工作很有用).我知道这fastLmRcppArmadillo包的一部分,但我想用它来编译它sourceCpp.代码可以在这里找到,也在下面.我遇到的第一个问题是,我不能简单地运行sourceCpp("fastLm.cpp")R中安装和加载后RcppRcppArmadillo.我得到了这个错误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
    ) ;

}
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

Rom*_*ois 9

您需要RcppArmadillo使用Rcpp::depends伪属性指示依赖性.这将需要寻找的照顾RcppArmadillo标题和链接反对blas, lapack等...

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

}
Run Code Online (Sandbox Code Playgroud)

此外,您使用#include <RcppArmadillo.h>和非使用非常重要#include <Rcpp.h>.RcppArmadillo.h包括Rcpp.h 在合适的时间,包括文件的顺序在这里非常重要.

此外,你可以退回List并放弃extern "C".