generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
8_descriptive_statistics.R
162 lines (131 loc) · 5 KB
/
8_descriptive_statistics.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
library(tidyverse)
library(data.table)
options(datatable.fread.datatable=FALSE)
# setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
# setwd('../')
source('analysis/cov_dist_cat.R')
source('analysis/cov_dist_cont.R')
incidence <- fread('output/adjusted_incidence_group.csv')
prevalence <- fread('output/adjusted_prevalence_group.csv')
# Need to read in matched person level data
# Only need to consider participants characteristics at the index date
# 100% of cases and 0% controls should have result_mk == 1
# Create bmi category variable
create_bmi_categories <- function(df){
df <- df %>%
mutate(bmi_category = case_when(
bmi == 0 ~ 'unknown or outlier',
bmi < 18.5 ~ 'underweight',
bmi >= 18.5 & bmi < 25 ~ 'ideal',
bmi >= 25 & bmi < 30 ~ 'overweight',
bmi >= 30 ~ 'obese'))
return(df)
}
# Rename English regions function
rename_regions_function<- function(df){
df <- df %>%
mutate(region = case_when(
gor9d == 'E12000001' ~ 'North East',
gor9d == 'E12000002' ~ 'North West',
gor9d =='E12000003' ~ 'Yorkshire and The Humber',
gor9d == 'E12000004' ~ 'East Midlands',
gor9d == 'E12000005' ~ 'West Midlands',
gor9d == 'E12000006' ~ 'East of England',
gor9d == 'E12000007' ~ 'London',
gor9d == 'E12000008' ~ 'South East',
gor9d == 'E12000009' ~ 'South West',
TRUE ~ "Missing"))
return(df)
}
# Create age bands
age_bands_function <- function(df){
df <- df %>% mutate(
#create categories
age_groups = case_when(
age >= 16 & age <= 24 ~ "16 to 24",
age >= 25 & age <= 34 ~ "25 to 34",
age >= 35 & age <= 49 ~ "35 to 49",
age >= 50 & age <= 69 ~ "50 to 69",
age >= 70 ~ "70 and over"))
return(df)
}
# apply the bmi categories function
incidence <- create_bmi_categories(incidence)
prevalence <- create_bmi_categories(prevalence)
# apply the region renaming function
incidence <- rename_regions_function(incidence)
prevalence <- rename_regions_function(prevalence)
# apply age band function &
# remove the old region column (gor9d)
incidence <- age_bands_function(incidence) %>% select(-gor9d)
prevalence <- age_bands_function(prevalence) %>% select(-gor9d)
cat_vars <- c("alcohol",
"obese_binary_flag",
"cancer",
"digestive_disorder",
"hiv_aids",
"metabolic_disorder",
"kidney_disorder",
"respiratory_disorder",
"mental_behavioural_disorder",
"CVD",
"musculoskeletal",
"neurological",
"bmi_category",
"sex",
"mh_history",
"mh_outcome",
"ethnicity",
"region",
"age_groups",
"hhsize",
"work_status_new",
"imd",
"rural_urban")
continuous_vars <- c('age')
if (nrow(incidence) > 0){
incidence_cat_stats <- cov.dist.cat(vars = cat_vars, dataset = incidence, exposure = 'exposed')
incidence_con_stats <- cov.dist.cont(vars = continuous_vars, dataset = incidence, exposure = 'exposed')
write_csv(incidence_cat_stats, 'output/1_descriptives_incidence_cat.csv')
write_csv(incidence_con_stats, 'output/2_descriptives_incidence_con.csv')
} else{
write_csv(data.frame(1), 'output/1_descriptives_incidence_cat.csv')
write_csv(data.frame(1), 'output/2_descriptives_incidence_con.csv')
}
if (nrow(prevalence) > 0){
prevalence_cat_stats <- cov.dist.cat(vars = cat_vars, dataset = prevalence, exposure = 'exposed')
prevalence_con_stats <- cov.dist.cont(vars = continuous_vars, dataset = prevalence, exposure = 'exposed')
write_csv(prevalence_cat_stats, 'output/3_descriptives_prevalence_cat.csv')
write_csv(prevalence_con_stats, 'output/4_descriptives_prevalence_con.csv')
} else{
write_csv(data.frame(1), 'output/3_descriptives_prevalence_cat.csv')
write_csv(data.frame(1), 'output/4_descriptives_prevalence_con.csv')
}
# bmi seperately as i had to filter out 0s
function_get_bmi_descriptives <- function(dataset){
ds3 <- dataset %>% filter(exposed == 0)
ds4 <- dataset %>% filter(exposed == 1)
all <- dataset %>% summarise(
mean_bmi = mean(bmi[bmi > 0]),
sd_bmi = sd(bmi[bmi>0]),
variance_bmi = var(bmi[bmi>0]))
all$type <- "all"
not_ex <- ds3 %>% summarise(
mean_bmi = mean(bmi[bmi > 0]),
sd_bmi = sd(bmi[bmi>0]),
variance_bmi = var(bmi[bmi>0]))
not_ex$type <- "not exposed"
exposed <- ds4 %>% summarise(
mean_bmi = mean(bmi[bmi > 0]),
sd_bmi = sd(bmi[bmi>0]),
variance_bmi = var(bmi[bmi>0]))
exposed$type <- "exposed"
bmi_desc <- rbind(all,not_ex,exposed)
return(bmi_desc)
}
incidence_bmi <- function_get_bmi_descriptives(incidence)
prevalence_bmi <- function_get_bmi_descriptives(prevalence)
#write_csv(incidence_bmi, 'output/incidence_cont_bmi_stats.csv')
#write_csv(prevalence_bmi, 'output/prevalence_cont_bmi_stats.csv')
#abs_std_diff <- abs((mu1 - mu0) / sqrt((var1 + var0) / 2))
# Poisson.test - to see rate with CIs