-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabela1.R
242 lines (196 loc) · 9.01 KB
/
tabela1.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
library(dplyr)
library(purrr)
library(tableone)
setwd("/home/pepper/dox/repos/amanda-masters/recorrencia/")
df <- read.csv('../data/banco-conversao-16-10-20.csv')
df_josi <- read.csv('../data/banco-conversao-josi.csv')
df_lu <- read.csv('../data/banco-apesm-3-anos.csv')
df <- df %>% filter(., !is.na(mora_t2))
colnames(df_josi)[colnames(df_josi) == "a02rec"] <- "rec"
colnames(df_lu)[colnames(df_lu) == "a02rec"] <- "rec"
df <- left_join(df, df_josi %>%
dplyr::select(rec, starts_with(c("CTQ", "Abuso", "Negligencia", "cluster")), miniA11, miniA12,
miniA03ATa, miniA03ATb, miniA03ATc1, miniA03ATc2, miniA03ATd, miniA03ATe1,
miniA03ATe2, miniA03ATf, miniA03ATg), by = "rec")
df <- left_join(df, df_lu %>%
dplyr::select(rec, miniC04, miniC05), by = "rec")
df <- df %>% mutate(bdi_total = rowSums(dplyr::select(., starts_with("BDI"))))
df <- df %>% dplyr::filter(., !(miniC04 == 10 | miniC05 == 10 | tpsicoticoatual == 2))
df <- df %>% mutate(., vive_companheiro = case_when(
a36relaciona == 1 | a36relaciona == 5 | a36relaciona == 6 ~ 0,
a36relaciona == 2 | a36relaciona == 3 | a36relaciona == 4 ~ 1
))
df <- df %>%
mutate(dep_severa = case_when(
# No depression
bdi_total <= 13 &
TB_erros != 3 ~ 0,
# Mild depression
bdi_total >= 14 & bdi_total <= 19 &
TB_erros != 3 ~ 1,
# Moderate depression
bdi_total >= 20 & bdi_total <= 28 &
TB_erros != 3 ~ 2,
# Severe depression
bdi_total >= 29 &
TB_erros != 3 ~ 3
))
amostra <- df %>% filter(dep_severa == 0 | dep_severa == 1 | dep_severa == 2 | dep_severa == 3)
amostra$a03sexo <- factor(amostra$a03sexo,
levels = c(1,2),
labels = c("Male", "Female"))
amostra$cordapele <- factor(amostra$cordapele,
levels = c(1,2),
labels = c("White", "Non-white"))
amostra$abepdicotomica <- factor(amostra$abepdicotomica,
levels = c(1,2),
labels = c("Lower", "Upper"))
amostra$vive_companheiro <- factor(amostra$vive_companheiro,
levels = c(0,1),
labels = c("No", "Yes"))
amostra$nemtrabnemestuda <- factor(amostra$nemtrabnemestuda,
levels = c(1,2),
labels = c("Does not work/study", "Works/study"))
amostra$suiciderisk_MINI <- factor(amostra$suiciderisk_MINI,
levels = c(0,1),
labels = c("No", "Yes"))
amostra$alcoolabudep <- factor(amostra$alcoolabudep,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$maconhaabudep <- factor(amostra$maconhaabudep,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$alucinogenosabudep <- factor(amostra$alucinogenosabudep,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$abudepoutrasdrogas <- factor(amostra$abudepoutrasdrogas,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$abudepoutrasdrogasshipnoticos <- factor(amostra$abudepoutrasdrogasshipnoticos,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$cigarroabudep <- factor(amostra$cigarroabudep,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$tpanicoatual <- factor(amostra$tpanicoatual,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$fobiasocialatual <- factor(amostra$fobiasocialatual,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$fobiaespatual <- factor(amostra$fobiaespatual,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$tagatual <- factor(amostra$tagatual,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$teptatual <- factor(amostra$teptatual,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$tocatual <- factor(amostra$tocatual,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$b08famil2[amostra$b08famil2 == 3 | amostra$b08famil2 == 4] <- 1
amostra$b01famil1[amostra$b01famil1 == 3 | amostra$b01famil1 == 4] <- 1
amostra$b08famil2 <- factor(amostra$b08famil2,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$b01famil1 <- factor(amostra$b01famil1,
levels = c(1,2),
labels = c("No", "Yes"))
amostra$dep_severa <- factor(amostra$dep_severa,
levels = c(0,1,2,3),
labels = c("Euthymic depression", "Mild depression", "Moderate depression", "Severe depression"))
list_vars <- c("a03sexo", "a05idade", "cordapele", "escolaridade", "abepdicotomica",
"vive_companheiro", "nemtrabnemestuda", "suiciderisk_MINI", "alcoolabudep",
"maconhaabudep", "alucinogenosabudep", "abudepoutrasdrogas",
"abudepoutrasdrogasshipnoticos", "cigarroabudep", "tpanicoatual",
"fobiasocialatual", "fobiaespatual", "tagatual", "teptatual", "tocatual")
cat_vars <- c("a03sexo", "cordapele", "abepdicotomica", "vive_companheiro", "nemtrabnemestuda",
"suiciderisk_MINI", "alcoolabudep", "maconhaabudep", "alucinogenosabudep",
"abudepoutrasdrogas", "abudepoutrasdrogasshipnoticos", "cigarroabudep",
"tpanicoatual", "fobiasocialatual", "fobiaespatual", "tagatual", "teptatual",
"tocatual")
table1 <- CreateTableOne(vars = list_vars,
data = amostra,
factorVars = cat_vars,
strata = "dep_severa")
table1_print <- print(table1, showAllLevels = TRUE)
setwd("/home/pepper/dox/repos/amanda-masters/")
write.csv(table1_print, file = "table1.csv")
library(table1)
label(amostra$a03sexo) <- "Sex"
label(amostra$a05idade) <- "Age"
units(amostra$a05idade) <- "years"
label(amostra$cordapele) <- "Skin color"
label(amostra$escolaridade) <- "Years of education"
label(amostra$abepdicotomica) <- "Socioeconomic status"
label(amostra$vive_companheiro) <- "Lives with partner"
label(amostra$nemtrabnemestuda) <- "Current occupation"
label(amostra$suiciderisk_MINI) <- "Suicide risk"
label(amostra$alcoolabudep) <- "Alcohol abuse/dependence"
label(amostra$maconhaabudep) <- "Marijuana abuse/dependence"
label(amostra$alucinogenosabudep) <- "Hallucinogenics abuse/dependence"
label(amostra$abudepoutrasdrogas) <- "Other drugs abuse/dependence"
label(amostra$abudepoutrasdrogasshipnoticos) <- "Hypnotics abuse/dependence"
label(amostra$cigarroabudep) <- "Tobacco abuse/dependence"
label(amostra$tpanicoatual) <- "Panic disorder"
label(amostra$fobiasocialatual) <- "Social phobia"
label(amostra$fobiaespatual) <- "Specific phobia"
label(amostra$tagatual) <- "Generalized anxiety disorder"
label(amostra$teptatual) <- "Posttraumatic stress disorder"
label(amostra$tocatual) <- "Obsessive-compulsive disorder"
label(amostra$b01famil1) <- "Maternal psychiatric illness"
label(amostra$b08famil2) <- "Paternal psychiatric illness"
pvalue <- function(x, ...) {
# Construct vectors of data y, and groups (strata) g
y <- unlist(x)
g <- factor(rep(1:length(x), times=sapply(x, length)))
if (is.numeric(y)) {
# For numeric variables, perform a standard 2-sample t-test
p <- summary(aov(y ~ g))[[1]][["Pr(>F)"]][[1]]
} else {
# For categorical variables, perform a chi-squared test of independence
p <- chisq.test(table(y, g))$p.value
}
# Format the p-value, using an HTML entity for the less-than sign.
# The initial empty string places the output on the line below the variable label.
c("", sub("<", "<", format.pval(p, digits=3, eps=0.001)))
}
options(browser = "firefox")
tabela_1_word <- table1(~ a03sexo + a05idade + cordapele + escolaridade + abepdicotomica +
vive_companheiro + nemtrabnemestuda + suiciderisk_MINI + alcoolabudep +
maconhaabudep + alucinogenosabudep + abudepoutrasdrogas +
abudepoutrasdrogasshipnoticos + cigarroabudep + tpanicoatual + fobiasocialatual +
fobiaespatual + tagatual + teptatual + tocatual + b01famil1 + b08famil2 | dep_severa,
data=amostra,
extra.col=list(`p-value`=pvalue))
tabela_1_word |>
table1::t1flex() |>
flextable::save_as_docx(path = "~/tmp/Table_1_BM.docx")
library(furniture)
table1(amostra,
"Sex" = a03sexo,
"Age" = a05idade,
"Skin color" = cordapele,
"Years of education" = escolaridade,
"Socioeconomic status" = abepdicotomica,
"Lives with partner" = vive_companheiro,
"Current occupation" = nemtrabnemestuda,
"Suicide risk" = suiciderisk_MINI,
"Alcohol abuse/dependence" = alcoolabudep,
"Marijuana abuse/dependence" = maconhaabudep,
#"Hallucinogenics abuse/dependence" = alucinogenosabudep,
"Other drugs abuse/dependence" = abudepoutrasdrogas,
"Hypnotics abuse/dependence" = abudepoutrasdrogasshipnoticos,
"Tobacco abuse/dependence" = cigarroabudep,
"Panic disorder" = tpanicoatual,
"Social phobia" = fobiasocialatual,
"Specific phobia" = fobiaespatual,
"Generalized anxiety disorder" = tagatual,
"Posttraumatic stress disorder" = teptatual,
"Obsessive-compulsive disorder" = tocatual,
"Maternal psychiatric illness" = b01famil1,
"Paternal psychiatric illness" = b08famil2,
splitby = ~dep_severa,
test = TRUE)