下载了一个R包,是用R调用C++写成的,也就是用到Rcpp。我想调用其中一个函数fastLasso,然后在此基础上修改。 里面包含了不少.R文件,.cpp文件和.h文件。也就是说同名的有fastLasso.R, fastLasso.cpp以及fastLasso.h。其中fastLasso.cpp和 fastLasso.h在一个文件夹中。
我试着调用其中的C++函数fastLasso.cpp。
我安装了Rcpp,Rtools,RcppArmadillo。
用sourceCpp(fastLasso.cpp),但是出了以下错误:
(前面的两行g++省略)
fastLasso.o:fastLasso.cpp:(.text+0x1689): undefined reference to `seqLen(unsigned int const&)'
fastLasso.o:fastLasso.cpp:(.text+0x2cfa): undefined reference to `sign(double const&)'
collect2: ld returned 1 exit status
Error in sourceCpp("fastLasso.cpp") :
Error occurred building shared library.
In addition: Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
path[1]="G:/0notesun1412/robust HD/robustHD/src/../inst/include": 系统找不到指定的文件。
下面是fastLasso.cpp,由于程序太长,我只贴前面部分和后面的R interface:
#include "fastLasso.h"
using namespace Rcpp;
using namespace arma;
// find maximum number of active variables
// n .............. number of observations
// p .............. number of predictors
// useIntercept ... logical indicating whether model has an intercept
uword findMaxActive(const uword& n, const uword& p, const bool& useIntercept) {
uword maxActive = n - useIntercept;
if(p < maxActive) {
maxActive = p;
}
return maxActive;
}
// compute step size in the direction of the equiangular vector
// corActiveY.. ... correlations of active variables with current response
// corInactiveY ... correlations of inactive variables with current response
// corActiveU ..... correlations of active variables with equiangular vector
// corInactiveU ... correlations of inactive variables with equiangular vector
// eps ............ small numerical value (effective zero)
double findStep(const double& corActiveY, const vec& corInactiveY,
const double& corActiveU, const vec& corInactiveU,
const double& eps) {
// construct vector of all values to consider
vec steps = join_cols((corActiveY - corInactiveY)/(corActiveU - corInactiveU),
(corActiveY + corInactiveY)/(corActiveU + corInactiveU));
steps = steps.elem(find(steps > eps));
// find and return step size
double step = corActiveY/corActiveU; // maximum possible step;
if(steps.n_elem > 0) {
double smallestPositive = steps.min(); // smallest positive value
if(smallestPositive < step) {
step = smallestPositive;
}
}
return step;
}
省略……
// R interface to fastLasso()
SEXP R_fastLasso(SEXP R_x, SEXP R_y, SEXP R_lambda, SEXP R_useSubset,
SEXP R_subset, SEXP R_normalize, SEXP R_intercept, SEXP R_eps,
SEXP R_useGram) {
// data initializations
NumericMatrix Rcpp_x(R_x); // predictor matrix
const int n = Rcpp_x.nrow(), p = Rcpp_x.ncol();
mat x(Rcpp_x.begin(), n, p, false); // reuse memory
NumericVector Rcpp_y(R_y); // response
vec y(Rcpp_y.begin(), n, false); // reuse memory
double lambda = as<double>(R_lambda);
bool useSubset = as<bool>(R_useSubset);
uvec subset;
if(useSubset) {
IntegerVector Rcpp_subset(R_subset); // subset to use for computation
const int h = Rcpp_subset.size();
subset = uvec(h);
for(int i = 0; i < h; i++) {
// can't use the same memory-saving conversion for integer vectors
subset(i) = Rcpp_subset - 1;
}
}
bool normalize = as<bool>(R_normalize);
bool useIntercept = as<bool>(R_intercept);
double eps = as<double>(R_eps);
bool useGram = as<bool>(R_useGram);
// call native C++ function and return results as list
double intercept, crit;
vec coefficients, residuals;
fastLasso(x, y, lambda, useSubset, subset, normalize, useIntercept, eps,
useGram, false, intercept, coefficients, residuals, crit);
if(useIntercept) {
// prepend intercept
coefficients.insert_rows(0, 1, false);
coefficients(0) = intercept;
}
return List::create(
Named("coefficients") = coefficients,
Named("fitted.values") = y - residuals,
Named("residuals") = residuals
);
}
本人Rcpp是新手,自己看了、试了有一段时间了,还是不得要领。请给予指点,任何的帮助都很感激。