<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/data-science-for-business-tutorial-using-machine-learning-with-lime-to-understand-employee-churn/"></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/06/25/lime-local-feature-interpretation.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>Data science tools are getting better and better, which is improving the predictive performance of machine learning models in business. With new, high-performance tools like, <a rel="nofollow" href="https://www.h2o.ai/" target="_blank"><strong><em>H2O</em></strong></a> for automated machine learning and <a rel="nofollow" href="https://tensorflow.rstudio.com/keras/" target="_blank"><strong><em>Keras</em></strong></a> for deep learning, the performance of models are increasing tremendously. <strong>There’s one catch: Complex models are unexplainable… that is until LIME came along!</strong> <a rel="nofollow" href="https://cran.r-project.org/package=lime" target="_blank"><strong><em>LIME</em></strong></a>, which stands for Local Interpretable Model-agnostic Explanations, has opened the doors to black-box (complex, high-performance, but unexplainable) models in business applications! <strong>Explanations are MORE CRITICAL to the business than PERFORMANCE.</strong> Think about it. What good is a high performance model that predicts employee attrition if we can’t tell what features are causing people to quit? We need explanations to improve business decision making. Not just performance.</p>
<blockquote>
<p><em>Explanations are <strong>MORE CRITICAL</strong> to the business than <strong>PERFORMANCE</strong>. Think about it. What good is a high performance model that predicts employee attrition if we can’t tell what features are causing people to quit? We need explanations to improve business decision making. Not just performance.</em></p>
</blockquote>
<p>In this Machine Learning Tutorial, <a rel="nofollow" href="https://www.linkedin.com/in/brad-boehmke-ph-d-9b0a257/" target="_blank">Brad Boehmke</a>, Director of Data Science at <a rel="nofollow" href="https://www.linkedin.com/company/84-51/" target="_blank">84.51°</a>, shows us how to use LIME for machine learning interpretability on a <strong>Human Resources Employee Turnover Problem</strong>, specifically showing the value of developing interpretablity visualizations. He shows us options for <strong><em>Global Importance</em></strong> and compares it to LIME for <strong><em>Local Importance</em></strong>. We use machine learning R packages <code>h2o</code>, <code>caret</code>, and <code>ranger</code> in the tutorial, showcasing how to use <code>lime</code> for local explanations. Let’s get started!</p>
<h2>LIME: A Secret Weapon For ROI-Driven Data Science <a></a></h2>
<blockquote>
<p>Introduction by Matt Dancho, Founder of Business Science</p>
</blockquote>
<p><strong><em>Business success</em></strong> is dependent on the ability for managers, process stakeholders, and key decision makers to make the right decisions often using data to understand what’s going on. This is where machine learning can help. Machine learning can analyze vast amounts of data, creating highly predictive models that tell managers key information such as how likely someone is likely to leave an organization. However, machine learning alone is not enough. Business leaders require explanations so they can determine adjustments that will improve results. These explanations require a different tool: <strong><em>LIME</em></strong>. Let’s find out why <strong>LIME is truly a secret weapon for ROI-driven data science</strong>!</p>
<p><strong>In the HR Employee Attrition example discussed in this article, the machine learning model predicts the probability of someone leaving the company.</strong> This probability is then converted to a prediction of either leave or stay through a process called <strong><em>Binary Classification</em></strong>. However, this doesn’t solve the main objective, which is to make better decisions. It only tells us if someone is a high flight risk (i.e. has high attrition probability).</p>
<p><img alt="Employee Attrition: Machine Learning Predicts Which Employees Are Likely To Leave" src="https://i2.wp.com/www.business-science.io/assets/2018-06-25-lime/employee-attrition.png?w=456" data-recalc-dims="1"></p>
<p>Employee Attrition: Machine Learning Predicts Which Employees Are Likely To Leave</p>
<p><strong>How do we change decision making and therefore improve? It comes down to levers and probability</strong>. Machine learning tells us which employees are highest risk and therefore <strong><em>high probability</em></strong>. We can hone in on these individuals, but we need a different tool to understand why an individual is leaving. This is where LIME comes into play. LIME uncovers the <strong><em>levers</em></strong> or features we can control to make business improvements.</p>
<p><img alt="LIME: Uncovering Levers We Can Control" src="https://i2.wp.com/www.business-science.io/assets/2018-06-25-lime/employee-attrition-lime-result.png?w=456" data-recalc-dims="1"></p>
<p>LIME: Uncovers Levers or Features We Can Control</p>
<p>In our HR Employee Attrition Example, LIME detects “Over Time” (lever) as a key feature that <em>supports</em> employee turnover. We can <em>control</em> the “Over Time” feature by implementing a “limited-overtime” or “no-overtime” policy.</p>
<p><img alt="Employee Attrition: Targeting Employees With Over Time" src="https://i1.wp.com/www.business-science.io/assets/2018-06-25-lime/employee-attrition-target-employees.png?w=456" data-recalc-dims="1"></p>
<p>Analyzing A Policy Change: Targeting Employees With Over Time</p>
<p>Toggling the “OverTime” feature to “No” enables calculating an expected value or benefit of reducing overtime by implementing a new OT policy. For the individual employee, a expected savings results. When applied to the entire organization, this process of <em>adjusting levers</em> can result in impactful policy changes that <strong>save the organization millions per year and generate ROI</strong>.</p>
<p><img alt="Employee Attrition: Adjusting Levers" src="https://i1.wp.com/www.business-science.io/assets/2018-06-25-lime/employee-attrition-expected-savings.png?w=456" data-recalc-dims="1"></p>
<p>Adjusting The Over Time Results In Expected Savings</p>
<h4>Interested in Learning LIME While Solving A Real-World Churn Problem?</h4>
<p>If you want to solve this real-world employee churn problem developing models with <strong>H2O Automated Machine Learning</strong>, using <strong>LIME For Black-Box ML Model Explanation</strong>, and <strong>analyzing the impact of a policy change through optimization and sensitivity analysis</strong>, get started today with <a rel="nofollow" href="https://university.business-science.io/courses/hr201-using-machine-learning-h2o-lime-to-predict-employee-turnover?product_id=635023&coupon_code=DS4B_15" target="_blank">Data Science For Business (DS4B 201 / HR 201)</a>. You’ll learn <strong><em>ROI-Driven Data Science</em></strong>, implementing the tools (H2O + LIME) and our data science framework (<a rel="nofollow" href="http://www.business-science.io/bspf.html" target="_blank">BSPF</a>) under my guidance (Matt Dancho, Instructor and Founder of Business Science) in our new, self-paced course part of the <a rel="nofollow" href="https://university.business-science.io/" target="_blank">Business Science University</a> virtual data science workshop.</p>
<p><span data-sumome-listbuilder-embed-id="1e13d987ad901ab4571b6d92bb2ab8a2230c397b886c1fd49eba5392ed5c88cb"></span></p>
<h2>Learning Trajectory</h2>
<p>Now that we have a flavor for what LIME does, let’s get on with learning how to use it! In this machine learning tutorial, you will learn:</p>
<ul>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/business/2018/06/25/lime-local-feature-interpretation.html#global-interp" target="_blank">Global Interpretation: How to perform global intepretation with <code>vip</code> and <code>pdp</code> and how global differs from local with LIME</a></p>
</li>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/business/2018/06/25/lime-local-feature-interpretation.html#local-interp" target="_blank">Local Interpretation: How To Perform Local Interpretation with <code>lime</code> using models developed with <code>h2o</code>, <code>caret</code>, and <code>ranger</code></a></p>
</li>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/business/2018/06/25/lime-local-feature-interpretation.html#viz" target="_blank">Visualizing LIME Results: Using <code>plot_features()</code> and <code>plot_explanations()</code></a></p>
</li>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/business/2018/06/25/lime-local-feature-interpretation.html#tuning-lime" target="_blank">Tuning LIME: Improving explanation accuracy</a></p>
</li>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/business/2018/06/25/lime-local-feature-interpretation.html#model-integration" target="_blank">Integrating Models: Working with unsupported models to get LIME integration</a></p>
</li>
</ul>
<p>In fact, one of the coolest things you’ll learn is how to create a visualization that compares multiple H2O modeling algorithms that examine employee attrition. This is akin to getting different perspectives for how each of the models view the problem:</p>
<ul>
<li>Random Forest (RF)</li>
<li>Generalized Linear Regression (GLM)</li>
<li>Gradient Boosted Machine (GBM).</li>
</ul>
<p><img alt="LIME Multiple Models" src="https://i2.wp.com/www.business-science.io/assets/2018-06-25-lime/lime-multiple-models.png?w=456" data-recalc-dims="1"></p>
<p>Comparing LIME results of different H2O modeling algorithms</p>
<h2>About The Author</h2>
<p>This <strong><em>MACHINE LEARNING TUTORIAL</em></strong> comes from <a rel="nofollow" href="https://www.linkedin.com/in/brad-boehmke-ph-d-9b0a257/" target="_blank">Brad Boehmke</a>, Director of Data Science at <a rel="nofollow" href="https://www.linkedin.com/company/84-51/" target="_blank">84.51°</a>, where he and his team develops algorithmic processes, solutions, and tools that enable 84.51° and its analysts to efficiently extract insights from data and provide solution alternatives to decision-makers. Brad is not only a talented data scientist, he’s an adjunct professor at the University of Cincinnati, Wake Forest, and Air Force Institute of Technology. Most importantly, he’s an <a rel="nofollow" href="https://github.com/bradleyboehmke" target="_blank">active contributor</a> to the <strong>Data Science Community</strong> and he enjoys giving back via advanced machine learning education available at the <a rel="nofollow" href="http://uc-r.github.io/" target="_blank">UC Business Analytics R Programming Guide</a>!</p>
<h2>Machine Learning Tutorial: Visualizing Machine Learning Models with LIME</h2>
<blockquote>
<p>By Brad Boehmke, Director of Data Science at 84.51°</p>
</blockquote>
<p>Machine learning (ML) models are often considered “black boxes” due to their complex inner-workings. More advanced ML models such as random forests, gradient boosting machines (GBM), artificial neural networks (ANN), among others are typically more accurate for predicting nonlinear, faint, or rare phenomena. Unfortunately, more accuracy often comes at the expense of interpretability, and interpretability is crucial for business adoption, model documentation, regulatory oversight, and human acceptance and trust. Luckily, several advancements have been made to aid in interpreting ML models.</p>
<p>Moreover, it’s often important to understand the ML model that you’ve trained on a global scale, and also to zoom into local regions of your data or your predictions and derive local explanations. <strong><em>Global interpretations</em></strong> help us understand the inputs and their entire modeled relationship with the prediction target, but global interpretations can be highly approximate in some cases. <strong><em>Local interpretations</em></strong> help us understand model predictions for a single row of data or a group of similar rows.</p>
<p>This post demonstrates how to use the <code>lime</code> package to perform local interpretations of ML models. This will not focus on the theoretical and mathematical underpinnings but, rather, on the practical application of using <code>lime</code>. <sup><a rel="nofollow" href="http://www.business-science.io/business/2018/06/25/lime-local-feature-interpretation.html#fn:lime_paper" target="_blank">1</a></sup></p>
<h2>Libraries</h2>
<p>This tutorial leverages the following packages.</p>
<figure>
<pre><code data-lang="r"><span># required packages</span><span>
</span><span># install vip from github repo: devtools::install_github(&quot;koalaverse/vip&quot;)</span><span>
</span><span>library</span><span>(</span><span>lime</span><span>)</span><span> </span><span># ML local interpretation</span><span>
</span><span>library</span><span>(</span><span>vip</span><span>)</span><span> </span><span># ML global interpretation</span><span>
</span><span>library</span><span>(</span><span>pdp</span><span>)</span><span> </span><span># ML global interpretation</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span> </span><span># visualization pkg leveraged by above packages</span><span>
</span><span>library</span><span>(</span><span>caret</span><span>)</span><span> </span><span># ML model building</span><span>
</span><span>library</span><span>(</span><span>h</span><span>2</span><span>o</span><span>)</span><span> </span><span># ML model building</span><span>
</span><span># other useful packages</span><span>
</span><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span> </span><span># Use tibble, dplyr</span><span>
</span><span>library</span><span>(</span><span>rsample</span><span>)</span><span> </span><span># Get HR Data via rsample::attrition</span><span>
</span><span>library</span><span>(</span><span>gridExtra</span><span>)</span><span> </span><span># Plot multiple lime plots on one graph</span><span>
</span><span># initialize h2o</span><span>
</span><span>h</span><span>2</span><span>o.init</span><span>()</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         3 minutes 17 seconds 
##     H2O cluster timezone:       America/New_York 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.19.0.4208 
##     H2O cluster version age:    4 months and 6 days !!! 
##     H2O cluster name:           H2O_started_from_R_mdancho_zbl980 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   3.50 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     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>
<figure>
<pre><code data-lang="r"><span>h</span><span>2</span><span>o.no_progress</span><span>()</span></code></pre>
</figure>
<p>To demonstrate model visualization techniques 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. Note: I force ordered factors to be unordered as <code>h2o</code> does not support ordered categorical variables.</p>
<p>For this exemplar I retain most of the observations in the training data sets and retain 5 observations in the <code>local_obs</code> set. These 5 observations are going to be treated as new observations that we wish to understand <strong><em>why</em></strong> the particular predicted response was made.</p>
<figure>
<pre><code data-lang="r"><span># create data sets</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>factor</span><span>(</span><span>Attrition</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Yes&quot;</span><span>,</span><span> </span><span>&quot;No&quot;</span><span>)))</span><span>
</span><span>index</span><span> </span><span>&lt;-</span><span> </span><span>1</span><span>:</span><span>5</span><span>
</span><span>train_obs</span><span> </span><span>&lt;-</span><span> </span><span>df</span><span>[</span><span>-</span><span>index</span><span>,</span><span> </span><span>]</span><span>
</span><span>local_obs</span><span> </span><span>&lt;-</span><span> </span><span>df</span><span>[</span><span>index</span><span>,</span><span> </span><span>]</span><span>
</span><span># create h2o objects for modeling</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>train_obs</span><span>),</span><span> </span><span>y</span><span>)</span><span>
</span><span>train_obs.h2o</span><span> </span><span>&lt;-</span><span> </span><span>as.h2o</span><span>(</span><span>train_obs</span><span>)</span><span>
</span><span>local_obs.h2o</span><span> </span><span>&lt;-</span><span> </span><span>as.h2o</span><span>(</span><span>local_obs</span><span>)</span></code></pre>
</figure>
<p>We will explore how to visualize a few of the more popular machine learning algorithms and packages in R. For brevity I train default models and do not emphasize hyperparameter tuning. The following produces:</p>
<ul>
<li>Random forest model using <code>ranger</code> via the <code>caret</code> package</li>
<li>Random forest model using <code>h2o</code></li>
<li>Elastic net model using <code>h2o</code></li>
<li>GBM model using <code>h2o</code></li>
<li>Random forest model using <code>ranger</code> directly</li>
</ul>
<figure>
<pre><code data-lang="r"><span># Create Random Forest model with ranger via caret</span><span>
</span><span>fit.caret</span><span> </span><span>&lt;-</span><span> </span><span>train</span><span>(</span><span>
</span><span>Attrition</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>train_obs</span><span>,</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>'ranger'</span><span>,</span><span>
</span><span>trControl</span><span> </span><span>=</span><span> </span><span>trainControl</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;cv&quot;</span><span>,</span><span> </span><span>number</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>classProbs</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span>
</span><span>tuneLength</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span>
</span><span>importance</span><span> </span><span>=</span><span> </span><span>'impurity'</span><span>
</span><span>)</span><span>
</span><span># create h2o models</span><span>
</span><span>h</span><span>2</span><span>o_rf</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.randomForest</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span> </span><span>training_frame</span><span> </span><span>=</span><span> </span><span>train_obs.h2o</span><span>)</span><span>
</span><span>h</span><span>2</span><span>o_glm</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.glm</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span> </span><span>training_frame</span><span> </span><span>=</span><span> </span><span>train_obs.h2o</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;binomial&quot;</span><span>)</span><span>
</span><span>h</span><span>2</span><span>o_gbm</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.gbm</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span> </span><span>training_frame</span><span> </span><span>=</span><span> </span><span>train_obs.h2o</span><span>)</span><span>
</span><span># ranger model --> model type not built in to LIME</span><span>
</span><span>fit.ranger</span><span> </span><span>&lt;-</span><span> </span><span>ranger</span><span>::</span><span>ranger</span><span>(</span><span>
</span><span>Attrition</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>train_obs</span><span>,</span><span> </span><span>importance</span><span> </span><span>=</span><span> </span><span>'impurity'</span><span>,</span><span>
</span><span>probability</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>
</span><span>)</span></code></pre>
</figure>
<h2>Global Interpretation <a></a></h2>
<p>The most common ways of obtaining global interpretation is through:</p>
<ul>
<li>variable importance measures</li>
<li>partial dependence plots</li>
</ul>
<p>Variable importance quantifies the global contribution of each input variable to the predictions of a machine learning model. Variable importance measures rarely give insight into the average direction that a variable affects a response function. They simply state the magnitude of a variable’s relationship with the response as compared to other variables used in the model. For example, the <code>ranger</code> random forest model identified monthly income, overtime, and age as the top 3 variables impacting the objective function.</p>
<figure>
<pre><code data-lang="r"><span>vip</span><span>(</span><span>fit.ranger</span><span>)</span><span> </span><span>+</span><span> </span><span>ggtitle</span><span>(</span><span>&quot;ranger: RF&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-4" src="https://i1.wp.com/www.business-science.io/figure/source/2018-06-25-lime-local-feature-interpretation/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<p>After the most globally relevant variables have been identified, the next step is to attempt to understand how the response variable changes based on these variables. For this we can use partial dependence plots (PDPs) and individual conditional expectation (ICE) curves. These techniques plot the change in the predicted value as specified feature(s) vary over their marginal distribution. Consequently, we can gain some local understanding how the reponse variable changes across the distribution of a particular variable but this still only provides a global understanding of this relationships across all observed data.</p>
<p>For example, if we plot the PDP of the monthly income variable we see that the probability of an employee attriting decreases, on average, as their monthly income approaches $5,000 and then remains relatively flat.</p>
<figure>
<pre><code data-lang="r"><span># built-in PDP support in H2O</span><span>
</span><span>h</span><span>2</span><span>o.partialPlot</span><span>(</span><span>h</span><span>2</span><span>o_rf</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>train_obs.h2o</span><span>,</span><span> </span><span>cols</span><span> </span><span>=</span><span> </span><span>&quot;MonthlyIncome&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://i0.wp.com/www.business-science.io/figure/source/2018-06-25-lime-local-feature-interpretation/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<figure>
<pre><code data-lang="text">## PartialDependence: Partial Dependence Plot of model DRF_model_R_1529928665020_106 on column 'MonthlyIncome'
##    MonthlyIncome mean_response stddev_response
## 1    1009.000000      0.229433        0.229043
## 2    2008.473684      0.216096        0.234960
## 3    3007.947368      0.167686        0.234022
## 4    4007.421053      0.161024        0.231287
## 5    5006.894737      0.157775        0.231151
## 6    6006.368421      0.156628        0.231455
## 7    7005.842105      0.157734        0.230267
## 8    8005.315789      0.160137        0.229286
## 9    9004.789474      0.164437        0.229305
## 10  10004.263158      0.169652        0.227902
## 11  11003.736842      0.165502        0.226240
## 12  12003.210526      0.165297        0.225529
## 13  13002.684211      0.165051        0.225335
## 14  14002.157895      0.165051        0.225335
## 15  15001.631579      0.164983        0.225316
## 16  16001.105263      0.165051        0.225019
## 17  17000.578947      0.165556        0.224890
## 18  18000.052632      0.165556        0.224890
## 19  18999.526316      0.166498        0.224726
## 20  19999.000000      0.182348        0.219882</code></pre>
</figure>
<p>We can gain further insight by using centered ICE curves which can help draw out further details. For example, the following ICE curves show a similar trend line as the PDP above but by centering we identify the decrease as monthly income approaches $5,000 followed by an increase in probability of attriting once an employee’s monthly income approaches $20,000. Futhermore, we see some turbulence in the flatlined region between $5-$20K) which means there appears to be certain salary regions where the probability of attriting changes.</p>
<figure>
<pre><code data-lang="r"><span>fit.ranger</span><span> </span><span>%>%</span><span>
</span><span>pdp</span><span>::</span><span>partial</span><span>(</span><span>pred.var</span><span> </span><span>=</span><span> </span><span>&quot;MonthlyIncome&quot;</span><span>,</span><span> </span><span>grid.resolution</span><span> </span><span>=</span><span> </span><span>25</span><span>,</span><span> </span><span>ice</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>autoplot</span><span>(</span><span>rug</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>train</span><span> </span><span>=</span><span> </span><span>train_obs</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.1</span><span>,</span><span> </span><span>center</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-6" src="https://i1.wp.com/www.business-science.io/figure/source/2018-06-25-lime-local-feature-interpretation/unnamed-chunk-6-1.png?w=456" data-recalc-dims="1"></p>
<p>These visualizations help us to understand our model from a global perspective: identifying the variables with the largest overall impact and the typical influence of a feature on the response variable across all observations. However, what these do not help us understand is given a new observation, what were the most <strong><em>influential variables that determined the predicted outcome</em></strong>. Say we obtain information on an employee that makes about $10,000 per month and we need to assess their probabilty of leaving the firm. Although monthly income is the most important variable in our model, it may not be the most influential variable driving this employee to leave. To retain the employee, leadership needs to understand what variables are most influential for that specific employee. This is where <code>lime</code> can help.</p>
<h2>Local Interpretation <a></a></h2>
<p>Local Interpretable Model-agnostic Explanations (<strong>LIME</strong>) is a visualization technique that helps explain individual predictions. As the name implies, it is model agnostic so it can be applied to any supervised regression or classification model. Behind the workings of LIME lies the assumption that <strong><em>every complex model is linear on a local scale</em></strong> and asserting that it is possible to fit a simple model around a single observation that will mimic how the global model behaves at that locality. The simple model can then be used to explain the predictions of the more complex model locally.</p>
<p>The generalized algorithm LIME applies is:</p>
<ol>
<li>Given an observation, <strong><em>permute</em></strong> it to create replicated feature data with slight value modifications.</li>
<li>Compute <strong><em>similarity distance measure</em></strong> between original observation and permuted observations.</li>
<li>Apply selected machine learning model to <strong><em>predict outcomes</em></strong> of permuted data.</li>
<li><strong><em>Select m number of features</em></strong> to best describe predicted outcomes.</li>
<li><strong><em>Fit a simple model</em></strong> to the permuted data, explaining the complex model outcome with <em>m</em> features from the permuted data weighted by its similarity to the original observation .</li>
<li>Use the resulting <strong><em>feature weights to explain local behavior</em></strong>.</li>
</ol>
<p>Each of these steps will be discussed in further detail as we proceed.</p>
<h3>lime::lime</h3>
<p>The application of the LIME algorithm via the <code>lime</code> package is split into two operations: <code>lime::lime()</code> and <code>lime::explain()</code>. The <code>lime::lime()</code> function creates an “explainer” object, which is just a list that contains the machine learning model and the feature distributions for the training data. The feature distributions that it contains includes distribution statistics for each categorical variable level and each continuous variable split into <em>n</em> bins (default is 4 bins). These feature attributes will be used to permute data.</p>
<p>The following creates our <code>lime::lime()</code> object and I change the number to bin our continuous variables into to 5.</p>
<figure>
<pre><code data-lang="r"><span>explainer_caret</span><span> </span><span>&lt;-</span><span> </span><span>lime</span><span>::</span><span>lime</span><span>(</span><span>train_obs</span><span>,</span><span> </span><span>fit.caret</span><span>,</span><span> </span><span>n_bins</span><span> </span><span>=</span><span> </span><span>5</span><span>)</span><span>
</span><span>class</span><span>(</span><span>explainer_caret</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## [1] &quot;data_frame_explainer&quot; &quot;explainer&quot;           
## [3] &quot;list&quot;</code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>summary</span><span>(</span><span>explainer_caret</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##                      Length Class  Mode     
## model                24     train  list     
## bin_continuous        1     -none- logical  
## n_bins                1     -none- numeric  
## quantile_bins         1     -none- logical  
## use_density           1     -none- logical  
## feature_type         31     -none- character
## bin_cuts             31     -none- list     
## feature_distribution 31     -none- list</code></pre>
</figure>
<h3>lime::explain</h3>
<p>Once we created our <code>lime</code> objects, we can now perform the generalized LIME algorithm using the <code>lime::explain()</code> function. This function has several options, each providing flexibility in how we perform the generalized algorithm mentioned above.</p>
<ul>
<li><code>x</code>: Contains the one or more single observations you want to create local explanations for. In our case, this includes the 5 observations that I included in the <code>local_obs</code> data frame. <em>Relates to algorithm step 1</em>.</li>
<li><code>explainer</code>: takes the explainer object created by <code>lime::lime()</code>, which will be used to create permuted data. Permutations are sampled from the variable distributions created by the <code>lime::lime()</code> explainer object. <em>Relates to algorithm step 1</em>.</li>
<li><code>n_permutations</code>: The number of permutations to create for each observation in <code>x</code> (default is 5,000 for tabular data). <em>Relates to algorithm step 1</em>.</li>
<li><code>dist_fun</code>: The distance function to use. The default is Gower’s distance but can also use euclidean, manhattan, or any other distance function allowed by <code>?dist()</code>. To compute similarity distance of permuted observations, categorical features will be recoded based on whether or not they are equal to the actual observation. If continuous features are binned (the default) these features will be recoded based on whether they are in the same bin as the observation. Using the recoded data the distance to the original observation is then calculated based on a user-chosen distance measure. <em>Relates to algorithm step 2</em>.</li>
<li><code>kernel_width</code>: To convert the distance measure to a similarity value, an exponential kernel of a user defined width (defaults to 0.75 times the square root of the number of features) is used. Smaller values restrict the size of the local region. <em>Relates to algorithm step 2</em>.</li>
<li><code>n_features</code>: The number of features to best describe predicted outcomes. <em>Relates to algorithm step 4</em>.</li>
<li><code>feature_select</code>: To select the best <em>n</em> features, <code>lime</code> can use forward selection, ridge regression, lasso, or a tree to select the features. In this example I apply a ridge regression model and select the <em>m</em> features with highest absolute weights. <em>Relates to algorithm step 4</em>.</li>
</ul>
<p>For classification models we also have two additional features we care about and one of these two arguments must be given:</p>
<ul>
<li><code>labels</code>: Which label do we want to explain? In this example, I want to explain the probability of an observation to attrit (“Yes”).</li>
<li><code>n_labels</code>: The number of labels to explain. With this data I could select <code>n_labels = 2</code> to explain the probability of “Yes” and “No” responses.</li>
</ul>
<figure>
<pre><code data-lang="r"><span>explanation_caret</span><span> </span><span>&lt;-</span><span> </span><span>lime</span><span>::</span><span>explain</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>local_obs</span><span>,</span><span> </span><span>explainer</span><span> </span><span>=</span><span> </span><span>explainer_caret</span><span>,</span><span> </span><span>n_permutations</span><span> </span><span>=</span><span> </span><span>5000</span><span>,</span><span>
</span><span>dist_fun</span><span> </span><span>=</span><span> </span><span>&quot;gower&quot;</span><span>,</span><span>
</span><span>kernel_width</span><span> </span><span>=</span><span> </span><span>.75</span><span>,</span><span>
</span><span>n_features</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>feature_select</span><span> </span><span>=</span><span> </span><span>&quot;highest_weights&quot;</span><span>,</span><span>
</span><span>labels</span><span> </span><span>=</span><span> </span><span>&quot;Yes&quot;</span><span>
</span><span>)</span></code></pre>
</figure>
<p>The <code>explain()</code> function above first creates permutations, then calculates similarities, followed by selecting the <em>m</em> features. Lastly, <code>explain()</code> will then fit a model (<em>algorithm steps 5 &amp; 6</em>). <code>lime</code> applies a ridge regression model with the weighted permuted observations as the simple model. [<sup><a rel="nofollow" href="http://www.business-science.io/business/2018/06/25/lime-local-feature-interpretation.html#fn:glmnet" target="_blank">2</a></sup>] If the model is a regressor, the simple model will predict the output of the complex model directly. If the complex model is a classifier, the simple model will predict the probability of the chosen class(es).</p>
<p>The <code>explain()</code> output is a data frame containing different information on the simple model predictions. Most importantly, for each observation in <code>local_obs</code> it contains the simple model fit (<code>model_r2</code>) and the weighted importance (<code>feature_weight</code>) for each important feature (<code>feature_desc</code>) that best describes the local relationship.</p>
<figure>
<pre><code data-lang="r"><span>tibble</span><span>::</span><span>glimpse</span><span>(</span><span>explanation_caret</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## Observations: 25
## Variables: 13
## $ model_type       </code></pre></figure>