generated from opensafely/research-template
/
create_BMI_all_2020.R
185 lines (131 loc) · 6.47 KB
/
create_BMI_all_2020.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
## Miriam Samuel
## Modified: 23rd March 2022
## Create a data set that has data on those who did and did not have BMI measured to perform a regression.
# 1) Read in files
# BMI_complete_median_2020.feather :: contains BMI data on those who had a BMI check
# input_all_2020:: contains demographic data on whole population
# BMI_complete_median: contains the pre-covid obese flag
# >> Combining these files to create a file for the analysis of who had a BMI
## packages
library(broom)
library(purrr)
library(dplyr)
library(janitor)
library(tidyverse)
library(arrow)
BMI_complete_categories <- read_feather (here::here ("output/data", "BMI_complete_median_2020.feather"))
input_all_2020_03_01 <- read_feather (here::here ("output/data", "input_all_2020-03-01.feather"))
precovid_obese <- read_feather (here::here ("output/data", "BMI_complete_median.feather"))
## Input data on all patients (not just those with a BMI)
all_patients_2020 <- as_tibble (input_all_2020_03_01)
# 2) recode and label some demographic components
# recode ethnicity so NA is 0
all_patients_2020 <- all_patients_2020 %>%
mutate(ethnic_no_miss = ifelse(is.na(ethnicity), 0, ethnicity ))
all_patients_2020 <- all_patients_2020 %>%
mutate(ethnicity_16_no_miss = ifelse(is.na(ethnicity_16), 0, ethnicity_16 ))
# label
all_patients_2020$ethnic_no_miss[all_patients_2020$ethnic_no_miss=="1"]<-"White"
all_patients_2020$ethnic_no_miss[all_patients_2020$ethnic_no_miss=="2"]<-"Mixed"
all_patients_2020$ethnic_no_miss[all_patients_2020$ethnic_no_miss=="3"]<-"Asian"
all_patients_2020$ethnic_no_miss[all_patients_2020$ethnic_no_miss=="4"]<-"Black"
all_patients_2020$ethnic_no_miss[all_patients_2020$ethnic_no_miss=="5"]<-"Other"
all_patients_2020$ethnic_no_miss[all_patients_2020$ethnic_no_miss=="0"]<-"Not_recorded"
all_patients_2020 <- all_patients_2020 %>%
mutate (ethnic_no_miss = as.factor(ethnic_no_miss)) %>%
mutate (ethnic_no_miss = fct_relevel(ethnic_no_miss, "White", "Asian", "Black", "Mixed","Other", "Not_recorded"))
all_patients_2020$imd[all_patients_2020$imd=="0"]<-"NA"
all_patients_2020 <- all_patients_2020 %>%
mutate (imd = as.factor(imd)) %>%
mutate (imd = fct_relevel(imd, "1", "2", "3", "4", "5", "NA"))
all_patients_2020 <- all_patients_2020 %>%
mutate (eth_group_16=case_when(
ethnicity_16_no_miss == "1" ~ "British",
ethnicity_16_no_miss == "2" ~ "Irish",
ethnicity_16_no_miss == "3" ~ "Other_White",
ethnicity_16_no_miss == "4" ~ "White_Black_Carib",
ethnicity_16_no_miss == "5" ~ "White_Black_African",
ethnicity_16_no_miss == "6" ~ "White_Asian",
ethnicity_16_no_miss == "7" ~ "Other_Mixed",
ethnicity_16_no_miss == "8" ~ "Indian",
ethnicity_16_no_miss == "9" ~ "Pakistani",
ethnicity_16_no_miss == "10" ~ "Bangladeshi",
ethnicity_16_no_miss == "11" ~ "Other_Asian",
ethnicity_16_no_miss == "12" ~ "Caribbean",
ethnicity_16_no_miss == "13" ~ "African",
ethnicity_16_no_miss == "14" ~ "Other_Black",
ethnicity_16_no_miss == "15" ~ "Chinese",
ethnicity_16_no_miss == "16" ~ "Other",
ethnicity_16_no_miss == "0" ~ "Missing"))
all_patients_2020 <- all_patients_2020 %>%
mutate (eth_group_16 = as.factor(eth_group_16)) %>%
mutate ( eth_group_16= fct_relevel(eth_group_16,
"British",
"Irish",
"Other_White",
"Indian",
"Pakistani",
"Bangladeshi",
"Other_Asian",
"Caribbean",
"African",
"Other_Black",
"Chinese",
"White_Asian",
"White_Black_Carib",
"White_Black_African",
"Other_Mixed",
"Other",
"Missing"))
# 3) Select relevant variables and restructure code to allow functions across rows
all_patients_2020 <- all_patients_2020 %>%
dplyr::select("patient_id",
"sex",
"age_group",
"region",
"imd",
"ethnic_no_miss",
"eth_group_16",
"learning_disability",
"dementia",
"depression",
"psychosis_schiz_bipolar",
"diabetes_type",
"diabetes_t1",
"diabetes_t2",
"bmi",
"had_bmi",
"asthma",
"COPD",
"stroke_and_TIA" ,
"chronic_cardiac",
"hypertension",
"all_cancer")
all_patients_2020 <- all_patients_2020 %>%
dplyr::mutate(
across(
.cols = c(learning_disability,depression, dementia,psychosis_schiz_bipolar, diabetes_type, diabetes_t1, diabetes_t2, asthma, COPD, stroke_and_TIA, chronic_cardiac, hypertension, all_cancer),
.names = "comorbid_{col}")) %>%
dplyr::select(
patient_id, had_bmi, sex, age_group, region, imd, ethnic_no_miss, eth_group_16, starts_with("comorbid_"))
#######################################################################
# 4) BMI_complete_categories has all the data on BMI. Need to link it with demographic data from the extracted cohort.
BMI_complete_categories <- as_tibble(BMI_complete_categories)
BMI_complete_categories_all <- BMI_complete_categories %>%
dplyr::select(patient_id,
year,
median_bmi,
obese,
BMI_categories,
BMI_over27.5,
DWMP)
BMI_complete_categories_all <- left_join(all_patients_2020, BMI_complete_categories_all, by='patient_id')
################################################################################
# 5) add the pre-covid obese flag
precovid_obese <- precovid_obese %>%
dplyr::filter(year==2020) %>%
dplyr::select(patient_id,
precovid_obese_flag)
BMI_complete_categories_all <- left_join(BMI_complete_categories_all, precovid_obese, by='patient_id')
### save outputs as feather
write_feather (BMI_complete_categories_all, here::here ("output/data","BMI_all_2020.feather"))