forked from dsasson48/niaid_TB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_E_2.Rmd
210 lines (151 loc) · 5.15 KB
/
file_E_2.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
---
title: "Data Viz/Table1"
author: "David Sasson"
date: "5/11/2018"
output:
html_document: default
pdf_document: default
word_document: default
---
```{r setup, include=FALSE}
# knitr::opts_knit$set(root.dir = "~/Desktop/MIT/HST936/Project/")
packs <- c("xlsx", "dplyr", "ggplot2", "gmodels", "ggcorrplot", "caret", "caTools", "gridExtra", "ggthemes", "grid", "ggpubr", "tableone", "knitr", "cowplot")
lapply(packs, require, character.only = TRUE)
set.seed(123)
```
```{r reading_file}
TB <- read.xlsx("data/tb_data.xlsx", sheetIndex = 1, stringsAsFactors = FALSE, header = TRUE)
```
```{r def_outcome}
# unique(TB$outcome) -- ignoring notreported and unknown
TB$trtfailure[TB$outcome == "completed" | TB$outcome == "cured" | TB$outcome == "still_on_treatment"] <- 0
TB$trtfailure[TB$outcome == "failure" | TB$outcome == "death" | TB$outcome == "default" | TB$outcome == "died"] <- 1
TB$trtfailure <- as.factor(TB$trtfailure)
# Remove duplicate entries from the same patient
TB <- TB[!duplicated(TB$patient_id), ]
```
```{r data cleaning}
# Country
TB$country <- as.factor(TB$country)
# Age
TB$age_of_onset <- as.numeric(TB$age_of_onset)
# Sex
TB$gender <- as.factor(TB$gender)
# Education
TB$education <- as.factor(TB$education)
# Employment
TB$employment <- as.factor(TB$employment)
# Number of daily contacts
TB$number_of_daily_contacts <- as.numeric(TB$number_of_daily_contacts)
# Type of resistance
TB$type_of_resistance <- as.factor(TB$type_of_resistance)
# BMI
TB$bmi <- as.numeric(TB$bmi)
# Localization of the lesion in the brain
TB$lung_localization <- as.factor(TB$lung_localization)
# Dissemination
TB$dissemination <- as.factor(TB$dissemination)
# Lung cavity size
TB$lungcavity_size <- as.factor(TB$lungcavity_size)
# Shadow pattern
TB$shadow_pattern <- as.factor(TB$shadow_pattern)
# Pneumothorax
TB$pneumothorax <- as.factor(TB$pneumothorax)
# Pleuritis
TB$plevritis <- as.factor(TB$plevritis)
# Node calcinosis
TB$nodicalcinatum <- as.factor(TB$nodicalcinatum)
# Caverna
TB$totalcavernum <- as.factor(TB$totalcavernum)
# Social risk factors
TB$social_risk_factors <- as.factor(TB$social_risk_factors)
# Treatment
TB$regimen_drug <- as.factor(TB$regimen_drug)
TB$number_of_children <- as.numeric(TB$number_of_children)
TB$x_ray_count <- as.numeric(TB$x_ray_count)
TB$ct_count <- as.numeric(TB$ct_count)
TB$registration_date <- as.Date(TB$registration_date)
# check results
# sapply(TB, class)
```
```{r age}
ggplot(TB, aes(age_of_onset, fill=gender)) +
geom_histogram(binwidth=2) +
facet_wrap(~country, nrow = 2) +
xlab("Age of Onset")+
ylab("Count") +
theme_pubr()+ guides(fill=guide_legend(title="")) +
scale_fill_manual(values=c("indianred1", "steelblue"))
```
```{r outcome}
ggplot(TB, aes(x = outcome, y = type_of_resistance)) +
geom_count(col="indianred1") +
scale_size_area() +
coord_flip() +
ylab("Type of Resistance") +
xlab("Outcome") +
theme_pubr()
```
```{r missing data}
g <-ggplot(TB, aes(x = outcome, fill = genomic_data_exists)) +
geom_bar() +
coord_flip() +
ggtitle("Genomic Data")+
guides(fill=FALSE) + ylab("") + xlab("") +
theme_pubr() +
scale_fill_manual(values=c("indianred1", "steelblue"))
x <- ggplot(TB, aes(x = outcome, fill = x_ray_exists)) +
geom_bar() +
coord_flip() +
ggtitle("X-Ray Data") +
guides(fill=FALSE)+ ylab("")+
theme_pubr() +
scale_fill_manual(values=c("indianred1", "steelblue"))
c <- ggplot(TB, aes(x = outcome, fill=ct_exists)) +
geom_bar() +
coord_flip()+
guides(fill=FALSE)+
ggtitle("CT Scan Data") + xlab("")+
theme_pubr() +
scale_fill_manual(values=c("indianred1", "steelblue"))
plot_grid(g, x, c, ncol=1)
```
```{r}
ggplot(TB) +
geom_bar(aes(outcome), fill="indianred1") +
xlab("Outcome") +
ylab("Count") +
coord_flip() +
facet_wrap(~country) +
theme_pubr()
```
```{r tableone}
# smaller table
listVars <- c("gender", "bmi","age_of_onset" , "country", "social_risk_factors","education", "regimen_drug")
table1 <- CreateTableOne(vars = listVars, data = TB, strata = c("trtfailure"), test=F)
table1 %>% print(
printToggle = FALSE,
showAllLevels = TRUE,
cramVars = "kon"
) %>%
{data.frame(
variable_name = gsub(" ", " ", rownames(.), fixed = TRUE), .,
row.names = NULL,
check.names = FALSE,
stringsAsFactors = FALSE)} %>% kable()
```
```{r appendix}
# all vars for appendix
allVars <- c("age_of_onset", "gender", "country", "affect_pleura", "bmi", "employment", "totalcavernum", "nodicalcinatum", "type_of_resistance", "lung_capacity_decrease","lungcavity_size", "pneumothorax", "dissemination", "regimen_drug", "shadow_pattern", "ct_count", "microscopy", "number_of_daily_contacts", "lung_localization", "x_ray_count", "process_prevalence","plevritis", "social_risk_factors","education", "regimen_drug")
table1.full <- CreateTableOne(vars = allVars, data = TB, strata = c("trtfailure"), test=F)
table1.full %>% print(
printToggle = FALSE,
showAllLevels = TRUE,
cramVars = "kon"
) %>%
{data.frame(
variable_name = gsub(" ", " ", rownames(.), fixed = TRUE), .,
row.names = NULL,
check.names = FALSE,
stringsAsFactors = FALSE)} %>% kable()
```