<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/iml-and-h2o-machine-learning-model-interpretability-and-feature-explanation/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html"> business-science.io - Articles</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>Model interpretability is critical to businesses</strong>. If you want to use high performance models (GLM, RF, GBM, Deep Learning, H2O, Keras, xgboost, etc), you need to learn how to explain them. With <strong>machine learning interpretability</strong> growing in importance, several R packages designed to provide this capability are gaining in popularity. We analyze the <a rel="nofollow" href="https://github.com/christophM/iml" target="_blank">IML package</a> in this article.</p>
<p>In recent blog posts we assessed <a rel="nofollow" href="http://www.business-science.io/business/2018/06/25/lime-local-feature-interpretation.html" target="_blank">LIME</a> for model agnostic local interpretability functionality and <a rel="nofollow" href="http://www.business-science.io/business/2018/07/23/dalex-feature-interpretation.html" target="_blank">DALEX</a> for both local and global machine learning explanation plots. This post examines the <code>iml</code> package (short for <strong>Interpretable Machine Learning</strong>) to assess its functionality in providing machine learning interpretability to help you determine if it should become part of your preferred machine learning toolbox.</p>
<p>We again utilize the high performance machine learning library, <code>h2o</code>, implementing three popular black-box modeling algorithms: GLM (generalized linear models), RF (random forest), and GBM (gradient boosted machines). For those that want a deep dive into model interpretability, the creator of the <code>iml</code> package, <a rel="nofollow" href="https://twitter.com/ChristophMolnar" target="_blank">Christoph Molnar</a>, has put together a free book: <a rel="nofollow" href="https://christophm.github.io/interpretable-ml-book/" target="_blank">Interpretable Machine Learning</a>. Check it out.</p>
<h2>Articles In The Model Interpretability Series</h2>
<p><strong>Articles related to machine learning and black-box model interpretability</strong>:</p>
<ul>
<li>
<p><strong>LIME</strong>: <a rel="nofollow" href="http://www.business-science.io/business/2018/06/25/lime-local-feature-interpretation.html" target="_blank">LIME and H2O: Using Machine Learning With LIME To Understand Employee Churn</a></p>
</li>
<li>
<p><strong>DALEX</strong>: <a rel="nofollow" href="http://www.business-science.io/business/2018/07/23/dalex-feature-interpretation.html" target="_blank">DALEX and H2O: Machine Learning Model Interpretability and Feature Explanation</a></p>
</li>
<li>
<p><strong>IML</strong>: <a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html" target="_blank">IML and H2O: Machine Learning Model Interpetability and Feature Explanation</a></p>
</li>
</ul>
<p><strong>Awesome Data Science Tutorials with LIME for black-box model explanation in business</strong>:</p>
<ul>
<li>
<p><strong>Credit Default Risk</strong>: <a rel="nofollow" href="http://www.business-science.io/business/2018/08/07/kaggle-competition-home-credit-default-risk.html" target="_blank">Kaggle Competition in 30 Minutes: Predict Home Credit Default Risk With R</a></p>
</li>
<li>
<p><strong>Human Resources Employee Turnover</strong>:<a rel="nofollow" href="http://www.business-science.io/business/2017/09/18/hr_employee_attrition.html" target="_blank">HR Analytics: Using Machine Learning To Predict Employee Turnover</a></p>
</li>
<li>
<p><strong>Customer Churn</strong>:<a rel="nofollow" href="http://www.business-science.io/business/2017/11/28/customer_churn_analysis_keras.html" target="_blank">Customer Analytics: Using Deep Learning With Keras To Predict Customer Churn</a></p>
</li>
<li>
<p><strong>Sales Product Backorders</strong>: <a rel="nofollow" href="http://www.business-science.io/business/2017/10/16/sales_backorder_prediction.html" target="_blank">Sales Analytics: How To Use Machine Learning To Predict And Optimize Product Backorders</a></p>
</li>
</ul>
<p></p>
<hr>
<h2>Transform Your Data Science Abilities In 10 Weeks</h2>
<p>
If you’re interested in learning how to apply critical thinking and data science while solving a real-world business problem following an end-to-end data science project, check out <strong>Data Science For Business (DS4B 201-R)</strong>. Over the course of 10 weeks you will solve an <strong>end-to-end Employee Churn data science project following our systematic Business Science Problem Framework</strong>.
</p>
<p>
<a rel="nofollow" href="https://university.business-science.io/p/hr201-using-machine-learning-h2o-lime-to-predict-employee-turnover/?product_id=635023&coupon_code=DS4B_15" target="_blank"><br>
<img alt="Data Science For Business With R" src="https://i0.wp.com/www.business-science.io/img/ds4b_201_r_small.png?w=456" data-recalc-dims="1"><br>
</a>
</p>
<p>
<a rel="nofollow" href="https://university.business-science.io/p/hr201-using-machine-learning-h2o-lime-to-predict-employee-turnover/?product_id=635023&coupon_code=DS4B_15" target="_blank"><strong>Start Learning Today!</strong></a>
</p>
<p>We have two new course offerings coming soon! <strong>Shiny Web Apps</strong> and <strong>Python And Spark For Customer Churn</strong>! Get started with <a rel="nofollow" href="https://university.business-science.io/" target="_blank"><strong>Business Science University</strong></a>.</p>
<hr>
<p></p>
<h2>FREE BOOK: Interpretable Machine Learning</h2>
<p>The creator of the <strong>IML (Interpretable Machine Learning)</strong> package has a great FREE resource for those interested in applying model interpretability techniques to complex, black-box models (high performance models). Check out the book: <a rel="nofollow" href="https://christophm.github.io/interpretable-ml-book/" target="_blank">Interpretable Machine Learning</a> by <a rel="nofollow" href="https://twitter.com/ChristophMolnar" target="_blank">Christoph Molnar</a>.</p>
<p>
<a rel="nofollow" href="https://christophm.github.io/interpretable-ml-book/" target="_blank"><br>
<img alt="Interpretable Machine Learning Book" src="https://i1.wp.com/www.business-science.io/assets/2018-08-13-iml/interpretable_machine_learning.jpg?w=456" data-recalc-dims="1"><br>
</a>
</p>
<p><a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html" target="_blank">Interpretable Machine Learning Book by Christoph Molnar</a></p>
<h2>Learning Trajectory</h2>
<p>We’ll cover the following topics on <code>iml</code>, combining with the <code>h2o</code> high performance machine learning package:</p>
<ul>
<li><a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html#adv-dis" target="_blank">Advantages &amp; disadvantages</a>: a quick breakdown of what <code>iml</code> does and does not do.</li>
<li><a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html#rep" target="_blank">Replication requirements</a>: what you’ll need to reproduce the analysis.</li>
<li><a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html#procedures" target="_blank"><code>iml</code> procedures</a>: necessary functions for downstream explainers.</li>
<li><a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html#global" target="_blank">Global interpretation</a>
<ul>
<li><a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html#vip" target="_blank">Feature importance</a>: a permutation-based approach</li>
<li><a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html#pdp" target="_blank">Partial dependence</a>: PDPs and ICE curves</li>
<li><a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html#interactions" target="_blank">Measuring interactions</a>: an implementation of the H-statistic</li>
<li><a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html#surrogate" target="_blank">Surrogate model</a>: simplifying your “black-box” model</li>
</ul>
</li>
<li><a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html#local" target="_blank">Local interpretation</a>
<ul>
<li><a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html#lime" target="_blank">Lime</a>: fitting a local linear model</li>
<li><a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html#shap" target="_blank">Shapley values</a>: a game theory approach</li>
</ul>
</li>
<li><strong>Transform Your Abilities In 10 Weeks</strong>: <a rel="nofollow" href="http://www.business-science.io/business/2018/08/13/iml-model-interpretability.html#next-steps" target="_blank">Next Steps</a></li>
</ul>
<h2>IML and H2O: Machine Learning Model Interpretability And Feature Explanation</h2>
<blockquote>
<p>By Brad Boehmke, Director of Data Science at 84.51°</p>
</blockquote>
<h3>Advantages &amp; disadvantages</h3>
<p>The <code>iml</code> package is probably the most robust ML interpretability package available. It provides both global and local model-agnostic interpretation methods. Although the interaction functions are notably slow, the other functions are faster or comparable to existing packages we use or have tested. I definitely recommend adding <code>iml</code> to your preferred ML toolkit. The following provides a quick list of some of its pros and cons:</p>
<p><strong>Advantages</strong></p>
<ul>
<li>ML model and package agnostic: can be used for any supervised ML model (many features are only relevant to regression and binary classification problems).</li>
<li>Variable importance: uses a permutation-based approach for variable importance, which is model agnostic, and accepts any loss function to assess importance.</li>
<li>Partial dependence plots: Fast PDP implementation and allows for ICE curves.</li>
<li>H-statistic: one of only a few implementations to allow for assessing interactions.</li>
<li>Local interpretation: provides both LIME and Shapley implementations.</li>
<li>Plots: built with <code>ggplot2</code> which allows for easy customization</li>
</ul>
<p><strong>Disadvantages</strong></p>
<ul>
<li>Does not allow for easy comparisons across models like <code>DALEX</code>.</li>
<li>The H-statistic interaction functions do not scale well to wide data (may predictor variables).</li>
<li>Only provides permutation-based variable importance scores (which become slow as number of features increase).</li>
<li>LIME implementation has less flexibilty and features than <code>lime</code>.</li>
</ul>
<h3>Replication Requirements</h3>
<h4>Libraries</h4>
<p>I leverage the following packages:</p>
<figure>
<pre><code data-lang="r"><span># load required packages</span><span>
</span><span>library</span><span>(</span><span>rsample</span><span>)</span><span> </span><span># data splitting</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span> </span><span># allows extension of visualizations</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span> </span><span># basic data transformation</span><span>
</span><span>library</span><span>(</span><span>h</span><span>2</span><span>o</span><span>)</span><span> </span><span># machine learning modeling</span><span>
</span><span>library</span><span>(</span><span>iml</span><span>)</span><span> </span><span># ML interprtation</span><span>
</span><span># initialize h2o session</span><span>
</span><span>h</span><span>2</span><span>o.no_progress</span><span>()</span><span>
</span><span>h</span><span>2</span><span>o.init</span><span>()</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## 
## H2O is not running yet, starting it now...
## 
## Note:  In case of errors look at the following log files:
##     C:\Users\mdanc\AppData\Local\Temp\RtmpotQxdq/h2o_mdanc_started_from_r.out
##     C:\Users\mdanc\AppData\Local\Temp\RtmpotQxdq/h2o_mdanc_started_from_r.err
## 
## 
## Starting H2O JVM and connecting: . Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         3 seconds 250 milliseconds 
##     H2O cluster version:        3.16.0.2 
##     H2O cluster version age:    8 months and 13 days !!! 
##     H2O cluster name:           H2O_started_from_R_mdanc_rkf359 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   3.53 GB 
##     H2O cluster total cores:    8 
##     H2O cluster allowed cores:  8 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Algos, AutoML, Core V3, Core V4 
##     R Version:                  R version 3.4.4 (2018-03-15)</code></pre>
</figure>
<h4>Data</h4>
<p>To demonstrate <code>iml</code>’s capabilities we’ll use the employee attrition data that has been included in the <code>rsample</code> package. This demonstrates a binary classification problem (“Yes” vs. “No”) but the same process that you’ll observe can be used for a regression problem.<br>
I perform a few house cleaning tasks on the data prior to converting to an <code>h2o</code> object and splitting.</p>
<p><strong>NOTE</strong>: The surrogate tree function uses <code>partykit::cpart</code>, which requires all predictors to be numeric or factors. Consequently, we need to coerce any character predictors to factors (or ordinal encode).</p>
<figure>
<pre><code data-lang="r"><span># classification data</span><span>
</span><span>df</span><span> </span><span>&lt;-</span><span> </span><span>rsample</span><span>::</span><span>attrition</span><span> </span><span>%>%</span><span> </span><span>mutate_if</span><span>(</span><span>is.ordered</span><span>,</span><span> </span><span>factor</span><span>,</span><span> </span><span>ordered</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>Attrition</span><span> </span><span>=</span><span> </span><span>recode</span><span>(</span><span>Attrition</span><span>,</span><span> </span><span>&quot;Yes&quot;</span><span> </span><span>=</span><span> </span><span>&quot;1&quot;</span><span>,</span><span> </span><span>&quot;No&quot;</span><span> </span><span>=</span><span> </span><span>&quot;0&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>factor</span><span>(</span><span>levels</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;1&quot;</span><span>,</span><span> </span><span>&quot;0&quot;</span><span>)))</span><span>
</span><span># convert to h2o object</span><span>
</span><span>df.h2o</span><span> </span><span>&lt;-</span><span> </span><span>as.h2o</span><span>(</span><span>df</span><span>)</span><span>
</span><span># create train, validation, and test splits</span><span>
</span><span>set.seed</span><span>(</span><span>123</span><span>)</span><span>
</span><span>splits</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.splitFrame</span><span>(</span><span>df.h2o</span><span>,</span><span> </span><span>ratios</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>.7</span><span>,</span><span> </span><span>.15</span><span>),</span><span> </span><span>destination_frames</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;train&quot;</span><span>,</span><span>&quot;valid&quot;</span><span>,</span><span>&quot;test&quot;</span><span>))</span><span>
</span><span>names</span><span>(</span><span>splits</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;train&quot;</span><span>,</span><span>&quot;valid&quot;</span><span>,</span><span>&quot;test&quot;</span><span>)</span><span>
</span><span># variable names for resonse &amp; features</span><span>
</span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>&quot;Attrition&quot;</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>setdiff</span><span>(</span><span>names</span><span>(</span><span>df</span><span>),</span><span> </span><span>y</span><span>)</span><span> </span></code></pre>
</figure>
<h4>H2O Models</h4>
<p>We will explore how to visualize a few of the more common machine learning algorithms implemented with <code>h2o</code>. For brevity I train default models and do not emphasize hyperparameter tuning. The following produces a regularized logistic regression, random forest, and gradient boosting machine models.</p>
<figure>
<pre><code data-lang="r"><span># elastic net model </span><span>
</span><span>glm</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.glm</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>x</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>y</span><span>,</span><span> </span><span>training_frame</span><span> </span><span>=</span><span> </span><span>splits</span><span>$</span><span>train</span><span>,</span><span>
</span><span>validation_frame</span><span> </span><span>=</span><span> </span><span>splits</span><span>$</span><span>valid</span><span>,</span><span>
</span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;binomial&quot;</span><span>,</span><span>
</span><span>seed</span><span> </span><span>=</span><span> </span><span>123</span><span>
</span><span>)</span><span>
</span><span># random forest model</span><span>
</span><span>rf</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.randomForest</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>x</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>y</span><span>,</span><span>
</span><span>training_frame</span><span> </span><span>=</span><span> </span><span>splits</span><span>$</span><span>train</span><span>,</span><span>
</span><span>validation_frame</span><span> </span><span>=</span><span> </span><span>splits</span><span>$</span><span>valid</span><span>,</span><span>
</span><span>ntrees</span><span> </span><span>=</span><span> </span><span>1000</span><span>,</span><span>
</span><span>stopping_metric</span><span> </span><span>=</span><span> </span><span>&quot;AUC&quot;</span><span>,</span><span> </span><span>stopping_rounds</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>stopping_tolerance</span><span> </span><span>=</span><span> </span><span>0.005</span><span>,</span><span>
</span><span>seed</span><span> </span><span>=</span><span> </span><span>123</span><span>
</span><span>)</span><span>
</span><span># gradient boosting machine model</span><span>
</span><span>gbm</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.gbm</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>x</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>y</span><span>,</span><span>
</span><span>training_frame</span><span> </span><span>=</span><span> </span><span>splits</span><span>$</span><span>train</span><span>,</span><span>
</span><span>validation_frame</span><span> </span><span>=</span><span> </span><span>splits</span><span>$</span><span>valid</span><span>,</span><span>
</span><span>ntrees</span><span> </span><span>=</span><span> </span><span>1000</span><span>,</span><span>
</span><span>stopping_metric</span><span> </span><span>=</span><span> </span><span>&quot;AUC&quot;</span><span>,</span><span> </span><span>stopping_rounds</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>stopping_tolerance</span><span> </span><span>=</span><span> </span><span>0.005</span><span>,</span><span>
</span><span>seed</span><span> </span><span>=</span><span> </span><span>123</span><span>
</span><span>)</span></code></pre>
</figure>
<p>All of the models provide AUCs ranging between 0.75 to 0.79. Although these models have distinct AUC scores, <strong>our objective is to understand how these models come to this conclusion in similar or different ways based on underlying logic and data structure</strong>.</p>
<figure>
<pre><code data-lang="r"><span># model performance</span><span>
</span><span>h</span><span>2</span><span>o.auc</span><span>(</span><span>glm</span><span>,</span><span> </span><span>valid</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## [1] 0.7870935</code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>h</span><span>2</span><span>o.auc</span><span>(</span><span>rf</span><span>,</span><span> </span><span>valid</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## [1] 0.7681021</code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>h</span><span>2</span><span>o.auc</span><span>(</span><span>gbm</span><span>,</span><span> </span><span>valid</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## [1] 0.7468242</code></pre>
</figure>
<blockquote>
<p><em>Although these models have distinct AUC scores, <strong>our objective is to understand how these models come to this conclusion in similar or different ways based on underlying logic and data structure</strong>.</em></p>
</blockquote>
<h3>IML procedures</h3>
<p>In order to work with <code>iml</code>, we need to adapt our data a bit so that we have the following three components:</p>
<ol>
<li>
<p>Create a data frame with just the features (must be of class <code>data.frame</code>, cannot be an <code>H2OFrame</code> or other class).</p>
</li>
<li>
<p>Create a vector with the actual responses (must be numeric – 0/1 for binary classification problems).</p>
</li>
<li>
<p><code>iml</code> has internal support for some machine learning packages (i.e. <code>mlr</code>, <code>caret</code>, <code>randomForest</code>). However, to use <code>iml</code> with several of the more popular packages being used today (i.e. <code>h2o</code>, <code>ranger</code>, <code>xgboost</code>) we need to create a custom function that will take a data set (again must be of class <code>data.frame</code>) and provide the predicted values as a vector.</p>
</li>
</ol>
<figure>
<pre><code data-lang="r"><span># 1. create a data frame with just the features</span><span>
</span><span>features</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>splits</span><span>$</span><span>valid</span><span>)</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>-</span><span>Attrition</span><span>)</span><span>
</span><span># 2. Create a vector with the actual responses</span><span>
</span><span>response</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>as.vector</span><span>(</span><span>splits</span><span>$</span><span>valid</span><span>$</span><span>Attrition</span><span>))</span><span>
</span><span># 3. Create custom predict function that returns the predicted values as a</span><span>
</span><span># vector (probability of purchasing in our example)</span><span>
</span><span>pred</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>model</span><span>,</span><span> </span><span>newdata</span><span>)</span><span> </span><span>{</span><span>
</span><span>results</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>h</span><span>2</span><span>o.predict</span><span>(</span><span>model</span><span>,</span><span> </span><span>as.h2o</span><span>(</span><span>newdata</span><span>)))</span><span>
</span><span>return</span><span>(</span><span>results</span><span>[[</span><span>3L</span><span>]])</span><span>
</span><span>}</span><span>
</span><span># example of prediction output</span><span>
</span><span>pred</span><span>(</span><span>rf</span><span>,</span><span> </span><span>features</span><span>)</span><span> </span><span>%>%</span><span> </span><span>head</span><span>()</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## [1] 0.18181818 0.27272727 0.06060606 0.54545455 0.03030303 0.42424242</code></pre>
</figure>
<p>Once we have these three components we can create a predictor object. Similar to <code>DALEX</code> and <code>lime</code>, the predictor object holds the model, the data, and the class labels to be applied to downstream functions. A unique characteristic of the <code>iml</code> package is that it uses <a rel="nofollow" href="https://adv-r.hadley.nz/r6.html" target="_blank">R6 classes</a>, which is rather rare. To main differences between R6 classes and the normal S3 and S4 classes we typically work with are:</p>
<ul>
<li>Methods belong to objects, not generics (we’ll see this in the next code chunk).</li>
<li>Objects are mutable: the usual copy-on-modify semantics do not apply (we’ll see this later in this tutorial).</li>
</ul>
<p>These properties make R6 objects behave more like objects in programming languages such as Python. So to construct a new <code>Predictor</code> object, you call the <code>new()</code> method which belongs to the R6 <code>Predictor</code> object and you use <code>$</code> to access <code>new()</code>:</p>
<figure>
<pre><code data-lang="r"><span># create predictor object to pass to explainer functions</span><span>
</span><span>predictor.glm</span><span> </span><span>&lt;-</span><span> </span><span>Predictor</span><span>$</span><span>new</span><span>(</span><span>
</span><span>model</span><span> </span><span>=</span><span> </span><span>glm</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>features</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>response</span><span>,</span><span> </span><span>predict.fun</span><span> </span><span>=</span><span> </span><span>pred</span><span>,</span><span>
</span><span>class</span><span> </span><span>=</span><span> </span><span>&quot;classification&quot;</span><span>
</span><span>)</span><span>
</span><span>predictor.rf</span><span> </span><span>&lt;-</span><span> </span><span>Predictor</span><span>$</span><span>new</span><span>(</span><span>
</span><span>model</span><span> </span><span>=</span><span> </span><span>rf</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>features</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>response</span><span>,</span><span> </span><span>predict.fun</span><span> </span><span>=</span><span> </span><span>pred</span><span>,</span><span>
</span><span>class</span><span> </span><span>=</span><span> </span><span>&quot;classification&quot;</span><span>
</span><span>)</span><span>
</span><span>predictor.gbm</span><span> </span><span>&lt;-</span><span> </span><span>Predictor</span><span>$</span><span>new</span><span>(</span><span>
</span><span>model</span><span> </span><span>=</span><span> </span><span>gbm</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>features</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>response</span><span>,</span><span> </span><span>predict.fun</span><span> </span><span>=</span><span> </span><span>pred</span><span>,</span><span>
</span><span>class</span><span> </span><span>=</span><span> </span><span>&quot;classification&quot;</span><span>
</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span># structure of predictor object</span><span>
</span><span>str</span><span>(</span><span>predictor.gbm</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## Classes 'Predictor', 'R6' </code></pre></figure>