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.
Refactor data loading and tidying into separate scripts
This will improve readability and modularity and make it easier for us to make further changes later on. It's only a start, there's much more we could do.
- Loading branch information
1 parent
70e8857
commit d89d6e3
Showing
4 changed files
with
171 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Load data based on execution environment | ||
if (Sys.getenv("OPENSAFELY_BACKEND") != "") { | ||
# Load data from generate_pf_measures action | ||
df_measures <- readr::read_csv( | ||
here("output", "measures", "pf_codes_conditions_measures.csv") | ||
) | ||
df_descriptive_stats <- read_csv( | ||
here("output", "measures", "pf_descriptive_stats_measures.csv") | ||
) | ||
df_pfmed <- read_csv( | ||
here("output", "measures", "pf_medications_measures.csv") | ||
) | ||
df_condition_provider <- read_csv( | ||
here("output", "measures", "pf_condition_provider_measures.csv") | ||
) | ||
} else { | ||
# Load data from released_output directory | ||
df_measures <- readr::read_csv( | ||
here("released_output", "measures", "pf_codes_conditions_measures.csv") | ||
) | ||
df_descriptive_stats <- read_csv( | ||
here("released_output", "measures", "pf_descriptive_stats_measures.csv") | ||
) | ||
df_pfmed <- read_csv( | ||
here("released_output", "measures", "pf_medications_measures.csv") | ||
) | ||
df_condition_provider <- read_csv( | ||
here("released_output", "measures", "pf_condition_provider_measures.csv") | ||
) | ||
} | ||
|
||
df_measures <- tidy_measures( | ||
data = df_measures, | ||
pf_measures_name_dict = pf_measures_name_dict, | ||
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 | ||
) | ||
|
||
df_measures <- df_measures %>% | ||
mutate(sex = factor(sex, | ||
levels = c("female", "male"), | ||
labels = c("Female", "Male") | ||
)) | ||
|
||
df_measures$age_band[is.na(df_measures$age_band)] <- "Missing" |
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,42 @@ | ||
library(readr) | ||
library(tidyr) | ||
library(dplyr) | ||
library(here) | ||
|
||
df_bsa_consultation_validation <- read_csv( | ||
here("lib", "validation", "data", "pf_consultation_validation_data.csv") | ||
) %>% | ||
rename(count_100pct = count) |> | ||
mutate(count_40pct = round(as.numeric(count_100pct * .4), digits = 0)) %>% | ||
mutate(source = "nhs_bsa") |> | ||
pivot_longer( | ||
cols = c(count_100pct, count_40pct), | ||
names_to = "count_method", | ||
values_to = "count" | ||
) | ||
|
||
df_bsa_consultation_validation <- df_bsa_consultation_validation %>% | ||
mutate(consultation_type = factor(consultation_type, | ||
levels = c( | ||
"sinusitis", | ||
"infected_insect_bites", | ||
"uncomplicated_uti", | ||
"acute_otitis_media", | ||
"acute_sore_throat", | ||
"shingles", | ||
"impetigo" | ||
), | ||
labels = c( | ||
"Acute Sinusitis", | ||
"Infected Insect Bite", | ||
"UTI", | ||
"Acute Otitis Media", | ||
"Acute Pharyngitis", | ||
"Herpes Zoster", | ||
"Impetigo" | ||
) | ||
)) | ||
|
||
df_bsa_medication_validation <- read_csv( | ||
here("lib", "validation", "data", "pf_medication_validation_data.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
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