-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathage_gender_count.Rmd
199 lines (163 loc) · 5.44 KB
/
age_gender_count.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
---
title: "Age and gender count data"
output: pdf_document
---
# import libraries
```{r}
library(haven)
library(lubridate)
# library("readxl")
# library(xlsx)
library(dplyr)
library(tidyr)
library(ggplot2)
library(stringr)
# library(RColorBrewer)
library(ivreg)
library(broom)
library(generics)
library(here)
# prevent masking
rename = dplyr::rename
select = dplyr::select
summarise = dplyr::summarise
filter = dplyr::filter
```
# import scripts
```{r}
working_dir = here()
script_dir = here('scripts')
plot_dir = here("viz")
csv_dir = here('data')
# paths = c("fear_index.R","conditions_setting.R","data_import.R")
for( path in paths){
source(file.path(script_dir, path))
}
```
# import dataset and subsetting
```{r}
# get dataset
original <- import_stata(dir=csv_dir, up_to="2021")
# name changing
original = original %>%
rename(age=AdmissionAgeYearepisodeba)
# select columns
selected_columns = c('eventdate',"DateofRegisteredDeath","age","Sex_num","death","censor","old_home")
# subset dataset
selected = original %>% select(selected_columns)
```
# Impute filtering condition info, group by age and gender
```{r}
# age and gender filtering conditions
demo_conditions = c("age>= 65 & Sex_num == 1","age >= 65 & Sex_num == 0",
"age>=55 & age<65 & Sex_num==1","age>=55 & age<65 & Sex_num==0",
"age>=45 & age <55 & Sex_num==1","age>=45 & age <55 & Sex_num==0",
"age>=35 & age <45 & Sex_num==1","age>=35 & age <45 & Sex_num==0",
"age>=18 & age <35 & Sex_num==1","age>=18 & age <35 & Sex_num==0",
"age>=0 & age <18 & Sex_num==1","age>=0 & age <18 & Sex_num==0")
# age and gender filtering conditions in readable form
demo_conditionsTextual = c("65+ & M","65+ & F",
"55-64 & M","55-64 & F",
"45-54 & M","45-54 & F",
"35-44 & M","35-44 & F",
"18-34 & M","18-34 & F",
"0-17 & M","0-17 & F")
# concat strings in dplyr-readable form
conditions = paste(paste0(demo_conditions,"~\"",demo_conditionsTextual,"\";"),collapse ='')
selected = selected %>%
mutate(cond=case_when(
!!! rlang::parse_exprs(conditions)
))
```
# Count data table of age and gender attn
```{r}
attn.age.gender = selected %>%
group_by(cond) %>%
compare_192021() %>%
simplify_yearlycomparison()
attn.age.gender
```
# Count data table of age and gender death
```{r}
death.age.gender = selected %>%
filter(death==1) %>%
group_by(cond) %>%
compare_192021() %>%
simplify_yearlycomparison()
death.age.gender
```
# Count data of 65+ RCHE attn
```{r}
plus65.attn = selected %>%
filter(age>65,old_home==1) %>%
compare_192021() %>%
simplify_yearlycomparison()
plus65.attn
```
# Count data of 65+ RCHE death
```{r}
selected %>% filter(death==1) %>% count(age>65,old_home==1,Year=year(eventdate)) %>% pivot_wider(names_from = "Year", names_glue="n{Year}", values_from = "n") %>% mutate(percent_diff = signif((n2020/n2019-1)*100,3))
```
# Count data of 65+ RCHE among waves
```{r}
daily = selected %>% filter(age>65) %>% count(old_home,eventdate,death) %>%
pivot_wider(names_from=death,names_glue="death_{death}",values_from=n,values_fill=0) %>%
mutate(attn=death_0+death_1) %>%
rename(death=death_1) %>%
select(-death_0) %>%
# mutate year and t, number of calendar days since 1st Jan
mutate(Year=year(eventdate)-2019,
t=ifelse(year(eventdate)==2020,
eventdate-as.Date("2020-01-01"),
eventdate-as.Date("2019-01-01")),
time_lag = 0) %>%
filter(eventdate!=as.Date("2020-02-29")) %>%
drop_na()
# merge with wave settings
daily = daily %>% merge(x=., y=wavePandemicHkIndex.1920, by.x="eventdate", by.y="eventdate")
daily %>%
filter(waveFactor != "0") %>%
group_by(old_home,waveFactor,Year=year(eventdate)) %>%
summarise(attn=sum(attn)) %>%
pivot_wider(names_from = "Year", names_glue="n{Year}", values_from = "attn") %>%
mutate(attn_percent_diff = signif((n2020/n2019-1)*100,3))
```
# Count data of 65+ 28-day death by DOA/DBA among waves
```{r}
daily = original %>% filter(age>65) %>% count(doa = destination_num==4,eventdate,death) %>%
pivot_wider(names_from=death,names_glue="death_{death}",values_from=n,values_fill=0) %>%
mutate(attn=death_0+death_1) %>%
rename(death=death_1) %>%
select(-death_0) %>%
# mutate year and t, number of calendar days since 1st Jan
mutate(Year=year(eventdate),
t=ifelse(year(eventdate)==2020,
eventdate-as.Date("2020-01-01"),
eventdate-as.Date("2019-01-01")),
time_lag = 0) %>%
filter(eventdate!=as.Date("2020-02-29")) %>%
drop_na()
# merge with wave settings
daily = daily %>% merge(x=., y=wavePandemicHkIndex.1920, by.x="eventdate", by.y="eventdate")
# group by in-wave or out-of-wave periods
daily %>%
group_by(doa,During_wave = waveFactor %in% c("1","2","3","4"),Year) %>%
summarise(death=sum(death)) %>%
pivot_wider(names_from = "Year", names_glue="n{Year}", values_from = "death") %>%
mutate(death_diff = n2020-n2019,
death_percent_diff = signif((n2020/n2019-1)*100,3))
```
# Count data of 65+ DOA/DBA death YoY
```{r}
original %>%
filter(age>65,death==1) %>%
group_by(doa=destination_num==4) %>%
compare_192021() %>%
simplify_yearlycomparison()
```
# Count data of 65+ death YoY
```{r}
original %>% filter(age>65,death==1) %>%
compare_192021() %>%
simplify_yearlycomparison()
```