generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
top_decile_weightgain_age_adj.R
219 lines (137 loc) · 5.87 KB
/
top_decile_weightgain_age_adj.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
## Load libraries
## Specify libraries
library(pacman)
library(tidyverse)
library(Hmisc)
library(here)
library(arrow)
library(purrr)
library(broom)
library(data.table)
library(forcats)
library(rstatix)
library(janitor)
library(lubridate)
library(skimr)
library(ggplot2)
library(gtsummary)
BMI_trajectories <- read_feather (here::here ("output/data", "BMI_trajectory_data_long.feather"))
BMI_trajectories <- BMI_trajectories %>%
dplyr::select("sex",
"age_group_2",
"region",
"imd",
"hypertension",
"diabetes_t1",
"diabetes_t2",
"learning_disability",
"depression",
"psychosis_schiz_bipolar",
"dementia",
"asthma",
"COPD",
"stroke_and_TIA",
"chronic_cardiac",
"all_cancer",
"smoking_status",
"ethnic_no_miss",
"eth_group_16",
"complete_bmi_data",
"bmi_change_cat",
"precovid_bmi_category",
"pandemic_stage",
"yearly_bmi_change")
BMI_trajectories$precovid_bmi_category <- factor(BMI_trajectories$precovid_bmi_category, levels = c("healthy","overweight", "obese", "underweight"))
explanatory_vars <- c("sex",
"age_group_2",
"region",
"imd",
"hypertension",
"diabetes_t1",
"diabetes_t2",
"learning_disability",
"depression",
"psychosis_schiz_bipolar",
"dementia",
"asthma",
"COPD",
"stroke_and_TIA",
"chronic_cardiac",
"all_cancer",
"smoking_status",
"ethnic_no_miss",
"eth_group_16",
"precovid_bmi_category")
## Precovid analysis proportions in groups
### Precovid analysis
precovid_change <- BMI_trajectories %>%
dplyr::filter(pandemic_stage == "precovid")
precovid_quantiles <- as.data.frame(quantile(precovid_change$yearly_bmi_change, probs = seq(.1, .9, by = .1))) %>%
dplyr::rename( precovid_yearly_bmi_change = 1)
## create a column for deciles
precovid_change$decile <- ntile(precovid_change$yearly_bmi_change, 10)
## create a flag for top 10% weight gain
precovid_change <- precovid_change %>%
dplyr::mutate(weightgain_90th = case_when(
decile == 10 ~ 1,
decile != 10 ~ 0
))
models_precov_rapidinc_bmi_univar <- explanatory_vars %>% # begin with variables of interest
str_c("weightgain_90th ~ age_group_2 +", .) %>% # combine each variable into formula ("outcome ~ variable of interest")
# iterate through each univariate formula
map(
.f = ~glm( # pass the formulas one-by-one to glm()
formula = as.formula(.x), # within glm(), the string formula is .x
family = "binomial", # specify type of glm (logistic)
data = precovid_change)) %>% # dataset
# tidy up each of the glm regression outputs from above
map(
.f = ~tidy(
.x,
exponentiate = TRUE, # exponentiate
conf.int = TRUE)) %>% # return confidence intervals
# collapse the list of regression outputs in to one data frame
bind_rows() %>%
# round all numeric columns
mutate(across(where(is.numeric), round, digits = 4))
## population composition
models_precov_rapidinc_bmi_univar <- models_precov_rapidinc_bmi_univar %>%
dplyr::mutate(stage = "precovid", .before = 1)
### Postcovid Analysis
postcovid_change <- BMI_trajectories %>%
dplyr::filter(pandemic_stage == "postcovid")
##postcovid_quantiles
postcovid_quantiles <- as.data.frame(quantile(postcovid_change$yearly_bmi_change, probs = seq(.1, .9, by = .1))) %>%
dplyr::rename( postcovid_yearly_bmi_change = 1)
## create a column for deciles
postcovid_change$decile <- ntile(postcovid_change$yearly_bmi_change, 10)
## create a flag for top 10% weight gain
postcovid_change <- postcovid_change %>%
dplyr::mutate(weightgain_90th = case_when(
decile == 10 ~ 1,
decile != 10 ~ 0
))
models_postcov_rapidinc_bmi_univar <- explanatory_vars %>% # begin with variables of interest
str_c("weightgain_90th ~ age_group_2 + ", .) %>% # combine each variable into formula ("outcome ~ variable of interest")
# iterate through each univariate formula
map(
.f = ~glm( # pass the formulas one-by-one to glm()
formula = as.formula(.x), # within glm(), the string formula is .x
family = "binomial", # specify type of glm (logistic)
data = postcovid_change)) %>% # dataset
# tidy up each of the glm regression outputs from above
map(
.f = ~tidy(
.x,
exponentiate = TRUE, # exponentiate
conf.int = TRUE)) %>% # return confidence intervals
# collapse the list of regression outputs in to one data frame
bind_rows() %>%
# round all numeric columns
mutate(across(where(is.numeric), round, digits = 4))
models_postcov_rapidinc_bmi_univar <- models_postcov_rapidinc_bmi_univar %>%
dplyr::mutate(stage="postcovid", .before=1)
### Write outputs
models_age <- models_precov_rapidinc_bmi_univar %>%
bind_rows(models_postcov_rapidinc_bmi_univar)
write_csv (models_age, here::here ("output/data","weightgain_90th_age_adj.csv"))