mlr3fairness
mlr3 extension for Fairness in Machine Learning
Science Score: 26.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
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (15.3%) to scientific vocabulary
Keywords
fairness
machine-learning
mlr3
r
r-package
Last synced: 6 months ago
·
JSON representation
Repository
mlr3 extension for Fairness in Machine Learning
Basic Info
- Host: GitHub
- Owner: mlr-org
- License: lgpl-3.0
- Language: HTML
- Default Branch: main
- Homepage: https://mlr3fairness.mlr-org.com
- Size: 157 MB
Statistics
- Stars: 14
- Watchers: 4
- Forks: 2
- Open Issues: 7
- Releases: 1
Topics
fairness
machine-learning
mlr3
r
r-package
Created almost 5 years ago
· Last pushed 8 months ago
Metadata Files
Readme
Changelog
License
README.Rmd
---
output: github_document
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/"
)
```
```{r, include = FALSE}
lgr::get_logger("mlr3")$set_threshold("warn")
set.seed(1)
options(datatable.print.class = FALSE, datatable.print.keys = FALSE)
```
# [mlr3fairness](https://github.com/mlr-org/mlr3fairness)
Machine Learning Fairness Extension for [mlr3](https://github.com/mlr-org/mlr3).
[](https://github.com/mlr-org/mlr3fairness/actions/workflows/r-cmd-check.yml)
[](https://CRAN.R-project.org/package=mlr3fairness)
[](https://lmmisld-lmu-stats-slds.srv.mwn.de/mlr_invite/)
## Installation
Install the development version from github:
```r
remotes::install_github("mlr-org/mlr3fairness")
```
## Why should I care about fairness in machine learning?
Machine Learning model predictions can be skewed by a range of factors and thus might be considered unfair towards certain groups or individuals.
An example would be the COMPAS algorithm, which is a popular commercial algorithm used by judges and parole officers for scoring criminal defendant's
likelihood of reoffending (recidivism).
[Studies](https://www.propublica.org/article/machine-bias-risk-assessments-in-criminal-sentencing) have shown, that the algorithm might be biased in favor of white defendants.
Biases can occur in a large variety of situations where algorithms automate or support human decision making e.g. credit checks, automatic HR tools along with a variety of other domains.
The **goal of `mlr3fairness`** is to allow for auditing of `mlr3` learners, visualization and subsequently trying to improve fairness using debiasing strategies.
> :warning: **Note**
> Bias auditing and debiasing solely based on observational data **can not** guarantee fairness of a decision making system.
> Several biases, for example comming from the data can not be detected using the approaches implemented in `mlr3fairness`.
> This goal of this software is thus to allow for a better understanding and first hints at possible fairness problems in a
> studied model.
## Feature Overview
* [**Fairness Measures:**](#fairness-metrics) Audit algorithmms for fairness using a variety of fairness criteria. This also allows for designing custom criteria.
* [**Fairness Visualizations:**](#fairness-visualizations) Diagnose fairness problems through visualizations.
* [**Debiasing Methods:**](#debiasing-methods) Correct fairness problems in three lines of code.
* [**Fairness Report:**](#model-cards--datasheets) Obtain a report regarding an algorithm's fairness. (Under development)
**More Information**
- [Debiasing](https://mlr3fairness.mlr-org.com/articles/debiasing-vignette.html)
- [Fairness Metrics](https://mlr3fairness.mlr-org.com/articles/measures-vignette.html)
- [Visualizations](https://mlr3fairness.mlr-org.com/articles/visualization-vignette.html)
- [Reports](https://mlr3fairness.mlr-org.com/articles/reports-vignette.html)
### Protected Attribute
`mlr3fairness` requires information about the protected attribute wrt. which we want to assess fairness. This can be set via the `col_role` "pta" (protected attribute).
```{r, eval = FALSE}
task$col_roles$pta = "variable_name"
```
In case a non-categorical or more complex protected attribute is required, it can be manually computed and added to the task. `mlr3fairness` does not require specific types for `pta`, but will compute one metric for every unique value in the `pta` column.
### Fairness Metrics {#metrics}
`mlr3fairness` offers a variety of fairness metrics.
Metrics are prefixed with `fairness.` and can be found in the `msr()` dictionary.
Most fairness metrics are based on a difference between two protected groups (e.g. male and female) for a given metric (e.g. the false positive rate: `fpr`).
See [the vignette](https://textbook.coleridgeinitiative.org/chap-bias.html) for a more in-depth introduction to fairness metrics and how to choose them.
```{r}
library(mlr3)
library(mlr3fairness)
```
```{r, echo = FALSE}
knitr::kable(mlr3fairness:::mlr_measures_fairness)
```
Additional **custom fairness metrics** can be easily constructed, [the vignette](https://textbook.coleridgeinitiative.org/chap-bias.html) contains more details.
The `fairness_tensor()` function can be used with a `Prediction` in order to print group-wise confusion matrices for each protected attribute group.
We can furthermore measure fairrness in each group separately using `MeasureSubgroup` and `groupwise_metrics`.
### Fairness Visualizations {#viz}
Visualizations can be used with either a `Prediction`, `ResampleResult` or a `BenchmarkResult`.
For more information regarding those objects, refer to the [mlr3 book](https://mlr3book.mlr-org.com/).
- **fairness_accuracy_tradeoff**: Plot available trade-offs between fairness and model performance.
- **compare_metrics**: Compare fairness across models and cross-validation folds.
- **fairness_prediction_density**: Density plots for each protected attribute.
```{r, echo = FALSE, fig.width = 12}
library(mlr3learners)
t = tsk("adult_train")$filter(1:5000)
design = benchmark_grid(
tasks = t,
learners = lrns(c("classif.ranger", "classif.rpart"), predict_type = "prob"),
resamplings = rsmps("cv", folds = 3)
)
bmr = benchmark(design)
library(ggplot2)
library(patchwork)
p1 = fairness_accuracy_tradeoff(bmr, msr("fairness.fpr")) + ggtitle("fairness_accuracy_tradeoff")
p2 = compare_metrics(bmr, msrs(c("fairness.fpr", "fairness.tpr"))) + ggtitle("compare_metrics")
p3 = fairness_prediction_density(bmr) + ggtitle("fairness_prediction_density")
p1 / (p2 | p3)
```
### Debiasing Methods {#debiasing}
Debiasing methods can be used to improve the fairness of a given model.
`mlr3fairness` includes several methods that can be used together with
`mlr3pipelines` to obtain fair(er) models:
```{r, eval = FALSE}
library(mlr3pipelines)
lrn = as_learner(po("reweighing_wts") %>>% lrn("classif.rpart"))
rs = resample(lrn, task = tsk("compas")$filter(1:500), rsmp("cv"))
rs$score(msr("fairness.acc"))
```
**Overview:**
```{r, echo = FALSE}
library(mlr3misc)
library(mlr3pipelines)
dt = as.data.table(mlr_pipeops)
knitr::kable(dt[map_lgl(dt$tags, function(x) "fairness" %in% x)][, c(1,7,8,9,10)])
```
### Fair Learners
`mlr3fairness` furthermore contains several learners that can be used to directly learn fair models:
```{r, echo = FALSE}
tab = mlr_learners_fairness
knitr::kable(tab)
```
### Datasets
`mlr3fairness` includes two fairness datasets: `adult` and `compas`.
See `?adult` and `?compas` for additional information regarding columns.
You can load them using `tsk()`.
### Model Cards & Datasheets
An important step towards achieving more equitable outcomes for ML models is adequate documentation
for datasets and models in machine learning. `mlr3fairness` comes with reporting aides for `models`
and `datasets`. This provides empty templates that can be used to create interactive reports through
`RMarkdown`.
| Report | Description | Reference | Example |
|--------------------|--------------------------|-----------------------------|----------------------------------------------------------------------------|
| `report_modelcard` | Modelcard for ML models | Mitchell et al., 2018 | [link](https://mlr3fairness.mlr-org.com/articles/modelcard/modelcard.html) |
| `report_datasheet` | Datasheet for data sets | Gebru et al., 2018 | [link](https://mlr3fairness.mlr-org.com/articles/datasheet/datasheet.html) |
| `report_fairness` | Fairness Report | -[^1] | [link](https://mlr3fairness.mlr-org.com/articles/fairness/fairness.html) |
**Usage:**
The `report_*` functions instantiate a new `.Rmd` template that contains a set of pre-defined
questions which can be used for reporting as well as initial graphics.
The goal is that a user extends this `.Rmd` file to create comprehensive documentation for
datasets, ML models or to document a model's fairness.
It can later be converted into a `html` report using`rmarkdown`'s `render`.
```{r, eval= FALSE}
rmdfile = report_datasheet()
rmarkdown::render(rmdfile)
```
### Demo for Adult Dataset
We provide a short example detailing how `mlr3fairness` integrates with the `mlr3` ecosystem.
```r
library(mlr3fairness)
#Initialize Fairness Measure
fairness_measure = msr("fairness.fpr")
#Initialize tasks
task_train = tsk("adult_train")
task_test = tsk("adult_test")
#Initialize model
learner = lrn("classif.rpart", predict_type = "prob")
#Verify fairness metrics
learner$train(task_train)
predictions = learner$predict(task_test)
predictions$score(fairness_measure, task = task_test)
#Visualize the predicted probability score based on protected attribute.
fairness_prediction_density(predictions, task_test)
```
### Extensions
- The [mcboost](https://github.com/mlr-org/mcboost) package integrates with **mlr3** and offers
additional debiasing post-processing functionality for **classification**, **regression** and **survival**.
### Other Fairness Toolkits in R
- [fairmodels](https://github.com/ModelOriented/fairmodels/) integrates with the [DALEX](https://github.com/ModelOriented/DALEX) R-packages and similarly allows for bias auditing, visualization and mitigation.
- The [fairness](https://github.com/kozodoi/fairness) package allows for bias auditing in R.
- The [fairml](https://cran.r-project.org/package=fairml) package contains methods for learning de-biased regression and classification models. Learners from `fairml` are included as learners in `mlr3fairness`.
### Other Fairness Toolkits
- [Aequitas](http://aequitas.dssg.io/) Allows for constructing a fairness report for different fairness metrics along with visualization in Python.
- [fairlearn](https://fairlearn.org/) Allows for model auditing and debiasing as well as visualization in Python.
- [AI Fairness 360](https://github.com/Trusted-AI/AIF360) Allows for model auditing and debiasing as well as visualization in R and Python.
### Future Development
Several future developments are currently planned. Contributions are highly welcome!
- Visualizations:
Improvement on visualizations, like anchor points and others. See issues.
- Debiasing Methods:
More debiasing methods, post-processing and in-processing.
## Bugs, Feedback and Questions
`mlr3fairness` is a free and open source software project that encourages participation and feedback.
If you have any issues, questions, suggestions or feedback, please do not hesitate to open an "issue" about it on the GitHub page!
In case of problems / bugs, it is often helpful if you provide a "minimum working example" that showcases the behaviour.
[^1]: The fairness report is inspired by the [Aequitas Bias report](http://aequitas.dssg.io/example.html).
Owner
- Name: mlr-org
- Login: mlr-org
- Kind: organization
- Location: Munich, Germany
- Website: https://mlr-org.com
- Repositories: 80
- Profile: https://github.com/mlr-org
GitHub Events
Total
- Issues event: 1
- Delete event: 4
- Issue comment event: 4
- Push event: 25
- Pull request event: 8
- Create event: 3
Last Year
- Issues event: 1
- Delete event: 4
- Issue comment event: 4
- Push event: 25
- Pull request event: 8
- Create event: 3
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 46
- Total pull requests: 36
- Average time to close issues: about 2 months
- Average time to close pull requests: 2 months
- Total issue authors: 5
- Total pull request authors: 8
- Average comments per issue: 1.0
- Average comments per pull request: 1.11
- Merged pull requests: 28
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 2
- Pull requests: 5
- Average time to close issues: 8 months
- Average time to close pull requests: 2 months
- Issue authors: 2
- Pull request authors: 3
- Average comments per issue: 0.0
- Average comments per pull request: 0.6
- Merged pull requests: 3
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- pfistfl (33)
- superp0tat0 (9)
- sebffischer (1)
- mb706 (1)
- be-marc (1)
Pull Request Authors
- pfistfl (16)
- superp0tat0 (11)
- mllg (5)
- pat-s (4)
- sebffischer (4)
- be-marc (2)
- mb706 (1)
- vollmersj (1)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 1
-
Total downloads:
- cran 1,154 last-month
- Total dependent packages: 0
- Total dependent repositories: 1
- Total versions: 4
- Total maintainers: 1
cran.r-project.org: mlr3fairness
Fairness Auditing and Debiasing for 'mlr3'
- Homepage: https://mlr3fairness.mlr-org.com
- Documentation: http://cran.r-project.org/web/packages/mlr3fairness/mlr3fairness.pdf
- License: LGPL-3
-
Latest release: 0.4.0
published 8 months ago
Rankings
Stargazers count: 15.1%
Forks count: 17.1%
Dependent repos count: 24.0%
Average: 24.6%
Dependent packages count: 28.8%
Downloads: 38.0%
Maintainers (1)
Last synced:
6 months ago
Dependencies
.github/workflows/pkgdown.yml
actions
- JamesIves/github-pages-deploy-action v4.4.1 composite
- actions/checkout v3 composite
- r-lib/actions/setup-pandoc v2 composite
- r-lib/actions/setup-r v2 composite
- r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/r-cmd-check.yml
actions
- actions/checkout v3 composite
- r-lib/actions/check-r-package v2 composite
- r-lib/actions/setup-pandoc v2 composite
- r-lib/actions/setup-r v2 composite
- r-lib/actions/setup-r-dependencies v2 composite
DESCRIPTION
cran
- R >= 3.4.0 depends
- R6 >= 2.4.1 imports
- backports * imports
- checkmate * imports
- data.table >= 1.13.6 imports
- ggplot2 * imports
- mlr3 >= 0.13.0 imports
- mlr3learners * imports
- mlr3measures * imports
- mlr3misc * imports
- mlr3pipelines * imports
- paradox * imports
- rlang * imports
- fairml * suggests
- iml * suggests
- kableExtra * suggests
- knitr * suggests
- linprog * suggests
- mlr3viz * suggests
- patchwork * suggests
- posterdown * suggests
- ranger * suggests
- rmarkdown * suggests
- rpart * suggests
- testthat >= 3.1.0 suggests