imlplots
is an R package that provides an interactive Shiny dashboard for three kinds of Interpretable Machine Learning (IML) plots
library(knitr) opts_chunk$set(echo=TRUE, warning=FALSE, message=FALSE) set.seed(42) load("../data/boston.rda") load("../data/fire.rda") load("../data/titanic.rda") library(mlr) library(png) library(grid)
The package can be installed directly from github with devtools
# install.packages("devtools") devtools::install_github('juliafried/imlplots') library(imlplots)
You can fit classification and regression problems from the mlr
package and analyse possible interaction effects in a Shiny dasbhoard.
For quickstart we take the popular Boston Housing data, where we want to predict the median housing price in Boston.
print(summarizeColumns(boston)[, -c(5, 6, 7)], digits = 4)
For using imlplots
Shiny dashboard, three input arguments need to be specified
data
- the input datatask
- the learning taskmodels
- one or several trained modelsWe create a regression task with medv
as target variable.
The task structure is determined by mlr
package.
boston.task = makeRegrTask(data = boston, target = "medv")
The imlplots
dashboard allows the comparison of multiple learning algorithms, therefore we fit two different models - first a random forest and second a GLM.
rf.mod = train("regr.randomForest", boston.task) glm.mod = train("regr.glm", boston.task)
The input for the Shiny app is a list of learners.
mod.list = list(rf.mod, glm.mod)
Now the Shiny app can be used.
imlplots(data = boston, task = boston.task, models = mod.list)
library(imlplots) boston.task = makeRegrTask(data = boston, target = "medv") rf.mod = train("regr.randomForest", boston.task) glm.mod = train("regr.glm", boston.task) mod.list = list(rf.mod, glm.mod) imlplots(data = boston, task = boston.task, models = mod.list)
To show how you can use the imlplots
Shiny app for regression tasks we use fire data, where the burned area of forests due to fires should be analyzed.
print(summarizeColumns(fire)[, -c(5, 6, 7)], digits = 4)
The target variable is area
, which is between 0.00 and 1090.84 ha.
summary(fire$area)
We create a regression task with target variable area
.
fire.task = makeRegrTask(data = fire, target = "area")
Next we train several mlr
models and save them in a list of models.
Note: The order in your model list will determine the model order in the Shiny dashboard.
fire.rf = train("regr.randomForest", fire.task) fire.glm = train("regr.glm", fire.task) fire.svm = train("regr.svm", fire.task) mod.list = list(fire.rf, fire.glm, fire.svm)
No we can open the imlplots
Shiny app.
imlplots(data = fire, task = fire.task, models = mod.list)
The Shiny dashboard contains four tabs
The Data
tab shows your input data. This data is taken to generate IML plots.
If you want to check how changes in the data effect your plot, you can simply filter in the Data
tab.
knitr::include_graphics('pictures/data.PNG')
For filtering two options are given
Plot all sampled observations
: In this setting you can filter via the filters beneath the column titles and all rows will be used for plotting.Plot indiviudal observations
: In this setting after using the filters, you have to manually select specific rows.The next tab Settings
contains all possible plot settings and the selected IML plot.
knitr::include_graphics('pictures/settings1.PNG')
There are various settings
Select graphics package
: You can select the graphics package - we offer ggplot2
and plotly
. Use ggplot2
if your computer is not the fastest one.Choose predictive model
: Choose one of your fitted models. The order in the dropdown is the order of your list.Choose plot type
: We offer PDP, ICE and ALE plots
If you select ICE plot, you will get a new selection field. Possible are centered
and regular
ICE plotsVariable of interest
: This dropdown will determine the x-axis of your plot and will determine the effect that is plotted On the right side of the dashboard page, the selected plot is shown.
To check out effects, you can turn on Select adjustable features
. This option allows you to set one of the variables to a specifc value.
knitr::include_graphics('pictures/settings2.PNG')
It is also possible to change the number of knots and lines (individual observations) with the shown sliders.
The ICE plot contains all sampled, individual observations in blue. The red line is from PDP.
knitr::include_graphics('pictures/settings3.PNG')
As described above, you can select between Regular
and Centered
ICE plots.
knitr::include_graphics('pictures/settings4.PNG')
The ALE plot can be selected, too. Please keep in mind, that the ALE plot has a different y-axis than the PDP and ICE plot.
knitr::include_graphics('pictures/settings5.PNG')
For ALE plots you can swith between two ALE Plot Modes
. The Main Effets
mode allows you to select one variable of interest and shows its interaction effect.
The Second Order Effects
setting allows to select another ALE interaction variable
and therefore shows the effect for this extra variable too.
If you select plotly
as graphics package, the second order effects ALE plot will be a 3D plot.
knitr::include_graphics('pictures/ale3d.PNG')
The third tab Plots
shows the selected IML plot in full screen via the sub-tab Zoomed plot
.
The sub-tab Scatterplot
shows the filtered and unfiltered scatterplot between the variable of interest
and the target
variable of the model.
In the Data
tab we filtered for a high value of burned area and selected three individual observations.
knitr::include_graphics('pictures/plots1.PNG')
The filtered data scatterplot shows the selected high area values and also the three individual observations (in red).
knitr::include_graphics('pictures/plots2.PNG')
The unfiltered data scatterplot shows all data points and also the three individual observations (in red).
The fourth tab Learner Summary
shows the currently selected learner summary. If you want to see another summary, you have to select another model in the Settings
tab.
knitr::include_graphics('pictures/learner1.PNG')
library(imlplots) fire.task = makeRegrTask(data = fire, target = "area") fire.rf = train("regr.randomForest", fire.task) fire.glm = train("regr.glm", fire.task) fire.svm = train("regr.svm", fire.task) mod.list = list(fire.rf, fire.glm, fire.svm) imlplots(data = fire, task = fire.task, models = mod.list)
For the classification example only the differences to the regression example will be explained. We use the titanic data set, where the aim is to predict the survival chance.
print(summarizeColumns(titanic)[, -c(5, 6, 7)], digits = 4)
Again we create a task and fit a model.
library(imlplots) titanic.task = makeClassifTask(data = titanic, target = "Survived") titanic.rf = train("classif.randomForest", titanic.task)
Next we open the Shiny dashboard.
imlplots(data = titanic, task = titanic.task, titanic.rf)
This time it is useful to select plotly
in the Select graphics package
dropdown.
knitr::include_graphics('pictures/settings6.PNG')
This allows you to deselect single classes to increase the visability of individual lines, which is very useful for ICE plot.
knitr::include_graphics('pictures/settings7.PNG')
knitr::include_graphics('pictures/settings8.PNG')
Please note that there is no second-order ALE plot for classification tasks.
library(imlplots) titanic.task = makeClassifTask(data = titanic, target = "Survived") titanic.rf = train("classif.randomForest", titanic.task) imlplots(data = titanic, task = titanic.task, titanic.rf)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.