About

This website serves as slides and script for the GESIS workshop Explainable AI and Fair Machine Learning (with R and Python) taught by Paul C. Bauer, Christopher Klamm and Christoph Kern. Original material is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license. Where we draw on other authors’ material, other licenses may apply. All material the we discuss in the workshop, R and Python code scripts (as well as the source code for this website) can be found in the dedicated GitHub repository for this workshop.

The syllabus for this workshop (including course goals and a detailed description of the structure of the workshop) can be downloaded here. Please note the references in the syllabus as well as the citations and links in the script. Before using any R or Python scripts presented in the material, make sure to install all the necessary packages (see Section 7).


Interested in a workshop or talk on explainable or fair machine learning at your institution/company? Please contact us at mail@paulcbauer.de.

1 Instructors: About us

1.1 Dr. Paul C. Bauer

Biography

Research (see Google Scholar): Substantive.. political sociology1 + Methods/data2

Gesis workshops: Applied Data Visualization, Interactive Data Analysis with Shiny, Applied Machine Learning (with R)

Contact: mail@paulcbauer.de; www.paulcbauer.de; Github

1.2 Christopher Klamm

Biography

  • Current: Research Associate at Cologne Center of Comparative Politics and PhD candidate at University of Mannheim
  • Links: klamm.ai

Contact: christopher@klamm.ai

1.3 Prof. Christoph Kern

Biography

  • Current: Junior Professor of Social Data Science and Statistical Learning at LMU Munich
  • Affiliations: MCML, relAI, MZES
  • Previously:
    • Postdoc at the University of Mannheim
    • Visiting Assistant Professor at the University of Maryland

Research (see Google Scholar): Social Data Science, Algorithmic Fairness, Responsible Computing, Survey Methodology, Computational Social Science

Contact: christoph.kern@stat.uni-muenchen.de

2 Your turn

  • Let’s check our the survey results…
  1. Name?
  2. Affiliation? Country?
  3. What do you want to use machine learning for? (research questions?)
  4. Where does interpretability & fairness enter the picture?

3 Contact & Outline & Dates

  • Paul Bauer (PB), ChristopheR Klamm (CRK), Christopher Kern (CK)
  • Important: lost initial co-instructor, 1st time, software
  • Day 1
    • 9:00 – 9:45: Workshop [PB]
    • 9:45 - 10:30: Introduction [PB]
    • 10:30 -10:45: Coffee break
    • 10:45 - 11:30: Interpretable Machine Learning Methods [CRK]
    • 11:30 - 12:15: Feature Importance [PB + CRK]
    • 12:15 - 13:15: Lunch break
    • 13:15 - 14:00: PDP/ICE/ALE [PB + CRK]
    • 14:00 - 14:45: Feature Interaction (H-Statistic) [PB + CRK]
    • 14:45 - 15:00: Coffee break
    • 15:00 - 15:45: Shapley Values [CRK]
    • 15:45 - 16:30: Shapley Values [CRK + PB]
  • Day 2
    • 9:00 – 9:45: Algorithmic Fairness [CK]
    • 9:45 - 10:30: Algorithmic Fairness [CK]
    • 10:30 -10:45: Coffee break
    • 10:45 - 11:30: Algorithmic Fairness [CK]
    • 11:30 - 12:15: Algorithmic Fairness [CK]
    • 12:15 - 13:15: Lunch break
    • 13:15 - 14:00: Local Interpretable Model-Agnostic Explanations (LIME) I [PB]
    • 14:00 - 14:45: Local Interpretable Model-Agnostic Explanations (LIME) II [PB]
    • 14:45 -15:00: Coffee break
    • 15:00 - 15:45: Work on your own project I
    • 15:45 - 16:30: Work on your own project II
  • Day 3
    • 09:00 – 09:45: SHAP I [CRK]
    • 09:45 - 10:30: SHAP II [CKR + PB]
    • 10:30 - 10:45: Coffee break
    • 10:45 - 11:30: Text data & image data I [CRK]
    • 11:30 - 12:15: Text data & image data II [CRK]
    • 12:15 - 13:15: Lunch break
    • 13:15 - 14:00: Other interpretability methods I [PB]
    • 14:00 - 14:45: Other interpretability methods II [PB]
    • 14:45 -15:00: Coffee break
    • 15:00 - 15:45: Work on your own project III
    • 15:45 - 16:30: Open topics & questions

