
Dual-strategy medication-class classifier
Source:R/medication_classes.R
extract_medication_classes.RdClassifies each cohort member's medication exposure two independent ways, so sites with very different drug coding (legacy CUI / NDC source strings vs standardized RxNorm) can be compared:
Usage
extract_medication_classes(
db_con,
cohort_table,
medication_specs,
strategy = c("both", "source", "standard"),
crosswalk = NULL,
cohort_definition_id = NULL,
person_col = NULL,
index_date_col = NULL,
site_col = NULL,
lookback_days = NULL,
lookahead_days = 0L,
window = NULL,
end_date_col = NULL,
on_unresolved = c("raise", "warn", "ignore"),
exclude_forms = NULL,
exclude_dose_form_concept_ids = NULL,
apply_exclude_to_source = FALSE,
exclude_forms_source = FALSE,
disaggregate_combinations = FALSE,
is_combination = FALSE,
vocab_schema = NULL,
vocab_db = NULL,
route_concept_ids = NULL,
by_type = FALSE,
offset_anchor = c("visit_start_date", "visit_end_date", "index_date"),
anchor = NULL,
drug_type_concept_ids = NULL,
return_audit = FALSE,
...
)Arguments
- db_con
DuckDB DBI connection with
drug_exposure(andconcept_ancestor/conceptfor the standard strategy).- cohort_table
Table / view with a person column (
subject_id/person_id) and an index date (cohort_start_date/index_date).- medication_specs
Named list
class_name -> spec.specis a list with any ofancestor_concept_ids(ATC class or RxNorm ingredient concept ids),atc_codes,rxnorm_ingredient_ids,source_values(inline strategy-A strings); or a bare vector, taken asancestor_concept_ids.- strategy
"source","standard"or"both".- crosswalk
Strategy-A mapping of source strings to classes (data frame, CSV path or table name with columns like
source_value/drug_class). Combined with inlinesource_values.- cohort_definition_id
Restrict a stacked OMOP cohort table to one definition.
- person_col, index_date_col, site_col
Override column auto-detection. A site column (
site_anon/site_id/site) carries through for per-site auditing.- lookback_days, lookahead_days
Exposure window around the index date.
NULLlookback = unbounded.- window
Optional named temporal frame overriding the lookback/lookahead days:
"pre_admission"=[index - lookback_days (default 365), index - 1];"inpatient"=[index, stay end];"discharge"=[stay end, stay end](orders written at discharge).- end_date_col
Stay end column for
"inpatient"/"discharge"(defaultcohort_end_date/visit_end_date).- on_unresolved
Action when concepts or ATC codes fail to resolve (
"raise","warn", or"ignore").- exclude_forms
Character vector of dosage/form patterns to exclude (e.g.
"topical","ophthalmic").- exclude_dose_form_concept_ids
Optional concept IDs of dose forms to exclude via 'RxNorm has dose form'.
- apply_exclude_to_source
Also apply form/dose-form exclusions to strategy A source values.
- exclude_forms_source
Alias for
apply_exclude_to_source.- disaggregate_combinations
If
TRUE, adds<class>_plainand<class>_combinationcolumns.- is_combination
Alias for
disaggregate_combinations.- vocab_schema
Optional schema name containing vocabulary tables.
- vocab_db
Optional attached database containing vocabulary tables.
- route_concept_ids
Vector of standard OMOP route concept IDs to filter exposures by.
- by_type
Logical; if
TRUE, also extracts flags separated by source concept type.- offset_anchor
Reference anchor date to evaluate offsets against (
"visit_start_date","visit_end_date","index_date").- anchor
Alias for
offset_anchor.- drug_type_concept_ids
Optional vector of concept IDs to restrict drug exposure type concepts.
- return_audit
Also return
audit_medication_strategies()output (needsstrategy = "both").- ...
Forwarded to
audit_medication_strategies().
Value
A data frame with person_id, index_date, optional site, and per class <class>_source,
<class>_standard (0/1, per the chosen strategy) and <class> (OR of the available flags). With
return_audit = TRUE, a list list(flags =, audit =).
Details
Strategy A, source / crosswalk (
"source"): a user-supplied crosswalk (CSV path, DuckDB table, or data frame) maps source strings to drug classes. Matched againstdrug_source_valueand theconcept_codeofdrug_source_concept_id.Strategy B, OMOP standard hierarchy (
"standard"):concept_ancestortraversal from ATC class concepts (levels 3/4/5, by concept id or ATC code) or RxNorm ingredients down todrug_concept_id.strategy = "both"runs both; the combined flag is their OR.
Mirror of omop_etl.medication_classes.extract_medication_classes in Python.