/
07_eol_WP3_MAIHDA.R
128 lines (90 loc) · 5.17 KB
/
07_eol_WP3_MAIHDA.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
##############################################################
# MAIHDA analysis
# Author: Miranda & Sophie
# Date: 07/12/23 # nolint # nolint: commented_code_linter.
# Initial aim: Running descriptive statistics
# for potential stratum variables to determine whether group sizes are sufficient
##############################################################
# Note: Patients with no IMD are excluded from the analysis as are patients aged 0-24.
# Analysis focuses on patients who die at home, with a cancer diagnosis.
# Ethnicity is considered as two groups for the purpose of MAIHDA, but more detailed analysis of ethnicity will be conducted separately.
# Analysis over a two calendar year period
# Load packages
library(tidyverse)
library(lubridate)
library(dplyr)
library(data.table)
# Create folder structure
fs::dir_create("output", "os_reports", "WP3")
# Code settings
startdate <- dmy("01-01-2022")
enddate <- dmy("31-12-2023")
# Prepare data
df <- read_csv(file = here::here("output", "os_reports", "input_os_reports.csv.gz")) %>%
mutate(dod_ons = as_date(dod_ons)
, Ethnicity_2 = case_when(ethnicity_Combined == "White" ~ "White"
, ethnicity_Combined == "Asian or Asian British" | ethnicity_Combined == "Black or Black British" | ethnicity_Combined == "Mixed" | ethnicity_Combined == "Chinese or Other Ethnic Groups" | ethnicity_Combined == "Not stated" ~ "All other ethnic groups")
, study_month = floor_date(dod_ons, unit = "month")
, pod_ons_new = case_when(pod_ons == "Elsewhere"
| pod_ons == "Other communal establishment" ~ "Elsewhere/other"
, TRUE ~ as.character(pod_ons))
, cod_ons_3 = str_sub(cod_ons, 1, 3)
, cod_ons_4 = str_sub(cod_ons, 1, 5)
, codgrp = case_when(cod_ons_4 %in% c("U071", "U072") ~ "Covid-19"
, cod_ons_3 >= "J09" & cod_ons_3 <= "J18" ~ "Flu and pneumonia"
, (cod_ons_3 >= "J00" & cod_ons_3 <= "J08") | (cod_ons_3 >= "J19" & cod_ons_3 <= "J99") ~ "Other respiratory diseases"
, cod_ons_3 %in% c("F01", "F03", "G30") ~ "Dementia and Alzheimer's disease"
, cod_ons_3 >= "I00" & cod_ons_3 <= "I99" ~ "Circulatory diseases"
, cod_ons_3 >= "C00" & cod_ons_3 <= "C99" ~ "Cancer"
, TRUE ~ "All other causes")) %>%
filter(study_month >= startdate & study_month <= enddate & imd_quintile >=1 & age_band != "0-24" & codgrp == "Cancer" & pod_ons_new == "Home")
# Counts by grouping variables _RAW not for release
count_by_group_RAW <- df %>%
filter(codgrp == "Cancer"
& pod_ons_new == "Home") %>%
count(sex, age_band, Ethnicity_2, imd_quintile)
fwrite(count_by_group_RAW, here::here("output", "os_reports", "WP3", "count_by_group_RAW.csv"))
cols_of_interest <- c("n");
count_by_group <- df %>%
filter(codgrp == "Cancer"
& pod_ons_new == "Home") %>%
count(sex, age_band, Ethnicity_2, imd_quintile) %>%
dplyr::mutate(across(.cols = all_of(cols_of_interest), .fns = ~ replace(.x, (. <= 7 & . > 0), NA))) %>%
dplyr::mutate(across(.cols = all_of(cols_of_interest), .fns = ~ .x %>% `/`(5) %>% round()*5));
fwrite(count_by_group, here::here("output", "os_reports", "WP3", "count_by_group.csv"))
# MAIDHA analysis with GP consultations as the outcome
# Table: Group level mean (GLM) GP Interactions (Counts rounded to the nearest 5)
cols_of_interest <- c("count");
GLM_sex <- df %>%
group_by(sex) %>%
summarise(count = n(),
mean = mean(gp_1m, na.rm = TRUE)
, sd = sd(gp_1m, na.rm = TRUE)) %>%
dplyr::mutate(across(.cols = all_of(cols_of_interest), .fns = ~ .x %>% `/`(5) %>% round()*5));
fwrite(GLM_sex, here::here("output", "os_reports", "WP3", "GLM_sex.csv"))
GLM_Ethnicity_2 <- df %>%
group_by(Ethnicity_2) %>%
summarise(count = n(),
mean = mean(gp_1m, na.rm = TRUE)
, sd = sd(gp_1m, na.rm = TRUE)) %>%
dplyr::mutate(across(.cols = all_of(cols_of_interest), .fns = ~ .x %>% `/`(5) %>% round()*5));
fwrite(GLM_Ethnicity_2, here::here("output", "os_reports", "WP3", "GLM_Ethnicity_2.csv"))
GLM_imd_quintile <- df %>%
group_by(imd_quintile) %>%
summarise(count = n(),
mean = mean(gp_1m, na.rm = TRUE)
, sd = sd(gp_1m, na.rm = TRUE)) %>%
dplyr::mutate(across(.cols = all_of(cols_of_interest), .fns = ~ .x %>% `/`(5) %>% round()*5));
fwrite(GLM_imd_quintile, here::here("output", "os_reports", "WP3", "GLM_imd_quintile.csv"))
GLM_age_band <- df %>%
group_by(age_band) %>%
summarise(count = n(),
mean = mean(gp_1m, na.rm = TRUE)
, sd = sd(gp_1m, na.rm = TRUE)) %>%
dplyr::mutate(across(.cols = all_of(cols_of_interest), .fns = ~ .x %>% `/`(5) %>% round()*5));
fwrite(GLM_age_band, here::here("output", "os_reports", "WP3", "GLM_age_band.csv"))
# Form intersectional strata (80 strata in total) - development syntax
# dplyr::group_by(sex, age_band, Ethnicity_2, imd_quintile) %>% dplyr::mutate(strata = cur_group_id())
# Calculate simple intersectional model
# model1 <- brm(gp_1m ~ 1 + (1|strata), data = gp_MAIHDA, family = poisson)
# summary(fm1)