-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-sampling_comparison.R
199 lines (172 loc) · 6.96 KB
/
11-sampling_comparison.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
#'
#' OBJECTIVES:
#' - performance of survey comparing to expected (census 2017 and inei 2020)
#' write_xlsx("table/02-seroprev-supp-table02.xlsx")
#' ggsave("figure/33-seroprev-supp-figure01-a.png")
#' ggsave("figure/33-seroprev-supp-figure01-b.png")
#'
library(tidyverse)
library(magrittr)
theme_set(theme_bw())
# reunis inei -------------------------------------------------------------
# source:
# https://www.minsa.gob.pe/reunis/index.asp
# https://www.minsa.gob.pe/reunis/data/poblacion_estimada.asp
# https://cloud.minsa.gob.pe/s/XJ3NoG3WsxgF6H8
rute_reunis <- "data-raw/population/Poblacion Peru 2020 Dpto Prov Dist Final INEI-actualizado.xlsx"
data_reunis <- cdcper::read_reunis_edad_quinquenio(file = rute_reunis,
year = 2020)
etapas_reunis <- cdcper::read_reunis_edad(file = rute_reunis,
year = 2020)
popstr_reference <- data_reunis %>%
filter(provincia=="LIMA"|provincia=="CALLAO") %>%
group_by(ano,sex,age) %>%
summarise(ref_sum_value=sum(value)) %>%
ungroup() %>%
mutate(ref_pct_value=ref_sum_value/sum(ref_sum_value))
# count(sex,age) %>%
# avallecam::print_inf()
# for 13-epicurve
popstr_reference %>%
group_by(ano) %>%
summarise(across(starts_with("ref_"),sum)) %>%
write_rds("data/local_population.rds")
etapas_reunis %>%
filter(provincia=="LIMA"|provincia=="CALLAO") %>%
group_by(ano,age) %>%
summarise(ref_sum_value=sum(value)) %>%
ungroup() %>%
write_rds("data/local_population_age.rds")
# local sutdy -------------------------------------------------------------
uu_clean_data <- read_rds("data/uu_clean_data.rds")
popstr_study <- uu_clean_data %>%
mutate(edad_80=if_else(edad>=80,"[80,Inf)",as.character(edad_quinquenal_raw))) %>%
count(sexo,edad_quinquenal_raw,edad_80) %>%
group_by(sexo,edad_80) %>%
summarise(loc_sum_value=sum(n)) %>%
ungroup() %>%
mutate(loc_pct_value=loc_sum_value/sum(loc_sum_value)) %>%
mutate(sex=case_when(
sexo=="femenino"~"m",
sexo=="masculino"~"h"
)) %>%
mutate(age_end=str_replace(edad_80,"\\[(.+),(.+)\\)","\\2")) %>%
mutate(age_end=as.numeric(age_end)-1) %>%
mutate(age_ini=str_replace(edad_80,"\\[(.+),(.+)\\)","\\1_")) %>%
mutate(age_all=str_c(age_ini,age_end,"a")) %>%
mutate(age=case_when(
age_all=="0_0a" ~ "00_00a",
age_all=="1_4a" ~ "01_04a",
age_all=="5_9a" ~ "05_09a",
age_all=="80_Infa" ~ "80_nna",
TRUE~age_all
)) %>%
select(-edad_80,-age_end,-age_ini,-age_all,-sexo)
# count(edad_quinquenal_raw,edad_80) %>%
# count(sexo,edad_quinquenal_raw) %>%
# avallecam::print_inf()
# compare -----------------------------------------------------------------
popstr_reference %>% count(sex)
popstr_reference %>% count(age)
popstr_study %>% count(sex)
popstr_study %>% count(age)
data_to_pyramid <- popstr_reference %>%
left_join(popstr_study) %>%
mutate(sex_label=case_when(
# sex=="m"~"Mujer con PR",
# sex=="h"~"Hombre con PR"
sex=="m"~"Female",
sex=="h"~"Male"
)) %>%
mutate(diff=loc_pct_value-ref_pct_value) %>%
mutate(age_label=str_replace(age,"_","-")) %>%
mutate(age_label=str_replace(age_label,"a","")) %>%
mutate(age_label=str_replace(age_label,"nn","+")) %>%
mutate(age_label=str_replace(age_label,"\\-\\+","+")) %>%
mutate(age_label=str_replace(age_label,"00-00","<0"))
data_to_pyramid %>%
avallecam::print_inf()
# statistical test --------------------------------------------------------
data_to_pyramid %>%
# count(sex)
# filter(sex=="m") %>%
# filter(sex=="h") %>%
select(sex,ref_sum_value,loc_sum_value) %>%
group_by(sex) %>%
nest() %>%
mutate(data=map(.x = data,.f = as.matrix)) %>%
mutate(data=map(.x = data,.f = chisq.test)) %>%
mutate(result=map(.x = data,.f = broom::tidy)) %>%
unnest(result)
# pyramid plot ------------------------------------------------------------
library(ggpol)
aplot <- data_to_pyramid %>%
mutate(ref_pct_value=ref_pct_value*100,
loc_pct_value=loc_pct_value*100) %>%
mutate(ref_pct_value=if_else(sex=="m",-ref_pct_value,ref_pct_value),
loc_pct_value=if_else(sex=="m",-loc_pct_value,loc_pct_value)) %>%
mutate(sex=sex_label) %>%
mutate(age=age_label) %>%
# mutate(sex=fct_relevel(sex,"Mujer con PR")) %>%
mutate(sex=fct_relevel(sex)) %>%
ggplot() +
geom_bar(aes(age,loc_pct_value,fill=sex),stat = "identity") +
geom_bar(aes(age,ref_pct_value),color="black",alpha=0,stat = "identity") +
facet_share(~sex, dir = "h", scales = "free", reverse_num = TRUE) +
coord_flip() +
scale_y_continuous(breaks = scales::pretty_breaks(n = 5),
#labels = scales::percent_format(accuracy = 1)
) +
colorspace::scale_fill_discrete_qualitative(rev=T) +
labs(
# title = "Distribución de participantes por edad y sexo con Prueba Rápida (PR)",
# subtitle = "Comparación con población en Lima y Callao para el 2020 (Fuente: REUNIS 2020)",
# x="Edad (años)",y="Sujetos (%)",fill="Participantes"
title = "Distribution of participants against population by age and sex",
subtitle = "Reference: Lima* estimates (Source: REUNIS 2020)",
caption = "* including Lima and Callao provinces",
x="Age (years)",y="Participants (%)",
fill="Participants with\na serological test\nresult"
) #+
# scale_fill_manual(alapha)
# coord_flip()
# aplot
ggsave("figure/33-seroprev-supp-figure01-a.png",height = 5,width = 9,dpi = "retina")
# % difference plot -------------------------------------------------------
data_to_pyramid %>%
mutate(across(c(contains("_pct_"),diff), ~ .*100)) %>%
writexl::write_xlsx("table/02-seroprev-supp-table02.xlsx")
# summary results
data_to_pyramid %>%
filter(sex=="h") %>%
filter(magrittr::is_in(age,
c("25_29a","30_34a","35_39a","40_44a",
"45_49a","50_54a","55_59a"#,"60_64a"
))) %>%
summarise(across(diff,list(sum=sum,mean=mean,median=median)))
# ggplot
bplot <- data_to_pyramid %>%
mutate(sex=sex_label) %>%
mutate(age=age_label) %>%
ggplot(aes(age,diff,fill=sex)) +
geom_col(position = position_dodge()) +
geom_hline(aes(yintercept=0),lty=2) +
colorspace::scale_fill_discrete_qualitative(rev=T) +
scale_y_continuous(labels = scales::percent_format(),
breaks = scales::breaks_pretty(10)) +
coord_flip() +
labs(
# title = "Diferencia entre participantes y población por edad y sexo.",
# subtitle = "Población en Lima y Callao para el 2020 (Fuente: REUNIS 2020)",
# x="Edad (años)",
# y="Diferencia (%)\n % participantes - % poblacion",
# fill=""
title = "Difference between participants and population by age and sex",
subtitle = "Reference: Lima* estimates (Source: REUNIS 2020)",
x="Age (years)",
y="Difference (%)\n % participants - % population",
fill="Participants with\na serological test\nresult"
)
ggsave("figure/33-seroprev-supp-figure01-b.png",height = 5,width = 8,dpi = "retina")
# aplot
# bplot