-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS_BABY_BIOME_TABLE_1_SUMMARY_STATS.Rmd
275 lines (221 loc) · 10.5 KB
/
CS_BABY_BIOME_TABLE_1_SUMMARY_STATS.Rmd
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
---
title: "CS_BABY_BIOME_Table_1_summary_statistics_phenotypes"
author: "Trishla Sinha"
date: "2/27/2023"
lastupdate:"09/06/2023"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r Load packages, echo=FALSE}
library(tidyverse)
library(ggplot2)
library(ggpubr)
```
```{r PHENOTYPE ANALYSIS, echo=FALSE}
metadata<-read.delim("~/Desktop/CS_Baby_Biome/OLD_ANALYSIS/EGA/Metadata_EGA_CS_BABY_BIOME.txt")
metadata[sapply(metadata, is.character)] <- lapply(metadata[sapply(metadata, is.character)], #convert character columns to factors
as.factor)
metadata_infants<-metadata %>% drop_na(Timepoint_numeric) # Only infant metadata
for_table_1<-metadata_infants[!duplicated(metadata_infants$CS_BABY_BIOME_ID), ]
#summary<-st(for_table_1, group = 'Randomization_AB_all', group.test = TRUE, out = "csv", file = "summary_stats_metadata_CS_Baby_Biome")
#write.table(summary, "summary_stats_table_1_CS_BABY_BIOME.txt", sep="\t", row.names=F, quote = F)
for_table_1_with_AB<-for_table_1[for_table_1$rand_AB=="Yes",]
for_table_1_no_AB<-for_table_1[for_table_1$rand_AB=="No",]
summary_statistics_metadata <- function (metadata_input, category_table) {
# Packages needed
library (psych) #describe r function
# Create other functions to calculate the different parameters
## Categorical values - create function to calculate the counts and the percentage for categorical variables
tblFun <- function(x) {
# Create a table
tbl <- table(x)
# Combine columnes/rows to get the counts and percentage (creates new table -> res)
res <- cbind(tbl,round(prop.table(tbl)*100,2))
# Give names to the columns
colnames(res) <- c('Count','Percentage')
res
}
## NA sum function - counts the number of NA
nzsum <- function(x) {
sum (is.na(x))
}
if (missing(category_table)) {
## Calculate table1 with the whole data:
my_results = matrix(ncol = 9, nrow = ncol(metadata_input))
for (k in 1:ncol(metadata_input)){
if (is.numeric(metadata_input[,k])) {
# Keep in "x" the result from describe function (done in the columns) - for each factor
x = describe(metadata_input[,k])
z = nzsum(metadata_input[,k])
# In the new table ("x"): keep different values in the different columns
my_results[k,1] = "numerical"
my_results[k,2] = x$median
my_results[k,3] = x$mean
my_results[k,4] = x$sd
my_results[k,5] = x$n
my_results[k,6] = z
my_results[k,7] = x$min
my_results[k,8] = x$max
my_results[k,9] = x$range
}
# Condition: if the column values are categorical
else {
# Keep in "x" the result from tblFun function (done in the columns) - for each factor
x = tblFun(metadata_input[,k])
z = nzsum(metadata_input[,k])
# In the new table ("x"): keep different values in the different columns
my_results[k,1]="categorical"
# toString to keep the possible different values/categories in the same vector/column
my_results[k,2]=toString(rownames(x))
# First column table x = 'Count'
my_results[k,3]=toString(x[,1])
# Second column table x = 'Percentage'
my_results[k,4]=toString(x[,2])
# Sum of the values on column1 ("x")
my_results[k,5]=sum(x[,1])
my_results[k,6]= z
my_results[k,7] = NA
my_results[k,8] = NA
my_results[k,9] = NA
}
}
# The column names from the original table = row names from the new table
rownames(my_results) = colnames(metadata_input)
# Give names to the columns of the new table
colnames(my_results) = c("Type", "Categories/Median", "Counts/Mean", "%/SD", "Number_non_zeros", "Number_NA",
"Min", "Max", "Range")
# Export the new table
write.table (my_results, file = "./meta_data_summary_stats.txt" , quote = F, sep = "\t")
}
}
# Generating summary stats for both groups
summary_statistics_metadata (for_table_1_with_AB)
summary_statistics_metadata (for_table_1_no_AB)
# These two files were then merged to create Table 1
# Testing p values for metadata file, for each comparison a different test was used based on the nature of the varaible
#Cefazoline
ggdensity(for_table_1_with_AB$cefazoline_measurement_mg_L,
main = "Density plot ofCefazoline_measurement_new",
xlab = "Cefzol")
ggqqplot(for_table_1_with_AB$cefazoline_measurement_mg_L) # Normal
shapiro.test(for_table_1_with_AB$cefazoline_measurement_mg_L) # Distribution of data not significantly different from normal distribution
t.test(for_table_1$cefazoline_measurement_mg_L~for_table_1$rand_AB)
# BMI beginning of pregnancy
ggdensity(for_table_1$pre_preg_bmi_mother,
main = "Density plot of BMI pre pregnancy",
xlab = "BMI")
ggqqplot(for_table_1$pre_preg_bmi_mother) # Normal
shapiro.test(for_table_1$pre_preg_bmi_mother) # Distribution of data not significantly different from normal distribution
t.test(for_table_1$pre_preg_bmi_mother~for_table_1$rand_AB)
wilcox.test(for_table_1$pre_preg_bmi_mother~for_table_1$rand_AB)
# Weight gain during pregnancy
ggdensity(for_table_1$preg_weight_gain,
main = "Density plot of preg weight gain ",
xlab = "Weight")
ggqqplot(for_table_1$preg_weight_gain) # Normal
shapiro.test(for_table_1$preg_weight_gain) # Distribution of data not significantly different from normal distribution
t.test(for_table_1$preg_weight_gain~for_table_1$rand_AB)
#wilcox.test(for_table_1$preg_weight_gain~for_table_1$rand_AB)
# Gestational age
ggdensity(for_table_1$preg_gest_age,
main = "Density plot of gestational age ",
xlab = "gestational age")
ggqqplot(for_table_1$preg_gest_age) # Not normal
shapiro.test(for_table_1$preg_gest_age) # Distribution of data significantly different from normal distribution
#t.test(for_table_1$preg_gest_age~for_table_1$rand_AB)
wilcox.test(for_table_1$preg_gest_age~for_table_1$rand_AB)
# Gravida
ggdensity(for_table_1$gravida,
main = "Density plot of gravida ",
xlab = "gravida")
ggqqplot(for_table_1$gravida) # Not normal
shapiro.test(for_table_1$gravida) # Distribution of data significantly different from normal distribution
#t.test(for_table_1$gravida~for_table_1$rand_AB)
wilcox.test(for_table_1$gravida~for_table_1$rand_AB)
# Para
ggdensity(for_table_1$para,
main = "Density plot of para ",
xlab = "para")
ggqqplot(for_table_1$para) # Not normal
shapiro.test(for_table_1$para) # Distribution of data significantly different from normal distribution
#t.test(for_table_1$gravida~for_table_1$rand_AB)
wilcox.test(for_table_1$para~for_table_1$rand_AB)
table(for_table_1$infant_sex, for_table_1$rand_AB)
fisher.test(table(for_table_1$infant_sex, for_table_1$rand_AB))
# Birthweight
ggdensity(for_table_1$infant_birthweight,
main = "Density plot of birthweight ",
xlab = "birthweight")
ggqqplot(for_table_1$infant_birthweight) # Normal
shapiro.test(for_table_1$infant_birthweight) # Distribution of data not significantly different from normal distribution
t.test(for_table_1$infant_birthweight~ for_table_1$rand_AB)
#wilcox.test(for_table_1$infant_birthweight~for_table_1$rand_AB)
# Maternal age
ggdensity(for_table_1$mother_age_at_delivery,
main = "Density plot of age at delivery",
xlab = "age at delivery ")
ggqqplot(for_table_1$mother_age_at_delivery) # Normal
shapiro.test(for_table_1$mother_age_at_delivery)# Distribution of data not significantly different from normal distribution
t.test(for_table_1$mother_age_at_delivery~ for_table_1$rand_AB)
#wilcox.test(for_table_1$infant_birthweight~for_table_1$rand_AB)
# APGAR 1
ggdensity(for_table_1$APGAR_1,
main = "Density plot of APGAR 1",
xlab = "age at APGAR 1 ")
ggqqplot(for_table_1$APGAR_1) # DEFINITELY NOT NORMAL
shapiro.test(for_table_1$APGAR_1) # DEFINITELY NOT NORMAL
wilcox.test(for_table_1$APGAR_1~for_table_1$rand_AB)
# APGAR 5
ggdensity(for_table_1$APGAR_5,
main = "Density plot of APGAR 5",
xlab = "age at APGAR 5")
ggqqplot(for_table_1$APGAR_5) # DEFINITELY NOT NORMAL
shapiro.test(for_table_1$APGAR_5) # DEFINITELY NOT NORMAL
wilcox.test(for_table_1$APGAR_5~for_table_1$rand_AB)
# Testing all the categorical variables
table(for_table_1$feeding_mode_pragmatic, for_table_1$rand_AB)
fisher.test(table(for_table_1$feeding_mode_pragmatic, for_table_1$rand_AB))
chisq.test(for_table_1$feeding_mode_pragmatic, for_table_1$rand_AB)
table(for_table_1$living_situation, for_table_1$rand_AB)
fisher.test(table(for_table_1$living_situation, for_table_1$rand_AB))
table(for_table_1$cats_dogs, for_table_1$rand_AB)
fisher.test(table(for_table_1$cats_dogs, for_table_1$rand_AB))
table(for_table_1$growth_p_limited, for_table_1$rand_AB)
fisher.test(table(for_table_1$growth_p_limited, for_table_1$rand_AB))
# Table 1 package i R
# Testing all the technical variables
# DNA concentration
ggdensity(metadata_infants$DNA_concentration_ng_ul,
main = "Density plot of DNA_concentration_ng_ul",
xlab = "DNA CONC")
ggqqplot(metadata_infants$DNA_concentration_ng_ul) # Not normal
shapiro.test(metadata_infants$DNA_concentration_ng_ul) # Distribution of data significantly different from normal distribution
wilcox.test(metadata_infants$DNA_concentration_ng_ul~metadata_infants$rand_AB)
metadata_infants %>%
group_by(rand_AB) %>%
summarize(Median = median(DNA_concentration_ng_ul, na.rm=TRUE))
# summarize(Min = min(DNA_concentration_ng_ul, na.rm=TRUE))
# summarize(Max = max(DNA_concentration_ng_ul, na.rm=TRUE))
# Read depth
ggdensity(metadata_infants$read_depth,
main = "Density plot of RD ",
xlab = "RD")
ggqqplot(metadata_infants$read_depth) # Not normal
shapiro.test(metadata_infants$read_depth) # Distribution of data significantly different from normal distribution
wilcox.test(metadata_infants$read_depth~metadata_infants$rand_AB)
metadata_infants %>%
group_by(rand_AB) %>%
# summarize(Median = median(read_depth, na.rm=TRUE))
# summarize(Min = min(read_depth, na.rm=TRUE))
summarize(Max = max(read_depth, na.rm=TRUE))
```
Summary stats of the early timepoints feeidng mode
```{r PHENOTYPE ANALYSIS, echo=FALSE}
metadata<-read.delim("~/Desktop/CS_Baby_Biome/ANALYSIS/METADATA_INFANTS_EARLY_CS_BABY_BIOME_09_06_2023_UPDATED_FEEDING.txt")
for_table_1<-metadata
for_table_1 = for_table_1[!duplicated(for_table_1$CS_BABY_BIOME_ID),]
table(for_table_1$feeding_mode_pragmatic, for_table_1$rand_AB)
fisher.test(table(for_table_1$feeding_mode_pragmatic, for_table_1$rand_AB))
chisq.test(for_table_1$feeding_mode_pragmatic, for_table_1$rand_AB)
```