Thursday, 15 July 2010

r - Rcpp quantile implementation -



r - Rcpp quantile implementation -

i have rcpp double containing numericvector x. .95 quantile of such x within rcpp code flow. not know how can it. there rcpparmadillo implementation?

i'm not rcpp expert , function needs lots of improvement, seems can create own rcpp quantile function (which not accurate on little vectors due high chance skewness , problems indexing on non-integer indexes, improves vector grows)

library(rcpp) # can utilize sourcecpp() instead of cppfunction if wish cppfunction('numericvector cquantile(numericvector x, numericvector q) { numericvector y = clone(x); std::sort(y.begin(), y.end()); homecoming y[x.size()*(q - 0.000000001)]; }')

testing on 1e+3 vector

set.seed(123) y <- rnorm(1000) quantile(y) # 0% 25% 50% 75% 100% # -2.809774679 -0.628324243 0.009209639 0.664601867 3.241039935 setnames(sapply(indx, function(x) cquantile(y, x)), paste0(indx * 100, "%")) # 0% 25% 50% 75% 100% # -2.80977468 -0.62957874 0.00729009 0.66441586 3.24103993

looks close enough. 95% quantile our vector y be

cquantile(y, .95) ## [1] 1.675697

there sugar quantile functions knows distributions such normal, gamma, etc. can used instead, see here examples.

r rcpp

No comments:

Post a Comment