-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEpiPackage-OBrien.Rmd
More file actions
251 lines (220 loc) · 7.38 KB
/
EpiPackage-OBrien.Rmd
File metadata and controls
251 lines (220 loc) · 7.38 KB
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
---
title: "EpiPackage-OBrien"
output: html_document
---
Helpful Links:
https://cran.r-project.org/web/packages/usmap/vignettes/mapping.html
Uploading Packages to Use
```{r}
library(epiR)
library(epitools)
library(incidence)
library(Epi)
library(epiDisplay)
library(epicalc)
library(devtools)
library(roxygen2)
library(leaflet)
library(maps)
library(AER)
library(prevalence)
```
Maps- use geom polygon (?)
```{r}
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m
mapStates <- map("state", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% addTiles() %>%
addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)
```
```{r}
nycounties <- geojsonio::geojson_read("json/nycounties.geojson",
what = "sp")
# Or use the rgdal equivalent:
nycounties <- rgdal::readOGR("json/nycounties.geojson", "OGRGeoJSON")
pal <- colorNumeric("viridis", NULL)
leaflet(nycounties) %>%
addTiles() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
fillColor = ~pal(log10(pop)),
label = ~paste0(county, ": ", formatC(pop, big.mark = ","))) %>%
addLegend(pal = pal, values = ~log10(pop), opacity = 1.0,
labFormat = labelFormat(transform = function(x) round(10^x)))
```
```{r}
library(jsonlite)
# From http://data.okfn.org/data/datasets/geo-boundaries-world-110m
geojson <- readLines("json/countries.geojson", warn = FALSE) %>%
paste(collapse = "\n") %>%
fromJSON(simplifyVector = FALSE)
# Default styles for all features
geojson$style = list(
weight = 1,
color = "#555555",
opacity = 1,
fillOpacity = 0.8
)
# Gather GDP estimate from all countries
gdp_md_est <- sapply(geojson$features, function(feat) {
feat$properties$gdp_md_est
})
# Gather population estimate from all countries
pop_est <- sapply(geojson$features, function(feat) {
max(1, feat$properties$pop_est)
})
# Color by per-capita GDP using quantiles
pal <- colorQuantile("Greens", gdp_md_est / pop_est)
# Add a properties$style list to each feature
geojson$features <- lapply(geojson$features, function(feat) {
feat$properties$style <- list(
fillColor = pal(
feat$properties$gdp_md_est / max(1, feat$properties$pop_est)
)
)
feat
})
# Add the now-styled GeoJSON object to the map
leaflet() %>% addGeoJSON(geojson)
```
Prevalence Function
```{r}
Rprev(Fatalities$fatal, Fatalities$pop)
datpack_prevalence <- function{data, existing_cases, population, state, time}{
data %>%
group_by(data$state,data$year) %>%
mutate(data["prevalence"] = data["existing_cases"]/ data["population"]) %>%
ggplot(data = states) +
geom_polygon(aes(x = long, y = lat, fill = region, group = group), color = "white") +
coord_fixed(1.3) +
guides(fill=prevalence)
}
# One Year apply across the state mcgrinr package tidyverse, r oxgen explaining argument merge and join and preprocessing function
#filter unique state , state refers to state column
map_prevalence <- function(data,existing_cases,population,state,year) {
data %>%
group_by(!! sym(state), !! sym(year)) %>%
mutate(prevalence = !! sym(existing_cases) / !!sym(population)) %>%
geom_polygon(aes(x = long, y = lat, fill = region, group = group), color = "white") +
coord_fixed(1.3) +
guides(fill=prevalence)
ditch_the_axes <- theme(
axis.text = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank()
)
states <- map_data("state")
all_states <- ggplot(data = states, mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = "gray")
all_states
all_states_fatal <- all_states +
geom_polygon(data = Fatalities, aes(fill = fatal), color = "white") +
geom_polygon(color = "black", fill = NA) +
theme_bw() +
ditch_the_axes
all_states_fatal
eb2 <- all_states_fatal +
scale_fill_gradientn(colours = rev(rainbow(7)),
breaks = c(2, 4, 10, 100, 1000, 10000),
trans = "log10")
eb2
}
map_prevalence(Fatalities,existing_cases= "fatal",population="pop",state="al",year="1982")
map_prevalence(Fatalities,existing_cases= "fatal",population="pop",state="state",year="year")
# Creating map
for (i in length(s)){
if (s = "california"){
plot_usmap(
data = d, values = "prevalence", include = c("CA"), lines = "red"
) +
scale_fill_continuous(
low = "white", high = "red", name = "Prevalence", label = scales::comma
) +
labs(title = "California", subtitle = "These are the states in the Pacific Timezone.") +
theme(legend.position = "right")
}
else {print "none"}
}
return(prevalence)
}
```
truePrev(x, n, SE = 1, SP = 1, prior = c(1, 1),
nchains = 2, burnin = 10000, update = 10000,
verbose = FALSE)
EXAMPLE CODE:
```{r}
pal <- colorNumeric("viridis", NULL)
leaflet(mapStates) %>%
addTiles() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
fillColor = ~pal(log10()),
label = ~paste0(county, ": ", formatC(pop, big.mark = ","))) %>%
addLegend(pal = pal, values = ~log10(pop), opacity = 1.0,
labFormat = labelFormat(transform = function(x) round(10^x)))
```
EXAMPLE CODE
```{r}
library(usmap)
library(ggplot2)
plot_usmap(include = c("CA", "ID", "NV", "OR", "WA")) +
labs(title = "Western US States", subtitle = "These are the states in the Pacific Timezone.")
plot_usmap(data = Fatalities, values = "mormon", lines = "red") +
scale_fill_continuous(name = "Mormon", label = scales::comma) +
theme(legend.position = "right")
plot_usmap(
data = Fatalities, values = "mormon", include = c("CA", "ID", "NV", "OR", "WA"), lines = "red"
) +
scale_fill_continuous(
low = "white", high = "red", name = "Mormon", label = scales::comma
) +
labs(title = "Western US States", subtitle = "These are the states in the Pacific Timezone.") +
theme(legend.position = "right")
```
Joining
```{r}
library(tidyverse)
library(fiftystater)
library(viridis)
tbl <- state.x77 %>%
as_tibble(rownames = "state") %>%
bind_cols(state_name = str_to_lower(state.abb)) %>%
rename(value_x = Income) %>%
select(state_name, value_x)
state_abbs <- tibble(state_full = str_to_lower(state.name), abb = str_to_lower(state.abb))
tbl_m <- left_join(tbl, state_abbs, by = c("state_name" = "abb")) %>%
rename(id = state_full)
us_map <- map_data("state")
Fatalities %>%
full_join(state_abbs, (by = "abb" = "state"))
tbl_df() %>%
mutate(state = rownames(votes.repub),
state = tolower(state)) %>%
right_join(us_map, by = c("state" = "region")) %>%
ggplot(aes(x = long, y = lat, group = group, fill = `fatal`)) +
geom_polygon(color = "black") +
theme_void() +
scale_fill_viridis(name = "Republican\fatal (%)")
ggplot(tbl_m) +
geom_map(map = states, aes(map_id = region, fill = value_x)) +
expand_limits(x = states$long, y = states$lat) +
coord_map() +
geom_text(
data = states %>%
group_by(region) %>%
summarise(lat = mean(c(max(lat), min(lat))),
long = mean(c(max(long), min(long)))) %>%
mutate(state = id) %>%
left_join(tbl_m, by = c("state" = "id")),
aes(x = long, y = lat, label = value_x )
) +
scale_fill_viridis() +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = NULL) +
labs(x = "", y = "") + theme(legend.position = "bottom",
panel.background = element_blank())
```