Fitting multievent capture-recapture models with Rcpp

Following my previous post on [using ADMB to fit hidden Markov models](, I took some time to learn how to use Rcpp ([Eddelbuettel & Francois 2011](; [Eddelbuettel 2013](, a package that gives friendly access to the power of C++ and increase the speed of your R programs. Kudos to Dirk Eddelbuettel, Romain Francois and their colleagues, Rcpp is awesome!

I started with the excellent Rcpp chapter in the Advanced R book by Hadley Wickham which I complemented with the various vignettes that come with the package. As always, I googled the problems I had and often ended up finding the solution on stackoverflow. The rcpp-devel discussion list is the place where questions should be asked about Rcpp.

My objective was to implement the likelihood of a relatively simple multievent capture-recapture model ( Pradel 2005) with Rcpp. I recycled some R code I had and a dataset on shearwaters I used in a paper ( Gimenez et al. 2012).

The R code is available on my GitHub here. To run it, you just need to type Rcpp::sourceCpp(‘multi event.cpp’) in the console. I’m convinced that the code can be improved, but this simple exercise showed that minimizing the deviance coded with Rcpp and calculating the Hessian was 10 times faster than using the deviance coded in standard R.

Next steps will be to go for RcppArmadillo for matrix computations and RcppNumerical for optimisation (and numerical integration for random effects).

Hope this is useful.

Update: Following an advice from Romain Francois and Dirk Eddelbuettel (the Rcpp gurus), I have switched to RcppArmadillo to rely on the code developed by professionals and decades of testing. Now the RcppArmadillo code is 50 times faster than basic R! The code is available on my GitHub.

Statistics, ecology & social sciences

I’m a scientist working at the interface of animal ecology, statistical modeling and social sciences.

comments powered by Disqus