Formally Check Resistance Profile Probability Constraints
Source:R/daly_resistance_profiles.R
check_profile_constraints.RdValidates that the profile probability distributions returned by
compute_resistance_profiles() satisfy all mathematical requirements:
non-negativity, sum-to-one, and reconstruction of the input marginal and
pairwise resistance rates within a specified tolerance.
Usage
check_profile_constraints(
profiles_output,
marginals = NULL,
pairwise = NULL,
tolerance = 1e-06,
pathogen_col = "pathogen",
class_col = "antibiotic_class",
rate_col = "marginal_resistance",
class1_col = "antibiotic_class_1",
class2_col = "antibiotic_class_2",
pairwise_rate_col = "pairwise_resistance_prevalence"
)Arguments
- profiles_output
Named list returned by
compute_resistance_profiles(). Each element must contain$profiles(data frame withprobabilityand binary class columns) and optionally$constraint_residuals.- marginals
Data frame or
NULL. Original marginal resistance rates used as QP constraints. When provided, reconstructed marginals are compared against these values. Must have columnspathogen_col,class_col, andrate_col. DefaultNULL.- pairwise
Data frame or
NULL. Original pairwise co-resistance rates. When provided, reconstructed pairwise values are also checked. Must have columnspathogen_col,class1_col,class2_col, andpairwise_rate_col. DefaultNULL.- tolerance
Numeric. Maximum absolute residual considered a pass. Default
1e-6.- pathogen_col
Character. Pathogen column in
marginals/pairwise. Default"pathogen".- class_col
Character. Class column in
marginals. Default"antibiotic_class".- rate_col
Character. Marginal rate column in
marginals. Default"marginal_resistance".- class1_col
Character. First class column in
pairwise. Default"antibiotic_class_1".- class2_col
Character. Second class column in
pairwise. Default"antibiotic_class_2".- pairwise_rate_col
Character. Pairwise rate column. Default
"pairwise_resistance_prevalence".
Value
A tibble with one row per constraint per pathogen:
pathogenPathogen name.
constraint_typeOne of
"nonneg","sum_to_one","marginal","pairwise".constraint_nameSpecific label (e.g.
"marg_3GC","pair_3GC_Carbapenems").targetThe constraint target value (
NAfornonneg).reconstructedThe value implied by \(\hat{p}\).
abs_residualAbsolute difference between target and reconstructed.
passLogical.
TRUEifabs_residual < toleranceor the structural check passed.sourceEither
"stored_residuals"(from solve-time values) or"recomputed"(from direct reconstruction).
Also prints a per-pathogen summary of passed and failed checks.
Details
The function uses two complementary sources of evidence:
Constraint residuals already stored in
profiles_output[[pathogen]]$constraint_residuals– these are \(M \hat{p} - v\) computed at solve time and are available without re-supplying the original rates.Direct reconstruction from the binary class-indicator columns in the profiles data frame when
marginalsand/orpairwiseare supplied – independently verifies the residuals and catches any post-solve modifications to the probability vector.
Examples
if (FALSE) { # \dontrun{
marg <- compute_marginal_resistance(amr_clean)
co_res <- compute_pairwise_coresistance(marg)
rp <- compute_resistance_profiles(marg, co_res)
# Using stored constraint residuals only
checks <- check_profile_constraints(rp)
# Full re-verification with original rates
checks <- check_profile_constraints(
rp,
marginals = marg$marginal,
pairwise = NULL,
tolerance = 1e-5
)
# Inspect failures
checks[!checks$pass, ]
} # }