diff --git a/analysis/measures_definition_pf_codes_conditions.py b/analysis/measures_definition_pf_codes_conditions.py index d87a3b9..25ddfce 100644 --- a/analysis/measures_definition_pf_codes_conditions.py +++ b/analysis/measures_definition_pf_codes_conditions.py @@ -1,5 +1,5 @@ from ehrql import INTERVAL, create_measures, months, codelist_from_csv, case, when -from ehrql.tables.tpp import clinical_events, practice_registrations, patients +from ehrql.tables.tpp import clinical_events, practice_registrations, patients, addresses measures = create_measures() measures.configure_dummy_data(population_size=1000) @@ -28,6 +28,7 @@ registration = practice_registrations.for_patient_on(INTERVAL.end_date) +# Age bands for age breakdown age = patients.age_on(INTERVAL.start_date) age_band = case( when((age >= 0) & (age < 20)).then("0-19"), @@ -38,6 +39,17 @@ when(age.is_null()).then("Missing"), ) +# IMD groupings for IMD breakdown +imd = addresses.for_patient_on(INTERVAL.start_date).imd_rounded +imd_quintile = case( + when((imd >=0) & (imd < int(32844 * 1 / 5))).then("1 (most deprived)"), + when(imd < int(32844 * 2 / 5)).then("2"), + when(imd < int(32844 * 3 / 5)).then("3"), + when(imd < int(32844 * 4 / 5)).then("4"), + when(imd < int(32844 * 5 / 5)).then("5 (least deprived)"), + otherwise="unknown" +) + # Select clinical events in interval date range selected_events = clinical_events.where( clinical_events.date.is_on_or_between(INTERVAL.start_date, INTERVAL.end_date) @@ -83,6 +95,17 @@ intervals=months(monthly_intervals).starting_on(start_date), ) + # Measures for IMD breakdown of clinical services + measures.define_measure( + name=f"count_{pharmacy_first_event}_by_imd", + numerator=numerator, + denominator=denominator, + group_by={ + "imd": imd_quintile, + }, + intervals=months(monthly_intervals).starting_on(start_date), + ) + # Create measures for pharmacy first conditions pharmacy_first_conditions_codes = {} for codes, term in pharmacy_first_conditions_codelist.items(): @@ -128,3 +151,14 @@ }, intervals=months(monthly_intervals).starting_on(start_date), ) + + # Measures for imd breakdown of clinical conditions + measures.define_measure( + name=f"count_{condition_name}_by_imd", + numerator=numerator, + denominator=denominator, + group_by={ + "imd": imd_quintile, + }, + intervals=months(monthly_intervals).starting_on(start_date), + ) \ No newline at end of file diff --git a/reports/pharmacy_first_report.Rmd b/reports/pharmacy_first_report.Rmd index e2cfdaf..fc51df1 100644 --- a/reports/pharmacy_first_report.Rmd +++ b/reports/pharmacy_first_report.Rmd @@ -57,6 +57,17 @@ pf_clinical_condition_dict_by_sex <- c( "count_uncomplicated_urinary_tract_infection_by_sex" = "UTI" ) +# Define the custom labels for clinical conditions by IMD +pf_clinical_condition_dict_by_imd <- c( + "count_acute_otitis_media_by_imd" = "Acute Otitis Media", + "count_herpes_zoster_by_imd" = "Herpes Zoster", + "count_acute_sinusitis_by_imd" = "Acute Sinusitis", + "count_impetigo_by_imd" = "Impetigo", + "count_infected_insect_bite_by_imd" = "Infected Insect Bite", + "count_acute_pharyngitis_by_imd" = "Acute Pharyngitis", + "count_uncomplicated_urinary_tract_infection_by_imd" = "UTI" +) + # Define the custom labels for clinical services pf_clinical_service_dict <- c( "count_blood_pressure_service" = "Blood Pressure Service", @@ -80,6 +91,14 @@ pf_clinical_service_dict_by_sex <- c( "count_consultation_service_by_sex" = "Consultation Service", "count_pharmacy_first_service_by_sex" = "Pharmacy First Service" ) + +# Define the custom labels for clinical services by IMD +pf_clinical_service_dict_by_imd <- c( + "count_blood_pressure_service_by_imd" = "Blood Pressure Service", + "count_contraception_service_by_imd" = "Contraception Service", + "count_consultation_service_by_imd" = "Consultation Service", + "count_pharmacy_first_service_by_imd" = "Pharmacy First Service" +) ``` # Background @@ -140,6 +159,20 @@ plot_measures(df_measures, rotate_x_labels = TRUE ) ``` + +### Clinical Services by IMD + +```{r, message=FALSE, warning=FALSE} + +plot_measures(df_measures, + title = "Number of consultations for each clinical service by IMD per month", + measure_names = names(pf_clinical_service_dict_by_imd), + custom_labels = pf_clinical_service_dict_by_imd, + y_label = "Number of codes for consultations", + facet_var = "imd", + rotate_x_labels = TRUE +) +``` ## Clinical Pathways This section focuses on the Clinical Pathways element of that Pharmacy First service. @@ -184,4 +217,18 @@ plot_measures(df_measures, facet_var = "sex", rotate_x_labels = TRUE ) +``` + +### Clinical Conditions by IMD + +```{r, message=FALSE, warning=FALSE} + +plot_measures(df_measures, + title = "Number of consultations for each clinical condition by IMD per month", + measure_names = names(pf_clinical_condition_dict_by_imd), + custom_labels = pf_clinical_condition_dict_by_imd, + y_label = "Number of codes for consultations", + facet_var = "imd", + rotate_x_labels = TRUE +) ``` \ No newline at end of file