counterfactuals: An R Package for Counterfactual Explanation Methods

Susanne Dandl, Andreas Hofheinz, Martin Binder, Bernd Bischl, Giuseppe CasalicchioarXiv 2023 · DOI

Abstract

Counterfactual explanations provide human-centric insights into machine learning models by suggesting actionable changes to flip a prediction. Despite their utility, the R ecosystem has lacked a unified framework for these methods. We introduce the counterfactuals R package, which implements and extends three major algorithms—MOC, WhatIf, and NICE—supporting classification and regression tasks through a modular R6 interface.

Introduction: The Implementation Gap

In the field of Interpretable Machine Learning (IML), counterfactual explanations are considered the gold standard for human-friendly insights. Rather than presenting complex weight matrices or abstract importance scores, a counterfactual provides a concrete scenario: "You were denied the loan because your income is £30k; if it were £45k, you would have been approved."

Despite the proliferation of research in this area, a significant "implementation gap" exists. Most methods are confined to specific Python scripts or restricted to certain model types. The counterfactuals package addresses this by providing a common R6-based framework, extending methods to support multiclass classification and regression, and focusing on generating diverse sets of explanations rather than single points.

The counterfactuals Framework

The package is built on a modular R6 class structure that integrates seamlessly with the iml package's Predictor objects. This modularity allows researchers to implement new algorithms by simply defining a $run() method, while the package handles evaluation, visualization, and data management.

Core Algorithms

The package implements and extends three primary methods:

  • MOC (Multi-objective Counterfactuals): This method treats explanation discovery as a multi-objective optimization problem. It balances four goals: Validity (flipping the prediction), Proximity (closeness to the original point), Sparsity (minimizing changed features), and Plausibility (ensuring the result is realistic). It utilizes the NSGA-II genetic algorithm to find a Pareto set of optimal trade-offs.
  • WhatIf: A fast baseline that searches training data for the most similar observations with the desired outcome. The authors extended this to return multiple similar points.
  • NICE (Nearest Instance Counterfactual Explanations): This method begins with a "nearest neighbor" from the data and iteratively restores original feature values to find the most minimal change that maintains the desired prediction.

Benchmarking and Performance

The authors evaluated the package using six diverse datasets from OpenML, including credit risk, diabetes, and sensor data. The results highlighted clear trade-offs between the methods:

  • MOC consistently produced the highest-quality and most diverse counterfactuals but required the most computational resources.
  • WhatIf proved to be the most scalable and fastest method, making it ideal for time-critical applications, though it often suggested changing more features than necessary (lower sparsity).
  • NICE offered a middle ground but struggled significantly with high-dimensional data. For instance, on the hill_valley dataset ($p=100$), NICE ran 17 times longer than MOC due to its iterative search process.

Conclusion

The counterfactuals package provides a robust toolkit for R users to explore model behavior through a "Pareto" philosophy—recognizing that the best explanation depends on the specific trade-offs a user is willing to make. Future development will focus on integrating causal graphs to ensure changes are physically possible (e.g., age and work experience) and refining complex actionability constraints.

Summary updated

BibTeX
@misc{dandl2023counterfactuals,
  author = {Susanne Dandl and Andreas Hofheinz and Martin Binder and Bernd Bischl and Giuseppe Casalicchio},
  title = {{counterfactuals: An R Package for Counterfactual Explanation Methods}},
  year = {2023},
  eprint = {2304.06569},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2304.06569},
}
Figure