<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/a-package-for-dimensionality-reduction-of-large-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ropensci.org/blog/2018/08/01/umapr/"> rOpenSci - open tools for open science</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img alt="Comparing UMAP to other algorithms" src="https://i0.wp.com/ropensci.org/blog/2018/08/01/umapr/img/blog-images/2018-08-01-umapr/multiple_algorithms_cancer.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Motivation</h2>
<blockquote>
<p>Note: Recently, two new UMAP R packages have appeared. These new packages provide more features than umapr does and they are more actively developed. These packages are:<br>
<a rel="nofollow" href="https://github.com/tkonopka/umap" target="_blank"><code>umap</code></a>, which provides the same Python wrapping function as umapr and also an R implementation, removing the need for the Python version to be installed. It is available on CRAN.<br>
<a rel="nofollow" href="https://github.com/jlmelville/uwot" target="_blank"><code>uwot</code></a>, which also provides an R implementation, removing the need for the Python version to be installed.</p>
</blockquote>
<p>A few weeks ago, as part of the <a rel="nofollow" href="https://ropensci.org/blog/2018/06/05/unconf18/" target="_blank">rOpenSci Unconference</a>, a group of us (Sean Hughes, Malisa Smith, Angela Li, Ju Kim, and Ted Laderas) decided to work on making the UMAP algorithm accessible within R. UMAP (Uniform Manifold Approximation and Projection) is a dimensionality reduction technique that allows the user to reduce high dimensional data (multiple columns) into a smaller number of columns for visualization purposes (<a rel="nofollow" href="https://github.com/lmcinnes/umap" target="_blank">github</a>, <a rel="nofollow" href="https://arxiv.org/abs/1802.03426" target="_blank">arxiv</a>). It is similar to both Principal Components Analysis (PCA) and t-SNE, which are techniques often used in the single-cell omics (such as <a rel="nofollow" href="https://en.wikipedia.org/wiki/Single_cell_sequencing" target="_blank">genomics</a>, <a rel="nofollow" href="https://en.wikipedia.org/wiki/Flow_cytometry" target="_blank">flow cytometry</a>, <a rel="nofollow" href="https://pubs.acs.org/doi/10.1021/acs.jproteome.8b00257" target="_blank">proteomics</a>) world to visualize high dimensional data. t-SNE is actually quite a slow algorithm; one of the advantages of UMAP is that it runs faster than t-SNE. Because the <code>data.frames</code> that are typically run with these algorithms can run into millions of rows, efficiency is important.</p>
<p>We decided to start working on the <code>umapr</code> package to make this technique accessible within R. As with most rOpenSci Unconf projects, this started with an <a rel="nofollow" href="https://github.com/ropensci/unconf18/issues/43" target="_blank">issue entry in the rOpenSci unconf repo</a>:</p>
<blockquote>
<p>I recently read about a new non-linear dimensionality reduction algorithm called <a rel="nofollow" href="https://doi.org/10.1101/298430" target="_blank">UMAP</a>, which is much faster than t-SNE, while producing two-dimensional visualizations that share many characteristics with t-SNE. I initially found out about it in the context of use on high-dimensional single-cell data in <a rel="nofollow" href="https://doi.org/10.1101/298430" target="_blank">this paper</a>.</p>
<p>….</p>
<p>My thought is that the ideal would be a package focused on UMAP specifically, implemented in R or Rcpp. Unfortunately I am not at all an expert in this topic or familiar with the mathematics involved, so the best I would be able to do is try to translate the Python implementation into R.</p>
</blockquote>
<p>We all met at the unconference the first day and decided that this was a project worth working on. Since t-SNE is so used in the single cell and flow-cytometry community, we thought that having an alternative that was just as good, but faster to run would be helpful.</p>
<h2>Making a Development Plan</h2>
<p>Rather than recreate the UMAP code completely from scratch in R, we decided to use the <a rel="nofollow" href="https://rstudio.github.io/reticulate/" target="_blank"><code>reticulate</code></a> package to call the implementation in Python from R. It was tempting to just wrap the function’s arguments with <code>...</code> and let the user refer to the python documentation. However, we didn’t really think that was in the spirit of the unconf. We wanted to make UMAP much more accessible.</p>
<h2>Learning about Package Building, Testing, and Documentation</h2>
<p>Although our package only really has one main function (<code>umap()</code>), we felt it was important to have good documentation and unit tests. We spent some time learning about <code>roxygen</code> for function documentation and <code>testthat</code> for unit testing, and setting up our package with Travis-CI for continuous integration testing. This included unit tests on each argument and including examples varying the essential parameters.</p>
<p>We spent a lot of time learning more about the specifics of package building and vignette building in R. We were definitely excited by all of the available tools and built a vignette profiling the performance of the UMAP algorithm versus other dimensionality reduction techniques, such as t-SNE. Our vignette can be read here: <a rel="nofollow" href="https://github.com/ropenscilabs/umapr#basic-use" target="_blank">https://github.com/ropenscilabs/umapr#basic-use</a></p>
<h2>Profiling <code>umapr</code> using different datasets</h2>
<p><img alt="Execution time of UMAP compared to other algorithms" src="https://i1.wp.com/ropensci.org/blog/2018/08/01/umapr/img/blog-images/2018-08-01-umapr/multiple_algorithms_time.png?w=456&ssl=1" data-recalc-dims="1"><br>
<img alt="Memory usage of UMAP comapred to other algorithms" src="https://i0.wp.com/ropensci.org/blog/2018/08/01/umapr/img/blog-images/2018-08-01-umapr/multiple_algorithms_memory.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Part of the appeal of UMAP is that it is faster than t-SNE. So we profiled the performance of UMAP on a number of different datasets: <code>iris</code> (of course!), the <a rel="nofollow" href="https://cran.r-project.org/web/packages/mlbench/index.html" target="_blank"><code>BreastCancer</code> dataset from the <code>mlbench</code> package</a>, a <a rel="nofollow" href="https://cran.r-project.org/web/packages/mlbench/index.html" target="_blank"><code>Soybean</code> dataset from <code>mlbench</code></a>, and finally, a <a rel="nofollow" href="https://hemberg-lab.github.io/scRNA.seq.datasets/human/tissues/" target="_blank">single cell RNA dataset</a>. You can <a rel="nofollow" href="https://github.com/ropenscilabs/umapr/blob/master/README.md" target="_blank">see our results in our readme file</a>.</p>
<p>Thankfully, UMAP does run faster than t-SNE on these datasets, showing a reduction of 66% compared to both versions of t-SNE for the <code>Soybean</code> dataset, and reduced memory usage for all of the datasets, except for the single cell RNA dataset (see above figure).</p>
<h2>Exploring the Results with Shiny</h2>
<p><img alt="Shiny App" src="https://i0.wp.com/ropensci.org/blog/2018/08/01/umapr/img/blog-images/2018-08-01-umapr/shiny.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>We built a small Shiny app that lets people explore their embedding vectors (the dimensionally reduced vectors) and how they separate the data into different groupings in the 2D space. The app is simple, but allows users to immediately assess the results of the UMAP algorithm in differentiating groupings in the data by coloring the <code>umap</code> result by the different variables included in the analysis.</p>
<h2>Final Results: Get <code>umapr</code></h2>
<p><code>umapr</code> is currently available in the <code>ropenscilabs</code> organization, and can be installed with the following commands, <a rel="nofollow" href="https://github.com/lmcinnes/umap#installing" target="_blank">after the python modules are installed</a>.</p>
<pre><code>install.packages(&quot;devtools&quot;) 
devtools::install_github(&quot;ropenscilabs/umapr&quot;)
</code></pre>
<p>As a group, we learned a lot by building the <code>umapr</code> package. More importantly, I think we’ll work together on future projects. It was great to work together, and we are talking about having a hackathon between our multiple groups to improve some current open source flow cytometry tools. This was a really fun project and we’re excited to do more!</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://ropensci.org/blog/2018/08/01/umapr/"> rOpenSci - open tools for open science</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/hVoj7rhpYdQ" height="1"></a></div>