Articles by Rcpp Gallery

Stochastic SIR Epidemiological Compartment Model

April 24, 2015 | Rcpp Gallery

Introduction This post is a simple introduction to Rcpp for disease ecologists, epidemiologists, or dynamical systems modelers - the sorts of folks who will benefit from a simple but fully-working example. My intent is to provide a complete, self-contained introduction to modeling with Rcpp. My hope is that this model ...
[Read more...]

Call matplotlib from R

April 1, 2015 | Rcpp Gallery

Motivation I often use Python and matplotlib for exploring measurement data (from e.g. accelerometers), even if I use R for the actual analysis. The reason is that I like to be able to flexibly zoom into different parts of the plot using the mouse and this works well for ... [Read more...]

Parsing Dates and Times

March 21, 2015 | Rcpp Gallery

Motivation R has excellent for dates and times via the built-in Date and POSIXt classes. Their usage, however, is not always as straightforward as one would want. Certain conversions are more cumbersome than we would like: while as.Date("2015-03-22"), would it not be nice if as.Date("20150322") (a ... [Read more...]

Implementing an EM Algorithm for Probit Regressions

September 30, 2014 | Rcpp Gallery

Users new to the Rcpp family of functionality are often impressed with the performance gains that can be realized, but struggle to see how to approach their own computational problems. Many of the most impressive performance gains are demonstrated with seemingly advanced statistical methods, advanced C++–related constructs, or both. ...
[Read more...]

Using RcppArmadillo with bigmemory

July 24, 2014 | Rcpp Gallery

The bigmemory package allows users to create matrices that are external to R, stored either in RAM or on disk, allowing them to be bigger than the system RAM, and allowing them to be shared across R sessions. While these objects are defined by the big.matrix class in R, ...
[Read more...]

Parallel Distance Matrix Calculation with RcppParallel

July 14, 2014 | Rcpp Gallery

The RcppParallel package includes high level functions for doing parallel programming with Rcpp. For example, the parallelFor function can be used to convert the work of a standard serial “for” loop into a parallel one. This article describes using RcppParallel to compute pairwise distances for each row in an input ...
[Read more...]

Computing an Inner Product with RcppParallel

July 14, 2014 | Rcpp Gallery

The RcppParallel package includes high level functions for doing parallel programming with Rcpp. For example, the parallelReduce function can be used aggreggate values from a set of inputs in parallel. This article describes using RcppParallel to parallelize the inner-product example previously posted to the Rcpp Gallery. Serial Version First the ...
[Read more...]

Transforming a Matrix in Parallel using RcppParallel

June 28, 2014 | Rcpp Gallery

The RcppParallel package includes high level functions for doing parallel programming with Rcpp. For example, the parallelFor function can be used to convert the work of a standard serial “for” loop into a parallel one. This article describes using RcppParallel to transform an R matrix in parallel. Serial Version First ...
[Read more...]

Summing a Vector in Parallel with RcppParallel

June 28, 2014 | Rcpp Gallery

The RcppParallel package includes high level functions for doing parallel programming with Rcpp. For example, the parallelReduce function can be used aggreggate values from a set of inputs in parallel. This article describes using RcppParallel to sum an R vector. Serial Version First a serial version of computing the sum ...
[Read more...]

Speed Chain Ladder Analysis with Rcpp

June 23, 2014 | Rcpp Gallery

The Chain Ladder method is an actuarial technique used for projecting incurred insurance claims to their ultimate loss values. The data exists as claims triangles where the claims for each accounting year increments down the rows and the claims for eac...
[Read more...]

Call Python from R through Rcpp

April 5, 2014 | Rcpp Gallery

Introduction This post provides a brief introduction to calling Python from R through Rcpp. The official Python documentation explains how to embed python into C/C++ applications. Moreover, the Boost.Python library provides seamless interoperability between C++ and the Python programming language. Similarlly, Rcpp provides interoperability between C++ and R. ...
[Read more...]

Using iterators for sparse vectors and matrices

March 31, 2014 | Rcpp Gallery

Iterating over a sparse vector Consider the following vector:
idx1 <span><-</span> <span>c</span><span>(</span><span>2L</span><span>,</span> <span>0L</span><span>,</span> <span>4L</span><span>,</span> <span>0L</span><span>,</span> <span>7L</span><span>)</span>
A sparse representation of this vector will tell that at entries 1,3,5 (or at entries 0,2,4 if we are 0-based) we will find the values 2,4,7. Using Eigen via RcppEigen we can obtain the coercion with .sparseView(). We can iterate over all elements (...
[Read more...]

Dynamic dispatch for sparse matrices

March 19, 2014 | Rcpp Gallery

We want to do matrix multiplication for 3 cases: dense times dense sparse times dense for sparse matrices of class dgCMatrix sparse times dense for sparse matrices of class indMatrix, using R’s Matrix package for sparse matrices in R and RcppArmadillo for C++ linear algebra:
<span>// [[Rcpp::depends(RcppArmadillo)]]</span>
<span>#include <RcppArmadillo.h></span>
<span>using</span> <span>namespace</span> <span>Rcpp</span> <span>;</span>

<span>arma</span><span>::</span><span>mat</span> <span>matmult_sp</span><span>(</span><span>const</span> <span>arma</span><span>::</span><span>sp_mat</span> <span>X</span><span>,</span> <span>const</span> <span>arma</span><span>::</span><span>mat</span> <span>Y</span><span>){</span>
    <span>arma</span><span>::</span><span>mat</span> <span>ret</span> <span>=</span> <span>X</span> <span>*</span> <span>Y</span><span>;</span>
    <span>return</span> <span>ret</span><span>;</span>
<span>};</span>
<span>arma</span><span>::</span><span>mat</span> <span>matmult_dense</span><span>(</span><span>const</span> <span>arma</span><span>::</span><span>mat</span> <span>X</span><span>,</span> <span>const</span> <span>arma</span><span>::</span><span>mat</span> <span>Y</span><span>){</span>
    <span>arma</span><span>::</span><span>mat</span> <span>ret</span> <span>=</span> <span>X</span> <span>*</span> <span>Y</span><span>;</span>
    <span>return</span> <span>ret</span><span>;</span>
<span>};</span>
<span>arma</span><span>::</span><span>mat</span> <span>matmult_ind</span><span>(</span><span>const</span> <span>SEXP</span> <span>Xr</span><span>,</span> <span>const</span> <span>arma</span><span>::</span><span>mat</span> <span>Y</span><span>){</span>
    <span>// pre-multiplication with index matrix is a permutation of Y's rows: </span>
    <span>arma</span><span>::</span><span>uvec</span> <span>perm</span> <span>=</span>  <span>as</span><span><</span><span>S4</span><span>></span><span>(</span><span>Xr</span><span>).</span><span>slot</span><span>(</span><span>"perm"</span><span>);</span>
    <span>arma</span><span>::</span><span>mat</span> <span>ret</span> <span>=</span> <span>Y</span><span>.</span><span>rows</span><span>(</span><span>perm</span> <span>-</span> <span>1</span><span>);</span>
    <span>return</span> <span>ret</span><span>;</span>
<span>};</span>

<span>//[[Rcpp::export]]</span>
<span>arma</span><span>::</span><span>mat</span> <span>matmult_cpp</span><span>(</span><span>SEXP</span> <span>Xr</span><span>,</span> <span>const</span> <span>arma</span><span>::</span><span>mat</span> <span>Y</span><span>)</span> <span>{</span>
    <span>if</span> <span>(</span><span>Rf_isS4</span><span>(</span><span>Xr</span><span>))</span> <span>{</span>
        <span>if</span><span>(</span><span>Rf_inherits</span><span>(</span><span>Xr</span><span>,</span> <span>"dgCMatrix"</span><span>))</span> <span>{</span>
            <span>return</span> <span>matmult_sp</span><span>(</span><span>as</span><span><</span><span>arma</span><span>::</span><span>sp_mat</span><span>></span><span>(</span><span>Xr</span><span>),</span> <span>Y</span><span>)</span> <span>;</span>
        <span>}</span> <span>;</span>
        <span>if</span><span>(</span><span>Rf_inherits</span><span>(</span><span>Xr</span><span>,</span> <span>"indMatrix"</span><span>))</span> <span>{</span>
            <span>return</span> <span>matmult_ind</span><span>(</span><span>Xr</span><span>,</span> <span>Y</span><span>)</span> <span>;</span> 
        <span>}</span> <span>;</span>
        <span>stop</span><span>(</span><span>"unknown class of Xr"</span><span>)</span> <span>;</span>
    <span>}</span> <span>else</span> <span>{</span>
        <span>return</span> <span>matmult_dense</span><span>(</span><span>as</span><span><</span><span>arma</span><span>::</span><span>mat</span><span>></span><span>(</span><span>Xr</span><span>),</span> <span>Y</span><span>)</span> <span>;</span>
    <span>}</span> 
<span>}</span>
Set up test cases: {{...
[Read more...]

Vector Subsetting in Rcpp

March 15, 2014 | Rcpp Gallery

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, ...
[Read more...]

Condorcet Voting with Rcpp

March 13, 2014 | Rcpp Gallery

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 ...
[Read more...]

Convex Hull of Polygon using Boost.Geometry

February 23, 2014 | Rcpp Gallery

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_...
[Read more...]

Detecting a Time Series Change Point

January 4, 2014 | Rcpp Gallery

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 ...
[Read more...]
1 2 3 4 5 6

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)