Blog Archives

Vector Subsetting in Rcpp

March 15, 2014
By
Vector Subsetting in Rcpp

Rcpp 0.11.1 has introduced flexible subsetting for Rcpp vectors. Subsetting is implemented for the Rcpp vector types through the [ operator, and intends to mimic R’s [ operator for most cases. We diverge from R’s subsetting semantics in a few important ways: For integer and numeric vectors, 0-based indexing is performed, rather than 1-based indexing, for subsets. ...

Read more »

Condorcet Voting with Rcpp

March 13, 2014
By
Condorcet Voting with Rcpp

There is a lot of literature and debate on how to rank candidates under preferential voting systems. Two of the methods used to determine winners are those based on some form of Borda count and those based on some form of Condorcet method. Many students of politics and voting systems prefer Condorcet methods to Borda ones for its stronger theoretical qualities. However, while...

Read more »

Convex Hull of Polygon using Boost.Geometry

February 23, 2014
By
Convex Hull of Polygon using Boost.Geometry

Rcpp can be used to convert basic R data types to and from Boost.Geometry models. In this example we take a matrix of 2d-points and convert it into a Boost.Geometry polygon. We then compute the convex hull of this polygon using a Boost.Geometry function boost::geometry::convex_hull. The convex hull is then converted back to an R matrix. The conversions to and...

Read more »

Detecting a Time Series Change Point

January 4, 2014
By
Detecting a Time Series Change Point

In this example we will detect the change point in a time series of counts using Bayesian methodology. A natural solution to this problem utilizes a Gibbs sampler. We’ll first implement the sampler in R naively, then create a vectorized R implementation, and lastly create an implementation of the sampler using Rcpp and RcppArmadillo. We will compare these implementations...

Read more »

Performance of the divide-and-conquer SVD algorithm

December 9, 2013
By
Performance of the divide-and-conquer SVD algorithm

The ubiquitous LAPACK library provides several implementations for the singular-value decomposition (SVD). We will illustrate possible speed gains from using the divide-and-conquer method by comparing it to the base case. #include <RcppArmadillo.h> // ] // ] arma::vec baseSVD(const arma::mat & X) { arma::mat U, V; arma::vec S; arma::svd(U, S, V, X, "standard"); ...

Read more »

Converting C code to C++ code: An example from plyr

December 2, 2013
By
Converting C code to C++ code: An example from plyr

The plyr package uses a couple of small C functions to optimise a number of particularly bad bottlenecks. Recently, two functions were converted to C++. This was mostly stimulated by a segmentation fault caused by some large inputs to the split_indices() function: rather than figuring out exactly what was going wrong with the complicated C code, it was...

Read more »

Munkres’ Assignment Algorithm with RcppArmadillo

September 24, 2013
By
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 permutation Gibbs sampler. For each sample...

Read more »

Getting indices of top elements from a vector using a priority queue

September 12, 2013
By
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),...

Read more »

First Derivative of the Multivariate Normal Densities with RcppArmadillo

September 9, 2013
By
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 particularly because I am not...

Read more »

Creating as and wrap for sparse matrices

August 5, 2013
By
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...

Read more »

Sponsors

Mango solutions



RStudio homepage



Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training



http://www.eoda.de









ODSC

CRC R books series













Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)