# Blog Archives

## Faster Multivariate Normal densities with RcppArmadillo and OpenMP

July 13, 2013
By

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, and some extra gain by using OpenMP. This project is based...

## Sobol Sensitivity Analysis

June 10, 2013
By

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 indice is the proportion of...

## Using RcppProgress to control the long computations in C++

May 16, 2013
By

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

## An accept-reject sampler using RcppArmadillo::sample()

May 8, 2013
By

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 call sample(), then calling a...

## Using the RcppArmadillo-based Implementation of R’s sample()

April 12, 2013
By

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

## Dynamic Wrapping and Recursion with Rcpp

April 8, 2013
By

We can leverage small parts of the R’s C API in order to infer the type of objects directly at the run-time of a function call, and use this information to dynamically wrap objects as needed. We’ll also present an example of recursing through a list. To get a basic familiarity with the main functions exported from R API, I...

## Using bigmemory with Rcpp

March 14, 2013
By

The bigmemory package allows users to create matrices that are stored on disk, rather than in RAM. When an element is needed, it is read from the disk and cached in RAM. These objects can be much larger than native R matrices. Objects stored as such larger-than-RAM matrices are defined in the big.matrix class and they are designed...

## Generating a multivariate gaussian distribution using RcppArmadillo

March 12, 2013
By

There are many ways to simulate a multivariate gaussian distribution assuming that you can simulate from independent univariate normal distributions. One of the most popular method is based on the Cholesky decomposition. Let’s see how Rcpp and Armadillo perform on this task. #include <RcppArmadillo.h> // ] using namespace Rcpp; // ] arma::mat mvrnormArma(int n, arma::vec mu, arma::mat sigma) { int ncols...

## Using Rcpp with Boost.Regex for regular expression

March 1, 2013
By

Gabor asked about Rcpp use with regular expression libraries. This post shows a very simple example, based onone of the Boost.RegEx examples. We need to set linker options. This can be as simple as Sys.setenv("PKG_LIBS"="-lboost_regex") With that, the following example can be built: // cf www.boost.org/doc/libs/1_53_0/libs/regex/example/snippets/credit_card_example.cpp #include <Rcpp.h> #include <string> #include <boost/regex.hpp> bool validate_card_format(const std::string& s) { static const boost::regex e("(\\d{4}){3}\\d{4}"); ...

## Fast factor generation with Rcpp

February 27, 2013
By

Recall that factors are really just integer vectors with ‘levels’, i.e., character labels that get mapped to each integer in the vector. How can we take an arbitrary character, integer, numeric, or logical vector and coerce it to a factor with Rcpp? It’s actually quite easy with Rcpp sugar: #include <Rcpp.h> using namespace Rcpp; template <int RTYPE> IntegerVector fast_factor_template( const Vector<RTYPE>& x )...