Learn R Programming

fairmodels

Overview

Flexible tool for bias detection, visualization, and mitigation. Use models explained with DALEX and calculate fairness classification metrics based on confusion matrices using fairness_check() or try newly developed module for regression models using fairness_check_regression(). R package fairmodels allows to compare and gain information about various machine learning models. Mitigate bias with various pre-processing and post-processing techniques. Make sure your models are classifying protected groups similarly.

Preview

Installation

Install it from CRAN:

install.packages("fairmodels")

or developer version from GitHub:

devtools::install_github("ModelOriented/fairmodels")

Example

Checking fairness is easy!

library(fairmodels)
library(ranger)
library(DALEX)

data("german")

# ------------ step 1 - create model(s)  -----------------

lm_model <- glm(Risk~.,
                data = german,
                family=binomial(link="logit"))

rf_model <- ranger(Risk ~.,
                   data = german,
                   probability = TRUE,
                   num.trees = 200)

# ------------  step 2 - create explainer(s)  ------------

# numeric y for explain function
y_numeric <- as.numeric(german$Risk) -1

explainer_lm <- explain(lm_model, data = german[,-1], y = y_numeric)
explainer_rf <- explain(rf_model, data = german[,-1], y = y_numeric)

# ------------  step 3 - fairness check  -----------------

fobject <- fairness_check(explainer_lm, explainer_rf,
                          protected = german$Sex,
                          privileged = "male")

 
print(fobject)
plot(fobject)

Compas recidivism data use case: Basic tutorial
Bias mitigation techniques on Adult data: Advanced tutorial

How to evaluate fairness in classification models?

Fairness checking is flexible

fairness_check parameters are

  • x, ... - explainers and fairness_objects (products of fairness_check).
  • protected - factor with different subgroups as levels. Usually specific race, sex etc...
  • privileged - subgroup, base on which to calculate parity loss metrics.
  • cutoff - custom cutoff, might be single value - cutoff same for all subgroups or vector - for each subgroup individually. Affecting only explainers.
  • label - character vector for every explainer.

Models might be trained on different data, even without protected variable. May have different cutoffs which gives different values of metrics. fairness_check() is place where explainers and fairness_objects are checked for compatibility and then glued together.
So it is possible to to something like this:

fairness_object <- fairness_check(explainer1, explainer2, ...)
fairness_object <- fairness_check(explainer3, explainer4, fairness_object, ...)

even with more fairness_objects!

If one is even more keen to know how fairmodels works and what are relations between objects, please look at this diagram class diagram

Metrics used

There are 12 metrics based on confusion matrix :

MetricFormulaFull namefairness names while checking among subgroups
TPRtrue positive rateequal opportunity
TNRtrue negative rate
PPVpositive predictive valuepredictive parity
NPVnegative predictive value
FNRfalse negative rate
FPRfalse positive ratepredictive equality
FDRfalse discovery rate
FORfalse omission rate
TSthreat score
STPstatistical paritystatistical parity
ACCaccuracyOverall accuracy equality
F1F1 score

and their parity loss.
How is parity loss calculated?

Where i denotes the membership to unique subgroup from protected variable. Unprivileged subgroups are represented by small letters and privileged by simply "privileged".

some fairness metrics like Equalized odds are satisfied if parity loss in both TPR and FPR is low

How easy it is to add custom fairness metric?

It is relatively easy! Check it out here

Fairness in regression

R package fairmodels has support for regression models. Check fairness using fairness_check_regression() to approximate classification fairness metrics in regression setting. Plot object with plot() to visualize fairness check or with plot_density() to see model's output.

Related works

Zafar, Valera, Rodriguez, Gummadi (2017) https://arxiv.org/pdf/1610.08452.pdf

Barocas, Hardt, Narayanan (2019) https://fairmlbook.org/

Steinberg, Daniel & Reid, Alistair & O'Callaghan, Simon. (2020). Fairness Measures for Regression via Probabilistic Classification. - https://arxiv.org/pdf/2001.06089.pdf

Copy Link

Version

Install

install.packages('fairmodels')

Monthly Downloads

362

Version

1.2.1

License

GPL-3

Maintainer

Jakub Wi<c5><9b>niewski

Last Published

August 23rd, 2022

Functions in fairmodels (1.2.1)

metric_scores

Metric scores
plot_density

Plot fairness object
plot.fairness_radar

Plot fairness radar
plot.fairness_pca

Plot fairness PCA
plot_fairmodels

Plot fairmodels
performance_and_fairness

Performance and fairness
plot.all_cutoffs

Plot all cutoffs
plot.fairness_regression_object

Plot fairness regression object
plot.fairness_object

Plot fairness object
plot.fairness_heatmap

Plot Heatmap
plot.group_metric

Plot group metric
stack_metrics

Stack metrics
plot.stacked_metrics

Plot stacked Metrics
plot.ceteris_paribus_cutoff

Ceteris paribus cutoff plot
plot.chosen_metric

Plot chosen metric
pre_process_data

Pre-process data
print.all_cutoffs

Print all cutoffs
print.fairness_regression_object

Print Fairness Regression Object
print.group_metric

Print group metric
print.fairness_pca

Print fairness PCA
print.fairness_radar

Print fairness radar
print.stacked_metrics

Print stacked metrics
print.chosen_metric

Print chosen metric
print.ceteris_paribus_cutoff

Print ceteris paribus cutoff
reweight

Reweight
resample

Resample
roc_pivot

Reject Option based Classification pivot
regression_metrics

Regression metrics
plot.performance_and_fairness

Plot fairness and performance
print.metric_scores

Print metric scores data
print.performance_and_fairness

Print performance and fairness
plot.metric_scores

Plot metric scores
print.fairness_heatmap

Print fairness heatmap
print.fairness_object

Print Fairness Object
disparate_impact_remover

Disparate impact remover
ceteris_paribus_cutoff

Ceteris paribus cutoff
choose_metric

Choose metric
calculate_group_fairness_metrics

Calculate fairness metrics in groups
all_cutoffs

All cutoffs
expand_fairness_object

Expand Fairness Object
adult_test

Adult test dataset
adult

Adult dataset
compas

Modified COMPAS dataset
confusion_matrix

Confusion matrix
group_model_performance

Group model performance
fairness_radar

Fairness radar
fairness_pca

Fairness PCA
fairness_heatmap

Fairness heatmap
fairness_check_regression

Fairness check regression
fairness_check

Fairness check
german

Modified German Credit data dataset
group_matrices

Group confusion matrices
group_metric

Group metric