counterfactuals

counterfactuals: An R package for Counterfactual Explanation Methods

https://github.com/dandls/counterfactuals

Science Score: 36.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
    Found 2 DOI reference(s) in README
  • Academic publication links
    Links to: arxiv.org, springer.com
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (18.2%) to scientific vocabulary

Keywords

interpretable-machine-learning local-explanations model-agnostic-explanations
Last synced: 6 months ago · JSON representation

Repository

counterfactuals: An R package for Counterfactual Explanation Methods

Basic Info
Statistics
  • Stars: 24
  • Watchers: 4
  • Forks: 4
  • Open Issues: 3
  • Releases: 0
Topics
interpretable-machine-learning local-explanations model-agnostic-explanations
Created almost 5 years ago · Last pushed over 1 year ago
Metadata Files
Readme License

README.Rmd

---
output: github_document
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  fig.height = 3,
  fig.width = 8
)
options(width = 200)
set.seed(12345)
```

# counterfactuals


[![R-CMD-check](https://github.com/dandls/counterfactuals/workflows/R-CMD-check/badge.svg)](https://github.com/dandls/counterfactuals/actions)


The `counterfactuals` package provides various (model-agnostic) counterfactual explanation methods via a unified R6-based interface.

Counterfactual explanation methods address questions of the form: 
"For input $\mathbf{x^{\star}}$, the model predicted $y$. What needs to be changed in $\mathbf{x^{\star}}$ for the model
to predict a desired outcome $\tilde{y}$ instead?". \
Denied loan applications serve as a common example; here a counterfactual explanation (or counterfactual for short) could be: 
"The loan was denied because the amount of €30k is too high 
given the income. If the amount had been €20k, the loan would have been granted."

For an introduction to counterfactual explanation methods, we recommend Chapter 9.3 of the [Interpretable Machine Learning book](https://christophm.github.io/interpretable-ml-book/) by Christoph Molnar. The package is based on the R code underlying the paper [Multi-Objective Counterfactual Explanations
    (MOC)](https://link.springer.com/chapter/10.1007/978-3-030-58112-1_31).

## Available methods

The following counterfactual explanation methods are currently implemented:

- [Multi-Objective Counterfactual Explanations (MOC)](https://link.springer.com/chapter/10.1007/978-3-030-58112-1_31) 
- [Nearest Instance Counterfactual Explanations (NICE)](https://arxiv.org/abs/2104.07411) (an extended version)
- [WhatIf](https://arxiv.org/abs/1907.04135) (an extended version)

## Installation

You can install the development version from [GitHub](https://github.com/) with:

``` r
# install.packages("devtools")
devtools::install_github("dandls/counterfactuals")
```
## Get started

In this example, we train a `randomForest` on the `iris` dataset and examine how a given `virginica` observation 
would have to change in order to be classified as `versicolor`. 

```{r example, message=FALSE}
library(counterfactuals)
library(randomForest)
library(iml)
```
### Fitting a model

First, we train a `randomForest` model to predict the target variable `Species`, omitting one observation from the training 
data, which is `x_interest` (the observation $x^{\star}$ for which we want to find counterfactuals).
```{r}
rf = randomForest(Species ~ ., data = iris[-150L, ])
```

### Setting up an iml::Predictor() object

We then create an [`iml::Predictor`](https://christophm.github.io/iml/reference/Predictor.html) object, which serves as 
a wrapper for different model types; it contains the model and the data for its analysis.

```{r}
predictor = Predictor$new(rf, type = "prob")
```

### Find counterfactuals

For `x_interest`, the model predicts a probability of 8% for class `versicolor`.

```{r}
x_interest = iris[150L, ]
predictor$predict(x_interest)
```

Now, we examine what needs to be changed in `x_interest` so that the model predicts a probability of at least 50% for class `versicolor`.

Here, we want to apply WhatIf and since it is a classification task, we create a `WhatIfClassif` object.

```{r}
wi_classif = WhatIfClassif$new(predictor, n_counterfactuals = 5L)
```


Then, we use the `find_counterfactuals()` method to find counterfactuals for `x_interest`.

```{r}
cfactuals = wi_classif$find_counterfactuals(
  x_interest, desired_class = "versicolor", desired_prob = c(0.5, 1)
)
```

### The counterfactuals object

`cfactuals` is a `Counterfactuals` object that contains the counterfactuals and has several methods for their 
evaluation and visualization.

```{r}
cfactuals
```

The counterfactuals are stored in the `data` field.
```{r}
cfactuals$data
```


With the `evaluate()` method, we can evaluate the counterfactuals using various quality measures.
```{r}
cfactuals$evaluate()
```

One visualization option is to plot the frequency of feature changes across all counterfactuals using the
`plot_freq_of_feature_changes()` method.

```{r, fig.height=2}
cfactuals$plot_freq_of_feature_changes()
```

Another visualization option is a parallel plot---created with the `plot_parallel()` method---that connects the (scaled) 
feature values of each counterfactual and highlights `x_interest` in blue.

```{r, fig.height=2.5, message=FALSE}
cfactuals$plot_parallel()
```


Owner

  • Name: Susanne Dandl
  • Login: dandls
  • Kind: user
  • Location: Munich
  • Company: Ludwig-Maximilians-Universität

GitHub Events

Total
  • Watch event: 1
  • Push event: 4
Last Year
  • Watch event: 1
  • Push event: 4

Committers

Last synced: almost 3 years ago

All Time
  • Total Commits: 376
  • Total Committers: 3
  • Avg Commits per committer: 125.333
  • Development Distribution Score (DDS): 0.311
Top Committers
Name Email Commits
Andreas a****z@o****m 259
susanne-207 d****e@g****m 116
Rifat a****t@g****m 1

Issues and Pull Requests

Last synced: 7 months ago

All Time
  • Total issues: 17
  • Total pull requests: 27
  • Average time to close issues: 5 months
  • Average time to close pull requests: about 1 month
  • Total issue authors: 8
  • Total pull request authors: 5
  • Average comments per issue: 1.06
  • Average comments per pull request: 0.3
  • Merged pull requests: 23
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 1
  • Pull requests: 2
  • Average time to close issues: N/A
  • Average time to close pull requests: about 12 hours
  • Issue authors: 1
  • Pull request authors: 2
  • Average comments per issue: 2.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 2
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • dandls (5)
  • andreash0 (4)
  • mb706 (2)
  • giuseppec (1)
  • RifatMehreen (1)
  • pfistfl (1)
  • be-marc (1)
  • larskotthoff (1)
Pull Request Authors
  • dandls (19)
  • andreash0 (9)
  • mb706 (3)
  • giuseppec (2)
  • RifatMehreen (1)
Top Labels
Issue Labels
enhancement (4) bug (2) urgent (1)
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 350 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 5
  • Total maintainers: 1
cran.r-project.org: counterfactuals

Counterfactual Explanations

  • Versions: 5
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 350 Last month
Rankings
Forks count: 21.9%
Stargazers count: 22.5%
Average: 29.3%
Dependent packages count: 29.8%
Dependent repos count: 35.5%
Downloads: 36.7%
Last synced: 6 months ago

Dependencies

DESCRIPTION cran
  • R >= 3.5.0 depends
  • R6 * imports
  • StatMatch * imports
  • bbotk * imports
  • checkmate * imports
  • data.table * imports
  • ecr * imports
  • iml * imports
  • miesmuschel * imports
  • paradox * imports
  • GGally * suggests
  • MASS * suggests
  • basefun * suggests
  • covr * suggests
  • cowplot * suggests
  • featureTweakR * suggests
  • ggplot2 * suggests
  • gower * suggests
  • keras * suggests
  • knitr * suggests
  • mlbench * suggests
  • partykit * suggests
  • randomForest * suggests
  • rchallenge * suggests
  • rmarkdown * suggests
  • testthat * suggests
  • trtf * suggests
.github/workflows/R-CMD-check.yaml actions
  • actions/cache v2 composite
  • actions/checkout v2 composite
  • actions/upload-artifact main composite
  • r-lib/actions/setup-pandoc v1 composite
  • r-lib/actions/setup-r v1 composite
.github/workflows/pkgdown.yaml actions
  • actions/cache v2 composite
  • actions/checkout v2 composite
  • r-lib/actions/setup-pandoc v1 composite
  • r-lib/actions/setup-r v1 composite