Skip to content

Commit

Permalink
Add results to write up figures
Browse files Browse the repository at this point in the history
  • Loading branch information
milanwiedemann committed Jan 29, 2025
1 parent 2791b88 commit 5f16f83
Showing 1 changed file with 41 additions and 229 deletions.
270 changes: 41 additions & 229 deletions reports/create_results_manuscript.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ library(here)
library(readr)
library(gt)
library(patchwork)
library(scales)
```

```{r load-data, message=FALSE, warning=FALSE}
Expand All @@ -35,8 +36,11 @@ source(here("lib", "functions", "load_validation_data.R"))
source(here("lib", "functions", "load_opensafely_outputs.R"))
```

# Figures
## Figure 1. OpenSAFELY total counts
# Results

## OpenSAFELY total counts

### Figure 1

```{r, message=FALSE, warning=FALSE}
# Create figure for total count of Pharmacy First consultations for each code (3 codes)
Expand Down Expand Up @@ -95,7 +99,30 @@ ggsave(
)
```

## Figure 2. OpenSAFELY Linkage
### Results for Figure 1 description

```{r, message=FALSE, warning=FALSE}
df_results_pf_total_counts <- df_measures %>%
filter(measure_desc == "clinical_service") %>%
filter(is.na(group_by)) |>
group_by(interval_start) |>
mutate(
pf_consultation_total = sum(numerator, na.rm = TRUE),
data_desc = "Pharmacy First Consultation"
) %>%
select(interval_start, pf_consultation_total) %>%
distinct() %>%
ungroup() %>%
mutate(pf_consultation_diff = pf_consultation_total - lag(pf_consultation_total))
df_results_pf_total_counts %>%
filter(interval_start %in% c("2024-02-01", "2024-12-01", "2024-08-01", "2024-09-01", "2024-10-01"))
```


## OpenSAFELY Linkage

### Figure 2

```{r, message=FALSE, warning=FALSE, echo = FALSE, fig.width=8}
# Create figure to show & of PF Med, Condition and both with linked PF consultations
Expand Down Expand Up @@ -154,236 +181,21 @@ ggsave(
)
```

### Figure 3. Validation

```{r, message=FALSE, warning=FALSE, echo = FALSE}
# Create figure to compare OS and BSA counts for PF clinical conditions
### Results for Figure 2 description

# OpenSAFELY data for clinical conditions into a tidy df
df_opensafely_validation <- df_measures %>%
filter(measure_desc == "clinical_condition") %>%
filter(is.na(group_by)) %>%
select(date = interval_start, consultation_type = measure, count = numerator) %>%
```{r}
df_results_pf_linkage <- df_pf_descriptive_stats %>%
select(measure, interval_start, ratio, numerator) %>%
group_by(interval_start) %>%
mutate(
source = "opensafely",
count_method = "opensafely_tpp"
) |>
filter(date >= "2024-01-01") %>%
relocate(date, consultation_type, source, count_method, count)
# Combining rows from OS and BSA validation dataframes
df_validation_condition_total <- bind_rows(
df_opensafely_validation,
df_bsa_consultation_validation
ratio_total_linked = sum(ratio),
ratio_total_unlinked = 1 - sum(ratio)
) %>%
filter(count_method %in% c("opensafely_tpp", "count_40pct")) %>%
group_by(date, source) %>%
mutate(total_count = sum(count, na.rm = TRUE)) %>%
select(date, source, count_method, total_count) %>%
distinct() %>%
filter(date >= "2024-02-01")
# Line graph comparing clinical condition counts of BSA and OS data
df_validation_condition_total_counts <- df_validation_condition_total %>%
filter(count_method %in% c("opensafely_tpp", "count_40pct")) %>%
filter(date >= "2024-01-01") %>%
mutate(source = factor(source,
levels = c("opensafely", "nhs_bsa"),
labels = c("OpenSAFELY-TPP", "NHS BSA (40%)")
))
# Create visualisation
fig_validation_condition_total_count <- plot_measures(
df_validation_condition_total_counts,
select_value = total_count,
select_interval_date = date,
guide_nrow = 2,
facet_wrap = TRUE,
facet_var = source,
y_label = "Count",
y_scale = "free_y",
shapes = "condition_shapes",
colour_palette = "plasma",
add_vline = FALSE,
date_breaks = "1 month"
)
fig_validation_condition_total_count
# Another plot visualising the percentage
df_validation_condition_pct <- df_validation_condition_total %>%
filter(count_method %in% c("opensafely_tpp", "count_40pct")) %>%
filter(date >= "2024-01-01") %>%
pivot_wider(names_from = c(source, count_method), values_from = total_count) %>%
mutate(source = "Percentage of NHS BSA (40%) in OpenSAFELY")
fig_validation_condition_pct <- plot_measures(
df_validation_condition_pct,
select_value = opensafely_opensafely_tpp / nhs_bsa_count_40pct,
select_interval_date = date,
guide_nrow = 2,
facet_wrap = TRUE,
facet_var = source,
scale_measure = "percent",
y_label = "Percent",
y_scale = "free_y",
shapes = "condition_shapes",
colour_palette = "plasma",
date_breaks = "2 month"
)
fig_validation_condition_comparison <- (fig_validation_condition_count + fig_validation_condition_pct) %>%
set_patchwork_theme()
save_figure(fig_validation_condition_comparison, width = 15)
```

