generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
obese_proportions_2019.R
744 lines (565 loc) · 31.4 KB
/
obese_proportions_2019.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#### Author: M Samuel
#### Date: 24th March 2022
#### This script calculates proportions of people who had a BMI >= 30 and (univariate) differences between groups using chi squared.
## 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)
## Read in files >>> Change PATH!!
# check working directory: getwd()
BMI_complete_categories <- read_feather (here::here ("output/data", "BMI_complete_median.feather"))
BMI_complete_categories <- BMI_complete_categories %>%
dplyr::ungroup() %>%
dplyr::filter (year == "2019") %>%
dplyr::mutate (imd = as.factor(imd)) %>%
dplyr::mutate (imd = fct_relevel(imd, "1", "2", "3", "4", "5")) %>%
dplyr::mutate(age_group = as.factor(age_group)) %>%
dplyr::mutate(age_group = fct_relevel(age_group, "18-39", "40-65", "65-80", "80+"))
BMI_complete_categories$age_group_2 <- factor(BMI_complete_categories$age_group_2, # Reordering group factor levels
levels = c("18-29", "30-39", "40-49", "50-59", "60-69", "70-79", "80+"))
BMI_complete_categories <- BMI_complete_categories %>%
replace_na(list(precovid_obese_flag=FALSE))
BMI_complete_categories$obese[BMI_complete_categories$obese == 1] <- "TRUE"
BMI_complete_categories$obese[BMI_complete_categories$obese == 0] <- "FALSE"
BMI_complete_categories_2019_DT <- data.table(BMI_complete_categories)
# develop a vector of explanatory variables
explanatory_vars <- c("sex",
"age_group",
"region",
"imd",
"ethnic_no_miss",
"eth_group_16",
"comorbid_learning_disability",
"comorbid_depression",
"comorbid_dementia",
"comorbid_psychosis_schiz_bipolar",
"comorbid_diabetes_type",
"comorbid_diabetes_t1",
"comorbid_diabetes_t2",
"comorbid_asthma",
"comorbid_COPD",
"comorbid_stroke_and_TIA",
"comorbid_chronic_cardiac",
"comorbid_hypertension",
"comorbid_all_cancer")
N_obese <- BMI_complete_categories_2019_DT[, .N, ]
obese_table<- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N),]
obese_table <- dplyr::mutate (obese_table, N = N_obese) %>%
dplyr::mutate(proportion=n_obese/N) %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(group = "all", .before=1) %>%
dplyr::mutate(variable = "all", .before=1)
## Develop count table and chi squared test of difference between population long hand for each variable to reduce memory
######### AGE GROUPS #######
#1. count by age_group
N_age_group <- BMI_complete_categories_2019_DT[, .N, by="age_group"]
obese_age_group <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="age_group"]
obese_age_group <- obese_age_group[order(age_group)]
obese_age_group <- dplyr::left_join(obese_age_group, N_age_group)
obese_age_group <- obese_age_group %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_age_group <- obese_age_group %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "age_group", .before=1) %>%
dplyr::rename(group = age_group)
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
age_group_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(age_group, obese) %>%
select(-1) %>%
chisq_test()
age_group_chisq <- dplyr::mutate (age_group_chisq, variable = "age_group") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_age_group <- obese_age_group %>%
dplyr::left_join(age_group_chisq, by = "variable")
###### SEX
#1. count by sex
N_sex <- BMI_complete_categories_2019_DT[, .N, by="sex"]
obese_sex <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="sex"]
obese_sex <- obese_sex[order(sex)]
obese_sex <- dplyr::left_join(obese_sex, N_sex)
obese_sex <- obese_sex %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_sex <- obese_sex %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "sex", .before=1) %>%
dplyr::rename(group = sex)
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
sex_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(sex, obese) %>%
select(-1) %>%
chisq_test()
sex_chisq <- dplyr::mutate (sex_chisq, variable = "sex") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_sex <- obese_sex %>%
dplyr::left_join(sex_chisq, by = "variable")
######### region #######
#1. count by region
N_region <- BMI_complete_categories_2019_DT[, .N, by="region"]
obese_region <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="region"]
obese_region <- obese_region[order(region)]
obese_region <- dplyr::left_join(obese_region, N_region)
obese_region <- obese_region %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_region <- obese_region %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "region", .before=1) %>%
dplyr::rename(group = region)
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
region_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(region, obese) %>%
select(-1) %>%
chisq_test()
region_chisq <- dplyr::mutate (region_chisq, variable = "region") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_region <- obese_region %>%
dplyr::left_join(region_chisq, by = "variable")
######### imd #######
#1. count by imd
N_imd <- BMI_complete_categories_2019_DT[, .N, by="imd"]
obese_imd <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="imd"]
obese_imd <- obese_imd[order(imd)]
obese_imd <- dplyr::left_join(obese_imd, N_imd)
obese_imd <- obese_imd %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_imd <- obese_imd %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "imd", .before=1) %>%
dplyr::rename(group = imd)
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
imd_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(imd, obese) %>%
select(-1) %>%
chisq_test()
imd_chisq <- dplyr::mutate (imd_chisq, variable = "imd") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_imd <- obese_imd %>%
dplyr::left_join(imd_chisq, by = "variable")
######### ethnic_no_miss #######
#1. count by ethnic_no_miss
N_ethnic_no_miss <- BMI_complete_categories_2019_DT[, .N, by="ethnic_no_miss"]
obese_ethnic_no_miss <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="ethnic_no_miss"]
obese_ethnic_no_miss <- obese_ethnic_no_miss[order(ethnic_no_miss)]
obese_ethnic_no_miss <- dplyr::left_join(obese_ethnic_no_miss, N_ethnic_no_miss)
obese_ethnic_no_miss <- obese_ethnic_no_miss %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_ethnic_no_miss <- obese_ethnic_no_miss %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "ethnic_no_miss", .before=1) %>%
dplyr::rename(group = ethnic_no_miss)
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
ethnic_no_miss_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(ethnic_no_miss, obese) %>%
select(-1) %>%
chisq_test()
ethnic_no_miss_chisq <- dplyr::mutate (ethnic_no_miss_chisq, variable = "ethnic_no_miss") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_ethnic_no_miss <- obese_ethnic_no_miss %>%
dplyr::left_join(ethnic_no_miss_chisq, by = "variable")
######### eth_group_16 #######
#1. count by eth_group_16
N_eth_group_16 <- BMI_complete_categories_2019_DT[, .N, by="eth_group_16"]
obese_eth_group_16 <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="eth_group_16"]
obese_eth_group_16 <- obese_eth_group_16[order(eth_group_16)]
obese_eth_group_16 <- dplyr::left_join(obese_eth_group_16, N_eth_group_16)
obese_eth_group_16 <- obese_eth_group_16 %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_eth_group_16 <- obese_eth_group_16 %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "eth_group_16", .before=1) %>%
dplyr::rename(group = eth_group_16)
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
eth_group_16_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(eth_group_16, obese) %>%
select(-1) %>%
chisq_test()
eth_group_16_chisq <- dplyr::mutate (eth_group_16_chisq, variable = "eth_group_16") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_eth_group_16 <- obese_eth_group_16 %>%
dplyr::left_join(eth_group_16_chisq, by = "variable")
######## comorbid_learning_disability #######
#1. count by comorbid_learning_disability
N_comorbid_learning_disability <- BMI_complete_categories_2019_DT[, .N, by="comorbid_learning_disability"]
obese_comorbid_learning_disability <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="comorbid_learning_disability"]
obese_comorbid_learning_disability <- obese_comorbid_learning_disability[order(comorbid_learning_disability)]
obese_comorbid_learning_disability <- dplyr::left_join(obese_comorbid_learning_disability, N_comorbid_learning_disability)
obese_comorbid_learning_disability <- obese_comorbid_learning_disability %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_comorbid_learning_disability <- obese_comorbid_learning_disability %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "comorbid_learning_disability", .before=1) %>%
dplyr::rename(group = comorbid_learning_disability) %>%
dplyr::mutate(group = as.character(group))
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
comorbid_learning_disability_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(comorbid_learning_disability, obese) %>%
select(-1) %>%
chisq_test()
comorbid_learning_disability_chisq <- dplyr::mutate (comorbid_learning_disability_chisq, variable = "comorbid_learning_disability") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_comorbid_learning_disability <- obese_comorbid_learning_disability %>%
dplyr::left_join(comorbid_learning_disability_chisq, by = "variable")
######### comorbid_depression #######
#1. count by comorbid_depression
N_comorbid_depression <- BMI_complete_categories_2019_DT[, .N, by="comorbid_depression"]
obese_comorbid_depression <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="comorbid_depression"]
obese_comorbid_depression <- obese_comorbid_depression[order(comorbid_depression)]
obese_comorbid_depression <- dplyr::left_join(obese_comorbid_depression, N_comorbid_depression)
obese_comorbid_depression <- obese_comorbid_depression %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_comorbid_depression <- obese_comorbid_depression %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "comorbid_depression", .before=1) %>%
dplyr::rename(group = comorbid_depression) %>%
dplyr::mutate(group = as.character(group))
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
comorbid_depression_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(comorbid_depression, obese) %>%
select(-1) %>%
chisq_test()
comorbid_depression_chisq <- dplyr::mutate (comorbid_depression_chisq, variable = "comorbid_depression") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_comorbid_depression <- obese_comorbid_depression %>%
dplyr::left_join(comorbid_depression_chisq, by = "variable")
######### comorbid_dementia #######
#1. count by comorbid_dementia
N_comorbid_dementia <- BMI_complete_categories_2019_DT[, .N, by="comorbid_dementia"]
obese_comorbid_dementia <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="comorbid_dementia"]
obese_comorbid_dementia <- obese_comorbid_dementia[order(comorbid_dementia)]
obese_comorbid_dementia <- dplyr::left_join(obese_comorbid_dementia, N_comorbid_dementia)
obese_comorbid_dementia <- obese_comorbid_dementia %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_comorbid_dementia <- obese_comorbid_dementia %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "comorbid_dementia", .before=1) %>%
dplyr::rename(group = comorbid_dementia) %>%
dplyr::mutate(group = as.character(group))
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
comorbid_dementia_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(comorbid_dementia, obese) %>%
select(-1) %>%
chisq_test()
comorbid_dementia_chisq <- dplyr::mutate (comorbid_dementia_chisq, variable = "comorbid_dementia") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_comorbid_dementia <- obese_comorbid_dementia %>%
dplyr::left_join(comorbid_dementia_chisq, by = "variable")
######### comorbid_psychosis_schiz_bipolar #######
#1. count by comorbid_psychosis_schiz_bipolar
N_comorbid_psychosis_schiz_bipolar <- BMI_complete_categories_2019_DT[, .N, by="comorbid_psychosis_schiz_bipolar"]
obese_comorbid_psychosis_schiz_bipolar <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="comorbid_psychosis_schiz_bipolar"]
obese_comorbid_psychosis_schiz_bipolar <- obese_comorbid_psychosis_schiz_bipolar[order(comorbid_psychosis_schiz_bipolar)]
obese_comorbid_psychosis_schiz_bipolar <- dplyr::left_join(obese_comorbid_psychosis_schiz_bipolar, N_comorbid_psychosis_schiz_bipolar)
obese_comorbid_psychosis_schiz_bipolar <- obese_comorbid_psychosis_schiz_bipolar %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_comorbid_psychosis_schiz_bipolar <- obese_comorbid_psychosis_schiz_bipolar %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "comorbid_psychosis_schiz_bipolar", .before=1) %>%
dplyr::rename(group = comorbid_psychosis_schiz_bipolar) %>%
dplyr::mutate(group = as.character(group))
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
comorbid_psychosis_schiz_bipolar_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(comorbid_psychosis_schiz_bipolar, obese) %>%
select(-1) %>%
chisq_test()
comorbid_psychosis_schiz_bipolar_chisq <- dplyr::mutate (comorbid_psychosis_schiz_bipolar_chisq, variable = "comorbid_psychosis_schiz_bipolar") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_comorbid_psychosis_schiz_bipolar <- obese_comorbid_psychosis_schiz_bipolar %>%
dplyr::left_join(comorbid_psychosis_schiz_bipolar_chisq, by = "variable")
########################
######### comorbid_diabetes_t1 #######
#1. count by comorbid_diabetes_t1
N_comorbid_diabetes_t1 <- BMI_complete_categories_2019_DT[, .N, by="comorbid_diabetes_t1"]
obese_comorbid_diabetes_t1 <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="comorbid_diabetes_t1"]
obese_comorbid_diabetes_t1 <- obese_comorbid_diabetes_t1[order(comorbid_diabetes_t1)]
obese_comorbid_diabetes_t1 <- dplyr::left_join(obese_comorbid_diabetes_t1, N_comorbid_diabetes_t1)
obese_comorbid_diabetes_t1 <- obese_comorbid_diabetes_t1 %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_comorbid_diabetes_t1 <- obese_comorbid_diabetes_t1 %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "comorbid_diabetes_t1", .before=1) %>%
dplyr::rename(group = comorbid_diabetes_t1) %>%
dplyr::mutate(group = as.character(group))
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
comorbid_diabetes_t1_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(comorbid_diabetes_t1, obese) %>%
select(-1) %>%
chisq_test()
comorbid_diabetes_t1_chisq <- dplyr::mutate (comorbid_diabetes_t1_chisq, variable = "comorbid_diabetes_t1") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_comorbid_diabetes_t1 <- obese_comorbid_diabetes_t1 %>%
dplyr::left_join(comorbid_diabetes_t1_chisq, by = "variable")
######### comorbid_diabetes_t2 #######
#1. count by comorbid_diabetes_t2
N_comorbid_diabetes_t2 <- BMI_complete_categories_2019_DT[, .N, by="comorbid_diabetes_t2"]
obese_comorbid_diabetes_t2 <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="comorbid_diabetes_t2"]
obese_comorbid_diabetes_t2 <- obese_comorbid_diabetes_t2[order(comorbid_diabetes_t2)]
obese_comorbid_diabetes_t2 <- dplyr::left_join(obese_comorbid_diabetes_t2, N_comorbid_diabetes_t2)
obese_comorbid_diabetes_t2 <- obese_comorbid_diabetes_t2 %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_comorbid_diabetes_t2 <- obese_comorbid_diabetes_t2 %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "comorbid_diabetes_t2", .before=1) %>%
dplyr::rename(group = comorbid_diabetes_t2) %>%
dplyr::mutate(group = as.character(group))
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
comorbid_diabetes_t2_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(comorbid_diabetes_t2, obese) %>%
select(-1) %>%
chisq_test()
comorbid_diabetes_t2_chisq <- dplyr::mutate (comorbid_diabetes_t2_chisq, variable = "comorbid_diabetes_t2") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_comorbid_diabetes_t2 <- obese_comorbid_diabetes_t2 %>%
dplyr::left_join(comorbid_diabetes_t2_chisq, by = "variable")
######### comorbid_asthma #######
#1. count by comorbid_asthma
N_comorbid_asthma <- BMI_complete_categories_2019_DT[, .N, by="comorbid_asthma"]
obese_comorbid_asthma <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="comorbid_asthma"]
obese_comorbid_asthma <- obese_comorbid_asthma[order(comorbid_asthma)]
obese_comorbid_asthma <- dplyr::left_join(obese_comorbid_asthma, N_comorbid_asthma)
obese_comorbid_asthma <- obese_comorbid_asthma %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_comorbid_asthma <- obese_comorbid_asthma %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "comorbid_asthma", .before=1) %>%
dplyr::rename(group = comorbid_asthma) %>%
dplyr::mutate(group = as.character(group))
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
comorbid_asthma_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(comorbid_asthma, obese) %>%
select(-1) %>%
chisq_test()
comorbid_asthma_chisq <- dplyr::mutate (comorbid_asthma_chisq, variable = "comorbid_asthma") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_comorbid_asthma <- obese_comorbid_asthma %>%
dplyr::left_join(comorbid_asthma_chisq, by = "variable")
######### comorbid_COPD #######
#1. count by comorbid_COPD
N_comorbid_COPD <- BMI_complete_categories_2019_DT[, .N, by="comorbid_COPD"]
obese_comorbid_COPD <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="comorbid_COPD"]
obese_comorbid_COPD <- obese_comorbid_COPD[order(comorbid_COPD)]
obese_comorbid_COPD <- dplyr::left_join(obese_comorbid_COPD, N_comorbid_COPD)
obese_comorbid_COPD <- obese_comorbid_COPD %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_comorbid_COPD <- obese_comorbid_COPD %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "comorbid_COPD", .before=1) %>%
dplyr::rename(group = comorbid_COPD) %>%
dplyr::mutate(group = as.character(group))
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
comorbid_COPD_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(comorbid_COPD, obese) %>%
select(-1) %>%
chisq_test()
comorbid_COPD_chisq <- dplyr::mutate (comorbid_COPD_chisq, variable = "comorbid_COPD") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_comorbid_COPD <- obese_comorbid_COPD %>%
dplyr::left_join(comorbid_COPD_chisq, by = "variable")
######### comorbid_stroke_and_TIA #######
#1. count by comorbid_stroke_and_TIA
N_comorbid_stroke_and_TIA <- BMI_complete_categories_2019_DT[, .N, by="comorbid_stroke_and_TIA"]
obese_comorbid_stroke_and_TIA <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="comorbid_stroke_and_TIA"]
obese_comorbid_stroke_and_TIA <- obese_comorbid_stroke_and_TIA[order(comorbid_stroke_and_TIA)]
obese_comorbid_stroke_and_TIA <- dplyr::left_join(obese_comorbid_stroke_and_TIA, N_comorbid_stroke_and_TIA)
obese_comorbid_stroke_and_TIA <- obese_comorbid_stroke_and_TIA %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_comorbid_stroke_and_TIA <- obese_comorbid_stroke_and_TIA %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "comorbid_stroke_and_TIA", .before=1) %>%
dplyr::rename(group = comorbid_stroke_and_TIA) %>%
dplyr::mutate(group = as.character(group))
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
comorbid_stroke_and_TIA_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(comorbid_stroke_and_TIA, obese) %>%
select(-1) %>%
chisq_test()
comorbid_stroke_and_TIA_chisq <- dplyr::mutate (comorbid_stroke_and_TIA_chisq, variable = "comorbid_stroke_and_TIA") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_comorbid_stroke_and_TIA <- obese_comorbid_stroke_and_TIA %>%
dplyr::left_join(comorbid_stroke_and_TIA_chisq, by = "variable")
######### comorbid_chronic_cardiac #######
#1. count by comorbid_chronic_cardiac
N_comorbid_chronic_cardiac <- BMI_complete_categories_2019_DT[, .N, by="comorbid_chronic_cardiac"]
obese_comorbid_chronic_cardiac <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="comorbid_chronic_cardiac"]
obese_comorbid_chronic_cardiac <- obese_comorbid_chronic_cardiac[order(comorbid_chronic_cardiac)]
obese_comorbid_chronic_cardiac <- dplyr::left_join(obese_comorbid_chronic_cardiac, N_comorbid_chronic_cardiac)
obese_comorbid_chronic_cardiac <- obese_comorbid_chronic_cardiac %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_comorbid_chronic_cardiac <- obese_comorbid_chronic_cardiac %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "comorbid_chronic_cardiac", .before=1) %>%
dplyr::rename(group = comorbid_chronic_cardiac) %>%
dplyr::mutate(group = as.character(group))
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
comorbid_chronic_cardiac_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(comorbid_chronic_cardiac, obese) %>%
select(-1) %>%
chisq_test()
comorbid_chronic_cardiac_chisq <- dplyr::mutate (comorbid_chronic_cardiac_chisq, variable = "comorbid_chronic_cardiac") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_comorbid_chronic_cardiac <- obese_comorbid_chronic_cardiac %>%
dplyr::left_join(comorbid_chronic_cardiac_chisq, by = "variable")
######### comorbid_hypertension #######
#1. count by comorbid_hypertension
N_comorbid_hypertension <- BMI_complete_categories_2019_DT[, .N, by="comorbid_hypertension"]
obese_comorbid_hypertension <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="comorbid_hypertension"]
obese_comorbid_hypertension <- obese_comorbid_hypertension[order(comorbid_hypertension)]
obese_comorbid_hypertension <- dplyr::left_join(obese_comorbid_hypertension, N_comorbid_hypertension)
obese_comorbid_hypertension <- obese_comorbid_hypertension %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_comorbid_hypertension <- obese_comorbid_hypertension %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "comorbid_hypertension", .before=1) %>%
dplyr::rename(group = comorbid_hypertension) %>%
dplyr::mutate(group = as.character(group)) %>%
dplyr::mutate(group = as.character(group))
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
comorbid_hypertension_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(comorbid_hypertension, obese) %>%
select(-1) %>%
chisq_test()
comorbid_hypertension_chisq <- dplyr::mutate (comorbid_hypertension_chisq, variable = "comorbid_hypertension") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_comorbid_hypertension <- obese_comorbid_hypertension %>%
dplyr::left_join(comorbid_hypertension_chisq, by = "variable")
######### comorbid_all_cancer #######
#1. count by comorbid_all_cancer
N_comorbid_all_cancer <- BMI_complete_categories_2019_DT[, .N, by="comorbid_all_cancer"]
obese_comorbid_all_cancer <- BMI_complete_categories_2019_DT[obese=="TRUE", .(n_obese= .N), by="comorbid_all_cancer"]
obese_comorbid_all_cancer <- obese_comorbid_all_cancer[order(comorbid_all_cancer)]
obese_comorbid_all_cancer <- dplyr::left_join(obese_comorbid_all_cancer, N_comorbid_all_cancer)
obese_comorbid_all_cancer <- obese_comorbid_all_cancer %>%
dplyr::mutate(proportion=n_obese/N)
# 2. calculate confidence interval of propotions
obese_comorbid_all_cancer <- obese_comorbid_all_cancer %>%
dplyr::mutate(lower_limit = (proportion - ((proportion*(1-proportion))/N*1.96))) %>% # confidence interval of proporion
dplyr::mutate(upper_limit = (proportion + ((proportion*(1-proportion))/N*1.96))) %>%
dplyr::mutate(across(where(is.numeric), round, 4)) %>%
dplyr::mutate(variable = "comorbid_all_cancer", .before=1) %>%
dplyr::rename(group = comorbid_all_cancer) %>%
dplyr::mutate(group = as.character(group))
#.... confidence interval proportion: (((proportion(1-proportion)/N))^0.5) * 1.96
# 3. chisq test
comorbid_all_cancer_chisq <- as_tibble(BMI_complete_categories) %>%
tabyl(comorbid_all_cancer, obese) %>%
select(-1) %>%
chisq_test()
comorbid_all_cancer_chisq <- dplyr::mutate (comorbid_all_cancer_chisq, variable = "comorbid_all_cancer") %>%
dplyr::select("variable", "p", "method")
# 4. Final table
obese_comorbid_all_cancer <- obese_comorbid_all_cancer %>%
dplyr::left_join(comorbid_all_cancer_chisq, by = "variable")
obese_table <- obese_table %>%
bind_rows(obese_age_group) %>%
bind_rows (obese_sex) %>%
bind_rows (obese_region) %>%
bind_rows (obese_imd) %>%
bind_rows (obese_ethnic_no_miss) %>%
bind_rows (obese_eth_group_16) %>%
bind_rows (obese_comorbid_hypertension) %>%
bind_rows (obese_comorbid_diabetes_t1) %>%
bind_rows (obese_comorbid_diabetes_t2) %>%
bind_rows (obese_comorbid_asthma) %>%
bind_rows (obese_comorbid_COPD) %>%
bind_rows (obese_comorbid_learning_disability) %>%
bind_rows (obese_comorbid_depression) %>%
bind_rows (obese_comorbid_psychosis_schiz_bipolar) %>%
bind_rows (obese_comorbid_dementia) %>%
bind_rows (obese_comorbid_stroke_and_TIA) %>%
bind_rows (obese_comorbid_chronic_cardiac) %>%
bind_rows (obese_comorbid_all_cancer)
obese_table
write.csv (obese_table, here::here ("output/data","proportion_obese_2019.csv"))