Learn R Programming

⚠️There's a newer version (0.9.0) of this package.Take me there.

The flashlight package

The goal of this package is shed light on black box machine learning models.

The main props of flashlight:

  1. It is simple, yet flexible.

  2. It offers model agnostic tools like model performance, variable importance, global surrogate models, ICE profiles, partial dependence, ALE, and further effects plots, scatter plots, interaction strength, and variable contribution breakdown/SHAP for single observations.

  3. It allows to assess multiple models in parallel.

  4. It supports "group by" operations.

  5. It works with case weights.

Currently, models with numeric or binary response are supported.

Installation

From CRAN:

install.packages("flashlight")

Latest version from github:

library(devtools)
install_github("mayer79/flashlight", subdir = "release/flashlight")

Example Code

library(MetricsWeighted)
library(flashlight)

# Fit model
fit <- lm(Sepal.Length ~ ., data = iris)

# Make flashlight
fl <- flashlight(
  model = fit, 
  data = iris, 
  y = "Sepal.Length", 
  label = "ols",               
  metrics = list(rmse = rmse, `R-squared` = r_squared)
)

# Performance: rmse and R-squared
plot(light_performance(fl), fill = "darkred")
plot(light_performance(fl, by = "Species"), fill = "darkred")

# Variable importance by increase in rmse
imp <- light_importance(fl, m_repetitions = 4)
plot(imp, fill = "darkred")

# ICE profiles for Petal.Width
plot(light_ice(fl, v = "Petal.Width"))
plot(light_ice(fl, v = "Petal.Width", center = "first"))

# Partial dependence profiles for Petal.Width
plot(light_profile(fl, v = "Petal.Width"))
plot(light_profile(fl, v = "Petal.Width", by = "Species"))

# 2D partial dependence
plot(light_profile2d(fl, v = c("Petal.Width", "Petal.Length")))

# Accumulated local effects (ALE) profiles for Petal.Width
plot(light_profile(fl, v = "Petal.Width", type = "ale"))

# Prediction, response and residual profiles, e.g.
plot(light_profile(fl, v = "Petal.Width", type = "residual"))

# All in one...
plot(light_effects(fl, v = "Petal.Width"), use = "all")

# Scatter plots
plot(light_scatter(fl, v = "Petal.Width", type = "predicted"))

# Variable contribution breakdown for single observation
plot(light_breakdown(fl, new_obs = iris[2, ]))

# Global surrogate
plot(light_global_surrogate(fl))

Check out the vignette to see the full capabilities of the package.

Copy Link

Version

Install

install.packages('flashlight')

Monthly Downloads

446

Version

0.8.0

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Michael Mayer

Last Published

April 21st, 2021

Functions in flashlight (0.8.0)

add_shap

Add SHAP values to (multi-)flashlight
auto_cut

Discretizes a Vector
grouped_stats

Grouped Weighted Means, Quartiles, or Variances
cut3

Modified cut
grouped_counts

Grouped count
ale_profile

ALE profile
common_breaks

Common Breaks for multiflashlight
all_identical

all_identical
flashlight

Create or Update a flashlight
grouped_center

Grouped, weighted mean centering
light_combine

Combine Objects
light_breakdown

Variable Contribution Breakdown for Single Observation
plot.light_effects

Visualize Multiple Types of Profiles Together
light_check

Check flashlight
light_profile

Partial Dependence and other Profiles
predict.multiflashlight

Predictions for multiflashlight
plot.light_profile

Visualize Profiles, e.g. of Partial Dependence
light_performance

Model Performance of Flashlight
print.flashlight

Prints a flashlight
plot.light_performance

Visualize Model Performance
plot.light_global_surrogate

Plot Global Surrogate Trees
response

Response of multi/-flashlight
light_importance

Variable Importance
light_interaction

Interaction Strength
grouped_weighted_mean

Fast Grouped Weighted Mean
light_effects

Combination of Response, Predicted, Partial Dependence, and ALE profiles.
light_profile2d

2D Partial Dependence and other 2D Profiles
light_scatter

Scatter
plot.light_profile2d

Visualize 2D-Profiles, e.g. of Partial Dependence
is.flashlight

Check functions for flashlight Classes
plot.light_scatter

Scatter Plot
most_important

Most Important Variables.
light_recode

Recode Factor Columns
plot_counts

Add Counts to Effects Plot
predict.flashlight

Predictions for flashlight
plot.light_importance

Visualize Variable Importance
plot.light_ice

Visualize ICE profiles
light_global_surrogate

Global Surrogate Tree
residuals.flashlight

Residuals for flashlight
residuals.multiflashlight

Residuals for multiflashlight
light_ice

Individual Conditional Expectation (ICE)
multiflashlight

Create or Update a multiflashlight
plot.light_breakdown

Visualize Variable Contribution Breakdown for Single Observation
print.multiflashlight

Prints a multiflashlight
print.light

Prints light Object