Skip to content

swastvedt/faircfint

Repository files navigation

faircfint

The goal of faircfint is to facilitate estimation and inference for intersectional, counterfactual unfairness metrics. The metrics and techniques are described in “An intersectional framework for counterfactual fairness in risk prediction” and “Counterfactual fairness for small subgroups”.

Installation

You can install the development version of faircfint from GitHub with:

# install.packages("devtools")
devtools::install_github("swastvedt/faircfint")

Usage

The main function, analysis_estimation, calculates the intersectional, counterfactual unfairness metrics from data (which must include risk predictions).

The ‘standard’ option for estimator_type uses the methods from “An intersectional framework for counterfactual fairness in risk prediction”. Options ‘small_internal’ and ‘small_borrow’ use the methods from “Counterfactual fairness for small subgroups”.

library(faircfint)

# Build propensity score model
pi_model_ex <- glm(as.factor(as.character(D)) ~ A1*A2 + A1 + A2 + S_prob + X.1 + X.2 + X.3 + X.4, 
                   data = faircfint::ex_data_estimation, family = "binomial")

# Estimate counterfactual error rates and unfairness metrics, including bootstrap and null distribution
results <- analysis_estimation(data = faircfint::ex_data_estimation, cutoff = 0.5, estimator_type = "standard",
                               pi_model = pi_model_ex, pi_model_type = "glm", pi_xvars = c("X.1", "X.2", "X.3", "X.4"),
                               gen_null = T, bootstrap = 'rescaled')

# Estimate metrics using small subgroup option with no data borrowing
pi_model_ex_small <- glm(as.factor(as.character(D)) ~ A1*A2 + A1 + A2 + S_prob + X.1 + X.2 + X.3 + X.4,
                         data = ex_data_small, family = "binomial")

results_smallsub <- analysis_estimation(ex_data_small, cutoff = 0.5, estimator_type = "small_internal",
                                     gen_null = T, R_null = 100, bootstrap = 'rescaled', B=100,
                                     pi_model = pi_model_ex_small, pi_model_type = "glm", pi_xvars = c("X.1", "X.2", "X.3", "X.4"),
                                     outcome_model_type = "glm", outcome_xvars = c("X_outcome.1", "X_outcome.2", "X_outcome.3", "X_outcome.4", "X_outcome.5", "X_outcome.6", "X_outcome.7", "X_outcome.8"),
                                     fit_method_int = "multinomial", nfolds = 5, pa_xvars_int = c("X_pa.1", "X_pa.2", "X_pa.3", "X_pa.4", "X_pa.5", "X_pa.6", "X_pa.7"))

# Estimate metrics using small subgroup option with data borrowing
## Optional fitting of external data model prior to running main function
pa_model_ext_ex <- get_pa_ext_small(ex_data_external, fit_method_ext = "multinomial", pa_xvars_ext = c("X_pa.1", "X_pa.2", "X_pa.3", "X_pa.4", "X_pa.5"))
#> # weights:  42 (30 variable)
#> initial  value 17917.594692 
#> iter  10 value 17706.446512
#> iter  20 value 17650.998495
#> iter  30 value 17650.359482
#> final  value 17650.355385 
#> converged

results_borrow <- analysis_estimation(ex_data_small, cutoff = 0.5, estimator_type = "small_borrow",
                                         gen_null = F, R_null = 100, bootstrap = 'none', B=100,
                                         pi_model = pi_model_ex_small, pi_model_type = "glm", pi_xvars = c("X.1", "X.2", "X.3", "X.4"),
                                         outcome_model_type = "glm", outcome_xvars = c("X_outcome.1", "X_outcome.2", "X_outcome.3", "X_outcome.4", "X_outcome.5", "X_outcome.6", "X_outcome.7", "X_outcome.8"),
                                         fit_method_int = "multinomial", nfolds = 5, pa_xvars_int = c("X_pa.1", "X_pa.2", "X_pa.3", "X_pa.4", "X_pa.5", "X_pa.6", "X_pa.7"),
                                         data_external = ex_data_external, fit_method_ext = "multinomial", pa_xvars_ext = c("X_pa.1", "X_pa.2", "X_pa.3", "X_pa.4", "X_pa.5"), borrow_metric = "brier",
                                         pa_model_ext = pa_model_ext_ex)

# Plot results
results_plots <- get_plots(results, sampsize = 5000, alpha = 0.05, m_factor = 0.75)


# Example plot of negative (cFNR) versions of small subgroup metrics
#results_plots$metrics_pos

About

Intersectional, counterfactual unfairness metrics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages