-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_table1_demographic.Rmd
264 lines (227 loc) · 7.84 KB
/
01_table1_demographic.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
---
title: "01_table1_demographic"
author: "randy"
date: "`r Sys.Date()`"
output: pdf_document
---
::: callout-important
- Table1 has three different versions :
- the basic training & testing
- the basic training & testing + overall
- the basic training & testing + pvalues
- Do we need to include too many variables not used in the paper?
- ethnicity
- genotype
:::
## Setup
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
warnings = FALSE,
message = FALSE,
comment = "#>",
#results = "hide",
error = FALSE)
## clean the R environment
graphics.off()
rm(list = ls())
freshr::freshr()
## load packages
library(here, quietly = TRUE)
library(tidyverse, quietly = TRUE)
library(gtsummary, quietly = TRUE)
library(flextable, quietly = TRUE)
## check the directory for the file
# here::dr_here()
here::set_here()
## the figure or results should be saved
# paste0("foldername/Sfilename_workingresult_",
#. Sys.Date(), ".filetype")
```
```{r "dataset", eval = FALSE}
## set seed
set.seed(555)
# load("data/sysdata.rda")
load("final/epic_clean_full_data.Rdata")
# the code to prepare for
# epic, demog, test and train
# data0, data1, and data2
# they are all saved in sysdata.rda files
data <- left_join(epic, demog, by = "id") %>%
mutate(sex = as.factor(sex))
test_id <- unique(test$id) %>% unlist()
train_id <- unique(train$id) %>% unlist()
data0 <- data %>%
mutate(group = case_when(id %in% test_id ~ "testing",
TRUE ~ "training"))
data1 <- data0 %>%
group_by(id, group) %>%
summarize(age_mean = mean(age),
age_min = min(age),
age_max = max(age),
age_n = length(age),
visitn = n(),
h_mean = mean(ht),
h_max = max(ht),
h_min = min(ht),
w_mean = mean(wt),
w_max = max(wt),
w_min = min(wt),
sex = sex,
genotype = genotype,
ethnic = ethnic,
race = race) %>%
ungroup() %>%
unique()
# working dataset
data2 <- full_join(data1, data,
by = join_by(id, sex, genotype, ethnic, race)) %>%
as.data.frame() %>%
mutate(time = age - age_min,
age_diff = age_max - age_min)
write.csv(data1, file = paste0("data/S01_table1_dataset_randy_", Sys.Date(), ".csv"))
write.csv(data2, file = paste0("data/S01_epic_clean_randy_", Sys.Date(), ".csv"))
```
## Making table1
```{r "table1", message = FALSE, warning = FALSE}
library(readr)
data1 <- read_csv("data/S01_table1_dataset_randy_2023-08-23.csv")
View(data1)
## table0 contains all the information about demgo for total
table0 <- data1 %>%
unique() %>%
dplyr::select(-id) %>%
mutate(ethnic = case_when(ethnic == 1 ~ "Hispanic",
ethnic == 2 ~ "Non-Hispanic"),
race = case_when(race == 1 ~ "White",
race != 1 ~ "Other"),
sex = case_when(sex == "F" ~ "Female",
sex == "M" ~ "Male"),
age_diff = age_max - age_min) %>%
dplyr::select(group,
Genotype = genotype,
Gender = sex,
Race = race,
Ethnicity = ethnic,
"Visit number" = visitn,
"Age baseline" = age_min,
"Age final" = age_max,
"Follow up years" = age_diff,
# "Height mean" = h_mean,
"Height baseline" = h_min) %>%
# "Weight mean" = w_mean,
# "Weight baseline" = w_min
## select all the variables for table1
tbl_summary(by = group) %>%
## just display all the variables in one column
modify_header(label = "**Characteristics**") %>%
# update the column header
bold_labels() %>%
italicize_labels() %>%
# as.data.frame()
as_flex_table() %>%
flextable::bold(part = "header") %>%
## auto adjust the column widths
flextable::autofit()
## table1 contains information of dataset grouped as training and testing
table1 <- data1 %>%
unique() %>%
dplyr::select(-id) %>%
mutate(ethnic = case_when(ethnic == 1 ~ "Hispanic",
ethnic == 2 ~ "Non-Hispanic"),
race = case_when(race == 1 ~ "White",
race != 1 ~ "Other"),
sex = case_when(sex == "F" ~ "Female",
sex == "M" ~ "Male"),
age_diff = age_max - age_min) %>%
dplyr::select(group,
Genotype = genotype,
Gender = sex,
Race = race,
Ethnicity = ethnic,
"Visit number" = visitn,
# "Age mean" = age_mean,
"Age baseline" = age_min,
"Age final" = age_max,
"Follow up years" = age_diff,
# "Height mean" = h_mean,
"Height baseline" = h_min) %>%
# "Weight mean" = w_mean,
# "Weight baseline" = w_min)
## select all the variables for table1
tbl_summary(by = group) %>%
## just display all the variables in one column
modify_header(label = "**Characteristics**") %>%
# update the column header
bold_labels() %>%
add_p() %>%
italicize_labels()
## table1 contains information of dataset grouped as training and testing
table2 <- data1 %>%
unique() %>%
dplyr::select(-id) %>%
mutate(ethnic = case_when(ethnic == 1 ~ "Hispanic",
ethnic == 2 ~ "Non-Hispanic"),
race = case_when(race == 1 ~ "White",
race != 1 ~ "Other"),
sex = case_when(sex == "F" ~ "Female",
sex == "M" ~ "Male"),
age_diff = age_max - age_min) %>%
dplyr::select(group,
Genotype = genotype,
Gender = sex,
Race = race,
Ethnicity = ethnic,
"Visit number" = visitn,
# "Age mean" = age_mean,
"Age baseline" = age_min,
# "Age final" = age_max,
"Follow up years" = age_diff,
# "Height mean" = h_mean,
"Height baseline" = h_min) %>%
tbl_summary(by = group,
statistic = list(all_continuous() ~ "{mean} ({sd})") ) %>%
## just display all the variables in one column
modify_header(label = "**Characteristics**") %>%
# update the column header
bold_labels() %>%
add_overall(last = TRUE) %>%
italicize_labels()
table0
```
## Saving for the table1
```{r "saving", eval=FALSE, include=FALSE}
## save .pptx file
## flextable can be saved directly to powerpoints
flextable::save_as_pptx(table0,
path = paste0("figure/S01_plmlmm_table1_", Sys.Date(), ".pptx"))
## save the .png file
library(webshot)
# webshot::install_phantomjs()
flextable::save_as_image(table0,
path = paste0("figure/S01_plmlmm_table1_", Sys.Date(), ".png"),
# zoom = 3,
# expand = 10,
webshot = "webshot")
# flextable::save_as_image(table0,
# path = paste0("figure/S01_plmlmm_table0_", Sys.Date(), ".pptx"),
# # zoom = 3,
# # expand = 10,
# webshot = "webshot")
## save the latex .text file
library(xtable)
# xtable(table0, type = "latex",
# file = paste0("figure/S01_plmlmm_table0_", Sys.Date(), ".tex"))
# print(xtable(table0, type = "latex"),
# file = paste0("figure/S01_plmlmm_table0_", Sys.Date(), ".tex"))
# xtable(table1, type = "latex",
# file = paste0("figure/S01_plmlmm_table1_", Sys.Date(), ".tex"))
print(xtable(table1, type = "latex"),
file = paste0("figure/S01_plmlmm_table1_pvalue_", Sys.Date(), ".tex"))
# xtable(table2, type = "latex",
# file = paste0("figure/S01_plmlmm_table2_", Sys.Date(), ".tex"))
print(xtable(table2, type = "latex"),
file = paste0("figure/S01_plmlmm_table1_overall_", Sys.Date(), ".tex"))
```
```{r}
sessionInfo()
```