-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_assay_model_by_ZIP.R
182 lines (151 loc) · 6.04 KB
/
two_assay_model_by_ZIP.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
library(rstan)
library(dplyr)
library(ggplot2)
library(bayesplot)
library(simstudy)
library(readr)
library(lubridate)
library(tidyverse)
library(patchwork)
source("~/../Box/PHI_scaleit/SCALEIT_2.0/scripts/Stan_functions_v1.R")
## Read in data
dat_SCALEIT <- read_csv("~/../Box/PHI_scaleit/SCALEIT_2.0/reshaped_redux.csv")
## Look at the raw univariate results
dat_SCALEIT %>%
group_by(seropos_Roche) %>%
tally()
dat_SCALEIT %>%
group_by(seropos_Vitros) %>%
tally()
dat_SCALEIT %>%
group_by(seropos_Vitros,seropos_Roche) %>%
tally()
dat_SCALEIT %>%
group_by(age_groups) %>%
tally()
dat_SCALEIT %>%
## Remove NAs
filter(!is.na(seropos_Vitros) & !is.na(seropos_Roche)) %>%
group_by(age_groups) %>%
tally()
dat_SCALEIT %>%
group_by(DateCollected) %>%
tally()
## Separate the raw univariate data by zcta
dat_SCALEIT %>%
## Remove NAs
filter(!is.na(seropos_Vitros) & !is.na(seropos_Roche)) %>%
filter(!is.na(zcta)) %>%
group_by(zcta, seropos_Vitros, seropos_Roche) %>%
tally() %>%
ungroup() %>%
## Complete missing combos for Vitros & Roche by zcta
complete(seropos_Vitros, seropos_Roche, nesting(zcta), fill=list(n=0)) %>%
arrange(zcta, desc(seropos_Vitros), desc(seropos_Roche)) %>%
## Remove Roche positive, Vitros negative results
filter((seropos_Vitros==0 & seropos_Roche==1)==FALSE) %>%
select(zcta, seropos_Vitros, seropos_Roche, n) %>%
as.data.frame() -> dat_for_Stan
dat_for_Stan %>%
group_by(zcta) %>%
summarise(n=sum(n)) %>%
arrange(n)
## -----------------------------------------------------------
########################################
## Fit the model - without prior data ##
########################################
fit_stan_1pvacc <- stan(
model_code=model_stan_Ngroup_1pvacc,
data=list(
n_groups=length(unique(dat_for_Stan$zcta)), ## Which one?
A_plus_C=dat_for_Stan %>% filter(seropos_Vitros==1 & seropos_Roche==1) %>% select(n) %>% pull(),
B=dat_for_Stan %>% filter(seropos_Vitros==1 & seropos_Roche==0) %>% select(n) %>% pull(),
D=dat_for_Stan %>% filter(seropos_Vitros==0 & seropos_Roche==0) %>% select(n) %>% pull()))
#save(dat_for_Stan, fit_stan_1pvacc, file="~/../Box/PHI_scaleit/SCALEIT_2.0/bivariate_Stan_results_by_ZIP.RData")
## Load in Stan output
load("~/../Box/PHI_scaleit/SCALEIT_2.0/bivariate_Stan_results_by_ZIP.RData")
## Look at 2D
summary(fit_stan_1pvacc)$summary %>%
as.data.frame() %>%
rownames_to_column(var="parameter") %>%
filter(str_starts(parameter, "p_inf") | str_starts(parameter, "p_vacc\\[")) %>%
separate(col=parameter, into=c("tmp","parameter","zcta")) %>%
select(-tmp) %>%
mutate(parameter=paste0("p_", parameter)) %>%
mutate(zcta = rep(unique(dat_for_Stan$zcta), times=2)) %>%
as.data.frame() %>%
pivot_wider(names_from=parameter, values_from=mean:Rhat) %>%
mutate(n_samples = dat_for_Stan %>% group_by(zcta) %>% summarise(n=sum(n)) %>% select(n) %>% pull()) %>%
filter(n_samples >= 10) %>%
ggplot() +
geom_linerange(aes(x=mean_p_inf, y=mean_p_vacc, xmin=`2.5%_p_inf`, xmax=`97.5%_p_inf`, colour=factor(zcta)), size=0.05) +
geom_linerange(aes(x=mean_p_inf, y=mean_p_vacc, ymin=`2.5%_p_vacc`, ymax=`97.5%_p_vacc`, colour=factor(zcta)), size=0.05) +
geom_abline(linetype="dotted") +
geom_point(aes(x=mean_p_inf, y=mean_p_vacc, colour=factor(zcta), size=n_samples)) +
#ggrepel::geom_text_repel(aes(x=mean_p_inf, y=mean_p_vacc, label=zcta), min.segment.length=0, direction="both", force=2) +
theme_bw(base_size=14) +
xlab("Probability of infection") + ylab("Probability of vaccination")
#######################
## Put them on a map ##
#######################
library(ggplot2)
library(ggmap)
library(ggpubr)
library(maps)
library(mapproj)
library(mapdata)
library(sf)
library(tidyverse)
library(geojsonio)
library(broom)
library(viridis)
spdf <- geojson_read("~/../Box/PHI_scaleit/SCALEIT_2.0/Bay Area ZIP Codes.geojson", what = "sp")
spdf<-spdf[substr(spdf$zip, 1,3) %in% c("941") , ]
spdf<-spdf[spdf$zip != "94128" , ]
spdf_fortified <- tidy(spdf, region = "zip")
spdf_fortified$id<-as.numeric(spdf_fortified$id)
## Clean modelled output
summary(fit_stan_1pvacc)$summary %>%
as.data.frame() %>%
rownames_to_column(var="parameter") %>%
filter(str_starts(parameter, "p_inf") | str_starts(parameter, "p_vacc\\[")) %>%
separate(col=parameter, into=c("tmp","parameter","zcta")) %>%
select(-tmp) %>%
mutate(parameter=paste0("p_", parameter)) %>%
mutate(zcta = rep(unique(dat_for_Stan$zcta), times=2)) %>%
as.data.frame() %>%
pivot_wider(names_from=parameter, values_from=mean:Rhat) %>%
mutate(n_samples = dat_for_Stan %>% group_by(zcta) %>% summarise(n=sum(n)) %>% select(n) %>% pull()) %>%
mutate(`50%_p_inf` = ifelse(n_samples < 10, NA, `50%_p_inf`)) %>%
mutate(`50%_p_vacc` = ifelse(n_samples < 10, NA, `50%_p_vacc`)) %>%
as.data.frame() -> zcta_modelled
spdf_fortified = spdf_fortified %>%
left_join(. , as.data.frame(zcta_modelled), by=c("id"="zcta"))
ggplot() +
geom_polygon(data = spdf_fortified, aes(fill = `50%_p_inf`, x = long, y = lat, group = group)) +
theme_void() +
coord_map()+
#theme(legend.position = "none")+
labs_pubr()+
rremove("axis.text")+
rremove("xlab")+
rremove("ylab")+
scale_fill_distiller(name="value", palette ="RdBu")+
ggtitle("a. Probability of prior infection") -> plot_inf
ggplot() +
geom_polygon(data = spdf_fortified, aes(fill = `50%_p_vacc`, x = long, y = lat, group = group)) +
theme_void() +
coord_map()+
#theme(legend.position = "none")+
labs_pubr()+
rremove("axis.text")+
rremove("xlab")+
rremove("ylab")+
scale_fill_distiller(name="value", palette = "BrBG")+
ggtitle("b. Probability of vaccination") -> plot_vacc
ggarrange(plot_inf, plot_vacc,
#labels = c("a", "b"),
ncol = 2, nrow = 1, align="hv")
ggsave("~/../Box/PHI_scaleit/SCALEIT_2.0/modeled_map.tiff", units="in", width=9, height=4, dpi=300, compression = 'lzw')
ggsave("~/../Box/PHI_scaleit/SCALEIT_2.0/modeled_map.png", units="in", width=9, height=4, dpi=300)
#writexl::write_xlsx(as.data.frame(zcta_modelled) %>% filter(n_samples >= 10), path="~/../Box/PHI_scaleit/SCALEIT_2.0/bivariate_ZIP_estimates.xlsx")