Publications by Rcpp Gallery
Using the RcppArmadillo-based Implementation of R’s sample()
Overview and Motivation All of R’s (r*, p*, q*, d*) distribution functions are available in C++ via the R API. R is written in C, and the R API has no concept of a vector (at least not in the STL sense). Consequently, R’s sample() function can’t just be exported via the R API, despite its importance and usefulness. The purpose of RcppArmadi...
3656 sym R (3900 sym/7 pcs) 2 img
An accept-reject sampler using RcppArmadillo::sample()
The recently added RcppArmadillo::sample() functionality provides the same algorithm used in R’s sample() to Rcpp-level code. Because R’s own sample() is written in C with minimal work done in R, writing a wrapper around RcppArmadillo::sample() to then call in R won’t get you much of a performance boost. However, if you need to repeatedly c...
2242 sym R (1668 sym/4 pcs) 2 img
Using RcppProgress to control the long computations in C++
Usually you write c++ code with R when you want to speedup some calculations. Depending on the parameters, and especially during the development, it is difficult to anticipate the execution time of your computation, so that you do not know if you have to wait for 1 minute or hours. RcppProgress is a tool to help you monitor the execution time of ...
2291 sym R (4017 sym/8 pcs) 2 img
Sobol Sensitivity Analysis
Sensitivity analysis is the task of evaluating the sensitivity of a model output Y to input variables (X1,…,Xp). Quite often, it is assumed that this output is related to the input through a known function f :Y= f(X1,…,Xp). Sobol indices are generalizing the coefficient of the coefficient of determination in regression. The ith first order in...
3288 sym R (2897 sym/3 pcs) 2 img
Faster Multivariate Normal densities with RcppArmadillo and OpenMP
The Multivariate Normal density function is used frequently in a number of problems. Especially for MCMC problems, fast evaluation is important. Multivariate Normal Likelihoods, Priors and mixtures of Multivariate Normals require numerous evaluations, thus speed of computation is vital. We show a twofold increase in speed by using RcppArmadillo, ...
1872 sym R (3033 sym/8 pcs) 2 img
Gibbs Sampler in C++
Markov Chain Monte Carlo (MCMC) is a popular simulation method. As it is somewhat demanding, it is also frequently used to benchmark different implementations or algorithms. One particular algorithm has been compared a number of times, starting with an article by Darren Wilkinson, and Darren’s follow–up article which in turns responded in par...
1864 sym R (1429 sym/3 pcs) 2 img
Creating as and wrap for sparse matrices
An earlier article discussed sparse matrix conversion but stopped short of showing how to create custom as<>() and wrap() methods or functions. This post starts to close this gap. We will again look at sparse matrices from the Matrix package for R, as well as the SpMat class from Armadillo.At least for now we will limit outselves to the case of d...
1518 sym R (4355 sym/4 pcs) 2 img
First Derivative of the Multivariate Normal Densities with RcppArmadillo
There is a great RcppArmadillo implementation of multivariate normal densities. But I was looking for the first derivative of the multivariate normal densities. Good implementations are surprisingly hard to come by. I wasn’t able to find any online and my first solutions in R were pretty slow. RcppArmadillo might be a great alternative particul...
2363 sym R (2604 sym/3 pcs) 2 img
Getting indices of top elements from a vector using a priority queue
This post is based on a question on Stack Overflow and more precisely on Martin Morgan’s answer. The problem is to find the indices of top n elements from a vector. An inefficient way of doing this is to run order on the vector and then only keep the last n values: top <- function(x, n){ tail( order(x), n ) } This is inefficient because it...
3440 sym R (3771 sym/5 pcs) 2 img
Munkres’ Assignment Algorithm with RcppArmadillo
Munkres’ Assignment Algorithm (Munkres (1957), also known as hungarian algorithm) is a well known algorithm in Operations Research solving the problem to optimally assign N jobs to N workers. I needed to solve the Minimal Assignment Problem for a relabeling algorithm in MCMC sampling for finite mixture distributions, where I use a random permut...
8438 sym R (12870 sym/20 pcs) 2 img