为什么在使用"pnorm"时会收到错误消息?在Rcpp中 [英] Why do I get the error for using "pnorm" in Rcpp

查看:81
本文介绍了为什么在使用"pnorm"时会收到错误消息?在Rcpp中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的Rcpp代码中包含来自 arma :: 的变量.但是在尝试使用Sugar函数 pnorm 时遇到了一个问题.这是一个演示:

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:

#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;

// [[Rcpp::export]]
double pget(NumericVector x, NumericVector beta) {
  arma::colvec xx = Rcpp::as<arma::colvec>(x) ;
  arma::colvec bb = Rcpp::as<arma::colvec>(beta) ;
  double tt = as_scalar( arma::trans(xx) * bb);
  double temp = Rcpp::pnorm(tt);
  return temp;
}

然后我得到一个错误:没有匹配的函数来调用'pnorm5'

Then I got an error: no matching function for call to 'pnorm5'

这是否意味着我不能使用 Rcpp :: pnorm ???

Does that mean I cannot use Rcpp::pnorm???

推荐答案

Rcpp 糖函数用于矢量类型参数,例如 Rcpp :: NumericVector .对于标量参数,可以使用 R 命名空间中的函数:

The Rcpp sugar functions are meant for vector type arguments like Rcpp::NumericVector. For scalar arguments you can use the functions in the R namespace:

#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;

// [[Rcpp::export]]
double pget(NumericVector x, NumericVector beta) {
  arma::colvec xx = Rcpp::as<arma::colvec>(x) ;
  arma::colvec bb = Rcpp::as<arma::colvec>(beta) ;
  double tt = as_scalar( arma::trans(xx) * bb);
  double temp = R::pnorm(tt, 0.0, 1.0, 1, 0);
  return temp;
}

/*** R
x <- rnorm(5)
beta <- rnorm(5)
pget(x, beta)
*/

顺便说一句,这里有两个变体.第一个变体使用 arma 而不是 Rcpp 向量作为参数.由于这些是 const 引用,因此不会复制任何数据.此外,还使用了 arma :: dot :

BTW, here two variants. First variant uses arma instead of Rcpp vectors as arguments. Since these are const references, no data is copied. In addition, arma::dot is used:

// [[Rcpp::export]]
double pget2(const arma::colvec& xx, const arma::colvec& bb) {
  double tt = arma::dot(xx, bb);
  return R::pnorm(tt, 0.0, 1.0, 1, 0);
}

第二个变量无需借助Armadillo即可计算标量积:

The second variant calculates the scalar product without resorting to Armadillo:

// [[Rcpp::export]]
double pget3(NumericVector x, NumericVector beta) {
  double tt = Rcpp::sum(x * beta);
  return R::pnorm(tt, 0.0, 1.0, 1, 0);
}

这篇关于为什么在使用"pnorm"时会收到错误消息?在Rcpp中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