<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/tuning-a-data-preprocessing-pipeline-with-recipes-and-modelgrid/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://smaakage85.netlify.com/2018/09/24/tuning-a-data-preprocessing-pipeline-with-recipes-and-modelgrid/"> Posts on pRopaganda by smaakagen</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post I will demonstrate, how the <a rel="nofollow" href="https://cran.r-project.org/web/packages/modelgrid/index.html" target="_blank"><code>modelgrid</code></a> package can be used to facilitate experiments with the data preprocessing pipeline of a predictive model.</p>
<div>
<h2>Data preprocessing – an integral part of a model configuration</h2>
<p>Model tuning is not just a matter of tuning the hyperparameters of an algorithm. Since data preprocessing is also an integral part of the model development workflow, it is just as relevant to experiment with the data preprocessing pipeline of a model configuration. When “tuning” a model, the data preprocessing pipeline should therefore also be tuned.</p>
<p>In the following I will present an example of tuning a data preprocessing pipeline using <code>modelgrid</code> in combination with <a rel="nofollow" href="https://github.com/tidymodels/recipes" target="_blank"><code>recipes</code></a>.</p>
</div>
<div>
<h2>Use case: Cell Segmentation in High-Content Screening</h2>
<p>I will use the Cell Segmentation data set described in the excellent book <a rel="nofollow" href="http://appliedpredictivemodeling.com/" target="_blank"><strong>‘Applied Predictive Modelling’</strong></a> as an example.</p>
<pre><code>library(AppliedPredictiveModeling)
data(segmentationOriginal)</code></pre>
<p>The data set consists of 2019 samples, where each sample represents a cell. Of these cells, 1300 were judged to be poorly segmented and 719 were well segmented; 1009 cells were reserved for the training set.</p>
<p>In each cell 116 measurements were taken. They are all available as numeric predictors.</p>
<p>For more information on the data set look <a rel="nofollow" href="https://bmcbioinformatics.biomedcentral.com/articles/10.1186/1471-2105-8-340" target="_blank">here</a>.</p>
<p>Our goal is to develop a classification model, that separates the poorly segmented from the well segmented cells.</p>
<div>
<h3>Data at a glance</h3>
<p>First, let us take a quick look at the data. We will do that by inspecting the between-predictor correlations of the predictors expressed by a correlation matrix of the training data set. The variables are grouped adjacent to each other according to their mutual correlations.</p>
<pre><code># Extract training data.
training &lt;- filter(segmentationOriginal, Case == &quot;Train&quot;)

# Extract predictors.
predictors &lt;- training %>% select(-(c(&quot;Class&quot;, &quot;Case&quot;, &quot;Cell&quot;))) 

# Identify variables with zero variance.
zero_variance_predictors &lt;- map_lgl(predictors, ~ n_distinct(.x) == 1)

# Remove predictors with zero variance.
predictors &lt;- predictors[, !zero_variance_predictors]

# Compute and plot a correlation matrix of remaining predictors.
library(corrplot)
predictors %>%
  cor(.) %>%
  corrplot(., order = &quot;hclust&quot;, tl.cex = .35)</code></pre>
<p><img src="https://i1.wp.com/smaakage85.netlify.com/post/2018-09-24-tuning-a-data-preprocessing-pipeline-with-recipes-and-modelgrid_files/figure-html/unnamed-chunk-4-1.png?w=450" data-recalc-dims="1"></p>
<p>From the graph, it seems that there are groups of predictors, that have strong positive correlations (dark blue).</p>
<p>There can be good reasons for avoiding variables, that are highly correlated, some of them being (as stated in <a rel="nofollow" href="http://appliedpredictivemodeling.com/" target="_blank"><strong>‘Applied Predictive Modelling’</strong></a>):</p>
<ul>
<li>Redundant/highly correlated predictors often add more complexity than information to the model</li>
<li>Mathematical disadvantages: can result in very unstable models (high variance), numerical errors and inferior predictive performance.</li>
</ul>
<p>The aim of my modelling experiments will be to apply different preprocessing techniques in order to mitigate the potential pitfalls of the “collinearity clusters”, that we are observing amongst the field of predictors.</p>
</div>
<div>
<h3>Create initial recipe</h3>
<p>First, let us set up a starting point for our data preprocessing pipeline in our modeling experiments. For this purpose I apply the <strong>awesome</strong> <code>recipes</code> package and create a – very basic – recipe, that will serve as an anchor for my model configurations.</p>
<p>In this recipe I declare the roles of all variables in the data set and state, that variables with zero variances should be removed.</p>
<pre><code>library(recipes)
initial_recipe &lt;- recipe(training) %>%
  add_role(Class, new_role = &quot;outcome&quot;) %>%
  add_role(Cell, new_role = &quot;id variable&quot;) %>%
  add_role(Case, new_role = &quot;splitting indicator&quot;) %>%
  add_role(-Class, -Cell, -Case, new_role = &quot;predictor&quot;) %>%
  step_zv(all_predictors())</code></pre>
<p>You can ‘prep’ the recipe and get an impression of, what it is actually doing. It seems, it removes two of the predictors due to them having variances of zero.</p>
<pre><code>prep_rec &lt;- prep(initial_recipe)
tidy(prep_rec, 1)
#> # A tibble: 2 x 1
#>   terms                       
#>   </code></pre></div></div>