Why do I get the error for using “pnorm” in Rcpp

前端 未结 2 1632
小鲜肉
小鲜肉 2021-01-21 18:27

I need to involve variable from arma::in my Rcpp code. But I ran into a problem when trying to use the sugar function pnorm. Here is a demo:

         


        
2条回答
  •  逝去的感伤
    2021-01-21 18:59

    I'm much less of an expert than @RalfStubner at Rcpp, so I had to hack around (with help from StackOverflow and the Rcpp cheat sheat) to get the following code. Instead of using the R-namespace versions on scalars, I converted back to a NumericVector ... this can almost certainly be done more efficiently/skipping a few steps by someone who actually knows what they're doing ... e.g. it's possible that the arma-to-NumericVector conversion could be done directly without going through as_scalar ... ?

    #include 
    #include 
    #include 
    
    // [[Rcpp::depends(RcppArmadillo)]]
    using namespace Rcpp;
    using namespace arma;
    
    // [[Rcpp::export]]
    NumericVector pget(NumericVector x, NumericVector beta) {
      colvec xx = as(x) ;
      colvec bb = as(beta) ;
      double tt = as_scalar(trans(xx) * bb);
      NumericVector tt2 = NumericVector::create( tt );
      NumericVector temp = Rcpp::pnorm(tt2);
      return temp;
    }
    

提交回复
热议问题