Learn R Programming

aihuman

Overview

aihuman provides statistical methods for analyzing experimental evaluation of the causal impacts of algorithmic recommendations on human decisions developed by Imai, Jiang, Greiner, Halen, and Shin (2023).

The data used for this paper, and made available here, are interim, based on only half of the observations in the study and (for those observations) only half of the study follow-up period. We use them only to illustrate methods, not to draw substantive conclusions.

Installation

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

# install.packages("devtools")
devtools::install_github("sooahnshin/aihuman", dependencies = TRUE, build_vignettes = TRUE)

If you have trouble with compilation on macOS, you may want to check this link.

Usage 1: Evaluation Based on Principal Stratification

The package provides main functions for the methods proposed by Imai, Jiang, Greiner, Halen, and Shin (2023) based on principal stratification.

CategoryFunctionTypeMain InputOutputPaperNotes
descriptivePlotStackedBar()vis.e.g., data(psa_synth)ggplotFig 1dist. of $D_i$
descriptiveCalDIMsubgroup()est.e.g., data(synth)dataframeSec 2.4diff-in-means
descriptivePlotDIMdecisions()vis.output of CalDIMsubgroup()ggplotFig 2 (left)
descriptivePlotDIMoutcomes()vis.output of CalDIMsubgroup() for each outcomeggplotFig 2 (right)
mainAiEvalmcmc()est.(c/h)e.g., data(synth)mcmcSec S5
mainCalAPCE()est.(c/h)output of PSAmcmc()listSec 3.4
mainAPCEsummary()est.output of CalAPCE()dataframeSec 3.4APCE
mainPlotAPCE()vis.output of APCEsummary()ggplotFig 4
strataCalPS()est.$P.R.mcmc of CalAPCE()dataframeEq 6$e_r$
strataPlotPS()vis.output of CalPS()ggplotFig 3
fairnessCalFairness()est.output of CalAPCE()dataframeSec 3.6$\Delta_r(z)$
fairnessPlotFairness()est.output of CalFairness()ggplotFig 5
optimalCalOptimalDecision()est.output of AiEvalmcmc()dataframeSec 3.7$\delta^\ast(\mathbf{x})$
optimalPlotOptimalDecision()vis.output of CalOptimalDecision()ggplotFig 6
comparisonPlotUtilityDiff()vis.output of CalOptimalDecision()ggplotFig 7$g_d(\mathbf{x})$
comparisonPlotUtilityDiffCI()vis.output of CalOptimalDecision()ggplotFig S17
crtSpilloverCRT()est.court event hearing date, $D_i$, $Z_i$daraframeSec S3.1
crtPlotSpilloverCRT()vis.output of SpilloverCRT()ggplotFig S8
crt powerSpilloverCRTpower()est.court event hearing date, $D_i$, $Z_i$dataframeSec S3.2
crt powerPlotSpilloverCRTpower()vis.output of SpilloverCRTpower()ggplotFig S9
frequentistCalAPCEipw()est.e.g., data(synth)dataframeSec S7
frequentistBootstrapAPCEipw()est.e.g., data(synth)dataframe
frequentistAPCEsummaryipw()est.outputs of CalAPCEipw() and BootstrapAPCEipw()dataframe
frequentist (RE)CalAPCEipwRE()est.e.g., data(synth)dataframe
frequentist (RE)BootstrapAPCEipwRE()est.e.g., data(synth)dataframe

vis. = visualization; est. = estimation; c/h = computation-heavy. You may use CalAPCEparallel() instead of CalAPCE() throughout the analysis.

For more details, see the aihuman package vignette, vignette("aihuman", package = "aihuman").

Example

library(aihuman)
## Using synthetic data with small run
data(synth)
sample_mcmc <- AiEvalmcmc(data = synth, n.mcmc = 10)
#> 10/10 done.
subgroup_synth <- list(1:nrow(synth),
                       which(synth$Sex == 0),
                       which(synth$Sex == 1),
                       which(synth$Sex == 1 & synth$White == 0),
                       which(synth$Sex == 1 & synth$White == 1))