```{r, message=FALSE, warning=FALSE, echo = FALSE}
# Create figure to compare OS and BSA counts for PF medication
df_bsa_medication_validation_sum <- df_bsa_medication_validation %>%
group_by(date) %>%
summarise(count = sum(count) * 0.4) %>%
mutate(
source = "nhs_bsa",
count_method = "count_40pct"
)
range(df_pfmed$interval_start)
df_opensafely_pfmed_sum <- df_pfmed %>%
rename(date = interval_start) %>%
group_by(date) %>%
summarise(count = sum(numerator)) %>%
mutate(
source = "opensafely_tpp",
count_method = "opensafely_tpp"
ratio_total_linked = percent(ratio_total_linked, accuracy = 0.1),
ratio_total_unlinked = percent(ratio_total_unlinked, accuracy = 0.1)
)
df_validation_med_counts <- bind_rows(df_opensafely_pfmed_sum, df_bsa_medication_validation_sum) |>
filter(date >= "2024-01-01" & date <= "2024-10-01")
df_validation_med_counts <- df_validation_med_counts %>%
mutate(
source = factor(source, levels = c("opensafely_tpp", "nhs_bsa"), labels = c("OpenSAFELY-TPP", "NHS BSA")),
count_method = factor(count_method, levels = c("opensafely_tpp", "count_40pct"), labels = c("OpenSAFELY-TPP", "NHS BSA (40%)"))
)
fig_validation_med_count <- plot_measures(
df_validation_med_counts,
select_value = count,
select_interval_date = date,
colour_var = count_method,
guide_nrow = 1,
facet_wrap = TRUE,
facet_var = source,
y_scale = "free_y",
y_label = "Count",
colour_palette = "plasma",
date_breaks = "1 month"
)
# Another plot visualising the percentage
df_validation_med_pct <- df_validation_med_counts %>%
filter(count_method %in% c("OpenSAFELY-TPP", "NHS BSA (40%)")) %>%
pivot_wider(names_from = c(source, count_method), values_from = count) %>%
mutate(source = "Percentage of NHS BSA (40%) in OpenSAFELY")
fig_validation_med_pct <- plot_measures(
df_validation_med_pct,
select_value = `OpenSAFELY-TPP_OpenSAFELY-TPP` / `NHS BSA_NHS BSA (40%)`,
select_interval_date = date,
colour_var = source,
guide_nrow = 1,
facet_wrap = TRUE,
facet_var = source,
scale_measure = "percent",
y_scale = "free_y",
y_label = "Count",
colour_palette = "plasma",
date_breaks = "1 month"
)
fig_validation_med_comparison <- (fig_validation_med_count + fig_validation_med_pct) %>%
set_patchwork_theme()
save_figure(fig_validation_med_comparison, width = 15)
```


## Figure A1

```{r, message=FALSE, warning=FALSE, fig.height=10, fig.width=8}
# Create figure for total count of Pharmacy First grouped conditions (no breakdowns)
df_measures_selected <- df_measures %>%
filter(measure_desc == "clinical_condition") %>%
filter(is.na(group_by))
# Create visualisation
fig_pf_grouped_conditions_count <- plot_measures(
df_measures_selected,
select_value = numerator,
select_interval_date = interval_end,
guide_nrow = 1,
facet_wrap = TRUE,
facet_var = measure,
title = "Pharmacy First Conditions",
y_label = "Number of codes for PF conditions",
)
save_figure(fig_pf_grouped_conditions_count)
df_results_pf_linkage %>%
filter(interval_start %in% c("2024-02-01", "2024-12-01", "2024-08-01", "2024-09-01", "2024-10-01"))
```





```{r, message=FALSE, warning=FALSE, echo = FALSE, fig.width=8}
# Create figure to compare clinical events linked to PF consultation and not linked
df_condition_provider_grouped <- df_condition_provider %>%
group_by(measure, interval_start, pf_status) %>%
summarise(
count = sum(numerator)
) %>%
mutate(
measure = factor(measure,
levels = c(
"count_acute_sinusitis_total",
"count_infected_insect_bite_total",
"count_uncomplicated_urinary_tract_infection_total",
"count_acute_otitis_media_total",
"count_acute_pharyngitis_total",
"count_herpes_zoster_total",
"count_impetigo_total"
),
labels = c(
"Acute Sinusitis",
"Infected Insect Bite",
"UTI",
"Acute Otitis Media",
"Acute Pharyngitis",
"Herpes Zoster",
"Impetigo"
)
),
pf_status = factor(pf_status,
levels = c(TRUE, FALSE),
labels = c("Linked to Pharmacy First consultation", "Not linked to Pharmacy First consultation")
)
)
fig_pf_condition_provider_count <- plot_measures(
df_condition_provider_grouped,
select_value = count,
select_interval_date = interval_start,
colour_var = pf_status,
guide_nrow = 1,
facet_wrap = TRUE,
facet_var = measure,
y_label = "Count",
date_breaks = "6 month",
colour_palette = "plasma"
)
save_figure(fig_pf_condition_provider_count)
```

# References

0 comments on commit 5f16f83

Please sign in to comment.