[Rcpp-devel] How to pf from Rcpp

Dirk Eddelbuettel edd at debian.org
Sat Nov 12 21:05:46 CET 2011


On 12 November 2011 at 08:58, Douglas Bates wrote:
| Check the declarations in the include file
| 
| Rmath.h
| 
| The function must be called as Rf_pf in Rcpp and has the signature
| 
| double Rf_pf(double x, double df1, double df2, int lower_tail, int log_p)
| 
| On Sat, Nov 12, 2011 at 1:09 AM, Hao Xiong <hao at biostat.ucsf.edu> wrote:
| > Hi,
| >
| > I need to use pf() from Rcpp but I couldn't seem to find out how.
| >
| > For example, the code looks like this:
| >
| > #include <Rcpp.h>
| > ...
| >  Rcpp::pf(1.2, 5, 2);
| > ...
| > The compiler couldn't find pf(). I also tried all arguments as doubles,
| > but no go.
| >
| > I also would like to computer the upper tail probability but again
| > I don't know the exact function signature, so I plan to just use 1-pf(...).

Beside Doug's tip of falling back to the C API of R, we also have "R sugar"
variants. However these mimic how the R functions are implemented, rather
than than the APIthey provide --- and more important 'pf' actually becomes
'pnf' because what we usually want as 'f' is the non-central f, ie f with
argument ncp=0:


R> suppressMessages(library(inline))
R> suppressMessages(library(Rcpp))
R> 
R> fun <- cxxfunction(signature(x_="numeric", df_="numeric"), body='
+    NumericVector x(x_);
+    NumericVector df(df_);
+    NumericVector res = pnf(x, df(0), df(1), 0, 1);
+    return(wrap(res));
+ ',plugin="Rcpp")
R> 
R> 
R> fun(1:3, c(6,8))                ## our function
[1] 0.514760 0.820800 0.923255
R> 
R> pf(1:3, 6, 8)                   ## equivalent R call
[1] 0.514760 0.820800 0.923255
R> 
R> pf(1:3, 6, 8, 0)                ## idem with ncp=0
[1] 0.514760 0.820800 0.923255
R> 

The nice things is that the sugar version vectorised. The not so nice thing
is that you _must_ supply the extra arguments for lower tail and log.

Dirk


| >
| > Thanks for your help.
| > Hao
| > _______________________________________________
| > Rcpp-devel mailing list
| > Rcpp-devel at lists.r-forge.r-project.org
| > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| >
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list