[Rcpp-devel] How to pf from Rcpp

Dirk Eddelbuettel edd at debian.org
Sat Nov 12 22:03:44 CET 2011


Sorry about the typos and grammar. One day I'll learn to proof-read. Edited
version below.

On 12 November 2011 at 14:05, Dirk Eddelbuettel wrote:
| Beside Doug's tip of falling back to the C API of R, we also have "Rcpp sugar"
| variants. However these mimic how the R functions are implemented, rather
| than micking the API they provide --- for example '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 thing is that the sugar versions are vectorised. The not so nice thing
| is that you _must_ supply the extra arguments for lower tail and log.

What I did here is what I'd recommend. Set up a working example in R, then
maybe expand the arguments:

R> pf(1:3, 6, 8, ncp=0, lower=1, log=0)
[1] 0.514760 0.820800 0.923255
R> 

and given that, look into (unfortunately somewhat underdocumented) Rcpp sugar
functions for statistics.

Dirk

-- 
"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