[Rcpp-devel] more on stats functions

Christian Gunning xian at unm.edu
Wed Dec 8 05:25:17 CET 2010


> Your contributions are useful. It helps us understanding how people from
> outside the team use Rcpp.

Thanks.  As per Dirk's comment, I went back to the unit tests and will
use those as guidance for quickref.

The problem I got stuck on looks to be the use of int versus double
values for dnorm's sd argument, due to argument ambiguity:

This works (copied from unitTests/runit.stats.R):

dtest1 = cxxfunction(signature( x = "numeric" ),
          'NumericVector xx(x);
           return( xx = dnorm( xx, 0.0, 1.0));'
 ,plugin='Rcpp')

This doesn't compile (copied from Rcpp-sugar.pdf):

dtest2 = cxxfunction(signature( x = "numeric" ),
          'NumericVector xx(x);
           return(xx = dnorm( xx, 0, 1));'
 ,plugin='Rcpp')

Compilation ERROR, function(s)/method(s) not created!
file49322701.cpp: In function ‘SEXPREC* file49322701(SEXPREC*)’:
file49322701.cpp:30: error: call of overloaded
‘dnorm4(Rcpp::NumericVector&, int, int)’ is ambiguous
/home/xian/R/x86_64-pc-linux-gnu-library/2.8/Rcpp/include/Rcpp/stats/norm.h:107:
note: candidates are: Rcpp::stats::D1<RTYPE, NA, T> Rcpp::dnorm4(const
Rcpp::VectorBase<RTYPE, NA, VECTOR>&, double, bool) [with int RTYPE =
14, bool NA = true, T = Rcpp::Vector<14>]
/home/xian/R/x86_64-pc-linux-gnu-library/2.8/Rcpp/include/Rcpp/stats/norm.h:108:
note:                 Rcpp::stats::D2<RTYPE, NA, T> Rcpp::dnorm4(const
Rcpp::VectorBase<RTYPE, NA, VECTOR>&, double, double, bool) [with int
RTYPE = 14, bool NA = true, T = Rcpp::Vector<14>]
make: *** [file49322701.o] Error 1

This works:

dtest2a = cxxfunction(signature( x = "numeric" ),
          'NumericVector xx(x);
           return(xx = dnorm( xx, 0, 1, true));'
 ,plugin='Rcpp')

This works, and is equivalent to dnorm(-2:2, 0, log=T) in R:

dtest3 = cxxfunction(signature( x = "numeric" ),
          'NumericVector xx(x);
           return(xx= dnorm( xx, 0, true));'
 ,plugin='Rcpp')
x=-2:2; identical(dtest3(x),  dnorm(x, 0, log=T))

This works, and is equivalent to dnorm(-2:2, 0, log=F) in R:

dtest4 = cxxfunction(signature( x = "numeric" ),
          'NumericVector xx(x);
           return(xx = dnorm( xx, 0, false));'
 ,plugin='Rcpp')
x=-2:2; identical(dtest4(x),  dnorm(x, 0, log=F))

To avoid ambiguity, is it reasonable to recommend that users of the
stats functions provide all scalar arguments as doubles rather than
integers, as is done in the unit tests (but not in Rcpp-sugar.pdf)?
e.g.
  pgamma( xx, 2.0, 1.0 )
vs.
  pgamma( xx, 2, 1 )

If so, I'll also patch Rcpp-sugar.pdf to reflect this.

In addition, some user guidance on what arguments are required for the
stats functions should cut down on questions in the future (esp. since
template compile errors can be daunting). For rnorm, all 3 of these
work:

rtest = cxxfunction(,
          'NumericVector xx = rnorm( 5);
           xx = rnorm( 5, 0.0);
           return(xx = rnorm( 5, 0.0, 1.0));'
 ,plugin='Rcpp')

With dnorm, a smaller subset work (commented don't compile):

dtest5 = cxxfunction(signature( x = "numeric" ),
          'NumericVector xx(x), yy(x);
           //yy = dnorm( xx);
           yy = dnorm( xx, 0.0);
           yy = dnorm( xx, 0.0, 1.0);
           yy = dnorm( xx, 0.0, 1.0, true);
           yy = dnorm( xx, 0.0, true);
           //yy = dnorm( xx, true);
           return(yy);'
 ,plugin='Rcpp')

best,
xian

-- 
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!


More information about the Rcpp-devel mailing list