c++ - 如何正确使用 Rcpp::pt( )

标签 c++ r rcpp

我正在阅读“与 Rcpp 无缝集成 R 和 C++”的第 4 章,我遇到了一个小问题。

在“ list 4.13”这本书中给出了一个关于如何使用 R 函数的示例。我尝试使用其他函数(与示例不同)并且我成功了。我的代码在这里:

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::DataFrame myrandom(Rcpp::NumericVector x) {
  int n = x.size();
  Rcpp::NumericVector y1(n), y2(n), y3(n);

  y1 = Rcpp::pexp(x,1.0,1,0);
  y2 = Rcpp::pnorm(x,0.0,1.0,1,0);
  y3 = Rcpp::ppois(x,3.0,1,0);

  return Rcpp::DataFrame::create(Rcpp::Named("Exp") = y1,Rcpp::Named("Norm") = y2, Rcpp::Named("Pois") = y3);
}


sourceCpp("random.cpp")
myrandom(c(0.5,1))

在这种情况下没问题,但是当我尝试使用 Rcpp::pt 时,我没有成功。我的代码在这里。

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::DataFrame myrandom2(Rcpp::NumericVector x) {
  int n = x.size();
  Rcpp::NumericVector y1(n);

  y1 = Rcpp::pt(x,3.0,0,1,0);

  return Rcpp::DataFrame::create(Rcpp::Named("T") = y1);
}

sourceCpp("random2.cpp")
myrandom2(c(0.5,1))

/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/Rcpp/stats/nt.h: In function ‘Rcpp::stats::P2<RTYPE, NA, T> Rcpp::pt(const Rcpp::VectorBase<RTYPE, NA, VECTOR>&, double, double, bool, bool) [with int RTYPE = 14, bool NA = true, T = Rcpp::Vector<14>]’:
random2.cpp:8:   instantiated from here
/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/Rcpp/stats/nt.h:25: error: invalid conversion from ‘double (*)(double, double, int, int)’ to ‘double (*)(double, double, double, int, int)’
/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/Rcpp/stats/nt.h:25: error:   initializing argument 1 of ‘Rcpp::stats::P2<RTYPE, NA, T>::P2(double (*)(double, double, double, int, int), const Rcpp::VectorBase<RTYPE, NA, VECTOR>&, double, double, bool, bool) [with int RTYPE = 14, bool NA = true, T = Rcpp::Vector<14>]’
make: *** [random2.o] Error 1
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include  -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include"    -fPIC  -mtune=core2 -g -O2  -c random2.cpp -o random2.o 

当我使用 Rcpp::pt(x,3) 时,我想控制函数的 de 参数

pt(q, df, ncp, lower.tail = TRUE, log.p = FALSE)

我认为我没有正确使用 de function 但我不知道是什么。

最佳答案

C++ 编译器消息不是很可爱吗? ;-)

我认为您刚刚帮助解决了一个非常老的错误。对于 t 和 F,R 实际上有两个 函数组 (r|p|q|d)t 和 (r|p|q|d)nt(f 和 nf 同上)其中第二种形式 允许非中心参数。我认为 nt.h 的标题是错误的。

如果您在文件 include/Rcpp/stats/nt.h 中进行此更改

@@ -22,7 +22,7 @@
 #ifndef Rcpp__stats__nt_h
 #define Rcpp__stats__nt_h

-RCPP_DPQ_2(t,::Rf_dt,::Rf_pt,::Rf_qt)
+RCPP_DPQ_2(nt,::Rf_dnt,::Rf_pnt,::Rf_qnt)

 #endif

(您只需在标题中进行编辑,无需重新安装)然后一切似乎都有效:

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::DataFrame myrandom(Rcpp::NumericVector x) {
  Rcpp::NumericVector y1, y2, y3, y4, y5, y6, y7, y8;

  y1 = Rcpp::pexp(x,1.0,1,0);
  y2 = Rcpp::pnorm(x,0.0,1.0,1,0);
  y3 = Rcpp::ppois(x,3.0,1,0);
  y4 = Rcpp::pt(x,3.0,1,0);
  y5 = Rcpp::pnt(x,3.0,2,TRUE,FALSE);
  y6 = Rcpp::pnt(x,3.0,2,TRUE,TRUE);
  y7 = Rcpp::pnt(x,3.0,2,FALSE,TRUE);
  y8 = Rcpp::pnt(x,3.0,2,FALSE,FALSE);

  return Rcpp::DataFrame::create(Rcpp::Named("Exp") = y1,
                                 Rcpp::Named("Norm") = y2, 
                                 Rcpp::Named("Pois") = y3,
                                 Rcpp::Named("t") = y4,
                                 Rcpp::Named("nt1") = y5,
                                 Rcpp::Named("nt2") = y6,
                                 Rcpp::Named("nt3") = y7,
                                 Rcpp::Named("nt4") = y8);
}

我仍然需要根据 R 检查数字,但至少它现在可以构建了。 (TRUE/FALSE 与 1/0 无关紧要;正确转换)。

编辑:这里是一些输出:

> sourceCpp("/tmp/so1.cpp")  
R> myrandom(seq(0.2, 0.5, by=0.1))  
       Exp     Norm      Pois        t       nt1      nt2        nt3      nt4   
1 0.181269 0.579260 0.0497871 0.572865 0.0351335 -3.34860 -0.0357655 0.964866   
2 0.259182 0.617911 0.0497871 0.608118 0.0434710 -3.13566 -0.0444441 0.956529  
3 0.329680 0.655422 0.0497871 0.642032 0.0535219 -2.92767 -0.0550074 0.946478  
4 0.393469 0.691462 0.0497871 0.674276 0.0654790 -2.72603 -0.0677212 0.934521   
R>   

关于c++ - 如何正确使用 Rcpp::pt( ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20144528/

相关文章:

c++ - 无法使用 wxsmith 获取指向 wxwidget 对象的指针

c++ - 使用 UNC 路径读取/写入文件 - 在 C++ 中

r - 从 {AER} 的 ivreg() 获取第一阶段结果

r - 具有 rcpp 和 RcppArmadillo 的多元正态/高斯的一阶导数

rcpp - 了解 `Makevars` 以链接到 R 包中的外部 C 库

c++ - 对从命令行获取文件名并将其填充到 C++ 感到困惑

c++ - 无法在 OpenCV 中读取视频文件

r - ggplot2 0.9.0 自动从图例中删除未使用的因子水平?

r - R::mgcv 中张量相互作用的方差分量

R 使用 data.table 计算依赖于前一行的列