Preprocess AST Data for Resistance Profile Estimation (Pathway 1)
Source:R/daly_resistance_profiles.R
preprocess_for_profiles.RdOrchestrates the full preprocessing pipeline required before marginal
resistance computation and convex optimisation. Calls existing
prep_* package functions in sequence and returns a wide tibble
(one row per isolate) with antibiotic-class columns ready for
compute_marginals_from_data().
Usage
preprocess_for_profiles(
data,
col_map = list(isolate_col = "isolate_id", pathogen_col = "pathogen", ast_col =
"ast_value", patient_col = "patient_id", date_col = "date_of_culture", geography_col
= "state", specimen_col = "specimen_type", age_col = "age", dob_col = "dob",
antibiotic_col = "antibiotic_name", class_col = "antibiotic_class", location_col =
NULL, outcome_col = NULL),
panel_map,
stratify_by = NULL,
outcome_col = NULL,
who_table = NULL
)Arguments
- data
Data frame. Raw AST data in long or wide format.
- col_map
Named list mapping logical roles to actual column names. Same structure as in
validate_profile_inputs(). Default names follow standard package conventions.- panel_map
Named list. Mandatory. Maps each pathogen name to a character vector of antibiotic class names to include in profile estimation. Classes not in the panel are excluded and logged with reason code
"not_in_user_panel". Example:list("Klebsiella pneumoniae" = c("3GC", "Carbapenems", "Fluoroquinolones")).- stratify_by
Character vector or
NULL. Stratification dimensions to carry through to output. Valid values:"geography","year","outcome". Each requested dimension adds the corresponding column to the wide output. DefaultNULL.- outcome_col
Character or
NULL. Column name for patient outcome (e.g."final_outcome"). Required when"outcome"is instratify_by. DefaultNULL.- who_table
Data frame or
NULL. Custom WHO AWaRe classification table passed toprep_standardize_antibiotics(). IfNULL, uses the built-ininst/extdata/WHO_aware_class.csv. DefaultNULL.
Value
A named list:
data_wideTibble. One row per
isolate_id. Columns: all metadata columns + one column per antibiotic class (values S / R / NA).preprocessing_logTibble. One row per pipeline step with columns
step,n_rows_in,n_rows_out,message,status.panel_exclusionsTibble. All organism-class combinations removed by the panel filter, with
reason_codeandreason_text.col_map_resolvedNamed list. Updated
col_mapwith any column names derived during preprocessing (e.g. the harmonised AST column name, the resolved class column name).detected_formatCharacter.
"long"or"wide".
Details
Pipeline steps (in order):
Input validation via
validate_profile_inputs().Wide -> long conversion if wide format detected (
prep_pivot_ast_wide_to_long()).AST value harmonisation – clean messy strings, resolve I values (
prep_harmonize_ast()).Antibiotic name standardisation if no class column (
prep_standardize_antibiotics()).Class assignment if still absent (
prep_classify_antibiotic_class()).Class-level collapse: any R in class => class = R (
prep_collapse_class_level()). Counting unit isisolate_id.Year derivation from
date_colwhen"year"is instratify_by.Organism-specific panel filter: retain only user-specified classes per pathogen.
Long -> wide pivot with antibiotic-class columns (
prep_create_wide_ast_matrix()).
Counting unit: All statistics are computed per unique
isolate_id. A patient with multiple isolates contributes one count
per isolate, not one count per patient.
Examples
if (FALSE) { # \dontrun{
result <- preprocess_for_profiles(
data = raw_ast,
col_map = list(
isolate_col = "isolate_id",
pathogen_col = "organism",
ast_col = "result",
patient_col = "pid",
date_col = "culture_date",
geography_col = "state",
specimen_col = "specimen",
age_col = "age",
antibiotic_col = "drug_name"
),
panel_map = list(
"Klebsiella pneumoniae" = c("3GC", "Carbapenems", "Fluoroquinolones"),
"Escherichia coli" = c("3GC", "Fluoroquinolones", "Aminoglycosides")
),
stratify_by = c("geography", "year"),
outcome_col = "final_outcome"
)
result$data_wide # ready for compute_marginals_from_data()
result$preprocessing_log # step-by-step row counts
result$panel_exclusions # classes dropped per organism
} # }