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 = 10)
plot(imp, fill = "darkred")
plot(light_importance(fl, by = "Species")) +
   scale_fill_viridis_d(begin = 0.2, end = 0.8)
most_important(imp, 2)

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

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

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

# Prediction, response and residual profiles
plot(light_profile(fl, v = "Petal.Width", type = "response", stats = "quartiles"))
plot(light_profile(fl, v = "Petal.Width", type = "predicted"))
plot(light_profile(fl, v = "Petal.Width", type = "residual", stats = "quartiles"))

# Response profiles, prediction profiles, partial depencence, and ALE profiles 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.7.5

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Michael Mayer

Last Published

February 13th, 2021

Functions in flashlight (0.7.5)

common_breaks

Common Breaks for multiflashlight
all_identical

all_identical
auto_cut

Discretizes a Vector
grouped_stats

Grouped Weighted Means, Quartiles, or Variances
cut3

Modified cut
light_combine

Combine Objects
light_recode

Recode Factor Columns
light_global_surrogate

Global Surrogate Tree
light_effects

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

Scatter
light_profile

Partial Dependence and other Profiles
add_shap

Add SHAP values to (multi-)flashlight
multiflashlight

Create or Update a multiflashlight
predict.flashlight

Predictions for flashlight
plot.light_breakdown

Visualize Variable Contribution Breakdown for Single Observation
ale_profile

ALE profile
predict.multiflashlight

Predictions for multiflashlight
print.light

Prints light Object
print.flashlight

Prints a flashlight
print.multiflashlight

Prints a multiflashlight
light_check

Check flashlight
light_performance

Model Performance of Flashlight
light_breakdown

Variable Contribution Breakdown for Single Observation
is.flashlight

Check functions for flashlight Classes
grouped_center

Grouped, weighted mean centering
grouped_weighted_mean

Fast Grouped Weighted Mean
flashlight

Create or Update a flashlight
plot.light_importance

Visualize Variable Importance
residuals.flashlight

Residuals for flashlight
plot.light_effects

Visualize Multiple Types of Profiles Together
plot.light_scatter

Scatter Plot
light_ice

Individual Conditional Expectation (ICE)
plot.light_global_surrogate

Plot Global Surrogate Trees
light_importance

Variable Importance
light_interaction

Interaction Strength
most_important

Most Important Variables.
midpoints

Midpoints
plot.light_performance

Visualize Model Performance
plot.light_profile

Visualize Profiles, e.g. of Partial Dependence
residuals.multiflashlight

Residuals for multiflashlight
plot_counts

Add Counts to Effects Plot
response

Response of multi/-flashlight
grouped_counts

Grouped count
plot.light_ice

Visualize ICE profiles