-
Notifications
You must be signed in to change notification settings - Fork 0
/
var_characteristics.R
300 lines (263 loc) · 13.7 KB
/
var_characteristics.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
## FUNCTION WITHOUT GROUPBY OPTION
var_characteristics <- function(varlist = varlist, varlist_cat = varlist_cat, dataset = dataset, numeric_option = c("meanCI", "meanSD", "medianIQR"), missingness = TRUE, group = NULL){
#browser()
if(is.null(group)){
#FOR CATEGORICAL VARIABLES
summaryTable_ncount <- data.frame(variable = NA, N = NA)
summaryTable_ncount$variable <- "N"
summaryTable_ncount$N <- nrow(dataset)
for(var_cat in varlist_cat){
sumtbl <- data.frame(variable = NA, category = NA, n_count = NA, perc = NA)
sumtbl$variable <- var_cat
categories <- unique(dataset[[var_cat]])
for (cat in categories){
sumtbl$category <- as.character(cat)
sumtbl$n <- sum(dataset[[var_cat]] == cat, na.rm = TRUE)
sumtbl$perc <- (sum(dataset[[var_cat]] == cat, na.rm = TRUE)/length(dataset[[var_cat]]))*100
sumtbl$total <- nrow(dataset[dataset[[var_cat]],])
summaryTable_ncount <- bind_rows(summaryTable_ncount, sumtbl)
}
}
#combine mean, lci and uci columns into 1 (mean_ci)
summaryTable_ncount$perc_1 <- paste0("(", round(summaryTable_ncount$perc, 2),"%",")")
summaryTable_ncount$n_count <- paste0(summaryTable_ncount$n, " ", summaryTable_ncount$perc_1)
summaryTable_ncount$perc <- NULL
summaryTable_ncount$n <- NULL
summaryTable_ncount$perc_1 <- NULL
summaryTable_ncount$total <- NULL
summaryTable_ncount <- as.data.frame(summaryTable_ncount)
#IF WANT MEAN & 95% CI FOR NUMERIC VARIABLES IN VARLIST
if(numeric_option == "meanCI"){
summaryTable_mCI <- data.frame()
for(var in varlist){
sumtbl <- data.frame(variable = NA, mean_ci = NA)
sumtbl$variable <- var
sumtbl$mean_ci <- list(mean_cl_normal(dataset[[var]]) %>%
rename(mean = y, lci = ymin, uci = ymax))
summaryTable_mCI <- rbind(summaryTable_mCI, sumtbl)
}
summaryTable_mCI <- as.data.frame(summaryTable_mCI) %>%
unnest(cols = mean_ci)
#convert to two decimals
summaryTable_mCI <- summaryTable_mCI %>%
mutate_if(is.numeric, round, digits = 2)
#combine mean, lci and uci columns into 1 (mean_ci)
summaryTable_mCI$uci <- paste(summaryTable_mCI$uci, ")")
summaryTable_mCI$lci <- paste("(", summaryTable_mCI$lci)
summaryTable_mCI <- summaryTable_mCI %>%
unite(lci_uci, c(lci, uci), sep = " ,", remove = TRUE) %>%
unite(mean_ci, c(mean,lci_uci), sep = " ", remove = TRUE)
summaryTable_count_num <- full_join(summaryTable_ncount, summaryTable_mCI)
}
#IF WANT MEAN & SD FOR NUMERIC VARIABLES IN VARLIST
if(numeric_option == "meanSD"){
summaryTable_mSD <- data.frame()
for(var in varlist){
sumtbl <- data.frame(variable = NA, mean = NA, sd = NA)
sumtbl$variable <- var
sumtbl$mean <- mean(dataset[[var]], na.rm = TRUE)
sumtbl$sd <- sd(dataset[[var]], na.rm = TRUE)
summaryTable_mSD <- rbind(summaryTable_mSD, sumtbl)
}
#convert to two decimals
summaryTable_mSD <- summaryTable_mSD %>%
mutate_if(is.numeric, round, digits = 2)
#combine mean, lci and uci columns into 1 (mean_ci)
summaryTable_mSD$sd <- paste0("(", round(summaryTable_mSD$sd, digits = 2), ")")
summaryTable_mSD$mean_sd <- paste0(round(summaryTable_mSD$mean, digits = 2), " ", summaryTable_mSD$sd)
summaryTable_mSD$mean <- NULL
summaryTable_mSD$sd <- NULL
summaryTable_mSD <- as.data.frame(summaryTable_mSD)
summaryTable_count_num <- full_join(summaryTable_ncount, summaryTable_mSD)
}
#IF WANT MEDIAN AND IQR FOR NUMERIC VARIABLES IN VARLIST
if(numeric_option == "medianIQR"){
summaryTable_mIQR <- data.frame()
for(var in varlist){
sumtbl <- data.frame(variable = NA, median = NA, Q1 = NA, Q3 = NA)
sumtbl$variable <- var
sumtbl$median <- median(dataset[[var]], na.rm = TRUE)
sumtbl$Q1 <- quantile(dataset[[var]], na.rm = TRUE, 0.25)
sumtbl$Q3 <- quantile(dataset[[var]], na.rm = TRUE, 0.75)
summaryTable_mIQR <- rbind(summaryTable_mIQR, sumtbl)
}
#convert to two decimals
summaryTable_mIQR <- summaryTable_mIQR %>%
mutate_if(is.numeric, round, digits = 2)
#combine mean, lci and uci columns into 1 (mean_ci)
summaryTable_mIQR$IQR <- paste0("[", round(summaryTable_mIQR$Q1, digits = 2),",", round(summaryTable_mIQR$Q3, 2), "]")
summaryTable_mIQR$median_IQR <- paste0(round(summaryTable_mIQR$median, digits = 2), " ", summaryTable_mIQR$IQR)
summaryTable_mIQR$median <- NULL
summaryTable_mIQR$Q1 <- NULL
summaryTable_mIQR$Q3 <- NULL
summaryTable_mIQR$IQR <- NULL
summaryTable_mIQR <- as.data.frame(summaryTable_mIQR)
summaryTable_count_num <- full_join(summaryTable_ncount, summaryTable_mIQR)
}
#IF WANT TO INCLUDE MISSINGNESS INFORMATION
if(missingness == TRUE){
summaryTable_nmiss <- data.frame()
varlist_all <- c(varlist, varlist_cat)
for(var_all in varlist_all){
sumtbl <- data.frame(variable = NA, n_miss = NA, perc = NA)
sumtbl$variable <- var_all
sumtbl$n <- sum(is.na(dataset[[var_all]]) == TRUE, na.rm = TRUE)
#sumtbl$perc <- (sum(is.na(dataset[[var_all]]) == TRUE, na.rm = TRUE)/nrow(dataset[[var_all]]))*100
sumtbl$perc <- (sum(is.na(dataset[[var_all]]) == TRUE, na.rm = TRUE)/length(dataset[[var_all]]))*100
summaryTable_nmiss <- rbind(summaryTable_nmiss, sumtbl)
}
#combine mean, lci and uci columns into 1 (mean_ci)
summaryTable_nmiss$perc_1 <- paste0("(", round(summaryTable_nmiss$perc, 2),"%",")")
summaryTable_nmiss$n_miss <- paste0(summaryTable_nmiss$n, " ", summaryTable_nmiss$perc_1)
summaryTable_nmiss$perc <- NULL
summaryTable_nmiss$n <- NULL
summaryTable_nmiss$perc_1 <- NULL
summaryTable_nmiss <- as.data.frame(summaryTable_nmiss)
summaryTable_count_num_miss <<- full_join(summaryTable_count_num, summaryTable_nmiss)
}
}
else{
#FOR CATEGORICAL VARIABLES
summaryTable_ncount_group <- data.frame(variable = varlist_cat)
groups <- unique(dataset[[group]])
groups <- groups[ !groups == 'NA']
for (g in groups) {
summaryTable_ncount <- data.frame()
dataset1 <- dataset[dataset[[group]]== g & !is.na(dataset[[group]]),]
for(var in varlist_cat){
sumtbl <- data.frame(variable = NA, category = NA, n = NA, perc = NA)
sumtbl$variable <- var
categories <- unique(dataset[[var]])
for (cat in categories){
sumtbl$category <- cat
sumtbl$n <- sum(dataset1[[var]] == cat, na.rm = TRUE)
sumtbl$perc <- (sum(dataset1[[var]] == cat, na.rm = TRUE)/length(dataset1[[var]]))*100
sumtbl$total <- nrow(dataset1[dataset1[[var]],])
summaryTable_ncount <- rbind(summaryTable_ncount, sumtbl)
}
summaryTable_ncount1 <- left_join(summaryTable_ncount_group, summaryTable_ncount)
}
summaryTable_ncount1$perc_1 <- paste0("(", round(summaryTable_ncount1$perc, 2),"%",")")
summaryTable_ncount1$n_perc <- paste0(summaryTable_ncount1$n, " ", summaryTable_ncount1$perc_1)
colnames(summaryTable_ncount1)[colnames(summaryTable_ncount1) == "n_perc"] <- paste(g, "n")
summaryTable_ncount1$perc <- NULL
summaryTable_ncount1$n <- NULL
summaryTable_ncount1$perc_1 <- NULL
summaryTable_ncount1$total <- NULL
summaryTable_ncount_group <- left_join(summaryTable_ncount_group, summaryTable_ncount1)
}
summaryTable_ncount_group <<- as.data.frame(summaryTable_ncount_group)
## FOR NUMERIC VARIABLES
#IF WANT MEAN & 95% CI
if(numeric_option == "meanCI"){
summaryTable_mCI_group <- data.frame(variable = varlist)
for(g in groups){
summaryTable_mCI <- data.frame()
dataset1 <- dataset[dataset[[group]]== g & !is.na(dataset[[group]]),]
for(var in varlist){
sumtbl <- data.frame(variable = NA, mean_ci = NA)
sumtbl$variable <- var
sumtbl$mean_ci <- list(mean_cl_normal(dataset1[[var]]) %>%
rename(mean = y, lci = ymin, uci = ymax))
summaryTable_mCI <- rbind(summaryTable_mCI, sumtbl)
}
summaryTable_mCI <- as.data.frame(summaryTable_mCI) %>%
unnest(cols = mean_ci)
#convert to two decimals
summaryTable_mCI <- summaryTable_mCI %>%
mutate_if(is.numeric, round, digits = 2)
#combine mean, lci and uci columns into 1 (mean_ci)
summaryTable_mCI$uci <- paste(summaryTable_mCI$uci, ")")
summaryTable_mCI$lci <- paste("(", summaryTable_mCI$lci)
summaryTable_mCI <- summaryTable_mCI %>%
unite(lci_uci, c(lci, uci), sep = " ,", remove = TRUE) %>%
unite(mean_ci, c(mean,lci_uci), sep = " ", remove = TRUE)
colnames(summaryTable_mCI)[colnames(summaryTable_mCI) == "mean_ci"] <- paste(g, "mean_ci")
summaryTable_mCI_group <- left_join(summaryTable_mCI_group, summaryTable_mCI)
}
summaryTable_GROUP <<- full_join(summaryTable_ncount_group, summaryTable_mCI_group)
}
#IF WANT MEAN & SD
if(numeric_option == "meanSD"){
summaryTable_mSD_group <- data.frame(variable = varlist)
for(g in groups){
summaryTable_mSD <- data.frame()
dataset1 <- dataset[dataset[[group]]== g & !is.na(dataset[[group]]),]
for(var in varlist){
sumtbl <- data.frame(variable = NA, mean = NA, sd = NA)
sumtbl$variable <- var
sumtbl$mean <- mean(dataset1[[var]], na.rm = TRUE)
sumtbl$sd <- sd(dataset1[[var]], na.rm = TRUE)
summaryTable_mSD <- rbind(summaryTable_mSD, sumtbl)
}
#convert to two decimals
summaryTable_mSD <- summaryTable_mSD %>%
mutate_if(is.numeric, round, digits = 2)
#combine mean, lci and uci columns into 1 (mean_ci)
summaryTable_mSD$sd <- paste0("(", round(summaryTable_mSD$sd, digits = 2), ")")
summaryTable_mSD$mean_sd <- paste0(round(summaryTable_mSD$mean, digits = 2), " ", summaryTable_mSD$sd)
summaryTable_mSD$mean <- NULL
summaryTable_mSD$sd <- NULL
colnames(summaryTable_mSD)[colnames(summaryTable_mSD) == "mean_sd"] <- paste(g, "mean_sd")
summaryTable_mSD_group <- left_join(summaryTable_mSD_group, summaryTable_mSD)
}
summaryTable_GROUP <<- full_join(summaryTable_ncount_group, summaryTable_mSD_group)
}
#IF WANT MEDIAN & IQR
if(numeric_option == "medianIQR"){
summaryTable_mIQR_group <- data.frame(variable = varlist)
for(g in groups){
summaryTable_mIQR <- data.frame()
dataset1 <- dataset[dataset[[group]]== g & !is.na(dataset[[group]]),]
for(var in varlist){
sumtbl <- data.frame(variable = NA, median = NA, Q1 = NA, Q3 = NA)
sumtbl$variable <- var
sumtbl$median <- median(dataset1[[var]], na.rm = TRUE)
sumtbl$Q1 <- quantile(dataset1[[var]], na.rm = TRUE, 0.25)
sumtbl$Q3 <- quantile(dataset1[[var]], na.rm = TRUE, 0.75)
summaryTable_mIQR <- rbind(summaryTable_mIQR, sumtbl)
}
#convert to two decimals
summaryTable_mIQR <- summaryTable_mIQR %>%
mutate_if(is.numeric, round, digits = 2)
#combine mean, lci and uci columns into 1 (mean_ci)
summaryTable_mIQR$IQR <- paste0("[", round(summaryTable_mIQR$Q1, digits = 2),",", round(summaryTable_mIQR$Q3, 2), "]")
summaryTable_mIQR$median_IQR <- paste0(round(summaryTable_mIQR$median, digits = 2), " ", summaryTable_mIQR$IQR)
summaryTable_mIQR$median <- NULL
summaryTable_mIQR$Q1 <- NULL
summaryTable_mIQR$Q3 <- NULL
summaryTable_mIQR$IQR <- NULL
colnames(summaryTable_mIQR)[colnames(summaryTable_mIQR) == "median_IQR"] <- paste(g, "median_IQR")
summaryTable_mIQR_group <- left_join(summaryTable_mIQR_group, summaryTable_mIQR)
}
summaryTable_GROUP <<- full_join(summaryTable_ncount_group, summaryTable_mIQR_group)
}
## IF WANT TO INCLUDE MISSINGNESS
if(missingness == TRUE){
varlist_all <- c(varlist, varlist_cat)
summaryTable_nmiss_group <- data.frame(variable = varlist_all)
for(g in groups){
summaryTable_nmiss <- data.frame()
dataset1 <- dataset[dataset[[group]]== g & !is.na(dataset[[group]]),]
for(var_all in varlist_all){
#if(var_all == "bmi") browser()
sumtbl <- data.frame(variable = NA, n_miss = NA, perc = NA)
sumtbl$variable <- var_all
sumtbl$n <- sum(is.na(dataset1[[var_all]]) == TRUE, na.rm = TRUE)
#sumtbl$perc <- (sum(is.na(dataset[[var]]) == TRUE, na.rm = TRUE)/nrow(dataset[[var]]))*100
sumtbl$perc <- (sum(is.na(dataset1[[var_all]]) == TRUE, na.rm = TRUE)/length(dataset1[[var_all]]))*100
summaryTable_nmiss <- rbind(summaryTable_nmiss, sumtbl)
}
#combine mean, lci and uci columns into 1 (mean_ci)
summaryTable_nmiss$perc_1 <- paste0("(", round(summaryTable_nmiss$perc, 2),"%",")")
summaryTable_nmiss$n_miss <- paste0(summaryTable_nmiss$n, " ", summaryTable_nmiss$perc_1)
summaryTable_nmiss$perc <- NULL
summaryTable_nmiss$n <- NULL
summaryTable_nmiss$perc_1 <- NULL
colnames(summaryTable_nmiss)[colnames(summaryTable_nmiss) == "n_miss"] <- paste(g, "n missing(%)")
summaryTable_nmiss_group <- left_join(summaryTable_nmiss_group, summaryTable_nmiss)
}
summaryTable_GROUP_missing <<- full_join(summaryTable_GROUP, summaryTable_nmiss_group)
}
}
}