generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Counts by Pharmacy First Service #3
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
Closed
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e0a66cd
Phamarcy First event measures & graph
viv3ckj f6eb975
added measures action
viv3ckj 25fa351
Delete unused files from template
milanwiedemann c8544c1
Restructure and rename plotting fun
milanwiedemann 988bb1f
Merge pull request #4 from opensafely/milanwiedemann/measures-suggest…
viv3ckj 2824469
Tidied and documented function and measures.
viv3ckj 7a80e1f
Graphs for events and conditions added
viv3ckj ed865e7
Modified legend key and created dictionary for codelist
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 was deleted.
Oops, something went wrong.
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,69 @@ | ||
from ehrql import INTERVAL, create_measures, months, codelist_from_csv | ||
from ehrql.tables.tpp import clinical_events, patients, practice_registrations | ||
|
||
measures = create_measures() | ||
|
||
# Dictionary of pharmacy first codes | ||
pharmacy_first_event_codes = { | ||
# Community Pharmacy (CP) Blood Pressure (BP) Check Service (procedure) | ||
"blood_pressure_service": ["1659111000000107"], | ||
# Community Pharmacy (CP) Contraception Service (procedure) | ||
"contraception_service": ["1659121000000101"], | ||
# Community Pharmacist (CP) Consultation Service for minor illness (procedure) | ||
"consultation_service": ["1577041000000109"], | ||
# Pharmacy First service (qualifier value) | ||
"pharmacy_first_service": ["983341000000102"], | ||
} | ||
|
||
registration = practice_registrations.for_patient_on(INTERVAL.end_date) | ||
|
||
# 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) | ||
) | ||
|
||
# Loop through each condition to create a measure | ||
for condition_name, codelist in pharmacy_first_event_codes.items(): | ||
condition_events = selected_events.where( | ||
clinical_events.snomedct_code.is_in(codelist) | ||
) | ||
|
||
# Define the numerator as the count of events for the condition | ||
numerator = condition_events.count_for_patient() | ||
|
||
|
||
# Define the denominator as the number of patients registered | ||
denominator = registration.exists_for_patient() | ||
|
||
# Define the measure | ||
measures.define_measure( | ||
name=f"count_{condition_name}", | ||
numerator=numerator, | ||
denominator=denominator, | ||
intervals=months(8).starting_on("2023-11-01") | ||
) | ||
|
||
measures.configure_dummy_data(population_size=1000) | ||
viv3ckj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# # Count pharmacy first codes | ||
# pharmacy_first_code_counts = {} | ||
|
||
# for code_desc, code in pharmacy_first_event_codes.items(): | ||
# count_codes_query = selected_events.where( | ||
# selected_events.snomedct_code.is_in(code) | ||
# ).count_for_patient() | ||
# pharmacy_first_code_counts[f"count_{code_desc}"] = count_codes_query | ||
|
||
|
||
# for measures_name, code_counts in pharmacy_first_code_counts.items(): | ||
# measures.define_measure( | ||
# name=measures_name, | ||
# numerator=code_counts, | ||
# group_by={ | ||
# "practice_region": registration.practice_nuts1_region_name | ||
# }, | ||
# denominator=patients.exists_for_patient(), | ||
# intervals=intervals, | ||
# ) | ||
|
||
|
This file was deleted.
Oops, something went wrong.
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,31 @@ | ||
plot_measures <- function( | ||
viv3ckj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data, | ||
title = NULL, | ||
x_label = NULL, | ||
y_label = NULL) { | ||
|
||
# Create plot | ||
plot <- ggplot( | ||
data, | ||
aes( | ||
x = interval_end, | ||
y = numerator, | ||
color = measure, | ||
group = measure | ||
viv3ckj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
) + | ||
geom_line() + | ||
labs( | ||
title = title, | ||
x = x_label, | ||
y = y_label, | ||
color = "Condition" | ||
) + | ||
# Setting the minimum y-value | ||
ylim(y_min, NA) + | ||
viv3ckj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Applying the minimal theme | ||
theme_minimal() | ||
viv3ckj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Return plot | ||
plot | ||
} |
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,23 @@ | ||
# In this file we're trying out some code | ||
library(tidyverse) | ||
library(ggplot2) | ||
library(gridExtra) # For arranging multiple plots | ||
|
||
# Load data | ||
df_measures <- readr::read_csv( | ||
here::here("output", "report", "conditions_measures.csv") | ||
) | ||
|
||
# Develop plotting function | ||
p <- ggplot(df_measures, aes(x = interval_end, y = numerator, color = measure, group = measure)) + | ||
geom_line() + | ||
labs( | ||
title = paste("Number of Consultations for each Pharmacy First Clinical Condition per month"), | ||
x = "Date", | ||
y = "Number of Consultations", | ||
color = "Condition" | ||
) + | ||
ylim(0, NA) | ||
|
||
source(here::here("lib", "functions", "graph_func.R")) | ||
viv3ckj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
plot1_conditions <- plot_pharmacy_first_conditions(df_measures) |
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.