-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2022_12_02.Rmd
377 lines (306 loc) · 12.5 KB
/
2022_12_02.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
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
---
title: "STATCOM + Poverty Solutions"
author: "Interim report"
date: '2022-12-02'
output: html_document
---
```{=html}
<style>
body{
text-align: justify;
font-family: Helvetica;
}
body .main-container{
max-width: 90%;
}
hr{
height: 2px;
background-color: #000000;
border: none;
}
</style>
```
```{r setup, include=FALSE, message=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(pacman)
`%notin%` <- Negate(`%in%`)
p_load(
#--- Packages to Fit Models
MASS, logistf, survival, pROC, fastR2, broom.mixed, lmerTest,
#--- Packages to Produce Tables
table1, gtsummary, flextable, janitor, broom, officer, kableExtra, reactable,latex2exp,
#--- Packages to Produce Figures
ggsci, ggridges, ggthemes, ggforce, ggpubr, patchwork,
grid, gridExtra, plotly, survminer, viridis, ggridges,
hrbrthemes, stickylabeller,
#--- Packages for Data Retrieval & Pre-Processing
readxl, here, rdrop2, lubridate, zoo, tidyverse, purrr, data.table, stringr, haven, stringr
)
library(pacman)
`%notin%` <- Negate(`%in%`)
p_load(
#--- Packages to Fit Models
MASS, logistf, survival,
#--- Packages to Produce Tables
gtsummary, flextable, janitor, broom, officer, kableExtra, reactable,
#--- Packages to Produce Figures
crayon, ggsci, ggridges, ggthemes, ggforce, ggpubr, patchwork, grid, gridExtra, plotly, survminer, viridis, ggridges, hrbrthemes, stickylabeller, latex2exp, scales, glue,
#--- Packages for Data Retrieval & Pre-Processing
readxl, here, rdrop2, lubridate, zoo, tidyverse, purrr, data.table, stringr, tidyr, table1
)
y <- read_csv(file.path(here(), "data", "created_data", "2022_11_21.csv")) %>%
group_by(Customer) %>%
summarise(tid = unique(Customer), total = n(), late = sum(!is.na(lateFees))) %>%
select(tid, total, late) %>%
rename(n_total = "total",
n_late = "late")
covariates <- read_csv(file.path(here(), "data", "created_data", "2022_07_31.csv")) %>%
rename(age = "Age on effective date of action",
sex = "Sex",
n_hh = "Total number in household",
n_dep = "Number of dependents",
n_br = "Number of bedrooms in unit",
income = "Adjusted annual income: 8a minus 8x (if 8x is larger, put 0)",
rent = "Tenant rent: 10d minus 10en If positive or 0, put tenant rent If negative, credit tenant") %>%
select(c(tid, age, sex, n_hh, n_dep, n_br, income, rent))
## some weird duplication issue, dropping duplicates for now
covariates <- covariates %>%
filter(tid %notin%
(covariates %>% group_by(tid) %>% summarise(n = n()) %>% filter(n > 1) %>% pull(tid)))
buildings <- read_csv(file.path(here(), "data", "created_data", "2022_03_21.csv")) %>%
select(c(tid, name, late_duration, late_amount))
data <- covariates %>% inner_join(y) %>% inner_join(buildings) %>% rowwise() %>%
mutate(n_hh = case_when(n_hh > 3 ~ factor("> 3", levels = c("1", "2", "3", "> 3")),
TRUE ~ factor(n_hh, levels = c("1", "2", "3", "> 3"))),
n_dep = case_when(TRUE ~ factor(n_dep, levels = c("0", "1", "2"))),
n_br = case_when(n_br >= 3 ~ factor(">=3", levels = c("1", "2", ">=3")),
TRUE ~ factor(n_br, levels = c("1", "2", ">=3"))))
data <- data %>% drop_na() %>%
rowwise() %>%
mutate(prop = wilson.ci(x = n_late, n = n_total, conf.level = 0.50)[1]) %>%
mutate(prop = case_when(prop < 0 ~ 0,
prop > 1 ~ 1,
TRUE ~ prop))
```
# Table 1
```{r table1, echo = FALSE, warning=FALSE, message=FALSE, fig.align='center'}
label(data$sex) <- "Sex"
label(data$age) <- "Age"
units(data$age) <- "years"
label(data$income) <- "Income"
units(data$income) <- "$"
label(data$n_hh) <- "# in household"
label(data$n_dep) <- "# of dependents"
label(data$n_br) <- "# of bedrooms"
label(data$prop) <- "Proportion of late payment"
#label(melanoma2$ulcer) <- "Ulceration"
#label(melanoma2$thickness) <- "Thickness"
#units(melanoma2$thickness) <- "mm"
table1(~ age + sex + n_hh + n_dep + n_br + income + prop| name, data %>% drop_na(),
render.continuous = c(.="Mean (SD)",
.="Median (IQR)",
.="[Min, Max]"))
```
<hr>
# Covariates
## Descriptive plots of covariates
### Univariate: age, stratified by buildings.
```{r fig1, echo = FALSE, warning=FALSE, message=FALSE, fig.align='center', fig.height=10, fig.width=15}
data %>%
ggplot(aes(y = name, x = age, fill = name)) +
geom_violin(width=1.4) +
geom_boxplot(width=0.1, color="black") +
theme_bw() +
xlab("Age (years)") +
ylab("") +
theme(legend.position = "bottom",
legend.box="vertical", legend.margin=margin(),
axis.text.x = element_text(face = "bold", size = 18),
axis.text.y = element_text(face = "bold", size = 18),
plot.title = element_text(face = "bold", size = 18),
plot.subtitle = element_text(size = 14),
#axis.text = element_text(size = 12),
axis.title = element_text(face = "bold", size = 18),
legend.title = element_text(face = "bold", size = 18),
legend.text = element_text(size = 18)) +
labs(fill = "Building")
```
### Univariate: income, stratified by buildings.
```{r fig2, echo = FALSE, warning=FALSE, message=FALSE, fig.align='center', fig.height=10, fig.width=15}
data %>%
filter(income <= 30000) %>%
ggplot(aes(y = name, x = income, fill = name)) +
geom_violin(width=1.4) +
geom_boxplot(width=0.1, color="black") +
theme_bw() +
xlab("Annual income ($)") +
ylab("") +
theme(legend.position = "bottom",
legend.box="vertical", legend.margin=margin(),
axis.text.x = element_text(face = "bold", size = 18),
axis.text.y = element_text(face = "bold", size = 18),
plot.title = element_text(face = "bold", size = 18),
plot.subtitle = element_text(size = 14),
#axis.text = element_text(size = 12),
axis.title = element_text(face = "bold", size = 18),
legend.title = element_text(face = "bold", size = 18),
legend.text = element_text(size = 18)) +
labs(fill = "Building")
```
### Univariate: sex, stratified by buildings.
```{r fig3, echo = FALSE, warning=FALSE, message=FALSE, fig.align='center', fig.height=10, fig.width=15}
data %>%
group_by(name, sex) %>%
summarize(n = n()) %>%
ungroup() %>%
drop_na() %>%
ggplot(aes(fill = sex, x = name, y = n)) +
geom_bar(position="fill", stat="identity") +
theme_bw() +
ylab("Fraction") +
xlab("") +
theme(legend.position = "bottom",
legend.box="vertical", legend.margin=margin(),
axis.text.x = element_text(face = "bold", size = 18),
axis.text.y = element_text(face = "bold", size = 18),
plot.title = element_text(face = "bold", size = 18),
plot.subtitle = element_text(size = 14),
#axis.text = element_text(size = 12),
axis.title = element_text(face = "bold", size = 18),
legend.title = element_text(face = "bold", size = 18),
legend.text = element_text(size = 18)) +
labs(fill = "Sex") +
coord_flip()
```
### Univariate: $#$ in household, stratified by buildings.
```{r fig4, echo = FALSE, warning=FALSE, message=FALSE, fig.align='center', fig.height=10, fig.width=15}
data %>%
group_by(name, n_hh) %>%
summarize(n = n()) %>%
ungroup() %>%
drop_na() %>%
ggplot(aes(fill = n_hh, x = name, y = n)) +
geom_bar(position="fill", stat="identity") +
theme_bw() +
ylab("Fraction") +
xlab("") +
theme(legend.position = "bottom",
legend.box="vertical", legend.margin=margin(),
axis.text.x = element_text(face = "bold", size = 18),
axis.text.y = element_text(face = "bold", size = 18),
plot.title = element_text(face = "bold", size = 18),
plot.subtitle = element_text(size = 14),
#axis.text = element_text(size = 12),
axis.title = element_text(face = "bold", size = 18),
legend.title = element_text(face = "bold", size = 18),
legend.text = element_text(size = 18)) +
labs(fill = "# in household") +
coord_flip()
```
### Univariate: $#$ of dependents, stratified by buildings.
```{r fig5, echo = FALSE, warning=FALSE, message=FALSE, fig.align='center', fig.height=10, fig.width=15}
data %>%
group_by(name, n_dep) %>%
summarize(n = n()) %>%
ungroup() %>%
drop_na() %>%
ggplot(aes(fill = n_dep, x = name, y = n)) +
geom_bar(position="fill", stat="identity") +
theme_bw() +
ylab("Fraction") +
xlab("") +
theme(legend.position = "bottom",
legend.box="vertical", legend.margin=margin(),
axis.text.x = element_text(face = "bold", size = 18),
axis.text.y = element_text(face = "bold", size = 18),
plot.title = element_text(face = "bold", size = 18),
plot.subtitle = element_text(size = 14),
#axis.text = element_text(size = 12),
axis.title = element_text(face = "bold", size = 18),
legend.title = element_text(face = "bold", size = 18),
legend.text = element_text(size = 18)) +
labs(fill = "# of dependents") +
coord_flip()
```
### Univariate: $#$ of bedrooms, stratified by buildings.
```{r fig6, echo = FALSE, warning=FALSE, message=FALSE, fig.align='center', fig.height=10, fig.width=15}
data %>%
group_by(name, n_br) %>%
summarize(n = n()) %>%
ungroup() %>%
drop_na() %>%
ggplot(aes(fill = n_br, x = name, y = n)) +
geom_bar(position="fill", stat="identity") +
theme_bw() +
ylab("Fraction") +
xlab("") +
theme(legend.position = "bottom",
legend.box="vertical", legend.margin=margin(),
axis.text.x = element_text(face = "bold", size = 18),
axis.text.y = element_text(face = "bold", size = 18),
plot.title = element_text(face = "bold", size = 18),
plot.subtitle = element_text(size = 14),
#axis.text = element_text(size = 12),
axis.title = element_text(face = "bold", size = 18),
legend.title = element_text(face = "bold", size = 18),
legend.text = element_text(size = 18)) +
labs(fill = "# of bedrooms") +
coord_flip()
```
<hr>
# Response of interest: proportion of lateness.
Instead of raw proportions, use [Wilson's CI](https://rpruim.github.io/fastR2/reference/wilson.ci.html).
## Descriptive plot of response
```{r fig7, echo = FALSE, warning=FALSE, message=FALSE, fig.align='center', fig.height=10, fig.width=15}
data %>%
ggplot(aes(y = name, x = prop, fill = name)) +
geom_violin(width=1.4) +
geom_boxplot(width=0.1, color="black") +
theme_bw() +
xlab("Proportion of lateness") +
ylab("") +
theme(legend.position = "bottom",
legend.box="vertical", legend.margin=margin(),
axis.text.x = element_text(face = "bold", size = 18),
axis.text.y = element_text(face = "bold", size = 18),
plot.title = element_text(face = "bold", size = 18),
plot.subtitle = element_text(size = 14),
#axis.text = element_text(size = 12),
axis.title = element_text(face = "bold", size = 18),
legend.title = element_text(face = "bold", size = 18),
legend.text = element_text(size = 18)) +
labs(fill = "Building")
```
<hr>
# Exploratory modeling
## Marginal, unadjusted models
### $Proportion \sim age$
```{r prop_age, echo=FALSE, messages=FALSE, warning=FALSE}
op <- tidy(lmer(prop ~ -1 + age + (1 | name), data = data))
op[-c(nrow(op)-1, nrow(op)), ] %>% kbl() %>% kable_paper("hover", full_width = T)
```
### $Proportion \sim sex$
```{r prop_sex, echo=FALSE, messages=FALSE, warning=FALSE}
op <- tidy(lmer(prop ~ sex + (1 | name), data = data))
op[-c(nrow(op)-1, nrow(op)), ] %>% kbl() %>% kable_paper("hover", full_width = T)
```
### $Proportion \sim num_{dep}$
```{r prop_numDep, echo=FALSE, messages=FALSE, warning=FALSE}
op <- tidy(lmer(prop ~ n_dep + (1 | name), data = data))
op[-c(nrow(op)-1, nrow(op)), ] %>% kbl() %>% kable_paper("hover", full_width = T)
```
### $Proportion \sim num_{BR}$
```{r prop_numBed, echo=FALSE, messages=FALSE, warning=FALSE}
op <- tidy(lmer(prop ~ n_br + (1 | name), data = data))
op[-c(nrow(op)-1, nrow(op)), ] %>% kbl() %>% kable_paper("hover", full_width = T)
```
### $Proportion \sim rent$
```{r prop_income, echo=FALSE, messages=FALSE, warning=FALSE}
op <- tidy(lmer(prop ~ rent + (1 | name), data = data))
op[-c(nrow(op)-1, nrow(op)), ] %>% kbl() %>% kable_paper("hover", full_width = T)
```
## Adjusted model
```{r prop_all, echo=FALSE, messages=FALSE, warning=FALSE}
op <- tidy(lmer(prop ~ age + sex + n_dep + rent + (1 | name), data = data))
op[-c(nrow(op)-1, nrow(op)), ] %>% kbl() %>% kable_paper("hover", full_width = T)
```