4 Script & material

  • Literature: See syllabus.
  • Website/script: stable link
    • Find it: Google “paul bauer applied machine learning”
    • Document = slides + script (Zoom in/out with STRG + mousewheel)
    • Code: can all be found in the script
    • Data: can usually be downloaded over links in the script. If not we’ll share the files.
    • Full screen: F11
    • Navigation: TOCs on left and right
    • Search document (upper left)
    • Document generated with quarto
  • Motivation: Have a go-to script for participants (and ourselves!)
  • Content: Mixture of theory, lab sessions, exercises and pure code examples for discussion

5 Strategy & Goals/Learning outcomes

  • Strategy: From the simple to the complex, slowly diving into methods of explainability using building blocks that we already know

  • Goals: By the end of the course participants will:

    • Categorize different explainable AI (XAI) methods.
    • Understand the most prominent XAI approaches.
    • Critically assess XAI approaches, including their advantages, disadvantages, and failure scenarios.
    • Apply XAI methods to supervised ML projects on tabular data using R or Python.
    • Visualize XAI insights to explain machine learning model behavior.
  • QUESTIONS: Ask questions directly & collect them in this table!

7 Software we will use: R & Python

  • Open-source software! (Q: Why?)
  • R (R Core Team 2023)3
    • Install the necessary packages using the code below.
# install.packages('pacman')
library(pacman)
p_load('tidyverse', 'skimr', 'readr',
'reticulate', 'keras', 'lemon', 'knitr', 'kableExtra', 'dplyr', 'plotly',
'randomNames', 'stargazer', 'tidymodels', 'gghighlight', 'gt', 'latex2exp',
'conflicted', 'DALEX', 'vip', 'yardstick', 'iml', 'rpart', 'rattle', 'magick',
'parallel', 'glmnet', 'hstats', 'kernelshap', 'shapviz', 'patchwork',
'PracTools', 'ranger', 'neuralnet', 'formattable', 'mlr3', 'mlr3learners',
'mlr3fairness', 'mcboost', 'ceterisParibus', 'counterfactuals', 'randomForest',
'tidyr', 'stringr', 'profvis', 'rsconnect', 'whereami', 'DT', 'tidytext')
  • Python
  • Ggplot24 (Wickham 2016)/Plotline
  • Note: Ideally cite the software you use in your research especially when it is open-source (e.g., run citation("ggplot2"))
  • BUT.. two software packages create more friction!

8 Data we will use (1): Folktables

  • Purpose: datasets created to support research in fair machine learning
  • Motivation: designed to overcome the limitations of older benchmarks, like the UCI Adult dataset5
  • Data Source: data is derived from major U.S. Census surveys, specifically the American Community Survey (ACS)
  • Scope: datasets cover multiple years and all U.S. states (study change & different locations)
  • Prediction Tasks: Pre-defined tasks are available across several real-world domains6
  • Access: Python package named folktables provides an easy-to-use API for accessing and customizing these datasets/tasks
  • Corresponding paper by (Ding et al. 2021) Retiring Adult: New datasets for fair machine learning

9 Data we will use (2): Folktables ACSPublicCoverage

  • ACSPublicCoverage: “predict whether an individual is covered by public health insurance, after filtering the ACS PUMS7 data sample to only include individuals under the age of 65, and those with an income of less than $30,000. This filtering focuses the prediction problem on low-income individuals who are not eligible for Medicare.(Ding et al. 2021, 7)
  • We filtered the data to include respondents from California in 2018 (N = 138554), filtered out indivduals with neg. income and sampled 5000 individuals.
  • Task Description: Predict whether a low-income individual (not eligible for Medicare), has coverage from public health insurance
  • Target Column: public_coverage (originally PUBCOV)
    • Yes = has coverage; No = does not have coverage
  • We edited the data (variable names etc.) to decrease cognitive load

10 Data we will use (3): Relevance

  • Why predict the coverage status of public health insurance?
  • Targeted Public Health Outreach
    • Efficiently find and enroll eligible but unenrolled populations in programs (e.g., Medicaid)
  • Strategic Resource Planning
    • Forecast demand to decide where to deploy resources like mobile health vans, new clinics, or financial counselors
  • Policy Impact Simulation
    • Model the effects of changing eligibility rules to understand the potential impact and cost of new legislation before it’s enacted

