generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from opensafely/viv3ckj/move_validation_data
Viv3ckj/move validation data
- Loading branch information
Showing
6 changed files
with
247 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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
72 changes: 72 additions & 0 deletions
72
analysis/measures_definition_pf_consultation_med_counts.py
This file contains 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 @@ | ||
from ehrql import INTERVAL, create_measures, months | ||
from ehrql.tables.tpp import ( | ||
patients, | ||
clinical_events, | ||
practice_registrations, | ||
) | ||
from ehrql.tables.raw.tpp import medications | ||
|
||
from config import start_date_measure_med_counts, monthly_intervals_measure_med_counts | ||
from codelists import ( | ||
pharmacy_first_consultation_codelist, | ||
pharmacy_first_med_codelist, | ||
) | ||
from pf_variables_library import select_events | ||
|
||
# Script taken from Pharmacy First Data Development (for top 10 PF meds table) | ||
|
||
measures = create_measures() | ||
measures.configure_dummy_data(population_size=1000) | ||
|
||
start_date = start_date_measure_med_counts | ||
monthly_intervals = monthly_intervals_measure_med_counts | ||
|
||
registration = practice_registrations.for_patient_on(INTERVAL.end_date) | ||
|
||
# Select Pharmacy First events during interval date range | ||
pharmacy_first_events = select_events( | ||
clinical_events, | ||
start_date=INTERVAL.start_date, | ||
end_date=INTERVAL.end_date).where( | ||
clinical_events.snomedct_code.is_in( | ||
pharmacy_first_consultation_codelist | ||
) | ||
) | ||
|
||
pharmacy_first_ids = pharmacy_first_events.consultation_id | ||
has_pharmacy_first_consultation = pharmacy_first_events.exists_for_patient() | ||
|
||
# Select Pharmacy First consultations during interval date range | ||
selected_medications = select_events( | ||
medications, | ||
start_date=INTERVAL.start_date, end_date=INTERVAL.end_date | ||
).where(medications.consultation_id.is_in(pharmacy_first_ids)) | ||
|
||
# First medication for each patient | ||
first_selected_medication = ( | ||
selected_medications.sort_by(selected_medications.date).first_for_patient().dmd_code | ||
) | ||
# Boolean variable that selected medication is part of pharmacy first med codelists | ||
has_pharmacy_first_medication = first_selected_medication.is_in(pharmacy_first_med_codelist) | ||
|
||
# Numerator, patients with a PF medication | ||
# This allows me to count all (first) medications linked to a PF consultation | ||
numerator = first_selected_medication.is_not_null() | ||
|
||
# Denominator, registered patients (f/m) with a PF consultation | ||
denominator = ( | ||
registration.exists_for_patient() | ||
& patients.sex.is_in(["male", "female"]) | ||
& has_pharmacy_first_consultation | ||
) | ||
|
||
measures.define_measure( | ||
name="pf_medication_count", | ||
numerator = first_selected_medication.is_not_null(), | ||
denominator=denominator, | ||
group_by={ | ||
"dmd_code": first_selected_medication, | ||
"pharmacy_first_med": has_pharmacy_first_medication, | ||
}, | ||
intervals=months(monthly_intervals).starting_on(start_date), | ||
) |
This file contains 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
This file contains 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
This file contains 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,36 @@ | ||
library(tidyverse) | ||
library(janitor) | ||
library(here) | ||
library(httr) | ||
|
||
# Function to download and read the xlsx files | ||
read_xlsx_from_url <- function(url_list, sheet = NULL, skip = NULL, ...) { | ||
temp_file <- tempfile(fileext = ".xlsx") | ||
GET( | ||
url_list, | ||
write_disk(temp_file, overwrite = TRUE) | ||
) | ||
readxl::read_xlsx( | ||
temp_file, | ||
col_names = TRUE, | ||
.name_repair = janitor::make_clean_names, | ||
sheet = sheet, | ||
skip = skip, | ||
... | ||
) | ||
} | ||
|
||
df <- read_xlsx_from_url( | ||
"https://github.com/user-attachments/files/17774058/EPS.and.eRD.Prescribing.Dashboard.July.2024.xlsx", | ||
skip = 2, | ||
sheet = "Historical Data" | ||
) | ||
|
||
df_filtered <- df %>% | ||
select(month, region_code, practice_code, eps_items, erd_items) %>% | ||
filter(month %in% c(202402, 202403, 202404, 202405, 202406, 202407)) %>% | ||
mutate(month = ym(month)) | ||
|
||
df_filtered |> write_csv( | ||
here("lib", "validation", "data", "eps_erd_prescribing_2024-02-01_to_2024-07-01.csv") | ||
) |
This file contains 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