inst/doc/introduction.R

## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#>",
  fig.width = 7,
  fig.height = 4.5,
  warning   = FALSE,
  message   = FALSE
)
library(AIBias)

## ----data-overview------------------------------------------------------------
data(lending_panel)
head(lending_panel)

## ----approval-rates-----------------------------------------------------------
# Pooled approval rates by group
tapply(lending_panel$approved, lending_panel$race, mean) |> round(3)

## ----build--------------------------------------------------------------------
obj <- aib_build(
  data     = lending_panel,
  id       = "applicant_id",
  time     = "year",
  group    = "race",
  decision = "approved"
)
print(obj)

## ----describe-----------------------------------------------------------------
obj <- aib_describe(obj, ref_group = "White")
obj$bias$cumulative

## ----plot-trajectory, fig.alt="Bias trajectory plot"--------------------------
plot(obj, type = "trajectory")

## ----plot-heatmap, fig.alt="Group-time disparity heatmap"---------------------
plot(obj, type = "heatmap")

## ----transition---------------------------------------------------------------
obj <- aib_transition(obj, ref_group = "White")

# Recovery and retention gaps
obj$transitions$recovery_gap
obj$transitions$retention_gap

## ----plot-transition, fig.alt="Transition probabilities plot"-----------------
plot(obj, type = "transition")

## ----amp-state----------------------------------------------------------------
obj$transitions$amp_state

## ----amplify------------------------------------------------------------------
obj <- aib_amplify(obj, ref_group = "White")
obj$amplification$cumulative

## ----plot-amplification, fig.alt="Amplification index plot"-------------------
plot(obj, type = "amplification")

## ----zero-index---------------------------------------------------------------
sim <- aib_simulate(n = 20000, n_times = 3, groups = c("ref", "focal"),
                    init = c(0.5, 0.5), p01 = c(0.2, 0.1),
                    p11 = c(0.8, 0.7), seed = 2026)
zobj <- aib_build(sim, "id", "time", "group", "decision", verbose = FALSE)
zobj <- aib_transition(zobj, ref_group = "ref", verbose = FALSE)
zobj <- aib_amplify(zobj, ref_group = "ref", verbose = FALSE)
round(zobj$amplification$index$A_index, 3)
zobj <- aib_decompose(zobj, ref_group = "ref", verbose = FALSE)
zobj$decomposition$steps[, c("time_from", "time_to", "gap_prev", "gap_curr",
                             "carry", "recovery", "retention")]

## ----decompose----------------------------------------------------------------
obj <- aib_decompose(obj, ref_group = "White")
obj$decomposition$totals

## ----plot-decompose, fig.alt="Decomposition of the change in the gap", fig.height=5----
plot(obj, type = "decomposition")

## ----adjust, eval=FALSE-------------------------------------------------------
# obj <- aib_adjust(
#   obj,
#   formula   = ~ income + credit_score,
#   method    = "glm",
#   ref_group = "White"
# )
# 
# # Adjusted trajectory
# head(obj$adjusted$trajectory)

## ----bootstrap, eval=FALSE----------------------------------------------------
# obj <- aib_bootstrap(obj, B = 500, seed = 2024, conf = 0.95)
# subset(obj$bootstrap$intervals, component == "decomposition_total")
# plot(obj, type = "decomposition")  # adds intervals for the observed change

## ----audit, eval=FALSE--------------------------------------------------------
# result <- aib_audit(
#   lending_panel,
#   id        = "applicant_id",
#   time      = "year",
#   group     = "race",
#   decision  = "approved",
#   ref_group = "White",
#   decompose = TRUE,
#   bootstrap = TRUE,
#   B         = 200,
#   seed      = 42
# )
# 
# summary(result)

Try the AIBias package in your browser

Any scripts or data that you put into this service are public.

AIBias documentation built on Dec. 1, 2026, 1:06 a.m.