-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatistical_Analysis_of_Clinical_Data.R
408 lines (256 loc) · 13.4 KB
/
Statistical_Analysis_of_Clinical_Data.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
### Filtering the clinical data by removing columns having more than 20% NA's
# read in your data
clinical_data = read.delim("/home/shweta/Desktop/ProjectChildren/Clinical Data/all_data.txt")
# calculate the percentage of missing values in each column
missing_percent = colSums(is.na(clinical_data)) / nrow(clinical_data) * 100
# create a vector of column names to remove columns having more than 20% NA's
cols_to_remove = names(clinical_data)[missing_percent >= 20]
# remove the columns from your data
clinical_data = clinical_data[, !(names(clinical_data) %in% cols_to_remove)]
write.table((clinical_data),file="/home/shweta/Desktop/ProjectChildren/Clinical Data/Filtered_clinical_data.xlsx", quote = FALSE,col.names = NA, sep = "\t")
#####################################################
### Filtering the clinical data by removing columns having more than 20% NA's
# read in your data
clinical_data = read.delim("/home/shweta/Desktop/ProjectChildren/Clinical Data/Clinical_continuous_data.txt")
# calculate the percentage of missing values in each column
missing_percent = colSums(is.na(clinical_data)) / nrow(clinical_data) * 100
# create a vector of column names to remove columns having more than 20% NA's
cols_to_remove = names(clinical_data)[missing_percent >= 20]
# remove the columns from your data
clinical_data = clinical_data[, !(names(clinical_data) %in% cols_to_remove)]
write.table((clinical_data),file="/home/shweta/Desktop/ProjectChildren/Clinical Data/filtered_continuous_data.txt", quote = FALSE,col.names = NA, sep = "\t")
########################################################
### Filtering the clinical data by removing columns having more than 20% NA's
# read in your data
categorical_data = read.delim("/home/shweta/Desktop/ProjectChildren/Clinical Data/categorical_data.txt")
# calculate the percentage of missing values in each column
missing_percent = colSums(is.na(categorical_data)) / nrow(categorical_data) * 100
# create a vector of column names to remove
cols_to_remove = names(categorical_data)[missing_percent >= 20]
# remove the columns from your data
categorical_data = categorical_data[, !(names(categorical_data) %in% cols_to_remove)]
write.table((categorical_data),file="/home/shweta/Desktop/ProjectChildren/Clinical Data/Filtered_categorical_data.txt", quote = FALSE,col.names = NA, sep = "\t")
########################################################
### To check if the Data is normalized
conti_data=read.delim("/home/shweta/Desktop/ProjectChildren/Clinical Data/Clinical_continuous_data.txt",header=TRUE,row.names = 1)
# Create a text file for the output
output_file = file("/home/shweta/Desktop/ProjectChildren/Clinical Data/Normal_distribution_for_continuous_data/outliers.txt", "w")
# Loop for all columns containing continuous variables
for (i in 2:42) {
# Create a PDF file with a name corresponding to the variable
pdf(paste0("/home/shweta/Desktop/ProjectChildren/Clinical Data/Normal_distribution_for_continuous_data/variable_", colnames(conti_data)[i], ".pdf"))
# Create a histogram
hist(conti_data[, i], main = paste("Histogram for", colnames(conti_data)[i]), xlab = "Value", ylab = "Frequency")
# Create a boxplot
boxplot(conti_data[, i], main = paste("Boxplot for", colnames(conti_data)[i]), ylab = "Value")
# Create a QQ plot
qqnorm(conti_data[,i], main=paste("QQ plot for", colnames(conti_data)[i]))
qqline(conti_data[,i])
# Save and close the PDF file
dev.off()
# Calculate z-scores
z_scores = scale(conti_data[, i])
# Write z-scores to a text file
write.table(z_scores, file = paste0("/home/shweta/Desktop/ProjectChildren/Clinical Data/Z_score/z_scores_", colnames(conti_data)[i], ".txt"))
# Identify outliers
outliers = which(abs(z_scores) > 3)
# Print the number of outliers
out = paste("There are", length(outliers), "outliers in", colnames(conti_data)[i], "\n")
# Write outliers to a text file
cat(out, file = output_file)
}
# Close the output file
close(output_file)
################################################
### Kruskal Wallis Test
df3=read.delim("/home/shweta/Desktop/ProjectChildren/Clinical Data/filtered_continuous_data.txt")
# Define the column names
col_names <- names(df3)[2:27]
# Open a file for writing results
result_file <- file("/home/shweta/Desktop/ProjectChildren/Clinical Data/kruskal_results2.txt", "w")
# Loop through each column name in a vector
for (col_name in col_names) {
# Perform the Kruskal-Wallis test for the current column
kw_result <- kruskal.test(as.formula(paste0(col_name, " ~ Clusters")), data = df3)
# get the p-values
p.values <- kw_result$p.value
# apply BH correction
p.adj <- p.adjust(p.values, method = "BH")
# get the absolute p-values that passed the BH correction
p.abs.adj <- abs(p.values[p.adj <= 0.05])
# Write the test results to the file
writeLines(capture.output(kw_result, p.values, p.adj, p.abs.adj), result_file)
writeLines("\n", result_file)
}
# Close the file
close(result_file)
# Open a file for writing results
median_result_file <- file("/home/shweta/Desktop/ProjectChildren/Clinical Data/kw_median_IQR_result.txt", "w")
for (col_name in col_names) {
# Calculate medians for each group
#medians <- aggregate(as.formula(paste0(col_name, " ~ Clusters")), data = df3, median)
medians_iqrs = aggregate(df3[col_names], by = list(Clusters = df3$Clusters),
FUN = function(x) c(median = median(x, na.rm = TRUE), IQR = IQR(x, na.rm = TRUE)))
# Write the test results to the file
writeLines(capture.output(medians_iqrs), median_result_file)
writeLines("\n", median_result_file)
}
# Close the file
close(median_result_file)
########################################################
### Mann Whitney U-test
result_file <- file("/home/shweta/Desktop/ProjectChildren/Clinical Data/mann_w_result.txt", "w")
for (i in 2:ncol(df3)) {
p_values <- pairwise.wilcox.test(df3[, i], df3$Clusters, p.adjust.method = "BH")
col_n <- paste("Results for variable:", names(df3)[i])
writeLines(col_n, result_file)
writeLines(capture.output(p_values), result_file)
writeLines("\n", result_file)
}
close(result_file)
###########################################
### Chi square test
categoical_data=read.delim("/home/shweta/Desktop/ProjectChildren/Clinical Data/Filtered_categorical_data.txt",header=TRUE)
View(categoical_data)
# Check the number of unique values in the column
predictor_cols = colnames(categoical_data)[-1]
output_file = file("/home/shweta/Desktop/ProjectChildren/Clinical Data/chi_square_results2.txt", "w")
# Loop over the predictor variables and perform chi-square tests
for (col in predictor_cols) {
contingency_table = table(categoical_data$Clusters, categoical_data[[col]])
chisq_result = chisq.test(contingency_table)
result_string = paste("Chi-square test for", col, "p-value:", chisq_result$p.value, "\n")
# get the p-values
p.values <- chisq_result$p.value
# apply BH correction
p.adj <- p.adjust(p.values, method = "BH")
# get the absolute p-values that passed the BH correction
p.abs.adj <- abs(p.values[p.adj <= 0.05])
writeLines(capture.output(result_string, p.values, p.adj, p.abs.adj), output_file)
writeLines("\n", output_file)
}
# Close the output file
close(output_file)
###########################################
# Chi square test Group1
categorical_data_Group1=read.delim("/home/shweta/Desktop/ProjectChildren/Clinical Data/Group1(Cluster1_2).txt",header=TRUE)
View(categorical_data_Group1)
# Check the number of unique values in the column
predictor_cols = colnames(categorical_data_Group1)[-1]
# write.table(chisq_result,"P_values_chi_Square test.txt")
output_file = file("/home/shweta/Desktop/ProjectChildren/Clinical Data/chi_square_group1.txt", "w")
# Loop over the predictor variables and perform chi-square tests
for (col in predictor_cols) {
contingency_table = table(categorical_data_Group1$Clusters, categorical_data_Group1[[col]])
chisq_result = chisq.test(contingency_table)
result_string = paste("Chi-square test for", col, "p-value:", chisq_result$p.value, "\n")
writeLines(result_string, output_file)
}
# Close the output file
close(output_file)
#####################################################
# Chi square test Group2
categorical_data_Group2=read.delim("/home/shweta/Desktop/ProjectChildren/Clinical Data/Group2(Cluster1_3).txt",header=TRUE)
View(categorical_data_Group2)
# Check the number of unique values in the column
predictor_cols = colnames(categorical_data_Group2)[-1]
# write.table(chisq_result,"P_values_chi_Square test.txt")
output_file = file("/home/shweta/Desktop/ProjectChildren/Clinical Data/chi_square_group2.txt", "w")
# Loop over the predictor variables and perform chi-square tests
for (col in predictor_cols) {
contingency_table = table(categorical_data_Group2$Clusters, categorical_data_Group2[[col]])
chisq_result = chisq.test(contingency_table)
result_string = paste("Chi-square test for", col, "p-value:", chisq_result$p.value, "\n")
writeLines(result_string, output_file)
}
# Close the output file
close(output_file)
#####################################################
# Chi square test Group3
categorical_data_Group3=read.delim("/home/shweta/Desktop/ProjectChildren/Clinical Data/Group3(Cluster1_4).txt",header=TRUE)
View(categorical_data_Group3)
# Check the number of unique values in the column
predictor_cols = colnames(categorical_data_Group3)[-1]
# write.table(chisq_result,"P_values_chi_Square test.txt")
output_file = file("/home/shweta/Desktop/ProjectChildren/Clinical Data/chi_square_group3.txt", "w")
# Loop over the predictor variables and perform chi-square tests
for (col in predictor_cols) {
contingency_table = table(categorical_data_Group3$Clusters, categorical_data_Group3[[col]])
chisq_result = chisq.test(contingency_table)
result_string = paste("Chi-square test for", col, "p-value:", chisq_result$p.value, "\n")
writeLines(result_string, output_file)
}
# Close the output file
close(output_file)
#####################################################
# Chi square test Group4
categorical_data_Group4=read.delim("/home/shweta/Desktop/ProjectChildren/Clinical Data/Group4(Cluster2_3).txt",header=TRUE)
View(categorical_data_Group4)
# Check the number of unique values in the column
predictor_cols = colnames(categorical_data_Group4)[-1]
# write.table(chisq_result,"P_values_chi_Square test.txt")
output_file = file("/home/shweta/Desktop/ProjectChildren/Clinical Data/chi_square_group4.txt", "w")
# Loop over the predictor variables and perform chi-square tests
for (col in predictor_cols) {
contingency_table = table(categorical_data_Group4$Clusters, categorical_data_Group4[[col]])
chisq_result = chisq.test(contingency_table)
result_string = paste("Chi-square test for", col, "p-value:", chisq_result$p.value, "\n")
writeLines(result_string, output_file)
}
# Close the output file
close(output_file)
#####################################################
# Chi square test Group5
categorical_data_Group5=read.delim("/home/shweta/Desktop/ProjectChildren/Clinical Data/Group5(Cluster2_4).txt",header=TRUE)
View(categorical_data_Group5)
# Check the number of unique values in the column
predictor_cols = colnames(categorical_data_Group5)[-1]
# write.table(chisq_result,"P_values_chi_Square test.txt")
output_file = file("/home/shweta/Desktop/ProjectChildren/Clinical Data/chi_square_group5.txt", "w")
# Loop over the predictor variables and perform chi-square tests
for (col in predictor_cols) {
contingency_table = table(categorical_data_Group5$Clusters, categorical_data_Group5[[col]])
chisq_result = chisq.test(contingency_table)
result_string = paste("Chi-square test for", col, "p-value:", chisq_result$p.value, "\n")
writeLines(result_string, output_file)
}
# Close the output file
close(output_file)
#####################################################
# Chi square test Group6
categorical_data_Group6=read.delim("/home/shweta/Desktop/ProjectChildren/Clinical Data/Group6(Cluster3_4).txt",header=TRUE)
View(categorical_data_Group6)
# Check the number of unique values in the column
predictor_cols = colnames(categorical_data_Group6)[-1]
# write.table(chisq_result,"P_values_chi_Square test.txt")
output_file = file("/home/shweta/Desktop/ProjectChildren/Clinical Data/chi_square_group6.txt", "w")
# Loop over the predictor variables and perform chi-square tests
for (col in predictor_cols) {
contingency_table = table(categorical_data_Group6$Clusters, categorical_data_Group6[[col]])
chisq_result = chisq.test(contingency_table)
result_string = paste("Chi-square test for", col, "p-value:", chisq_result$p.value, "\n")
writeLines(result_string, output_file)
}
# Close the output file
close(output_file)
#####################################################
set.seed(123)
# load the dplyr package
library(dplyr)
# gather all the categorical columns into key-value pairs
df = categoical_data %>%
tidyr::gather(key = Column, value = Value, -Clusters)
# group the data frame by clusters and column name, and count the number of each category
count_df = df %>%
group_by(Clusters, Column, Value) %>%
summarise(count = n()) %>%
ungroup()
# calculate the percentage of each category for each cluster
percent_df = count_df %>%
group_by(Clusters, Column) %>%
mutate(percent = count/sum(count) * 100) %>%
arrange(Column)
# view the result
View(percent_df)
write.table((percent_df),file = "/home/shweta/Desktop/ProjectChildren/Clinical Data/percentage_categorical_data.txt", quote = FALSE,col.names = NA)
#########################################################