<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/imputing-missing-values-in-parallel-using-furrr/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.brodrigues.co/blog/2018-04-14-playing_with_furrr/"> Econometrics and Free Software</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Today I saw this tweet on my timeline:</p>
<blockquote class="twitter-tweet">
<p lang="en" dir="ltr">For those of us that just can't wait until RStudio officially supports parallel purrr in <a rel="nofollow" href="https://twitter.com/hashtag/rstats?src=hash&ref_src=twsrc%5Etfw" target="_blank">#rstats</a>, boy have I got something for you. </p>
<p>Introducing `furrr`, parallel purrr through the use of futures. Go ahead, break things, you know you want to:<a rel="nofollow" href="https://t.co/l9z1UC2Tew" target="_blank">https://t.co/l9z1UC2Tew</a></p>
<p>— Davis Vaughan (@dvaughan32) <a rel="nofollow" href="https://twitter.com/dvaughan32/status/984828716181319680?ref_src=twsrc%5Etfw" target="_blank">April 13, 2018</a></p></blockquote>
<p></p>
<p>and as a heavy <code>{purrr}</code> user, as well as the happy owner of a 6-core AMD Ryzen 5 1600X cpu, I was very excited to try out <code>{furrr}</code>. For those unfamiliar with <code>{purrr}</code>, you can read some of my previous blog posts on it <a rel="nofollow" href="http://www.brodrigues.co/blog/2017-03-24-lesser_known_purrr/" target="_blank">here</a>, <a rel="nofollow" href="http://www.brodrigues.co/blog/2017-03-29-make-ggplot2-purrr/" target="_blank">here</a> or <a rel="nofollow" href="http://www.brodrigues.co/blog/2018-01-19-mapping_functions_with_any_cols/" target="_blank">here</a>.</p>
<p>To summarize very quickly: <code>{purrr}</code> contains so-called higher order functions, which are functions that take other functions as argument. One such function is <code>map()</code>. Consider the following simple example:</p>
<pre><code>numbers &lt;- seq(1, 10)</code></pre>
<p>If you want the square root of this numbers, you can of course simply use the <code>sqrt()</code> function, because it is vectorized:</p>
<pre><code>sqrt(numbers)</code></pre>
<pre><code>##  [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751
##  [8] 2.828427 3.000000 3.162278</code></pre>
<p>But in a lot of situations, the solution is not so simple. Sometimes you have to loop over the values. This is what we would need to do if <code>sqrt()</code> was not vectorized:</p>
<pre><code>sqrt_numbers &lt;- rep(0, 10)

for(i in length(numbers)){
  sqrt_numbers[i] &lt;- sqrt(numbers[i])
}</code></pre>
<p>First, you need to initialize a container, and then you have to populate the <code>sqrt_numbers</code> list with the results. Using, <code>{purrr}</code> is way easier:</p>
<pre><code>library(tidyverse)
map(numbers, sqrt)</code></pre>
<pre><code>## [[1]]
## [1] 1
## 
## [[2]]
## [1] 1.414214
## 
## [[3]]
## [1] 1.732051
## 
## [[4]]
## [1] 2
## 
## [[5]]
## [1] 2.236068
## 
## [[6]]
## [1] 2.44949
## 
## [[7]]
## [1] 2.645751
## 
## [[8]]
## [1] 2.828427
## 
## [[9]]
## [1] 3
## 
## [[10]]
## [1] 3.162278</code></pre>
<p><code>map()</code> is only one of the nice functions that are bundled inside <code>{purrr}</code>. Mastering <code>{purrr}</code> can really make you a much more efficient R programmer. Anyways, recently, I have been playing around with imputation and the <code>{mice}</code> package. <code>{mice}</code> comes with an example dataset called <code>boys</code>, let’s take a look at it:</p>
<pre><code>library(mice)

data(boys)

brotools::describe(boys) %>%
  select(variable, type, n_missing, everything())</code></pre>
<pre><code>## # A tibble: 9 x 12
##   variable type    n_missing   mean    sd mode      min   max   q25 median
##   </code></pre>