generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Viv3ckj/add hyp001 measures #18
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
Open
viv3ckj
wants to merge
6
commits into
main
Choose a base branch
from
viv3ckj/add-hyp001-measures
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f0a3941
Added measures.py
viv3ckj 059a966
Added measures to project.yaml
viv3ckj f3a8bcd
Added additional measures and groupings
viv3ckj 7dd49f2
outputs produced and measures amended
viv3ckj 5ea5ca5
added plot graph action and save plots as png
viv3ckj 3f35b30
Added suggested changes
viv3ckj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| from ehrql import INTERVAL, case, create_measures, months, when, codelist_from_csv, create_dataset | ||
| from ehrql.tables.tpp import ( | ||
| clinical_events, | ||
| patients, | ||
| practice_registrations, | ||
| ) | ||
|
|
||
| # Hypertension codes | ||
| hyp_cod = codelist_from_csv( | ||
| "codelists/nhsd-primary-care-domain-refsets-hyp_cod.csv", | ||
| column="code" | ||
| ) | ||
| # Hypertension resolved codes | ||
| hypres_cod = codelist_from_csv( | ||
| "codelists/nhsd-primary-care-domain-refsets-hypres_cod.csv", | ||
| column="code" | ||
| ) | ||
|
|
||
| index_date = "2024-03-31" | ||
| measures = create_measures() | ||
|
|
||
| ### variables ### | ||
|
|
||
| selected_events = clinical_events.where( | ||
| clinical_events.date.is_on_or_before(INTERVAL.end_date) | ||
| ) | ||
|
|
||
| # date of the most recent hypertension diagnosis up to and including the achievement date | ||
| hyplat_dat = ( | ||
| selected_events.where(selected_events.snomedct_code.is_in(hyp_cod)) | ||
| .sort_by(selected_events.date) | ||
| .last_for_patient() | ||
| .date | ||
| ) | ||
|
|
||
| # date of the most recent hypertension diagnosis resolved code recorded after the most recent hypertension diagnosis and up to and including the achievement date | ||
| hypres_dat = ( | ||
| selected_events.where(selected_events.snomedct_code.is_in(hypres_cod)) | ||
| .sort_by(selected_events.date) | ||
| .last_for_patient() | ||
| .date | ||
| ) | ||
|
|
||
| # Rule for patients with a hypertension code that has not been resolved | ||
| hyp_reg_r1 = hyplat_dat.is_not_null() & hypres_dat.is_null() | (hyplat_dat > hypres_dat) | ||
|
|
||
| age = patients.age_on(INTERVAL.start_date) | ||
| age_band = case( | ||
| when((age >= 0) & (age < 20)).then("0-19"), | ||
| when((age >= 20) & (age < 40)).then("20-39"), | ||
| when((age >= 40) & (age < 60)).then("40-59"), | ||
| when((age >= 60) & (age < 80)).then("60-79"), | ||
| when(age >= 80).then("80+"), | ||
| ) | ||
|
|
||
| registration = practice_registrations.for_patient_on(INTERVAL.end_date) | ||
|
|
||
| # Measure defining the rate of patients with unresolved hypertension per age band and sex | ||
| measures.define_measure( | ||
| name="diagnosis_by_age", | ||
| numerator=hyp_reg_r1 == True, | ||
| denominator=registration.exists_for_patient(), | ||
| group_by={ | ||
| "age_band": age_band, | ||
| "sex": patients.sex, | ||
| }, | ||
| intervals=months(12).starting_on("2023-04-01"), | ||
| ) | ||
|
|
||
| # Measure defining the rate of patients with resolved hypertension per age band and sex | ||
| measures.define_measure( | ||
| name="resolved_by_age", | ||
| numerator=hyp_reg_r1 == False, | ||
| denominator=registration.exists_for_patient(), | ||
| group_by={ | ||
| "age_band": age_band, | ||
| "sex": patients.sex, | ||
| }, | ||
| intervals=months(12).starting_on("2023-04-01"), | ||
| ) | ||
viv3ckj marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| library(tidyverse) | ||
| library(patchwork) | ||
| library(here) | ||
|
|
||
| # Load data | ||
| df_measures <- read_csv( | ||
| here("output", "hyp", "hyp001_measures.csv")) %>% | ||
| replace_na(list(age_band = "(Missing)")) %>% | ||
| mutate( | ||
| start_date = as.Date(interval_start, format = "%Y-%m-%d"), | ||
| end_date = as.Date(interval_end, format = "%Y-%m-%d"), | ||
| age_band = factor( | ||
| age_band, | ||
| levels = c("0-19", "20-39", "40-59", "60-79", "80+", "(Missing)"), | ||
| labels = c("0-19", "20-39", "40-59", "60-79", "80+", "(Missing)") | ||
| ) | ||
| ) | ||
|
|
||
| plot_hypres <- df_measures %>% | ||
| filter(measure == "resolved_by_age") %>% | ||
| ggplot(aes( | ||
| x = end_date, | ||
| y = ratio, | ||
| colour = age_band, | ||
| )) + | ||
| geom_point() + | ||
| geom_line(alpha = .5) + | ||
| labs( | ||
| title = NULL, | ||
| x = NULL, | ||
| y = "Patients with hypertension resolved code", | ||
| colour = "Age Band" | ||
| ) + | ||
| scale_y_continuous( | ||
| labels = scales::label_percent(), | ||
| limits = c(0,1) | ||
| ) + | ||
| facet_wrap(~ factor( | ||
| sex, | ||
| levels = c("female", "male", "intersex", "unknown"), | ||
| labels = c("Female", "Male", "Intersex", "Unknown") | ||
| )) | ||
|
|
||
| plot_hyp <- df_measures %>% | ||
| filter(measure == "diagnosis_by_age") %>% | ||
| ggplot(aes( | ||
| x = end_date, | ||
| y = ratio, | ||
| colour = age_band, | ||
| )) + | ||
| geom_point() + | ||
| geom_line(alpha = .5) + | ||
| labs( | ||
| title = NULL, | ||
| x = NULL, | ||
| y = "Patients with hypertension code", | ||
| colour = "Age Band" | ||
| ) + | ||
| scale_y_continuous( | ||
| labels = scales::label_percent(), | ||
| limits = c(0,1) | ||
| ) + | ||
| facet_wrap(~ factor( | ||
| sex, | ||
| levels = c("female", "male", "intersex", "unknown"), | ||
| labels = c("Female", "Male", "Intersex", "Unknown") | ||
| )) | ||
|
|
||
| plot_hyp <- (plot_hyp / plot_hypres) + | ||
| plot_layout(guides = "collect") | ||
|
|
||
| ggsave("output/hyp/hyp_plot.png", width = 8, height = 8) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.