[Rcpp-devel] How to pf from Rcpp

Dirk Eddelbuettel edd at debian.org
Sat Nov 12 22:41:57 CET 2011


On 12 November 2011 at 12:47, Hao Xiong wrote:
| Thank you, Dirk and Douglas. You both provided the answers I need.
| 
| On 11/12/2011 12:05 PM, Dirk Eddelbuettel wrote:
| > 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.
| Can you tell me in which file is pnf() declared? It would help me later 
| when I need
| to look up similar functions for other distributions.

'grep -r' is often my best friend -- but in this case you need to poke by hand

   include/Rcpp/stats/f.h
   include/Rcpp/stats/nf.h

as all these contains (beside the copyright header) are a mere 

  #ifndef Rcpp__stats__f_h
  #define Rcpp__stats__f_h

  RCPP_DPQ_3(f,::Rf_df,::Rf_pf,::Rf_qf)

  #endif

and 

  #ifndef Rcpp__stats__nf_h
  #define Rcpp__stats__nf_h

  RCPP_DPQ_3(nf,::Rf_dnf,::Rf_pnf,::Rf_qnf)

  #endif

which you need strong glasses to read -- basically a 'expand this for us'
factory-style macro is invoked to provide d-p-q variants of f and nf,
respectively, by unrolling the vector elements of the first arguments on the
corresponding Rf_{d,p,n}* functions.
 
As I said, a tad underdocumented. And mostly magic and pixie dust.

| Thanks for answering my question. Rcpp has been a God-sent for me.

Doing MCMC or other Monte Carlo, eh?  ;-)

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