sample_apce <- CalAPCE(data = synth, 
                       mcmc.re = sample_mcmc, 
                       subgroup = subgroup_synth)
# You can also use the parallelized version: check CalAPCEparallel()
sample_apce_summary <- APCEsummary(sample_apce[["APCE.mcmc"]])
PlotAPCE(sample_apce_summary, 
         y.max = 0.25, 
         decision.labels = c("signature", "small cash", "medium cash", "large cash"), 
         shape.values = c(16, 17, 15, 18), 
         col.values = c("blue", "black", "red", "brown", "purple"), 
         label = FALSE)

Usage 2: Evaluation With Minimal Assumptions

The package provides main functions for the methods proposed by Ben-Michael, Greiner, Huang, Imai, Jiang, and Shin (2024) based on a minimal set of assumptions.

CategoryFunctionTypeOutputFigure #
Human+AI v. Humancompute_stats_aipw()est.dataframe
Human+AI v. Humanplot_diff_human_aipw()vis.ggplotFig 1
AI v. Humancompute_bounds_aipw()est.dataframe
AI v. Humanplot_diff_ai_aipw()vis.ggplotFig 2, 5, S4
Preferenceplot_preference()vis.ggplotFig 3, S5
Agreementtable_agreement()estggplot
Agreementplot_agreement()vis.ggplotFig S1
Overridesplot_diff_subgroup()vis.ggplotFig S2, S3
Policy learningSee the vignettevis.ggplotFig 4

For more details, see the ablity vignette, vignette("ability", package = "aihuman").

Example

library(ggplot2)
## set default ggplot theme
theme_set(theme_bw(base_size = 15) + theme(plot.title = element_text(hjust = 0.5)))

## Human+AI v. Human
plot_diff_human_aipw(
  Y = NCAdata$Y,
  D = ifelse(NCAdata$D == 0, 0, 1),
  Z = NCAdata$Z,
  nuis_funcs = nuis_func,
  true.pscore = rep(0.5, nrow(NCAdata)),
  l01 = 1,
  subgroup1 = ifelse(NCAdata$White == 1, "White", "Non-white"),
  subgroup2 = ifelse(NCAdata$Sex == 1, "Male", "Female"),
  label.subgroup1 = "Race",
  label.subgroup2 = "Gender",
  x.order = c("Overall", "Non-white", "White", "Female", "Male"),
  p.title = NULL, p.lb = -0.3, p.ub = 0.3
)

## AI v. Human
plot_diff_ai_aipw(
  Y = NCAdata$Y,
  D = ifelse(NCAdata$D == 0, 0, 1),
  Z = NCAdata$Z,
  A = PSAdata$DMF,
  z_compare = 0,
  nuis_funcs = nuis_func,
  nuis_funcs_ai = nuis_func_ai,
  true.pscore = rep(0.5, nrow(NCAdata)),
  l01 = 1,
  subgroup1 = ifelse(NCAdata$White == 1, "White", "Non-white"),
  subgroup2 = ifelse(NCAdata$Sex == 1, "Male", "Female"),
  label.subgroup1 = "Race",
  label.subgroup2 = "Gender",
  x.order = c("Overall", "Non-white", "White", "Female", "Male"),
  zero.line = TRUE, arrows = TRUE, y.min = -Inf,
  p.title = NULL, p.lb = -0.3, p.ub = 0.3
)

Copy Link

Version

Install

install.packages('aihuman')

Monthly Downloads

644

Version

1.0.0

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Sooahn Shin

Last Published

January 9th, 2025

Functions in aihuman (1.0.0)

APCEsummaryipw

Summary of APCE for frequentist analysis
APCEsummary

Summary of APCE
CalDIMsubgroup

Calculate diff-in-means estimates
CalDelta

Calculate the delta given the principal stratum
CalFairness

Calculate the principal fairness
CalOptimalDecision

Calculate optimal decision & utility
PlotSpilloverCRT

Plot conditional randomization test
CalDIM

