-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path06_descriptives.R
More file actions
306 lines (262 loc) · 10.3 KB
/
Copy path06_descriptives.R
File metadata and controls
306 lines (262 loc) · 10.3 KB
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
# Set working directory
setwd("~/GitHub/NHANES_depression/Data")
# load libraries
# Load needed libraries
library(tidyverse)
library(survey)
library(tableone)
# Create Rubin's Rules functions
# Rubin's Rules - pooling means
rubin_mean <- function(average) {
Reduce("+", average) / length(average)
}
# Rubin's Rules - pooling SEs
rubin_se <- function(average, standard_error) {
# Within imputation variance:
within_var <- Reduce("+", lapply(standard_error, function(i){i*i})) / length(standard_error)
# Between imputation variance:
between_var <- Reduce("+", lapply(average, function(i){(i-rubin_mean(average))*(i-rubin_mean(average))})) / (length(average)-1)
between_var2 <- between_var/ length(average)
# Total variance:
total_var <- within_var+between_var+between_var2
# Pooled SE:
sqrt(total_var)
}
# result collection dataframe
means_list <- list()
ses_list <- list()
cat_list <- list()
# DESCRIPTIVES OVERALL
for (i in 1:15) {
filename <- paste0("cluster_merged_",i,".rds")
df <- readRDS(filename)
# survey design
nhanes.y <- svydesign(data=df, id=~SDMVPSU, strata=~SDMVSTRA, weights=~survey_weight, nest=TRUE)
# subsetting data
sub.y <- subset(nhanes.y, (df$depressed == 1))
# table creation
k <- tableone::svyCreateTableOne(vars = c('age', "BMI", "PA_sedentary",
"PA_moderate", "PA_vigorous",
"DPQ_total",
'gender', "marital",
"education", "ethnicity",
"ann_household_income",
"family_pov_level",
"asthma", "arthritis",
"heart_failure", "CHD",
"angina", "heart_attack",
"stroke", "bronchitis",
"liver_condition", "thyroid_condition",
"cancer",
"diabetes", "hypertension",
"hyperlipidemia" ,
"smoking", "alcohol",
"dep_category"),
data = sub.y,
includeNA = T,
test = F, addOverall = F)
res_means <- as.matrix(k$ContTable[[1]][,4])
res_ses <- as.matrix(k$ContTable[[1]][,5])/sqrt(nrow(sub.y))
means_list[[i]] <- res_means
ses_list[[i]] <- res_ses
cat_list[[i]] <- c(
k$CatTable[[1]][[1]][,6],
k$CatTable[[1]][[2]][,6],
k$CatTable[[1]][[3]][,6],
k$CatTable[[1]][[4]][,6],
k$CatTable[[1]][[5]][,6],
k$CatTable[[1]][[6]][,6],
k$CatTable[[1]][[7]][,6],
k$CatTable[[1]][[8]][,6],
k$CatTable[[1]][[9]][,6],
k$CatTable[[1]][[10]][,6],
k$CatTable[[1]][[11]][,6],
k$CatTable[[1]][[12]][,6],
k$CatTable[[1]][[13]][,6],
k$CatTable[[1]][[14]][,6],
k$CatTable[[1]][[15]][,6],
k$CatTable[[1]][[16]][,6],
k$CatTable[[1]][[17]][,6],
k$CatTable[[1]][[18]][,6],
k$CatTable[[1]][[19]][,6],
k$CatTable[[1]][[20]][,6],
k$CatTable[[1]][[21]][,6],
k$CatTable[[1]][[22]][,6],
k$CatTable[[1]][[23]][,6])
}
means_survey <- rubin_mean(average = means_list)
SDs_survey <- rubin_se(average = means_list, standard_error = ses_list)*sqrt(nrow(sub.y))
cat_survey <- rubin_mean(average = cat_list)
c(round(means_survey,1))
c(round(SDs_survey,1))
c(round(cat_survey,1)[2], # sex: female
round(cat_survey,1)[3:8], # marital
round(cat_survey,1)[9:13], # education
round(cat_survey,1)[14:17], # ethnicity
round(cat_survey,1)[18:20], # income
round(cat_survey,1)[21:23], # family POV
round(cat_survey,1)[24], # asthma: yes
round(cat_survey,1)[26], # arthritis : yes
round(cat_survey,1)[28], # heart failure : yes
round(cat_survey,1)[30], # CHD : yes
round(cat_survey,1)[32], # angina : yes
round(cat_survey,1)[34], # heart attack : yes
round(cat_survey,1)[36], # stroke : yes
round(cat_survey,1)[38], # bronchitis : yes
round(cat_survey,1)[40], # liver_condition : yes
round(cat_survey,1)[42], # thyroid_condition : yes
round(cat_survey,1)[44], # cancer : yes
round(cat_survey,1)[46], # diabetes : yes
round(cat_survey,1)[49], # hypertension : yes
round(cat_survey,1)[51], # hypercholesterolemia : yes
round(cat_survey,1)[53:56], # smoking : never, ex, sometimes, current
round(cat_survey,1)[57:59], # alcohol : never, light, heavy
round(cat_survey,1)[60:62]) # depression category: mod, mod-sev, sev
# DESCRIPTIVES PER CLUSTER
# result collection dataframe
means_list <- list()
ses_list <- list()
cat_list <- list()
# loading data
for (i in 1:15) {
filename <- paste0("cluster_merged_",i,".rds")
df <- readRDS(filename)
# survey design
nhanes.y <- svydesign(data=df, id=~SDMVPSU, strata=~SDMVSTRA, weights=~survey_weight, nest=TRUE)
# subsetting data
sub.y <- subset(nhanes.y, (df$cluster_membership == "6"))
# table creation
k <- tableone::svyCreateTableOne(vars = c('age', "BMI", "PA_sedentary",
"PA_moderate", "PA_vigorous",
"DPQ_total",
'gender', "marital",
"education", "ethnicity",
"ann_household_income",
"family_pov_level",
"asthma", "arthritis",
"heart_failure", "CHD",
"angina", "heart_attack",
"stroke", "bronchitis",
"liver_condition", "thyroid_condition",
"cancer",
"diabetes", "hypertension",
"hyperlipidemia" ,
"smoking", "alcohol",
"dep_category"),
data = sub.y,
includeNA = T,
test = F, addOverall = F)
res_means <- as.matrix(k$ContTable[[1]][,4])
res_ses <- as.matrix(k$ContTable[[1]][,5])/sqrt(nrow(sub.y))
means_list[[i]] <- res_means
ses_list[[i]] <- res_ses
cat_list[[i]] <- c(
k$CatTable[[1]][[1]][,6],
k$CatTable[[1]][[2]][,6],
k$CatTable[[1]][[3]][,6],
k$CatTable[[1]][[4]][,6],
k$CatTable[[1]][[5]][,6],
k$CatTable[[1]][[6]][,6],
k$CatTable[[1]][[7]][,6],
k$CatTable[[1]][[8]][,6],
k$CatTable[[1]][[9]][,6],
k$CatTable[[1]][[10]][,6],
k$CatTable[[1]][[11]][,6],
k$CatTable[[1]][[12]][,6],
k$CatTable[[1]][[13]][,6],
k$CatTable[[1]][[14]][,6],
k$CatTable[[1]][[15]][,6],
k$CatTable[[1]][[16]][,6],
k$CatTable[[1]][[17]][,6],
k$CatTable[[1]][[18]][,6],
k$CatTable[[1]][[19]][,6],
k$CatTable[[1]][[20]][,6],
k$CatTable[[1]][[21]][,6],
k$CatTable[[1]][[22]][,6],
k$CatTable[[1]][[23]][,6])
}
means_survey <- rubin_mean(average = means_list)
SDs_survey <- rubin_se(average = means_list, standard_error = ses_list)*sqrt(nrow(sub.y))
cat_survey <- rubin_mean(average = cat_list)
nrow(sub.y)
c(round(means_survey,1))
c(round(SDs_survey,1))
c(round(cat_survey,1)[2], # sex: female
round(cat_survey,1)[3:8], # marital
round(cat_survey,1)[9:13], # education
round(cat_survey,1)[14:17], # ethnicity
round(cat_survey,1)[18:20], # income
round(cat_survey,1)[21:23], # family POV
round(cat_survey,1)[24], # asthma: yes
round(cat_survey,1)[26], # arthritis : yes
round(cat_survey,1)[28], # heart failure : yes
round(cat_survey,1)[30], # CHD : yes
round(cat_survey,1)[32], # angina : yes
round(cat_survey,1)[34], # heart attack : yes
round(cat_survey,1)[36], # stroke : yes
round(cat_survey,1)[38], # bronchitis : yes
round(cat_survey,1)[40], # liver_condition : yes
round(cat_survey,1)[42], # thyroid_condition : yes
round(cat_survey,1)[44], # cancer : yes
round(cat_survey,1)[46], # diabetes : yes
round(cat_survey,1)[49], # hypertension : yes
round(cat_survey,1)[51], # hypercholesterolemia : yes
round(cat_survey,1)[53:56], # smoking : never, ex, sometimes, current
round(cat_survey,1)[57:59], # alcohol : never, light, heavy
round(cat_survey,1)[60:62]) # depression category: mod, mod-sev, sev
# COMPARING NUMERIC PER CLUSTER
varlist <- c("age", "BMI", "PA_sedentary", "PA_moderate", "PA_vigorous", "DPQ_total")
for (variable_in_loop in varlist) {
# P value collection
p_list <- list()
for (i in 1:15) {
filename <- paste0("cluster_merged_",i,".rds")
df <- readRDS(filename)
# survey design
nhanes.y <- svydesign(data=df, id=~SDMVPSU, strata=~SDMVSTRA, weights=~survey_weight, nest=TRUE)
# subsetting data
sub.y <- subset(nhanes.y, (df$depressed == 1))
# ANOVA
pasted_formula <- paste0(variable_in_loop, " ~ cluster_membership")
form_loop <- as.formula(pasted_formula)
survey_glm <- svyglm(form_loop, sub.y)
anova_res <- aov(survey_glm)
p_list[[i]] <- summary(anova_res)[[1]]$'Pr(>F)'[[1]]
}
print(variable_in_loop)
print(median(unlist(p_list)))
}
# COMPARING CATEGORICAL PER CLUSTER
varlist <- c("gender", "marital",
"education", "ethnicity",
"ann_household_income",
"family_pov_level",
"asthma", "arthritis",
"heart_failure", "CHD",
"angina", "heart_attack",
"stroke", "bronchitis",
"liver_condition", "thyroid_condition",
"cancer",
"diabetes", "hypertension",
"hyperlipidemia" ,
"smoking", "alcohol",
"dep_category")
for (variable_in_loop in varlist) {
# P value collection
p_list <- list()
for (i in 1:15) {
filename <- paste0("cluster_merged_",i,".rds")
df <- readRDS(filename)
# survey design
nhanes.y <- svydesign(data=df, id=~SDMVPSU, strata=~SDMVSTRA, weights=~survey_weight, nest=TRUE)
# subsetting data
sub.y <- subset(nhanes.y, (df$depressed == 1))
# chi squared
pasted_formula <- paste0("~",variable_in_loop, " + cluster_membership")
form_loop <- as.formula(pasted_formula)
tbl <- svychisq(form_loop, sub.y)
p_list[[i]] <- as.numeric(tbl$p.value)
}
print(variable_in_loop)
print(median(unlist(p_list)))
}