<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/divide-and-parallelize-large-data-problems-with-rcpp/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2018/01/parallelize-rcpp.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><em>by <a rel="nofollow" href="https://www.linkedin.com/in/b%C5%82a%C5%BCej-moska-9a316113a/" target="_blank">Błażej Moska</a>, computer science student and data science intern </em></p>
<p>Got stuck with too large a dataset? R speed drives you mad? Divide, parallelize and go with Rcpp!</p>
<p>One of the frustrating moments while working with data is when you need results urgently, but your dataset is large enough to make it impossible. This happens often when we need to use algorithm with high computational complexity. I will demonstrate it on the example I’ve been working with.</p>
<p>Suppose we have large dataset consisting of association rules. For some reasons we want to slim it down. Whenever two rules consequents are the same and one rule’s antecedent is a subset of second rule’s antecedent, we want to choose the smaller one (probability of obtaining smaller set is bigger than probability of obtaining bigger set). This is illustrated below:</p>
<p><strong>{A,B,C}=>{D}</strong></p>
<p>{E}=>{F}</p>
<p><strong>{A,B}=>{D}</strong></p>
<p>{A}=>D</p>
<p>How can we achieve that? For example, using below pseudo algorithm:</p>
<pre><code>For i=1 to n:
  For j=i+1 to n:
   # check if antecedent[i] contains antecedent[j]
   (if consequents[i]=consequents[j]), then flag antecedent[i] with 1, 
                                       otherwise with 0
    else: # check if antecedent[j] contains antecedent[i] 
          (if consequents[i]=consequents[j]), then flag antecedent[j] with 1, 
                                              otherwise with 0
</code></pre>
<p>How many operations do we need to perform with this simple algorithm?</p>
<p>For the first <code>i</code> we need to iterate \(n-1\) times, for the second <code>i</code> \(n-2\) times, for the third <code>i</code> \(n-3\) and so on, reaching finally \(n-(n-1)\). This leads to (proof can be <a rel="nofollow" href="http://ms.uky.edu/~ma123o/lecturenotes/Chapter9_answers.pdf" target="_blank">found here</a>): </p>
<p>\[<br>
\sum_{i=1}^{n}{i}= \frac{n(n-1)}{2}<br>
\]</p>
<p>So the above has asymptotic complexity of \(O(n^2)\). It means, more or less, that the computational complexity grows with the square of the size of the data. Well, for the dataset containing around 1,300,000 records this becomes serious issue. With R I was unable to perform computation in reasonable time. Since a compiled language performs better with simple arithmetic operations, the second idea was to use Rcpp. Yes, it is faster, to some extent — but with such a large dataframe I was still unable to get results in satisfying time. So are there any other options? </p>
<p>Yes, there are. If we take a look at our dataset, we can see that it can be aggregated in such way that each individual “chunk” will consist of records with exactly same consequents:</p>
<p><strong><em>{A,B}=>{D}</em></strong> </p>
<p><strong><em>{A}=>{D}</em></strong></p>
<p><strong>{C,G}=>{F}</strong> </p>
<p><strong>{Y}=>{F}</strong></p>
<p>After such division I got 3300 chunks, so the average number of observations per chunk was around 400. Next step was to retry sequentially for each chunk. Since our algorithm has square complexity, it is faster to do it that way rather than on the whole dataset at once. While R failed again, Rcpp finally returned result (after 5 minutes). But still there is a room for improvement. Since our chunks can be calculated independently, there is a possibility to perform parallel computation using for example, foreach package (which I demonstrated in <a rel="nofollow" href="http://blog.revolutionanalytics.com/2017/10/bias-bootstrap-foreach.html" target="_blank">previous article</a>). While passing R functions to foreach is a simple task, parallelizing Rcpp is a little bit more time consuming. We need to do below steps:</p>
<ol>
<li>Create <code>.cpp</code> file, which includes all of functions needed</li>
<li>Create a package using Rcpp. This can be achieved using for example:<br> <code>Rcpp.package.skeleton(&quot;nameOfYourPackage&quot;,cpp_files = &quot;directory_of_your_cpp_file&quot;)</code></li>
<li>Install your Rcpp package from source:<br> <code>install.packages(&quot;directory_of_your_rcpp_package&quot;, repos=NULL, type=&quot;source&quot;)</code> </li>
<li>Load your library:<br> <code>library(name_of_your_rcpp_package)</code></li>
</ol>
<p>Now you can use your Rcpp function in foreach:</p>
<pre><code>results=foreach(k=1:length(len),
                .packages=c(name_of_your_package)) %dopar% 
                              {your_cpp_function(data)}
</code></pre>
<p>Even with foreach I waited forever for the R results, but Rcpp gave them in approximately 2.5 minutes. Not too bad!</p>
<ul>
<li><a rel="nofollow" href="https://gist.github.com/bmoska/cfca53c1fa18af86ee6b950b8ea98b14" target="_blank">Here is the Rcpp code</a> for the <code>issub</code> function</li>
<li><a rel="nofollow" href="https://gist.github.com/bmoska/cfca53c1fa18af86ee6b950b8ea98b14" target="_blank">Here is the R code</a> that partitions the data and calls the <code>issub</code> function in parallel</li>
</ul>
<p>Here are some conclusions. Firstly, it’s worth knowing more languages/tools than just R. Secondly, there is often escape from the large dataset trap. There is little chance that somebody will do exactly the same task as mentioned in above example, but much higher probability that someone will face similar problem, with a possibility to solve it in the same way.</p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2018/01/parallelize-rcpp.html"> Revolutions</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/JWfmhDwVRQQ" height="1"></a></div>