{"id":772,"date":"2022-04-25T09:54:41","date_gmt":"2022-04-25T07:54:41","guid":{"rendered":"https:\/\/lorentzen.ch\/?p=772"},"modified":"2022-04-25T09:54:42","modified_gmt":"2022-04-25T07:54:42","slug":"let-the-flashlight-shine-with-plotly","status":"publish","type":"post","link":"https:\/\/lorentzen.ch\/index.php\/2022\/04\/25\/let-the-flashlight-shine-with-plotly\/","title":{"rendered":"Let the flashlight shine with plotly"},"content":{"rendered":"\n<p>There are different R packages devoted to model agnostic interpretability,&nbsp;<a href=\"https:\/\/cran.r-project.org\/web\/packages\/DALEX\/index.html\">DALEX<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/cran.r-project.org\/web\/packages\/iml\/index.html\">iml<\/a>&nbsp;being among the best known. In 2019, I added&nbsp;<a href=\"https:\/\/cran.r-project.org\/web\/packages\/flashlight\/index.html\">flashlight<\/a>&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><a href=\"https:\/\/cran.r-project.org\/web\/packages\/flashlight\/index.html\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/github.com\/mayer79\/flashlight\/blob\/master\/logo.png?raw=true\" alt=\"logo.png\" width=\"202\" height=\"233\"\/><\/a><\/figure>\n\n\n\n<p>for a couple of reasons:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Its explainers work with case weights.<\/li><li>Multiple explainers can be combined to a multi-explainer.<\/li><li>Stratified calculation is possible.<\/li><\/ol>\n\n\n\n<p>Since almost all plots in flashlight are constructed with&nbsp;<code>ggplot<\/code>, it is super easy to turn them into interactive plotly objects: just add a simple&nbsp;<code>ggplotly()<\/code>&nbsp;to the end of the call. <\/p>\n\n\n\n<p>However&#8230; it is not straightforward to show interactive plots in a blog! Thus, we show only screenshots of the resulting plots here and refer to the complete HTML report here: <a href=\"https:\/\/mayer79.github.io\/flashlight_plotly\/flashlight_plotly.html\">https:\/\/mayer79.github.io\/flashlight_plotly\/flashlight_plotly.html<\/a><\/p>\n\n\n\n<p>We will use a sweet dataset with more than 20\u2019000 houses to model house prices by a set of derived features such as the logarithmic living area. The location will be represented by the postal code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Data preparation<\/h3>\n\n\n\n<p>We first load the data and prepare some of the columns for modeling. Furthermore, we specify the set of features and the response.<\/p>\n\n\n<div class=\"wp-block-ub-tabbed-content wp-block-ub-tabbed-content-holder wp-block-ub-tabbed-content-horizontal-holder-mobile wp-block-ub-tabbed-content-horizontal-holder-tablet\" id=\"ub-tabbed-content-97ca5d4b-e09a-4e40-9297-0063e10277da\" style=\"\">\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-holder horizontal-tab-width-mobile horizontal-tab-width-tablet\">\n\t\t\t\t<div role=\"tablist\" class=\"wp-block-ub-tabbed-content-tabs-title wp-block-ub-tabbed-content-tabs-title-mobile-horizontal-tab wp-block-ub-tabbed-content-tabs-title-tablet-horizontal-tab\" style=\"justify-content: flex-start; \"><div role=\"tab\" id=\"ub-tabbed-content-97ca5d4b-e09a-4e40-9297-0063e10277da-tab-0\" aria-controls=\"ub-tabbed-content-97ca5d4b-e09a-4e40-9297-0063e10277da-panel-0\" aria-selected=\"true\" class=\"wp-block-ub-tabbed-content-tab-title-wrap active\" style=\"--ub-tabbed-title-background-color: #6d6d6d; --ub-tabbed-active-title-color: inherit; --ub-tabbed-active-title-background-color: #6d6d6d; text-align: center; \" tabindex=\"-1\">\n\t\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-title\">R<\/div>\n\t\t\t<\/div><\/div>\n\t\t\t<\/div>\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tabs-content\" style=\"\"><div role=\"tabpanel\" class=\"wp-block-ub-tabbed-content-tab-content-wrap active\" id=\"ub-tabbed-content-97ca5d4b-e09a-4e40-9297-0063e10277da-panel-0\" aria-labelledby=\"ub-tabbed-content-97ca5d4b-e09a-4e40-9297-0063e10277da-tab-0\" tabindex=\"0\">\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting='{\"showPanel\":true,\"languageLabel\":\"language\",\"fullScreenButton\":true,\"copyButton\":true,\"mode\":\"r\",\"mime\":\"text\/x-rsrc\",\"theme\":\"material\",\"lineNumbers\":false,\"styleActiveLine\":false,\"lineWrapping\":false,\"readOnly\":true,\"fileName\":\"\",\"language\":\"R\",\"maxHeight\":\"400px\",\"modeName\":\"r\"}'>library(dplyr)\nlibrary(flashlight)\nlibrary(plotly)\nlibrary(ranger)\nlibrary(lme4)\nlibrary(moderndive)\nlibrary(splitTools)\nlibrary(MetricsWeighted)\n\nset.seed(4933)\n\ndata(\"house_prices\")\n\nprep &lt;- house_prices %&gt;% \n  mutate(\n    log_price = log(price),\n    log_sqft_living = log(sqft_living),\n    log_sqft_lot = log(sqft_lot),\n    log_sqft_basement = log1p(sqft_basement),\n    year = as.numeric(format(date, '%Y')),\n    age = year - yr_built\n  )\n\nx &lt;- c(\n  \"year\", \"age\", \"log_sqft_living\", \"log_sqft_lot\", \n  \"bedrooms\", \"bathrooms\", \"log_sqft_basement\", \n  \"condition\", \"waterfront\", \"zipcode\"\n)\n\ny &lt;- \"log_price\"\n\nhead(prep[c(y, x)])\n\n## # A tibble: 6 x 11\n##   log_price  year   age log_sqft_living log_sqft_lot bedrooms bathrooms\n##       &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;           &lt;dbl&gt;        &lt;dbl&gt;    &lt;int&gt;     &lt;dbl&gt;\n## 1      12.3  2014    59            7.07         8.64        3      1   \n## 2      13.2  2014    63            7.85         8.89        3      2.25\n## 3      12.1  2015    82            6.65         9.21        2      1   \n## 4      13.3  2014    49            7.58         8.52        4      3   \n## 5      13.1  2015    28            7.43         9.00        3      2   \n## 6      14.0  2014    13            8.60        11.5         4      4.5 \n## # ... with 4 more variables: log_sqft_basement &lt;dbl&gt;, condition &lt;fct&gt;,\n## #   waterfront &lt;lgl&gt;, zipcode &lt;fct&gt;<\/pre><\/div>\n\n<\/div><\/div>\n\t\t<\/div>\n\n\n<h3 class=\"wp-block-heading\">Train \/ test split<\/h3>\n\n\n\n<p>Then, we split the dataset into 80% training and 20% test rows, stratified on the (binned) response&nbsp;<code>log_price<\/code>.<\/p>\n\n\n<div class=\"wp-block-ub-tabbed-content wp-block-ub-tabbed-content-holder wp-block-ub-tabbed-content-horizontal-holder-mobile wp-block-ub-tabbed-content-horizontal-holder-tablet\" id=\"ub-tabbed-content-327c42fc-b879-41f7-a87d-3ee2e4f83b2c\" style=\"\">\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-holder horizontal-tab-width-mobile horizontal-tab-width-tablet\">\n\t\t\t\t<div role=\"tablist\" class=\"wp-block-ub-tabbed-content-tabs-title wp-block-ub-tabbed-content-tabs-title-mobile-horizontal-tab wp-block-ub-tabbed-content-tabs-title-tablet-horizontal-tab\" style=\"justify-content: flex-start; \"><div role=\"tab\" id=\"ub-tabbed-content-327c42fc-b879-41f7-a87d-3ee2e4f83b2c-tab-0\" aria-controls=\"ub-tabbed-content-327c42fc-b879-41f7-a87d-3ee2e4f83b2c-panel-0\" aria-selected=\"true\" class=\"wp-block-ub-tabbed-content-tab-title-wrap active\" style=\"--ub-tabbed-title-background-color: #6d6d6d; --ub-tabbed-active-title-color: inherit; --ub-tabbed-active-title-background-color: #6d6d6d; text-align: center; \" tabindex=\"-1\">\n\t\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-title\">R<\/div>\n\t\t\t<\/div><\/div>\n\t\t\t<\/div>\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tabs-content\" style=\"\"><div role=\"tabpanel\" class=\"wp-block-ub-tabbed-content-tab-content-wrap active\" id=\"ub-tabbed-content-327c42fc-b879-41f7-a87d-3ee2e4f83b2c-panel-0\" aria-labelledby=\"ub-tabbed-content-327c42fc-b879-41f7-a87d-3ee2e4f83b2c-tab-0\" tabindex=\"0\">\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting='{\"showPanel\":true,\"languageLabel\":\"language\",\"fullScreenButton\":true,\"copyButton\":true,\"mode\":\"r\",\"mime\":\"text\/x-rsrc\",\"theme\":\"material\",\"lineNumbers\":false,\"styleActiveLine\":false,\"lineWrapping\":false,\"readOnly\":true,\"fileName\":\"\",\"language\":\"R\",\"maxHeight\":\"400px\",\"modeName\":\"r\"}'>idx &lt;- partition(prep[[y]], c(train = 0.8, test = 0.2), type = \"stratified\")\n\ntrain &lt;- prep[idx$train, ]\ntest &lt;- prep[idx$test, ]<\/pre><\/div>\n\n<\/div><\/div>\n\t\t<\/div>\n\n\n<h3 class=\"wp-block-heading\">Models<\/h3>\n\n\n\n<p>We fit two models:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>A linear mixed model with random postal code effect.<\/li><li>A random forest with 500 trees.<\/li><\/ol>\n\n\n<div class=\"wp-block-ub-tabbed-content wp-block-ub-tabbed-content-holder wp-block-ub-tabbed-content-horizontal-holder-mobile wp-block-ub-tabbed-content-horizontal-holder-tablet\" id=\"ub-tabbed-content-24d9a832-5cc9-4a67-9189-6b679678a77c\" style=\"\">\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-holder horizontal-tab-width-mobile horizontal-tab-width-tablet\">\n\t\t\t\t<div role=\"tablist\" class=\"wp-block-ub-tabbed-content-tabs-title wp-block-ub-tabbed-content-tabs-title-mobile-horizontal-tab wp-block-ub-tabbed-content-tabs-title-tablet-horizontal-tab\" style=\"justify-content: flex-start; \"><div role=\"tab\" id=\"ub-tabbed-content-24d9a832-5cc9-4a67-9189-6b679678a77c-tab-0\" aria-controls=\"ub-tabbed-content-24d9a832-5cc9-4a67-9189-6b679678a77c-panel-0\" aria-selected=\"true\" class=\"wp-block-ub-tabbed-content-tab-title-wrap active\" style=\"--ub-tabbed-title-background-color: #6d6d6d; --ub-tabbed-active-title-color: inherit; --ub-tabbed-active-title-background-color: #6d6d6d; text-align: center; \" tabindex=\"-1\">\n\t\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-title\">R<\/div>\n\t\t\t<\/div><\/div>\n\t\t\t<\/div>\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tabs-content\" style=\"\"><div role=\"tabpanel\" class=\"wp-block-ub-tabbed-content-tab-content-wrap active\" id=\"ub-tabbed-content-24d9a832-5cc9-4a67-9189-6b679678a77c-panel-0\" aria-labelledby=\"ub-tabbed-content-24d9a832-5cc9-4a67-9189-6b679678a77c-tab-0\" tabindex=\"0\">\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting='{\"showPanel\":true,\"languageLabel\":\"language\",\"fullScreenButton\":true,\"copyButton\":true,\"mode\":\"r\",\"mime\":\"text\/x-rsrc\",\"theme\":\"material\",\"lineNumbers\":false,\"styleActiveLine\":false,\"lineWrapping\":false,\"readOnly\":true,\"fileName\":\"\",\"language\":\"R\",\"maxHeight\":\"400px\",\"modeName\":\"r\"}'># Mixed-effects model\nfit_lmer &lt;- lmer(\n  update(reformulate(x, \"log_price\"), . ~ . - zipcode + (1 | zipcode)),\n  data = train\n)\n\n# Random forest\nfit_rf &lt;- ranger(\n  reformulate(x, \"log_price\"),\n  always.split.variables = \"zipcode\",\n  data = train\n)\ncat(\"R-squared OOB:\", fit_rf$r.squared)\n## R-squared OOB: 0.8463311<\/pre><\/div>\n\n<\/div><\/div>\n\t\t<\/div>\n\n\n<h3 class=\"wp-block-heading\">Model inspection<\/h3>\n\n\n\n<p>Now, we are ready to inspect our two models regarding performance, variable importance, and effects.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Set up explainers<\/h4>\n\n\n\n<p>First, we pack all model dependent information into flashlights (the explainer objects) and combine them to a multiflashlight. As evaluation dataset, we pass the test data. This ensures that interpretability tools using the response (e.g., performance measures and permutation importance) are not being biased by overfitting.<\/p>\n\n\n<div class=\"wp-block-ub-tabbed-content wp-block-ub-tabbed-content-holder wp-block-ub-tabbed-content-horizontal-holder-mobile wp-block-ub-tabbed-content-horizontal-holder-tablet\" id=\"ub-tabbed-content-e0ef2b0a-2ce7-4cfa-bd1e-b4b48cd819c9\" style=\"\">\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-holder horizontal-tab-width-mobile horizontal-tab-width-tablet\">\n\t\t\t\t<div role=\"tablist\" class=\"wp-block-ub-tabbed-content-tabs-title wp-block-ub-tabbed-content-tabs-title-mobile-horizontal-tab wp-block-ub-tabbed-content-tabs-title-tablet-horizontal-tab\" style=\"justify-content: flex-start; \"><div role=\"tab\" id=\"ub-tabbed-content-e0ef2b0a-2ce7-4cfa-bd1e-b4b48cd819c9-tab-0\" aria-controls=\"ub-tabbed-content-e0ef2b0a-2ce7-4cfa-bd1e-b4b48cd819c9-panel-0\" aria-selected=\"true\" class=\"wp-block-ub-tabbed-content-tab-title-wrap active\" style=\"--ub-tabbed-title-background-color: #6d6d6d; --ub-tabbed-active-title-color: inherit; --ub-tabbed-active-title-background-color: #6d6d6d; text-align: center; \" tabindex=\"-1\">\n\t\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-title\">R<\/div>\n\t\t\t<\/div><\/div>\n\t\t\t<\/div>\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tabs-content\" style=\"\"><div role=\"tabpanel\" class=\"wp-block-ub-tabbed-content-tab-content-wrap active\" id=\"ub-tabbed-content-e0ef2b0a-2ce7-4cfa-bd1e-b4b48cd819c9-panel-0\" aria-labelledby=\"ub-tabbed-content-e0ef2b0a-2ce7-4cfa-bd1e-b4b48cd819c9-tab-0\" tabindex=\"0\">\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting='{\"showPanel\":true,\"languageLabel\":\"language\",\"fullScreenButton\":true,\"copyButton\":true,\"mode\":\"r\",\"mime\":\"text\/x-rsrc\",\"theme\":\"material\",\"lineNumbers\":false,\"styleActiveLine\":false,\"lineWrapping\":false,\"readOnly\":true,\"fileName\":\"\",\"language\":\"R\",\"maxHeight\":\"400px\",\"modeName\":\"r\"}'>fl_lmer &lt;- flashlight(model = fit_lmer, label = \"LMER\")\nfl_rf &lt;- flashlight(\n  model = fit_rf,\n  label = \"RF\",\n  predict_function = function(mod, X) predict(mod, X)$predictions\n)\nfls &lt;- multiflashlight(\n  list(fl_lmer, fl_rf),\n  y = \"log_price\",\n  data = test,\n  metrics = list(RMSE = rmse, `R-squared` = r_squared)\n)<\/pre><\/div>\n\n<\/div><\/div>\n\t\t<\/div>\n\n\n<h4 class=\"wp-block-heading\">Model performance<\/h4>\n\n\n\n<p>Let\u2019s evaluate model RMSE and R-squared on the hold-out dataset. Here, the mixed-effects model performs a tiny little bit better than the random forest:<\/p>\n\n\n<div class=\"wp-block-ub-tabbed-content wp-block-ub-tabbed-content-holder wp-block-ub-tabbed-content-horizontal-holder-mobile wp-block-ub-tabbed-content-horizontal-holder-tablet\" id=\"ub-tabbed-content-2e88d5c7-0363-4047-9a19-5328838f7e6a\" style=\"\">\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-holder horizontal-tab-width-mobile horizontal-tab-width-tablet\">\n\t\t\t\t<div role=\"tablist\" class=\"wp-block-ub-tabbed-content-tabs-title wp-block-ub-tabbed-content-tabs-title-mobile-horizontal-tab wp-block-ub-tabbed-content-tabs-title-tablet-horizontal-tab\" style=\"justify-content: flex-start; \"><div role=\"tab\" id=\"ub-tabbed-content-2e88d5c7-0363-4047-9a19-5328838f7e6a-tab-0\" aria-controls=\"ub-tabbed-content-2e88d5c7-0363-4047-9a19-5328838f7e6a-panel-0\" aria-selected=\"true\" class=\"wp-block-ub-tabbed-content-tab-title-wrap active\" style=\"--ub-tabbed-title-background-color: #6d6d6d; --ub-tabbed-active-title-color: inherit; --ub-tabbed-active-title-background-color: #6d6d6d; text-align: center; \" tabindex=\"-1\">\n\t\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-title\">R<\/div>\n\t\t\t<\/div><\/div>\n\t\t\t<\/div>\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tabs-content\" style=\"\"><div role=\"tabpanel\" class=\"wp-block-ub-tabbed-content-tab-content-wrap active\" id=\"ub-tabbed-content-2e88d5c7-0363-4047-9a19-5328838f7e6a-panel-0\" aria-labelledby=\"ub-tabbed-content-2e88d5c7-0363-4047-9a19-5328838f7e6a-tab-0\" tabindex=\"0\">\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting='{\"showPanel\":true,\"languageLabel\":\"language\",\"fullScreenButton\":true,\"copyButton\":true,\"mode\":\"r\",\"mime\":\"text\/x-rsrc\",\"theme\":\"material\",\"lineNumbers\":false,\"styleActiveLine\":false,\"lineWrapping\":false,\"readOnly\":true,\"fileName\":\"\",\"language\":\"R\",\"maxHeight\":\"400px\",\"modeName\":\"r\"}'>(light_performance(fls) %&gt;%\n  plot(fill = \"darkred\") +\n    labs(title = \"Model performance\", x = element_blank())) %&gt;%\n  ggplotly()<\/pre><\/div>\n\n<\/div><\/div>\n\t\t<\/div>\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-3.png\" alt=\"\" class=\"wp-image-777\" width=\"650\" height=\"438\" srcset=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-3.png 1021w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-3-300x202.png 300w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-3-768x518.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><figcaption>Model performance (png)<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Permutation importance<\/h4>\n\n\n\n<p>Next, we inspect the variable strength based on permutation importance. It shows by how much the RMSE is being increased when shuffling a variable before prediction. The results are quite similar between the two models.<\/p>\n\n\n<div class=\"wp-block-ub-tabbed-content wp-block-ub-tabbed-content-holder wp-block-ub-tabbed-content-horizontal-holder-mobile wp-block-ub-tabbed-content-horizontal-holder-tablet\" id=\"ub-tabbed-content-4fd4d5ee-c9d4-4beb-b6ad-9effba83db16\" style=\"\">\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-holder horizontal-tab-width-mobile horizontal-tab-width-tablet\">\n\t\t\t\t<div role=\"tablist\" class=\"wp-block-ub-tabbed-content-tabs-title wp-block-ub-tabbed-content-tabs-title-mobile-horizontal-tab wp-block-ub-tabbed-content-tabs-title-tablet-horizontal-tab\" style=\"justify-content: flex-start; \"><div role=\"tab\" id=\"ub-tabbed-content-4fd4d5ee-c9d4-4beb-b6ad-9effba83db16-tab-0\" aria-controls=\"ub-tabbed-content-4fd4d5ee-c9d4-4beb-b6ad-9effba83db16-panel-0\" aria-selected=\"true\" class=\"wp-block-ub-tabbed-content-tab-title-wrap active\" style=\"--ub-tabbed-title-background-color: #6d6d6d; --ub-tabbed-active-title-color: inherit; --ub-tabbed-active-title-background-color: #6d6d6d; text-align: center; \" tabindex=\"-1\">\n\t\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-title\">R<\/div>\n\t\t\t<\/div><\/div>\n\t\t\t<\/div>\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tabs-content\" style=\"\"><div role=\"tabpanel\" class=\"wp-block-ub-tabbed-content-tab-content-wrap active\" id=\"ub-tabbed-content-4fd4d5ee-c9d4-4beb-b6ad-9effba83db16-panel-0\" aria-labelledby=\"ub-tabbed-content-4fd4d5ee-c9d4-4beb-b6ad-9effba83db16-tab-0\" tabindex=\"0\">\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting='{\"showPanel\":true,\"languageLabel\":\"language\",\"fullScreenButton\":true,\"copyButton\":true,\"mode\":\"r\",\"mime\":\"text\/x-rsrc\",\"theme\":\"material\",\"lineNumbers\":false,\"styleActiveLine\":false,\"lineWrapping\":false,\"readOnly\":true,\"fileName\":\"\",\"language\":\"R\",\"maxHeight\":\"400px\",\"modeName\":\"r\"}'>(light_importance(fls, v = x) %&gt;%\n    plot(fill = \"darkred\") +\n    labs(title = \"Permutation importance\", y = \"Drop in RMSE\")) %&gt;%\n  ggplotly()<\/pre><\/div>\n\n<\/div><\/div>\n\t\t<\/div>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"982\" height=\"706\" src=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-4.png\" alt=\"\" class=\"wp-image-779\" srcset=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-4.png 982w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-4-300x216.png 300w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-4-768x552.png 768w\" sizes=\"auto, (max-width: 982px) 100vw, 982px\" \/><figcaption>Variable importance (png)<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">ICE plot<\/h4>\n\n\n\n<p>To get an impression of the effect of the living area, we select 200 observations and profile their predictions with increasing (log) living area, keeping everything else fixed (Ceteris Paribus). These ICE (individual conditional expectation) plots are vertically centered in order to highlight potential interaction effects. If all curves coincide, there are no interaction effects and we can say that the effect of the feature is modelled in an additive way (no surprise for the additive linear mixed-effects model).<\/p>\n\n\n<div class=\"wp-block-ub-tabbed-content wp-block-ub-tabbed-content-holder wp-block-ub-tabbed-content-horizontal-holder-mobile wp-block-ub-tabbed-content-horizontal-holder-tablet\" id=\"ub-tabbed-content-84984d4c-d478-405d-9aab-91938103e914\" style=\"\">\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-holder horizontal-tab-width-mobile horizontal-tab-width-tablet\">\n\t\t\t\t<div role=\"tablist\" class=\"wp-block-ub-tabbed-content-tabs-title wp-block-ub-tabbed-content-tabs-title-mobile-horizontal-tab wp-block-ub-tabbed-content-tabs-title-tablet-horizontal-tab\" style=\"justify-content: flex-start; \"><div role=\"tab\" id=\"ub-tabbed-content-84984d4c-d478-405d-9aab-91938103e914-tab-0\" aria-controls=\"ub-tabbed-content-84984d4c-d478-405d-9aab-91938103e914-panel-0\" aria-selected=\"true\" class=\"wp-block-ub-tabbed-content-tab-title-wrap active\" style=\"--ub-tabbed-title-background-color: #6d6d6d; --ub-tabbed-active-title-color: inherit; --ub-tabbed-active-title-background-color: #6d6d6d; text-align: center; \" tabindex=\"-1\">\n\t\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-title\">R<\/div>\n\t\t\t<\/div><\/div>\n\t\t\t<\/div>\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tabs-content\" style=\"\"><div role=\"tabpanel\" class=\"wp-block-ub-tabbed-content-tab-content-wrap active\" id=\"ub-tabbed-content-84984d4c-d478-405d-9aab-91938103e914-panel-0\" aria-labelledby=\"ub-tabbed-content-84984d4c-d478-405d-9aab-91938103e914-tab-0\" tabindex=\"0\">\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting='{\"showPanel\":true,\"languageLabel\":\"language\",\"fullScreenButton\":true,\"copyButton\":true,\"mode\":\"r\",\"mime\":\"text\/x-rsrc\",\"theme\":\"material\",\"lineNumbers\":false,\"styleActiveLine\":false,\"lineWrapping\":false,\"readOnly\":true,\"fileName\":\"\",\"language\":\"R\",\"maxHeight\":\"400px\",\"modeName\":\"r\"}'>(light_ice(fls, v = \"log_sqft_living\", n_max = 200, center = \"middle\") %&gt;%\n    plot(alpha = 0.05, color = \"darkred\") +\n    labs(title = \"Centered ICE plot\", y = \"log_price (shifted)\")) %&gt;%\n  ggplotly()<\/pre><\/div>\n\n<\/div><\/div>\n\t\t<\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"707\" src=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-7-1024x707.png\" alt=\"\" class=\"wp-image-783\" srcset=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-7-1024x707.png 1024w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-7-300x207.png 300w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-7-768x530.png 768w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-7.png 1030w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Partial dependence plots<\/h4>\n\n\n\n<p>Averaging many uncentered ICE curves provides the famous partial dependence plot, introduced in Friedman\u2019s seminal paper on gradient boosting machines (2001).<\/p>\n\n\n<div class=\"wp-block-ub-tabbed-content wp-block-ub-tabbed-content-holder wp-block-ub-tabbed-content-horizontal-holder-mobile wp-block-ub-tabbed-content-horizontal-holder-tablet\" id=\"ub-tabbed-content-5e8317c3-0be7-4ab6-82db-ac91b9f5cafe\" style=\"\">\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-holder horizontal-tab-width-mobile horizontal-tab-width-tablet\">\n\t\t\t\t<div role=\"tablist\" class=\"wp-block-ub-tabbed-content-tabs-title wp-block-ub-tabbed-content-tabs-title-mobile-horizontal-tab wp-block-ub-tabbed-content-tabs-title-tablet-horizontal-tab\" style=\"justify-content: flex-start; \"><div role=\"tab\" id=\"ub-tabbed-content-5e8317c3-0be7-4ab6-82db-ac91b9f5cafe-tab-0\" aria-controls=\"ub-tabbed-content-5e8317c3-0be7-4ab6-82db-ac91b9f5cafe-panel-0\" aria-selected=\"true\" class=\"wp-block-ub-tabbed-content-tab-title-wrap active\" style=\"--ub-tabbed-title-background-color: #6d6d6d; --ub-tabbed-active-title-color: inherit; --ub-tabbed-active-title-background-color: #6d6d6d; text-align: center; \" tabindex=\"-1\">\n\t\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-title\">R<\/div>\n\t\t\t<\/div><\/div>\n\t\t\t<\/div>\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tabs-content\" style=\"\"><div role=\"tabpanel\" class=\"wp-block-ub-tabbed-content-tab-content-wrap active\" id=\"ub-tabbed-content-5e8317c3-0be7-4ab6-82db-ac91b9f5cafe-panel-0\" aria-labelledby=\"ub-tabbed-content-5e8317c3-0be7-4ab6-82db-ac91b9f5cafe-tab-0\" tabindex=\"0\">\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting='{\"showPanel\":true,\"languageLabel\":\"language\",\"fullScreenButton\":true,\"copyButton\":true,\"mode\":\"r\",\"mime\":\"text\/x-rsrc\",\"theme\":\"material\",\"lineNumbers\":false,\"styleActiveLine\":false,\"lineWrapping\":false,\"readOnly\":true,\"fileName\":\"\",\"language\":\"R\",\"maxHeight\":\"400px\",\"modeName\":\"r\"}'>(light_profile(fls, v = \"log_sqft_living\", n_bins = 21) %&gt;%\n    plot(rotate_x = FALSE) +\n    labs(title = \"Partial dependence plot\", y = y) +\n    scale_colour_viridis_d(begin = 0.2, end = 0.8)) %&gt;%\n  ggplotly()<\/pre><\/div>\n\n<\/div><\/div>\n\t\t<\/div>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"999\" height=\"723\" src=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-6.png\" alt=\"\" class=\"wp-image-782\" srcset=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-6.png 999w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-6-300x217.png 300w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-6-768x556.png 768w\" sizes=\"auto, (max-width: 999px) 100vw, 999px\" \/><figcaption>Partial dependence plots (png)<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Multiple effects visualized together<\/h4>\n\n\n\n<p>The last figure extends the partial dependence plot with three additional curves, all evaluated on the hold-out dataset:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Average observed values<\/li><li>Average predictions<\/li><li>ALE plot (\u201caccumulated local effects\u201d, an alternative to partial dependence plots with relaxed Ceteris Paribus assumption)<\/li><\/ul>\n\n\n<div class=\"wp-block-ub-tabbed-content wp-block-ub-tabbed-content-holder wp-block-ub-tabbed-content-horizontal-holder-mobile wp-block-ub-tabbed-content-horizontal-holder-tablet\" id=\"ub-tabbed-content-8b5493d8-7b17-4c70-8daf-935d655c13bb\" style=\"\">\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-holder horizontal-tab-width-mobile horizontal-tab-width-tablet\">\n\t\t\t\t<div role=\"tablist\" class=\"wp-block-ub-tabbed-content-tabs-title wp-block-ub-tabbed-content-tabs-title-mobile-horizontal-tab wp-block-ub-tabbed-content-tabs-title-tablet-horizontal-tab\" style=\"justify-content: flex-start; \"><div role=\"tab\" id=\"ub-tabbed-content-8b5493d8-7b17-4c70-8daf-935d655c13bb-tab-0\" aria-controls=\"ub-tabbed-content-8b5493d8-7b17-4c70-8daf-935d655c13bb-panel-0\" aria-selected=\"true\" class=\"wp-block-ub-tabbed-content-tab-title-wrap active\" style=\"--ub-tabbed-title-background-color: #6d6d6d; --ub-tabbed-active-title-color: inherit; --ub-tabbed-active-title-background-color: #6d6d6d; text-align: center; \" tabindex=\"-1\">\n\t\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-title\">R<\/div>\n\t\t\t<\/div><\/div>\n\t\t\t<\/div>\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tabs-content\" style=\"\"><div role=\"tabpanel\" class=\"wp-block-ub-tabbed-content-tab-content-wrap active\" id=\"ub-tabbed-content-8b5493d8-7b17-4c70-8daf-935d655c13bb-panel-0\" aria-labelledby=\"ub-tabbed-content-8b5493d8-7b17-4c70-8daf-935d655c13bb-tab-0\" tabindex=\"0\">\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting='{\"showPanel\":true,\"languageLabel\":\"language\",\"fullScreenButton\":true,\"copyButton\":true,\"mode\":\"r\",\"mime\":\"text\/x-rsrc\",\"theme\":\"material\",\"lineNumbers\":false,\"styleActiveLine\":false,\"lineWrapping\":false,\"readOnly\":true,\"fileName\":\"\",\"language\":\"R\",\"maxHeight\":\"400px\",\"modeName\":\"r\"}'>(light_effects(fls, v = \"log_sqft_living\", n_bins = 21) %&gt;%\n    plot(use = \"all\")  +\n    labs(title = \"Different effect estimates\", y = y) +\n    scale_colour_viridis_d(begin = 0.2, end = 0.8)) %&gt;%\n  ggplotly()<\/pre><\/div>\n\n<\/div><\/div>\n\t\t<\/div>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1006\" height=\"703\" src=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-5.png\" alt=\"\" class=\"wp-image-781\" srcset=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-5.png 1006w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-5-300x210.png 300w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2022\/04\/image-5-768x537.png 768w\" sizes=\"auto, (max-width: 1006px) 100vw, 1006px\" \/><figcaption>Multiple effects together (png)<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Combining&nbsp;<code>flashlight<\/code>&nbsp;with&nbsp;<code>plotly<\/code>&nbsp;works well and provides nice, interactive plots. Using <code>rmarkdown<\/code>, an analysis like this look quite neat if shipped as an HTML like this one here: <a href=\"https:\/\/mayer79.github.io\/flashlight_plotly\/flashlight_plotly.html\">https:\/\/mayer79.github.io\/flashlight_plotly\/flashlight_plotly.html<\/a><\/p>\n\n\n\n<p>The rmarkdown script can be found here on <a href=\"https:\/\/github.com\/mayer79\/flashlight_plotly\">github<\/a>. <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to combine model interpretation package &#8220;flashlight&#8221; with &#8220;plotly&#8221;?<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,17,9],"tags":[5],"class_list":["post-772","post","type-post","status-publish","format-standard","hentry","category-machine-learning","category-programming","category-statistics","tag-r"],"featured_image_src":null,"author_info":{"display_name":"Michael Mayer","author_link":"https:\/\/lorentzen.ch\/index.php\/author\/michael\/"},"_links":{"self":[{"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/posts\/772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/comments?post=772"}],"version-history":[{"count":11,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/posts\/772\/revisions"}],"predecessor-version":[{"id":788,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/posts\/772\/revisions\/788"}],"wp:attachment":[{"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/media?parent=772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/categories?post=772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/tags?post=772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}