-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoraga-RJ-2018-036.R
170 lines (139 loc) · 4.63 KB
/
moraga-RJ-2018-036.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
####################################################################
# Small Area Disease Risk Estimation and Visualization using R
# by Paula Moraga
# (adapted to a more tidyverse-style by Andree Valle)
####################################################################
# The R package is not on CRAN because it uses some external C libraries that make difficult to build the binaries.
# Therefore, we need to install it adding the URL of the INLA repository:
#install.packages("INLA", repos = "https://inla.r-inla-download.org/R/stable", dep = TRUE)
library(tidyverse)
library(sf)
# Data ---------------------------------
library(SpatialEpi)
data(pennLC)
attributes(pennLC)
# Y: Observed cases ---------------------------------
# X: Smokers proportions ---------------------------------
#outcome at county level + covariates (race,sex,age_strata)
d <- pennLC$data %>%
as_tibble() %>%
group_by(county) %>%
summarise(Y=sum(cases),
pop=sum(population)) %>%
ungroup() %>%
#add exposure at county level
left_join(pennLC$smoking) %>%
rownames_to_column() %>%
#add polygon to tibble
left_join(pennLC$spatial.polygon %>%
st_as_sf() %>%
as_tibble() %>%
rownames_to_column())
# Expected cases (using stratas!) ---------------------------------
e <- pennLC$data %>%
as_tibble() %>%
select(county,cases,population,race,gender,age)
e %>% count(race,gender,age)
population <- e$population
cases <- e$cases
n.strata <- 16 # = 2 races * 2 genders * 4 age bands
E <- expected(population, cases, n.strata)
# SIR ---------------------------------
map <- d %>%
left_join(
e %>%
select(county) %>%
distinct() %>%
mutate(E=E)
) %>%
mutate(SIR=Y/E) %>%
# add data to map
st_as_sf() %>%
# transform to SpatialPolygonDataFrame
as('Spatial')
# Mapping SIR ---------------------------------
map %>%
st_as_sf() %>%
ggplot(aes(fill=SIR)) +
geom_sf() +
scale_fill_distiller(palette = "YlOrRd",direction = 1) +
#scale_fill_continuous(trans = 'reverse') +
guides(fill = guide_colorbar(reverse=F)) +
theme_bw() +
labs(title = "Standardize Incidence Ratio",
subtitle = "Political Map: Polygon boundaries for each county")
# alternative - cartogram
plot(map)
map_carto <- cartogram(map, "pop", itermax=5)
plot(map_carto)
map_carto %>%
st_as_sf() %>%
ggplot(aes(fill=SIR)) +
geom_sf() +
scale_fill_distiller(palette = "YlOrRd",direction = 1) +
#scale_fill_continuous(trans = 'reverse') +
guides(fill = guide_colorbar(reverse=F)) +
theme_bw() +
labs(title = "Standardize Incidence Ratio",
subtitle = "Cartogram: polygon area proportional to the population size")
# Neighbourhood matrix ---------------------------------
library(spdep)
library(INLA)
nb <- poly2nb(map)
head(nb)
nb2INLA("map.adj", nb)
g <- inla.read.graph(filename = "map.adj")
# Inference using INLA ---------------------------------
#index both random effects
map$re_u <- 1:nrow(map@data) #spatial residual variation
map$re_v <- 1:nrow(map@data) #modeling unstructure noise
#create formula
#iid: independent and identically distributed
formula <-
Y ~ # outcome
smoking + # exposure
f(re_u, model = "besag", graph = g) + #random effect - spatial
f(re_v, model = "iid") #random effect - noise
res <- inla(formula,
family = "poisson",
data = map@data,
E = E,
control.predictor = list(compute = TRUE))
# Results ---------------------------------
summary(res)
marginal <- inla.smarginal(res$marginals.fixed$smoking)
marginal <- data.frame(marginal)
ggplot(marginal, aes(x = x, y = y)) +
geom_line() +
geom_vline(xintercept = 0, col = "blue") +
labs(x = expression(beta[1]),
y = "Density") +
theme_bw()
head(res$summary.fitted.values)
map$RR <- res$summary.fitted.values[, "mean"]
map$LL <- res$summary.fitted.values[, "0.025quant"]
map$UL <- res$summary.fitted.values[, "0.975quant"]
# Mapping disease risk ---------------------------------
map %>%
st_as_sf() %>%
ggplot(aes(fill=RR)) +
geom_sf() +
scale_fill_distiller(palette = "YlOrRd",direction = 1) +
#scale_fill_continuous(trans = 'reverse') +
guides(fill = guide_colorbar(reverse=F)) +
theme_bw()
# alternative - cartogram
plot(map)
map_carto <- cartogram(map, "pop", itermax=5)
plot(map_carto)
map_carto %>%
st_as_sf() %>%
ggplot(aes(fill=RR)) +
geom_sf() +
scale_fill_distiller(palette = "YlOrRd",direction = 1) +
#scale_fill_continuous(trans = 'reverse') +
guides(fill = guide_colorbar(reverse=F)) +
theme_bw()
# Range of values of SIRs and RRs ---------------------------------
range(map@data$SIR)
range(map@data$RR)