Calculate diff-in-means estimates
PlotDIMdecisions

Plot diff-in-means estimates
PlotSpilloverCRTpower

Plot power analysis of conditional randomization test
CalPS

Calculate the proportion of principal strata (R)
CalAPCEparallel

Calculate APCE using parallel computing
FTAdata

Interim Dane data with failure to appear (FTA) as an outcome
PlotAPCE

Plot APCE
PlotOptimalDecision

Plot optimal decision
HearingDate

Interim court event hearing date
NVCAdata

Interim Dane data with new violent criminal activity (NVCA) as an outcome
NCAdata

Interim Dane data with new criminal activity (NCA) as an outcome
PSAdata

Interim Dane PSA data
TestMonotonicity

Test monotonicity
PlotPS

Plot the proportion of principal strata (R)
SpilloverCRT

Conduct conditional randomization test
TestMonotonicityRE

Test monotonicity with random effects
PlotStackedBar

Stacked barplot for the distribution of the decision given psa
aihuman-package

tools:::Rd_package_title("aihuman")
PlotDIMoutcomes

Plot diff-in-means estimates
compute_stats_aipw

Compute Risk (Human+AI v. Human)
SpilloverCRTpower

Conduct power analysis of conditional randomization test
compute_nuisance_functions_ai

Fit outcome/decision and propensity score models conditioning on the AI recommendation
compute_nuisance_functions

Fit outcome/decision and propensity score models
crossfit

Crossfitting for nuisance functions
PlotStackedBarDMF

Stacked barplot for the distribution of the decision given DMF recommendation
g_legend

Pulling ggplot legend
nca_follow_policy

NCA follow policy (internal; increasing monotonicity)
PlotUtilityDiff

Plot utility difference
PlotFairness

Plot the principal fairness
PlotUtilityDiffCI

Plot utility difference with 95% confidence interval
compute_stats

Compute Risk (Human+AI v. Human)
compute_stats_agreement

Agreement of Human and AI Decision Makers
hearingdate_synth

Synthetic court event hearing date
compute_bounds_aipw

Compute Risk (AI v. Human)
nuis_func_ai

Nuisance functions conditioning on AI (internal)
plot_diff_ai_aipw

Visualize Difference in Risk (AI v. Human)
nca_provide_policy

NCA provide policy (internal; increasing monotonicity)
nca_follow_policy_dec

NCA follow policy (internal; decreasing monotonicity)
plot_diff_human_aipw

Visualize Difference in Risk (Human+AI v. Human)
vis_diff_human

Visualize Risk (Human+AI v. Human; internal)
plot_diff_human

Visualize Difference in Risk (Human+AI v. Human)
vis_diff_subgroup

Visualize Risk (Human+AI v. Human; internal)
nca_provide_policy_dec

NCA provide policy (internal; decreasing monotonicity)
plot_diff_subgroup

Visualize Difference in Risk (Human+AI v. Human) for a Subgroup Defined by AI Recommendation
vis_agreement

Visualize Agreement (internal)
vis_diff_ai

Visualize Risk (AI v. Human; internal)
psa_synth

Synthetic PSA data
plot_preference

Visualize Preference
compute_stats_subgroup

Compute Risk (Human+AI v. Human) for a Subgroup Defined by AI Recommendation
plot_agreement

Visualize Agreement
table_agreement

Table of Agreement
synth

Synthetic data
vis_preference

Visualize Preference (internal)
nuis_func

Nuisance functions (internal)
BootstrapAPCEipwREparallel

Bootstrap for estimating variance of APCE with random effects
BootstrapAPCEipw

Bootstrap for estimating variance of APCE
A_llama

Llama3 Recommendations (internal)
CalAPCEipw

Compute APCE using frequentist analysis
CalAPCE

Calculate APCE
AiEvalmcmc

Gibbs sampler for the main analysis
CalAPCEipwRE

Compute APCE using frequentist analysis with random effects
BootstrapAPCEipwRE

Bootstrap for estimating variance of APCE with random effects