<p><a href="https://twitter.com/drsimonj">@drsimonj</a> here to introduce pipelearner – a package I’m developing to make it easy to create machine learning pipelines in R – and to spread the word in the hope that some readers may be interested in contributing or testing it.</p>
<p>This post will demonstrate some examples of what pipeleaner can currently do. For example, the Figure below plots the results of a model fitted to 10% to 100% (in 10% increments) of training data in 50 cross-validation pairs. Fitting all of these models takes about four lines of code in pipelearner.</p>
<p><a href="https://svbtleusercontent.com/ta8knsjs2heeg.png"></a></p>
<p>Head to the <a href="https://github.com/drsimonj/pipelearner">pipelearner Github page</a> to learn more and contact me if you have a chance to test it yourself or are interested in contributing (my contact details are at the end of this post).</p>
<h2><a href="https://drsimonj.svbtle.com/#examples_2"> </a>Examples</h2><h4><a href="https://drsimonj.svbtle.com/#some-setup_4"> </a>Some setup</h4>
<pre><code>library(pipelearner)
library(tidyverse)
library(nycflights13)

# Help functions
r_square &lt;- function(model, data) {
  actual    &lt;- eval(formula(model)[[2]], as.data.frame(data))
  residuals &lt;- predict(model, data) - actual
  1 - (var(residuals, na.rm = TRUE) / var(actual, na.rm = TRUE))
}
add_rsquare &lt;- function(result_tbl) {
  result_tbl %>% 
    mutate(rsquare_train = map2_dbl(fit, train, r_square),
           rsquare_test  = map2_dbl(fit, test,  r_square))
}

# Data set
d &lt;- weather %>%
  select(visib, humid, precip, wind_dir) %>% 
  drop_na() %>%
  sample_n(2000)

# Set theme for plots
theme_set(theme_minimal())
</code></pre>
<h4><a href="https://drsimonj.svbtle.com/#kfold-cross-validation_4"> </a>k-fold cross validation</h4>
<pre><code>results &lt;- d %>% 
  pipelearner(lm, visib ~ .) %>% 
  learn_cvpairs(k = 10) %>% 
  learn()

results %>%
  add_rsquare() %>% 
  select(cv_pairs.id, contains(&quot;rsquare&quot;)) %>% 
  gather(source, rsquare, contains(&quot;rsquare&quot;)) %>%
  mutate(source = gsub(&quot;rsquare_&quot;, &quot;&quot;, source)) %>% 
  ggplot(aes(cv_pairs.id, rsquare, color = source)) +
    geom_point() +
    labs(x = &quot;Fold&quot;,
         y = &quot;R Squared&quot;)
</code></pre>
<p><a href="https://svbtleusercontent.com/xampv0dkkpbztg.png"></a></p>
<h4><a href="https://drsimonj.svbtle.com/#learning-curves_4"> </a>Learning curves</h4>
<pre><code>results &lt;- d %>% 
  pipelearner(lm, visib ~ .) %>% 
  learn_curves(seq(.1, 1, .1)) %>% 
  learn()

results %>%
  add_rsquare() %>%
  select(train_p, contains(&quot;rsquare&quot;)) %>%
  gather(source, rsquare, contains(&quot;rsquare&quot;)) %>%
  mutate(source = gsub(&quot;rsquare_&quot;, &quot;&quot;, source)) %>% 
  ggplot(aes(train_p, rsquare, color = source)) +
   geom_line() +
   geom_point(size = 2) +
   labs(x = &quot;Proportion of training data used&quot;,
       y = &quot;R Squared&quot;)
</code></pre>
<p><a href="https://svbtleusercontent.com/q9k2m6wgnt4niw.png"></a></p>
<h4><a href="https://drsimonj.svbtle.com/#grid-search_4"> </a>Grid Search</h4>
<pre><code>results &lt;- d %>% 
  pipelearner(rpart::rpart, visib ~ .,
              minsplit = c(2, 50, 100),
              cp = c(.005, .01, .1)) %>% 
  learn()

results %>%
  mutate(minsplit = map_dbl(params, ~ .$minsplit),
         cp       = map_dbl(params, ~ .$cp)) %>% 
  add_rsquare() %>% 
  select(minsplit, cp, contains(&quot;rsquare&quot;)) %>%
  gather(source, rsquare, contains(&quot;rsquare&quot;)) %>%
  mutate(source = gsub(&quot;rsquare_&quot;, &quot;&quot;, source),
         minsplit = paste(&quot;minsplit&quot;, minsplit, sep = &quot;\n&quot;),
         cp       = paste(&quot;cp&quot;, cp, sep = &quot;\n&quot;)) %>% 
  ggplot(aes(source, rsquare, fill = source)) +
   geom_col() +
   facet_grid(minsplit ~ cp) +
   guides(fill = &quot;none&quot;) +
   labs(x = NULL, y = &quot;R Squared&quot;)
</code></pre>
<p><a href="https://svbtleusercontent.com/0aonfamnbkgevg.png"></a></p>
<h4><a href="https://drsimonj.svbtle.com/#model-comparisons_4"> </a>Model comparisons</h4>
<pre><code>results &lt;- d %>% 
  pipelearner() %>% 
  learn_models(
    c(lm, rpart::rpart, randomForest::randomForest),
    visib ~ .) %>% 
  learn()

results %>%
  add_rsquare() %>%
  select(model, contains(&quot;rsquare&quot;)) %>%
  gather(source, rsquare, contains(&quot;rsquare&quot;)) %>%
  mutate(source = gsub(&quot;rsquare_&quot;, &quot;&quot;, source)) %>% 
  ggplot(aes(model, rsquare, fill = source)) +
   geom_col(position = &quot;dodge&quot;, size = .5) +
   labs(x = NULL, y = &quot;R Squared&quot;) +
   coord_flip()
</code></pre>
<p><a href="https://svbtleusercontent.com/sdc72kiz0un22w.png"></a></p>
<h2><a href="https://drsimonj.svbtle.com/#sign-off_2"> </a>Sign off</h2>
<p>Thanks for reading and I hope this was useful for you.</p>
<p>For updates of recent blog posts, follow <a href="https://twitter.com/drsimonj">@drsimonj</a> on Twitter, or email me at <a href="mailto:drsimonjackson@gmail.com">drsimonjackson@gmail.com</a> to get in touch.</p>
<p>If you’d like the code that produced this blog, check out the <a href="https://github.com/drsimonj/blogR">blogR GitHub repository</a>.</p>