11 Data we will use (4): Variables

  • public_coverage: Public health insurance coverage status
  • income: Total personal income in dollars for the past 12 months
  • age: Age in years
  • education: Highest level of educational attainment
  • marital_status: Marital status
  • sex: Sex
  • disability: Disability status (has one or more of the six disability types)
  • parent_employment: Employment status of parents (for individuals in school)
  • citizenship: Citizenship status (e.g., ‘Citizen, born in U.S.’ or ‘Not a citizen’)
  • mobility: Mobility status (residence 1 year ago)
  • military_service: Veteran service status
  • ancestry: Ancestry (self-identified ethnic heritage) recoded
  • nativity: Nativity (place of birth, e.g., ‘Native’ or ‘Foreign born’)
  • hearing_difficulty: Is deaf or has serious difficulty hearing
  • vision_difficulty: Is blind or has serious difficulty seeing
  • cognitive_difficulty: Cognitive difficulty (remembering or making decisions)
  • employment: Employment status
  • gave_birth: Gave birth within the last 12 months (for females age 15-50)
  • race: Recoded detailed race

12 Data we will use (5): Summary stats

Data summary
Name data
Number of rows 5000
Number of columns 19
_______________________
Column type frequency:
factor 17
numeric 2
________________________
Group variables None

Variable type: factor

skim_variable n_missing complete_rate ordered n_unique top_counts
public_coverage 0 1.00 FALSE 2 No: 3111, Yes: 1889
education 0 1.00 FALSE 5 Som: 1566, No : 1426, Hig: 1211, Bac: 570
marital_status 0 1.00 FALSE 5 Nev: 2726, Mar: 1789, Div: 305, Sep: 103
sex 0 1.00 FALSE 2 Fem: 2831, Mal: 2169
disability 0 1.00 FALSE 2 Wit: 4434, Wit: 566
parent_employment 4544 0.09 FALSE 8 2 p: 189, 2 p: 98, Mot: 80, Fat: 46
citizenship 0 1.00 FALSE 5 Cit: 3405, Not: 869, Nat: 644, Cit: 78
mobility 0 1.00 FALSE 3 Sam: 4186, Mov: 749, Mov: 65
military_service 329 0.93 FALSE 4 Nev: 4539, Vet: 78, Act: 28, Tra: 26
ancestry 0 1.00 FALSE 4 Sin: 3222, Mul: 931, Not: 796, Unc: 51
nativity 0 1.00 FALSE 2 Nat: 3487, For: 1513
hearing_difficulty 0 1.00 FALSE 2 No: 4922, Yes: 78
vision_difficulty 0 1.00 FALSE 2 No: 4883, Yes: 117
cognitive_difficulty 0 1.00 FALSE 2 No: 4703, Yes: 297
employment 184 0.96 FALSE 4 Not: 2264, Emp: 2231, Une: 293, Emp: 28
gave_birth 2885 0.42 FALSE 2 No: 2020, Yes: 95
race 0 1.00 FALSE 7 Whi: 2810, Som: 834, Asi: 712, Bla: 314

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
income 0 1 9282.43 9908.18 -4800 0 6000 17400 30000 ▇▃▃▂▂
age 0 1 35.67 15.21 15 22 33 49 64 ▇▅▃▃▃

13 Loading the dataset

  • We will load our files from a Google Drive (URL) for simplicity reasons
  • In R…
library(readr)
data <- read_csv(url(sprintf("https://docs.google.com/uc?id=%s&export=download",
                         "1dnCK79T45Qa7RZrDg1qv6-EdGBoxCPjv")))
# data <- read_csv("data/data_acspubliccoverage.csv")
head(data)
  • In Python…
import pandas as pd
data = pd.read_csv(f"https://docs.google.com/uc?export=download&id={'1dnCK79T45Qa7RZrDg1qv6-EdGBoxCPjv'}", dtype=str)
# data = pd.read_csv(f"data/data_acspubliccoverage.csv", dtype=str)
print(data.head())

14 Model(s) we will use

  • We focus on random forest as exemplary model
    • Perfect ‘Grey Box’: Not too simple, not too complex—ideal for teaching XAI & audience to understand
    • Ideal for Comparing Methods: Ideal for contrasting its built-in importance with advanced tools like SHAP
    • Shows Advanced Concepts: Reveals non-linear patterns, making it perfect for demonstrating PDPs and ALEs
    • Highlights Proxy Bias: Powerfully demonstrates how models use proxies for protected attributes (because it uses them)

15 Naming things to increase productivity

  • Ideally, we systematicall name objects in R & Python
  • One idea
    • Name objects from general to specific
    • e.g., data -> data_train and data_test
    • e.g., model_rf, fit_rf, workflow_rf
  • Helps when searching for objects, looping over objects etc.

16 Tools and software

16.0.1 R: Installation and setup

