Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viv3ckj/update viz #25

Merged
merged 6 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 47 additions & 13 deletions analysis/measures_definition_pf_codes_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
practice_registrations,
patients,
addresses,
ethnicity_from_sus,
)
from codelists import pharmacy_first_conditions_codelist, ethnicity_codelist

measures = create_measures()
measures.configure_dummy_data(population_size=1000)

start_date = "2023-11-01"
monthly_intervals = 8
monthly_intervals = 9

# Create dictionary of pharmacy first codes
pharmacy_first_event_codes = {
Expand All @@ -27,23 +28,49 @@

registration = practice_registrations.for_patient_on(INTERVAL.end_date)

latest_ethnicity_category_num = (
latest_ethnicity_from_codes_category_num = (
clinical_events.where(clinical_events.snomedct_code.is_in(ethnicity_codelist))
.where(clinical_events.date.is_on_or_before(INTERVAL.start_date))
.sort_by(clinical_events.date)
.last_for_patient()
.snomedct_code.to_category(ethnicity_codelist)
)

latest_ethnicity_category_desc = case(
when(latest_ethnicity_category_num == "1").then("White"),
when(latest_ethnicity_category_num == "2").then("Mixed"),
when(latest_ethnicity_category_num == "3").then("Asian or Asian British"),
when(latest_ethnicity_category_num == "4").then("Black or Black British"),
when(latest_ethnicity_category_num == "5").then("Chinese or Other Ethnic Groups"),
when(latest_ethnicity_category_num.is_null()).then("Missing"),
latest_ethnicity_from_codes = case(
when(latest_ethnicity_from_codes_category_num == "1").then("White"),
when(latest_ethnicity_from_codes_category_num == "2").then("Mixed"),
when(latest_ethnicity_from_codes_category_num == "3").then(
"Asian or Asian British"
),
when(latest_ethnicity_from_codes_category_num == "4").then(
"Black or Black British"
),
when(latest_ethnicity_from_codes_category_num == "5").then(
"Chinese or Other Ethnic Groups"
),
)

ethnicity_from_sus = case(
when(ethnicity_from_sus.code.is_in(["A", "B", "C"])).then("White"),
when(ethnicity_from_sus.code.is_in(["D", "E", "F", "G"])).then("Mixed"),
when(ethnicity_from_sus.code.is_in(["H", "J", "K", "L"])).then(
"Asian or Asian British"
),
when(ethnicity_from_sus.code.is_in(["M", "N", "P"])).then("Black or Black British"),
when(ethnicity_from_sus.code.is_in(["R", "S"])).then(
"Chinese or Other Ethnic Groups"
),
)

ethnicity_combined = case(
when(latest_ethnicity_from_codes.is_not_null()).then(latest_ethnicity_from_codes),
when(latest_ethnicity_from_codes.is_null() & ethnicity_from_sus.is_not_null()).then(
ethnicity_from_sus
),
otherwise="Missing",
)


# Age bands for age breakdown
age = patients.age_on(INTERVAL.start_date)
age_band = case(
Expand All @@ -59,11 +86,18 @@
imd = addresses.for_patient_on(INTERVAL.start_date).imd_rounded
max_imd = 32844
imd_quintile = case(
when((imd >= 0) & (imd < int(max_imd * 1 / 5))).then("1"),
when((imd >= 0) & (imd < int(max_imd * 1 / 5))).then("1 (Most Deprived)"),
when(imd < int(max_imd * 2 / 5)).then("2"),
when(imd < int(max_imd * 3 / 5)).then("3"),
when(imd < int(max_imd * 4 / 5)).then("4"),
when(imd <= max_imd).then("5"),
when(imd <= max_imd).then("5 (Least Deprived)"),
otherwise="Missing",
)

latest_region = case(
when(
registration.practice_nuts1_region_name.is_not_null()
).then(registration.practice_nuts1_region_name),
otherwise="Missing",
)

Expand All @@ -77,8 +111,8 @@
"age_band": age_band,
"sex": patients.sex,
"imd": imd_quintile,
"region": registration.practice_nuts1_region_name,
"ethnicity": latest_ethnicity_category_desc,
"region": latest_region,
"ethnicity": ethnicity_combined,
}

# Define the denominator as the number of patients registered
Expand Down
8 changes: 6 additions & 2 deletions lib/functions/function_plot_measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ plot_measures <- function(
color = guide_legend(nrow = guide_nrow)
) +
labs(
title = title,
x = x_label,
y = y_label,
colour = guide_label,
) +
theme(
legend.position = legend_position
)
legend.position = legend_position,
plot.title = element_text(hjust = 0.5)
) +

scale_colour_brewer(palette = "Set1")

# Automatically change y scale depending selected value
if (rlang::as_label(enquo(select_value)) %in% c("numerator", "denominator")) {
Expand Down
62 changes: 57 additions & 5 deletions reports/pharmacy_first_report.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,34 @@ df_measures <- tidy_measures(
pf_measures_name_mapping = pf_measures_name_mapping,
pf_measures_groupby_dict = pf_measures_groupby_dict
)

df_measures$ethnicity <- factor(
df_measures$ethnicity,
levels = c("White", "Mixed", "Asian or Asian British",
"Black or Black British", "Chinese or Other Ethnic Groups",
"Missing"),
ordered = TRUE
)

df_measures$age_band <- factor(
df_measures$age_band,
levels = c("0-19", "20-39", "40-59",
"60-79", "80+",
"Missing"),
ordered = TRUE
)

df_measures$region <- factor(
df_measures$region,
levels = c("East", "East Midlands", "London",
"North East", "North West", "South East",
"South West", "West Midlands", "Yorkshire and The Humber",
"Missing"),
ordered = TRUE
)
Comment on lines +71 to +94
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does what we want, but it would be could do this as one call using mutate. at some later point we should integrate this into the tidy_measures() function, for now I think it's ok if we do this outside of the function.

df_measures <- df_measures %>%
  mutate(
    ethnicity = factor(ethnicity, ...),
    region = factor(region, ...),
    age_band = factor(age_band, ...),
    sex = factor(sex, ...)
)


df_measures <- df_measures %>%
mutate(sex = factor(sex, levels = c("female", "male"), labels = c("Female", "Male")))
```

# Background
Expand All @@ -93,7 +121,7 @@ Links to the codelist for each analysis can be found beneath the relevant sectio

### Total population

```{r, message=FALSE, warning=FALSE, fig.height=4, fig.width=4}
```{r, message=FALSE, warning=FALSE}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep this? it makes sure all the panels are roughly the same size? or what was your reason for removing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was an issue with the titles and axes being cropped out

# Select measures and breakdown
df_measures_selected <- df_measures %>%
filter(measure_desc == "clinical_service") %>%
Expand All @@ -108,6 +136,8 @@ plot_measures(
guide_nrow = 1,
facet_wrap = FALSE,
facet_var = NULL,
title = "Number of consultations for each clinical service per month",
y_label = "Number of codes for consultations",
)
```

Expand All @@ -127,7 +157,9 @@ plot_measures(
colour_var = age_band,
guide_nrow = 1,
facet_wrap = TRUE,
facet_var = measure
facet_var = measure,
title = "Number of consultations for each clinical service by age band per month",
y_label = "Number of codes for consultations",
)
```

Expand All @@ -148,6 +180,8 @@ plot_measures(
guide_nrow = 1,
facet_wrap = TRUE,
facet_var = measure,
title = "Number of consultations for each clinical service by sex per month",
y_label = "Number of codes for consultations",
)
```

Expand All @@ -168,6 +202,8 @@ plot_measures(
guide_nrow = 1,
facet_wrap = TRUE,
facet_var = measure,
title = "Number of consultations for each clinical service by IMD per month",
y_label = "Number of codes for consultations",
)
```

Expand All @@ -188,6 +224,8 @@ plot_measures(
guide_nrow = 2,
facet_wrap = TRUE,
facet_var = measure,
title = "Number of consultations for each clinical service by region per month",
y_label = "Number of codes for consultations",
)
```

Expand All @@ -210,6 +248,8 @@ plot_measures(
guide_nrow = 2,
facet_wrap = TRUE,
facet_var = measure,
title = "Number of consultations for each clinical service by ethnicity per month",
y_label = "Number of codes for consultations",
)
```

Expand All @@ -220,7 +260,7 @@ Here we show the number of consultations for each of the Pharmacy First Clinical

### Total population

```{r, message=FALSE, warning=FALSE, fig.height=4, fig.width=4}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above

```{r, message=FALSE, warning=FALSE}
# Select measures and breakdown
df_measures_selected <- df_measures %>%
filter(measure_desc == "clinical_condition") %>%
Expand All @@ -234,6 +274,8 @@ plot_measures(
guide_nrow = 1,
facet_wrap = FALSE,
facet_var = NULL,
title = "Number of consultations for each clinical condition per month",
y_label = "Number of codes for consultations",
)
```

Expand All @@ -253,7 +295,9 @@ plot_measures(
colour_var = age_band,
guide_nrow = 1,
facet_wrap = TRUE,
facet_var = measure
facet_var = measure,
title = "Number of consultations for each clinical condition by age band per month",
y_label = "Number of codes for consultations",
)
```

Expand All @@ -274,6 +318,8 @@ plot_measures(
guide_nrow = 1,
facet_wrap = TRUE,
facet_var = measure,
title = "Number of consultations for each clinical condition by sex per month",
y_label = "Number of codes for consultations",
)
```

Expand All @@ -294,6 +340,8 @@ plot_measures(
guide_nrow = 1,
facet_wrap = TRUE,
facet_var = measure,
title = "Number of consultations for each clinical condition by IMD per month",
y_label = "Number of codes for consultations",
)
```

Expand All @@ -314,12 +362,14 @@ plot_measures(
guide_nrow = 2,
facet_wrap = TRUE,
facet_var = measure,
title = "Number of consultations for each clinical condition by region per month",
y_label = "Number of codes for consultations",
)
```

### Clinical Conditions by ethnicity

```{r, message=FALSE, warning=FALSE}
```{r, message=FALSE, warning=FALSE, fig.height=15, fig.width=8}
viv3ckj marked this conversation as resolved.
Show resolved Hide resolved

# Select measures and breakdown
df_measures_selected <- df_measures %>%
Expand All @@ -335,5 +385,7 @@ plot_measures(
guide_nrow = 2,
facet_wrap = TRUE,
facet_var = measure,
title = "Number of consultations for each clinical condition by ethnicity per month",
y_label = "Number of codes for consultations",
)
```