-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
234 lines (210 loc) · 5.66 KB
/
README.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
---
output: github_document
always_allow_html: true
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
# COVID-19 U.S. County Model
Bayesian model of COVID-19 cases in U.S. counties.
***
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = FALSE,
message = FALSE,
warning = FALSE,
error = FALSE,
include = FALSE,
collapse = TRUE,
comment ="#>",
fig.retina = 2
)
library(tidyverse)
library(rstan)
library(tidybayes)
library(sf)
library(leaflet)
library(scales)
library(glue)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
```
## Data
The data is from the [COVID-19 Event Risk Planner](https://github.com/appliedbinf/covid19-event-risk-planner), which combines data from several sources including the [NYTimes COVID19 data project](https://github.com/nytimes/covid-19-data) and [U.S. Census](https://www.census.gov/data/tables/time-series/demo/popest/2010s-state-total.html).
It includes U.S. county-level COVID-19 data such as number of cases, deaths, and population.
```{r get-data}
covid_cases <-
read_csv(
file = "https://raw.githubusercontent.com/appliedbinf/covid19-event-risk-planner/master/usa_risk_counties.csv",
col_types = cols(
GEOID = col_integer(),
NAME = col_character(),
stname = col_character(),
cases = col_double(),
deaths = col_double(),
cases_past = col_double(),
X = col_double(),
pop = col_double(),
Nr = col_double(),
risk = col_double(),
asc_bias = col_double(),
event_size = col_double()
)
) %>%
arrange(GEOID) %>%
remove_missing(
na.rm = TRUE,
vars = c("GEOID", "cases", "pop")
) %>%
select(GEOID, NAME, stname, cases, pop) %>%
distinct()
counties <-
sf::read_sf(
dsn = "https://raw.githubusercontent.com/appliedbinf/covid19-event-risk-planner/master/COVID19-Event-Risk-Planner/map_data/tl_2017_us_county.geojson"
)
data <-
covid_cases %>%
left_join(counties)
```
## Stan Model
I fit a hierarchical binomial model for the counts of COVID-19 cases in each U.S. county.
The model treats each county as population members and uses partial pooling to estimate county-level COVID-19 cases.
Partial pooling means the county-level COVID-19 probabilities are modeled by a distribution.
This allows for information sharing among these parameters.
The Stan model is below:
```{r model}
model_code <- "
data {
int<lower=0> N; // counties
int<lower=0> y[N]; // cases
int<lower=0> K[N]; // populations
}
parameters {
real<lower=0, upper=1> phi; // population chance of covid
real<lower=1> kappa; // population concentration
vector<lower=0, upper=1>[N] theta; // chance of covid
}
model {
kappa ~ pareto(1, 1.5); // hyperprior
theta ~ beta(phi * kappa, (1 - phi) * kappa); // prior
y ~ binomial(K, theta); // likelihood
}
"
```
```{stan, echo = TRUE, include=TRUE, output.var="model"}
data {
int<lower=0> N; // counties
int<lower=0> y[N]; // cases
int<lower=0> K[N]; // populations
}
parameters {
real<lower=0, upper=1> phi; // population chance of covid
real<lower=1> kappa; // population concentration
vector<lower=0, upper=1>[N] theta; // chance of covid
}
model {
kappa ~ pareto(1, 1.5); // hyperprior
theta ~ beta(phi * kappa, (1 - phi) * kappa); // prior
y ~ binomial(K, theta); // likelihood
}
```
```{r fit, cache=TRUE}
fit <-
stan(
model_code = model_code,
data=list(
"N"=nrow(data),
"K"=data$pop,
"y"=data$cases
),
iter=10000,
chains=4
)
```
## Results
```{r post-processing}
predictions <-
fit %>%
recover_types() %>%
gather_draws(theta[i]) %>%
group_by(i) %>%
summarize(
posterior_median = median(.value),
) %>%
ungroup()
map_data <-
data %>%
bind_cols(predictions) %>%
mutate(
yhat = pop * posterior_median,
res = yhat - cases,
stdres = res/sd(res),
pct = (yhat - cases)/cases
) %>%
st_sf()
```
### COVID-19 Rate
```{r map-estimates, include=TRUE, screenshot.opts=list(zoom = 2)}
leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
setView(lat = 37.1, lng = -95.7, zoom = 4) %>%
addPolygons(
data = map_data,
color = "#444444",
weight = 0.2,
smoothFactor = 0.1,
opacity = 1.0,
fillOpacity = 0.7,
fillColor = ~colorNumeric(
palette = "viridis",
domain = map_data$posterior_median
)(posterior_median),
highlight = highlightOptions(weight = 1),
label = glue("{map_data$NAME}, {map_data$stname}\n{percent(map_data$posterior_median, accuracy = 0.1)}")
) %>%
addLegend(
data = map_data,
position = "bottomright",
pal = colorNumeric(
palette = "viridis",
domain = map_data$posterior_median
),
values = ~posterior_median,
title = "Rate",
opacity = .7,
labFormat = function(type, cuts, p) {
percent(cuts)
}
)
```
### Residuals
```{r map-residuals, include=TRUE, screenshot.opts=list(zoom = 2)}
leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
setView(lat = 37.1, lng = -95.7, zoom = 4) %>%
addPolygons(
data = map_data,
color = "#444444",
weight = 0.2,
smoothFactor = 0.1,
opacity = 1.0,
fillOpacity = 0.7,
fillColor = ~colorNumeric(
palette = "viridis",
domain = map_data$res
)(res),
highlight = highlightOptions(weight = 1),
label = glue("{map_data$NAME}, {map_data$stname}\n{percent(map_data$stdres, accuracy = 0.1)}")
) %>%
addLegend(
data = map_data,
position = "bottomright",
pal = colorNumeric(
palette = "viridis",
domain = map_data$res
),
values = ~res,
title = "Residuals",
opacity = .7
)
```