Below some notes on the installation and setup of R and relevant packages on your own computer:

  1. Install Rtools for Windows machines from CRAN (https://cran.r-project.org/bin/windows/Rtools/). If you are using OS X, you will need to to install XCode, available for free from the App Store. This will install a compiler (if you don’t have a compiler installed) which will be needed when installing packages from GitHub that require compilation from C++ source code.
  2. Install the latest version of R from CRAN (https://cran.r-project.org/).
  3. Install the latest version of RStudio (https://www.rstudio.com/products/RStudio/). RStudio is the editor we’ll rely on, i.e. we’ll write code in RStudio which is subsequently sent to and run within R.
  4. Start RStudio and install & load the latest versions of various packages that we need.
install.packages("pacman", repos = "http://cran.us.r-project.org")
library(pacman)
pacman::p_load(conflicted, tidyverse, foreign,
       knitr, printr,
       stargazer, plotly,
       scales, Matching,
       rgenoud, AER, lfe, plm,
       aod, randomizr, rdrobust, rddensity, 
       reshape2, mnormt, rmarkdown,
       cobalt, haven, tidyselect,
       kableExtra, sandwich, lmtest, randomNames,
       DiagrammeR, textdata,
       RSelenium, wordcloud, printr, keras,
               googleCloudVisionR, ggridges,
       stm, stminsights,
       ggthemes, RSQLite,
       emo,
       update = FALSE) # Set TRUE to update all

# reticulate::install_miniconda()
# devtools::install_github("hadley/emo")
# Install Anaconda beforehand: https://www.anaconda.com/products/individual
# install.packages("tensorflow")
# install_tensorflow()
# use_condaenv("r-tensorflow")
# Sometime you have to run R in admin mode.


library(reticulate)
library(keras)
virtualenv_create("myenv")
use_virtualenv("myenv")
install_keras(method="virtualenv", envname="myenv")
use_virtualenv("myenv")

# Choose functions with the conflicted package
  conflict_prefer("mutate", "dplyr")
  conflict_prefer("group_by", "dplyr")
  conflict_prefer("ungroup", "dplyr")
  conflict_prefer("filter", "dplyr")
  conflict_prefer("pivot_wider", "tidyr")
  conflict_prefer("pivot_longer", "tidyr")
  conflict_prefer("arrange", "dplyr")
  conflict_prefer("layout", "plotly")
  conflict_prefer("select", "dplyr")
  1. You may also read up on how to create and “knit” an RMarkdown files. Essentially, such files allow you to integrate the analyses you conduct with the text you write which is ideal for reproducability. Here is an intro to the concept and a simple example: http://rmarkdown.rstudio.com/lesson-1.html.

16.0.2 Python

16.0.3 Positron: R/Python communication

  • Positron, the new data science IDE from Posit (formerly Rstudio) allows to use both R and Python at the same time (with them communicating)
  • To install this communication see here: https://positron.posit.co/reticulate
  1. Start Positron
  2. Run reticulate::repl_python() in the R console
  3. Try out the communication between the two
    • From R to Python: Run data <- 1 in R and r.data in Python (reticulate)
    • From Python to R: Run data2 = 2 in Python (reticulate) and py$data2 in R

16.0.4 Datacamp

References

Ding, Frances, Moritz Hardt, John Miller, and Ludwig Schmidt. 2021. “Retiring Adult: New Datasets for Fair Machine Learning.” arXiv [Cs.LG].
Molnar, Christoph. 2022. “Interpretable Machine Learning.” https://christophm.github.io/interpretable-ml-book/.
R Core Team. 2023. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.

Footnotes

  1. Trust, polarization, social media, fake sources/news↩︎

  2. Causal inference, experiments, text data, data visualization, machine learning, big data↩︎

  3. Creators: Core contributors and thousands of package authors.↩︎

  4. Creators: https://github.com/tidyverse/ggplot2↩︎

  5. The primary problem with older fairness datasets like UCI Adult, as described in the paper, is that their widespread and continued use as a benchmark for fairness research is based on a foundation with limited external validity, stemming from specific, unrepresentative idiosyncrasies—such as the 1994 dataset’s extreme $50,000 income threshold—which arbitrary choice is shown to significantly alter and misrepresent the observed magnitude of fairness violations, the trade-offs between criteria, and the effectiveness of fairness interventions.↩︎

  6. ..includes income, employment, health, transportation, and housing.↩︎

  7. Basis for this dataset is the American Community Survey (ACS) Public Use Microdata Sample (PUMS). PUMS is a subset of ACS sent to around 3.5 million households.↩︎