| Title: | Machine Learning Prediction Auditing and Model Card Reporting |
|---|---|
| Description: | Audits predictions from fitted machine learning models without requiring retraining or access to the fitted model object. It supports binary classification, multiclass classification, and regression, providing performance metrics with bootstrap confidence intervals, calibration and threshold analyses, residual diagnostics, subgroup comparisons, user-defined acceptance criteria, risk warnings, and self-contained reports. For binary-classification and regression models, the package can also assemble and render structured model cards documenting intended use, evaluation data, performance, subgroup results, assumptions, and limitations. The model-card reporting approach is described by Mitchell et al. (2019) "Model Cards for Model Reporting" <doi:10.1145/3287560.3287596>. |
| Authors: | Georgios P. Georgiou [aut, cre] |
| Maintainer: | Georgios P. Georgiou <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-10-06 08:33:08 UTC |
| Source: | https://github.com/cran/modelcardr |
Evaluates predictions without retraining or requiring the fitted model. For binary classification it calculates bootstrap confidence intervals, threshold trade-offs, ROC and precision-recall curves, probability calibration, subgroup performance, and automatic warning rules.
.audit_binary_predictions( truth, probability = NULL, estimate = NULL, positive = NULL, threshold = 0.5, thresholds = seq(0.05, 0.95, by = 0.01), groups = NULL, bootstrap = 500L, conf_level = 0.95, seed = NULL, threshold_objective = c("balanced_accuracy", "f1", "youden", "cost"), false_positive_cost = 1, false_negative_cost = 1, calibration_bins = 10L, reference_probability = NULL, drift_bins = 10L, subgroup_min_n = 30L, title = "Prediction Audit", requirements = NULL ).audit_binary_predictions( truth, probability = NULL, estimate = NULL, positive = NULL, threshold = 0.5, thresholds = seq(0.05, 0.95, by = 0.01), groups = NULL, bootstrap = 500L, conf_level = 0.95, seed = NULL, threshold_objective = c("balanced_accuracy", "f1", "youden", "cost"), false_positive_cost = 1, false_negative_cost = 1, calibration_bins = 10L, reference_probability = NULL, drift_bins = 10L, subgroup_min_n = 30L, title = "Prediction Audit", requirements = NULL )
truth |
Observed outcomes. |
probability |
Numeric probability of the positive class. |
estimate |
Optional predicted classes. When omitted, classes are created
from |
positive |
Label of the positive class. |
threshold |
Threshold used for the main confusion matrix and metrics. |
thresholds |
Candidate thresholds to evaluate. |
groups |
Optional subgroup data frame, named list, or vector. |
bootstrap |
Number of stratified bootstrap samples for confidence intervals. Use zero to disable. |
conf_level |
Confidence level for bootstrap percentile intervals. |
seed |
Optional random seed used for bootstrap resampling. The default
|
threshold_objective |
Criterion for the recommended threshold:
|
false_positive_cost |
Relative cost of a false positive. |
false_negative_cost |
Relative cost of a false negative. |
calibration_bins |
Number of probability bins used in the calibration table. |
reference_probability |
Optional probabilities from an earlier or reference population for prediction-distribution drift checks. |
drift_bins |
Number of bins used for the population stability index. |
subgroup_min_n |
Subgroups below this size are flagged. |
title |
Report title. |
requirements |
Optional pass/fail criteria created by
|
An object of class model_audit.
Add data documentation
add_data_description( card, source = NULL, collection_period = NULL, population = NULL, sampling = NULL, exclusions = NULL, preprocessing = NULL, license = NULL, notes = NULL )add_data_description( card, source = NULL, collection_period = NULL, population = NULL, sampling = NULL, exclusions = NULL, preprocessing = NULL, license = NULL, notes = NULL )
card |
A model card. |
source |
Data source. |
collection_period |
Collection period. |
population |
Population represented by the data. |
sampling |
Sampling method. |
exclusions |
Exclusion criteria. |
preprocessing |
Important preprocessing steps. |
license |
Data license or access restrictions. |
notes |
Additional notes. |
The updated card.
Add known limitations
add_limitations(card, ...)add_limitations(card, ...)
card |
A model card. |
... |
Character vectors containing limitations. |
The updated card.
Add intended-use information
add_purpose( card, intended_use, users = NULL, out_of_scope = NULL, decision_context = NULL, owner = NULL )add_purpose( card, intended_use, users = NULL, out_of_scope = NULL, decision_context = NULL, owner = NULL )
card |
A model card. |
intended_use |
Intended use of the model. |
users |
Intended users. |
out_of_scope |
Unsupported or prohibited uses. |
decision_context |
How predictions enter a decision process. |
owner |
Responsible person or team. |
The updated card.
Diagnostics are screening evidence, not certification that assumptions hold.
audit_assumptions(card)audit_assumptions(card)
card |
A model card. |
The updated card.
This function reports subgroup metrics and disparities. It does not declare a model to be fair or unfair; interpretation depends on the use case.
audit_fairness(card, groups = NULL, min_n = 30L)audit_fairness(card, groups = NULL, min_n = 30L)
card |
A model card. |
groups |
Optional character vector naming stored sensitive features.
Defaults to all sensitive features supplied to |
min_n |
Minimum subgroup size before a subgroup is marked as small. |
The updated card.
Calculate model performance
audit_performance(card)audit_performance(card)
card |
A model card. |
The updated card.
Audits completed binary-classification, multiclass-classification, or regression predictions without requiring the fitted model.
audit_predictions( truth, probability = NULL, estimate = NULL, task = c("auto", "binary_classification", "multiclass_classification", "regression"), positive = NULL, class_names = NULL, threshold = 0.5, thresholds = seq(0.05, 0.95, by = 0.01), groups = NULL, bootstrap = 500L, conf_level = 0.95, seed = NULL, threshold_objective = c("balanced_accuracy", "f1", "youden", "cost"), false_positive_cost = 1, false_negative_cost = 1, calibration_bins = 10L, reference_probability = NULL, drift_bins = 10L, subgroup_min_n = 30L, title = "Prediction Audit", requirements = NULL )audit_predictions( truth, probability = NULL, estimate = NULL, task = c("auto", "binary_classification", "multiclass_classification", "regression"), positive = NULL, class_names = NULL, threshold = 0.5, thresholds = seq(0.05, 0.95, by = 0.01), groups = NULL, bootstrap = 500L, conf_level = 0.95, seed = NULL, threshold_objective = c("balanced_accuracy", "f1", "youden", "cost"), false_positive_cost = 1, false_negative_cost = 1, calibration_bins = 10L, reference_probability = NULL, drift_bins = 10L, subgroup_min_n = 30L, title = "Prediction Audit", requirements = NULL )
truth |
Observed outcomes. |
probability |
A binary positive-class probability vector or multiclass probability matrix. |
estimate |
Predicted classes or numeric regression estimates. |
task |
Task type, or |
positive |
Positive class for binary classification. |
class_names |
Optional multiclass probability-column names. |
threshold |
Binary decision threshold. |
thresholds |
Candidate binary thresholds. |
groups |
Optional subgroup variables. |
bootstrap |
Number of bootstrap samples. |
conf_level |
Bootstrap confidence level. |
seed |
Optional random seed used for bootstrap resampling. The default
|
threshold_objective |
Binary threshold objective. |
false_positive_cost |
Relative binary false-positive cost. |
false_negative_cost |
Relative binary false-negative cost. |
calibration_bins |
Number of calibration bins. |
reference_probability |
Optional binary reference probabilities for drift. |
drift_bins |
Number of drift bins. |
subgroup_min_n |
Minimum subgroup size before a warning. |
title |
Audit title. |
requirements |
Optional criteria from |
An object of class model_audit.
audit_predictions(c(1, 2, 3), estimate = c(1.1, 1.9, 3.2), task = "regression", bootstrap = 0)audit_predictions(c(1, 2, 3), estimate = c(1.1, 1.9, 3.2), task = "regression", bootstrap = 0)
Creates optional acceptance criteria for binary classification,
multiclass classification, and regression audits. Requirements that
cannot be evaluated from the supplied data are reported as
INCONCLUSIVE.
audit_requirements( min_n = NULL, min_accuracy = NULL, min_balanced_accuracy = NULL, min_sensitivity = NULL, min_specificity = NULL, min_precision = NULL, min_f1 = NULL, min_roc_auc = NULL, min_average_precision = NULL, min_macro_f1 = NULL, min_weighted_f1 = NULL, min_macro_roc_auc = NULL, min_minimum_class_recall = NULL, min_r_squared = NULL, max_false_positive_rate = NULL, max_false_negative_rate = NULL, max_brier_score = NULL, max_log_loss = NULL, max_multiclass_log_loss = NULL, max_multiclass_brier_score = NULL, max_rmse = NULL, max_mae = NULL, max_absolute_bias = NULL, max_mape = NULL, max_smape = NULL, max_expected_calibration_error = NULL, calibration_slope_range = NULL, max_threshold_difference = NULL, max_subgroup_sensitivity_gap = NULL, max_subgroup_specificity_gap = NULL, max_subgroup_selection_rate_gap = NULL, max_subgroup_accuracy_gap = NULL, max_subgroup_macro_f1_gap = NULL, max_subgroup_rmse_gap = NULL, max_subgroup_mae_gap = NULL, max_population_stability_index = NULL, use_confidence_bounds = TRUE )audit_requirements( min_n = NULL, min_accuracy = NULL, min_balanced_accuracy = NULL, min_sensitivity = NULL, min_specificity = NULL, min_precision = NULL, min_f1 = NULL, min_roc_auc = NULL, min_average_precision = NULL, min_macro_f1 = NULL, min_weighted_f1 = NULL, min_macro_roc_auc = NULL, min_minimum_class_recall = NULL, min_r_squared = NULL, max_false_positive_rate = NULL, max_false_negative_rate = NULL, max_brier_score = NULL, max_log_loss = NULL, max_multiclass_log_loss = NULL, max_multiclass_brier_score = NULL, max_rmse = NULL, max_mae = NULL, max_absolute_bias = NULL, max_mape = NULL, max_smape = NULL, max_expected_calibration_error = NULL, calibration_slope_range = NULL, max_threshold_difference = NULL, max_subgroup_sensitivity_gap = NULL, max_subgroup_specificity_gap = NULL, max_subgroup_selection_rate_gap = NULL, max_subgroup_accuracy_gap = NULL, max_subgroup_macro_f1_gap = NULL, max_subgroup_rmse_gap = NULL, max_subgroup_mae_gap = NULL, max_population_stability_index = NULL, use_confidence_bounds = TRUE )
min_n |
Minimum required number of evaluation observations.
Use |
min_accuracy |
Minimum acceptable classification accuracy.
Use |
min_balanced_accuracy |
Minimum acceptable balanced accuracy.
Use |
min_sensitivity |
Minimum acceptable binary-classification
sensitivity. Use |
min_specificity |
Minimum acceptable binary-classification
specificity. Use |
min_precision |
Minimum acceptable binary-classification precision.
Use |
min_f1 |
Minimum acceptable binary-classification F1 score.
Use |
min_roc_auc |
Minimum acceptable binary-classification ROC AUC.
Use |
min_average_precision |
Minimum acceptable average precision for
binary classification. Use |
min_macro_f1 |
Minimum acceptable macro F1 score for multiclass
classification. Use |
min_weighted_f1 |
Minimum acceptable weighted F1 score for multiclass
classification. Use |
min_macro_roc_auc |
Minimum acceptable macro ROC AUC for multiclass
classification. Use |
min_minimum_class_recall |
Minimum acceptable recall for the
worst-performing class in multiclass classification. Use |
min_r_squared |
Minimum acceptable R-squared value for regression.
Use |
max_false_positive_rate |
Maximum acceptable false-positive rate for
binary classification. Use |
max_false_negative_rate |
Maximum acceptable false-negative rate for
binary classification. Use |
max_brier_score |
Maximum acceptable Brier score for binary
classification. Use |
max_log_loss |
Maximum acceptable log loss for binary classification.
Use |
max_multiclass_log_loss |
Maximum acceptable log loss for multiclass
classification. Use |
max_multiclass_brier_score |
Maximum acceptable Brier score for
multiclass classification. Use |
max_rmse |
Maximum acceptable root mean squared error for regression.
Use |
max_mae |
Maximum acceptable mean absolute error for regression.
Use |
max_absolute_bias |
Maximum acceptable absolute mean bias for
regression. Use |
max_mape |
Maximum acceptable mean absolute percentage error for
regression. Use |
max_smape |
Maximum acceptable symmetric mean absolute percentage
error for regression. Use |
max_expected_calibration_error |
Maximum acceptable expected
calibration error for classification. Use |
calibration_slope_range |
Numeric vector of length two containing the
minimum and maximum acceptable binary calibration slope. Use |
max_threshold_difference |
Maximum acceptable absolute difference
between the selected binary threshold and the package-recommended
threshold. Use |
max_subgroup_sensitivity_gap |
Maximum acceptable difference between
the highest and lowest subgroup sensitivities. Use |
max_subgroup_specificity_gap |
Maximum acceptable difference between
the highest and lowest subgroup specificities. Use |
max_subgroup_selection_rate_gap |
Maximum acceptable difference
between the highest and lowest subgroup selection rates. Use |
max_subgroup_accuracy_gap |
Maximum acceptable difference between
the highest and lowest subgroup accuracies. Use |
max_subgroup_macro_f1_gap |
Maximum acceptable difference between
the highest and lowest subgroup macro F1 scores. Use |
max_subgroup_rmse_gap |
Maximum acceptable difference between the
highest and lowest subgroup RMSE values. Use |
max_subgroup_mae_gap |
Maximum acceptable difference between the
highest and lowest subgroup MAE values. Use |
max_population_stability_index |
Maximum acceptable population
stability index for prediction drift. Use |
use_confidence_bounds |
Logical. When |
An object of class audit_requirements containing the configured
audit criteria.
rules <- audit_requirements( min_n = 100, min_accuracy = 0.75, min_roc_auc = 0.80, max_log_loss = 0.60 ) print(rules)rules <- audit_requirements( min_n = 100, min_accuracy = 0.75, min_roc_auc = 0.80, max_log_loss = 0.60 ) print(rules)
Evaluates configured acceptance criteria and adds a rule table plus an overall PASS, FAIL, or INCONCLUSIVE result to a model audit. Missing evidence is marked INCONCLUSIVE rather than passing automatically.
check_requirements(audit, requirements)check_requirements(audit, requirements)
audit |
An object of class |
requirements |
An object created by |
The updated model_audit object.
truth <- c("no", "no", "yes", "yes") probability <- c(0.1, 0.2, 0.8, 0.9) audit <- audit_predictions(truth, probability, positive = "yes", bootstrap = 0) rules <- audit_requirements(min_roc_auc = 0.80, min_sensitivity = 0.80) audit <- check_requirements(audit, rules) audit$overall_status audit$rule_resultstruth <- c("no", "no", "yes", "yes") probability <- c(0.1, 0.2, 0.8, 0.9) audit <- audit_predictions(truth, probability, positive = "yes", bootstrap = 0) rules <- audit_requirements(min_roc_auc = 0.80, min_sensitivity = 0.80) audit <- check_requirements(audit, rules) audit$overall_status audit$rule_results
Compare two model cards
compare_cards(old, new)compare_cards(old, new)
old |
Older model card. |
new |
Newer model card. |
A list containing metadata and metric comparisons.
Uses paired stratified bootstrap resampling so each model is evaluated on the same resampled observations.
compare_predictions( truth, probability_a, probability_b, positive = NULL, threshold = 0.5, bootstrap = 1000L, conf_level = 0.95, seed = NULL, model_names = c("model_a", "model_b") )compare_predictions( truth, probability_a, probability_b, positive = NULL, threshold = 0.5, bootstrap = 1000L, conf_level = 0.95, seed = NULL, model_names = c("model_a", "model_b") )
truth |
Observed outcomes. |
probability_a |
Positive-class probabilities from model A. |
probability_b |
Positive-class probabilities from model B. |
positive |
Positive-class label. |
threshold |
Threshold applied to both models. |
bootstrap |
Number of paired bootstrap samples. |
conf_level |
Confidence level for differences. |
seed |
Optional random seed used for paired bootstrap resampling. The
default |
model_names |
Two model names. |
An object of class prediction_comparison.
Creates a structured model-card object for a regression or binary
classification model. Predictions may be generated from model, or supplied
directly through estimate and probability.
model_card( model = NULL, evaluation_data = NULL, truth, estimate = NULL, probability = NULL, task = c("auto", "regression", "binary_classification"), positive = NULL, threshold = 0.5, title = NULL, version = "0.1.0", sensitive_features = NULL, metadata = list() )model_card( model = NULL, evaluation_data = NULL, truth, estimate = NULL, probability = NULL, task = c("auto", "regression", "binary_classification"), positive = NULL, threshold = 0.5, title = NULL, version = "0.1.0", sensitive_features = NULL, metadata = list() )
model |
A fitted model object, or |
evaluation_data |
A data frame used for evaluation. |
truth |
Outcome vector or the name of its column in |
estimate |
Predicted values/classes or a column name. |
probability |
Positive-class probabilities or a column name. |
task |
One of |
positive |
Positive outcome label for classification. |
threshold |
Probability threshold used to create class predictions. |
title |
Card title. |
version |
Model/card version. |
sensitive_features |
Character vector of columns to retain for subgroup evaluation. Raw predictor columns are not stored. |
metadata |
Named list of extra metadata. |
An object of class model_card.
Calls audit_predictions() for all supported prediction tasks.
model_card_from_predictions( truth, estimate = NULL, probability = NULL, task = c("auto", "regression", "binary_classification", "multiclass_classification"), positive = NULL, class_names = NULL, threshold = 0.5, title = "Prediction Audit", groups = NULL, bootstrap = 500L, ... )model_card_from_predictions( truth, estimate = NULL, probability = NULL, task = c("auto", "regression", "binary_classification", "multiclass_classification"), positive = NULL, class_names = NULL, threshold = 0.5, title = "Prediction Audit", groups = NULL, bootstrap = 500L, ... )
truth |
Observed outcomes. |
estimate |
Predicted classes or numeric estimates. |
probability |
Binary probability vector or multiclass probability matrix. |
task |
Task type. |
positive |
Binary positive class. |
class_names |
Optional multiclass probability-column names. |
threshold |
Binary classification threshold. |
title |
Audit title. |
groups |
Optional subgroup variables. |
bootstrap |
Bootstrap samples. |
... |
Additional arguments passed to |
A model_audit object.
Plot a prediction audit
## S3 method for class 'model_audit' plot(x, type = NULL, metric = "balanced_accuracy", ...)## S3 method for class 'model_audit' plot(x, type = NULL, metric = "balanced_accuracy", ...)
x |
A |
type |
Plot type. Binary audits support |
metric |
Metric shown for a binary threshold plot. |
... |
Additional graphical arguments. |
The input model_audit object, returned invisibly. The function is
called for its side effect of drawing the selected diagnostic plot.
Print audit requirements
## S3 method for class 'audit_requirements' print(x, ...)## S3 method for class 'audit_requirements' print(x, ...)
x |
An |
... |
Unused. |
The input audit_requirements object, returned invisibly. The
function is called for its side effect of printing the configured audit
criteria and whether confidence bounds are used.
Print a prediction audit
## S3 method for class 'model_audit' print(x, ...)## S3 method for class 'model_audit' print(x, ...)
x |
A |
... |
Unused. |
The input model_audit object, returned invisibly. The function is
called for its side effect of printing a concise overview of the audit,
including its task, sample size, selected metrics, status, and number of
findings.
Print a model card
## S3 method for class 'model_card' print(x, ...)## S3 method for class 'model_card' print(x, ...)
x |
A |
... |
Unused. |
The input model_card object, returned invisibly. The function is
called for its side effect of printing the card title, version, task,
model class, evaluation sample size, and number of performance metrics.
Print a prediction comparison
## S3 method for class 'prediction_comparison' print(x, ...)## S3 method for class 'prediction_comparison' print(x, ...)
x |
A |
... |
Unused. |
The input prediction_comparison object, returned invisibly. The
function is called for its side effect of printing the compared model
names, evaluation sample size, and selected comparison metrics.
Renders task-specific evidence and omits sections that do not apply.
render_audit(audit, output_file, format = NULL, detailed = FALSE)render_audit(audit, output_file, format = NULL, detailed = FALSE)
audit |
A |
output_file |
Destination HTML, Markdown, or JSON file. |
format |
Optional explicit output format. |
detailed |
Whether to include detailed diagnostic tables. |
The output path, invisibly.
Render a model card
render_card(card, output_file, format = NULL)render_card(card, output_file, format = NULL)
card |
A model card. |
output_file |
Destination file ending in |
format |
Optional explicit format: |
The normalized output path, invisibly.
Summarize a prediction audit
## S3 method for class 'model_audit' summary(object, ...)## S3 method for class 'model_audit' summary(object, ...)
object |
A |
... |
Unused. |
A named list summarizing the audit. It contains the audit title,
task, evaluation-data information, overall and class-level metrics,
recommended threshold, calibration summary, subgroup disparities,
diagnostics, requirement-checking results, findings, and risk warnings.
Components that are not applicable to the audit task may be NULL.
Summarize a model card
## S3 method for class 'model_card' summary(object, ...)## S3 method for class 'model_card' summary(object, ...)
object |
A |
... |
Unused. |
A named list containing the model-card title, version, task, model metadata, intended purpose, performance results, fairness summary, assumption checks, limitations, and completeness-validation results.
Validate model-card completeness
validate_card(card)validate_card(card)
card |
A model card. |
A data frame of validation checks.