-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathm_Section 16 (INLA)
205 lines (159 loc) · 7.88 KB
/
m_Section 16 (INLA)
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
######################## Install inla
###### Inlaのインストールについては下記も参照ください:
## https://www.r-inla.org/download-install
install.packages("INLA",repos=c(getOption("repos"),INLA="https://inla.r-inla-download.org/R/stable"), dep=TRUE)
### or install.packages("INLA",repos=c(getOption("repos"),INLA="https://inla.r-inla-download.org/R/testing"), dep=TRUE)
### For Windows
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install(c("graph", "Rgraphviz"), dep=TRUE)
########################
library(INLA)
names(inla.models()$likelihood)
names(inla.models()$latent)
inla.list.models()
####################################################
########### example 1: disease mapping #############
####################################################
#install.packages( c( "spdep", "CARBayesdata", "sf", "RColorBrewer", fmesher ) )
library(spdep);library(CARBayesdata);library(RColorBrewer);library(fmesher)
##### pollutionhealth data
data(pollutionhealthdata)
##### Adjacency matrix
data(GGHB.IZ)
W.nb <- poly2nb(GGHB.IZ)
W <- nb2mat(W.nb, style = "B")
#### Poisson regression
formula <- observed ~ offset(log(expected)) + jsa + price + pm10
res <- inla(formula=formula, data=pollutionhealthdata, family="poisson",
control.compute = list(dic = TRUE))
summary(res)
#### Poisson CAR model with group effects
pollutionhealthdata$ID <- as.integer(pollutionhealthdata$IZ) # zone ID
formula <- observed ~ pm10 + jsa + offset(log(expected)) +
f(ID, model = "besag", graph= W) + f(year, model="iid")
res <- inla(formula=formula, data=pollutionhealthdata, family="nbinomial",
control.compute = list(dic = TRUE))
summary(res)
#### Estimated group effects
res$summary.random[[1]][1:4,]
res$summary.random[[2]]
#### Predicted values
pred <-res$summary.fitted.values
pred[1:4,]
#### Plot the predicted value (posterior mean)
year <- 2011
dat2011 <- pollutionhealthdata[pollutionhealthdata$year==year,]
dat2011$pred<- pred$mean[pollutionhealthdata$year==year]
data(GGHB.IZ)
dat <- merge(x=GGHB.IZ, y=dat2011, by="IZ", all.x=FALSE, duplicateGeoms = TRUE)
dat2 <- st_as_sf(dat)
nc <- 11
breaks <- seq(10, 200, len=nc+1)
pal <- rev(brewer.pal(nc, "RdYlBu"))
plot(dat2[, c("observed", "pred")], pal=pal, breaks=breaks, axes=TRUE, lwd=0.1)
####################################################
########### example 2: spatial prediction ##########
####################################################
# install.packages( c("gstat", "sf") )
library(sf); library(sp)#library(gstat);
##### Meuse data
data(meuse)
data(meuse.grid)
y <- meuse[,c("zinc")]
x <- meuse[,c("dist","ffreq")]
coords <- as.matrix( meuse[,c("x","y")] )
x0 <- meuse.grid[,c("dist","ffreq")]
coords0 <- as.matrix( meuse.grid[,c("x","y")] )
##### Generate mesh
# This part is updated because use of fm_mesh_2d is now recommended
mesh1 <- fm_mesh_2d( loc=coords, max.edge = c(1000,1000))
mesh2 <- fm_mesh_2d( loc=coords, max.edge = c(300,1000))
mesh3 <- fm_mesh_2d( loc=coords, max.edge = c(300,300))
mesh4 <- fm_mesh_2d( loc=coords, max.edge = c(300,1000),cutoff=200)
mesh5 <- fm_mesh_2d( loc=coords, max.edge = c(300,1000),offset=c(-0.3,-0.1))
mesh6 <- fm_mesh_2d( loc=coords, max.edge = c(300,1000),offset=c(-0.1,-0.3))
### or
#mesh1 <- inla.mesh.2d( loc=coords, max.edge = c(1000,1000))
#mesh2 <- inla.mesh.2d( loc=coords, max.edge = c(300,1000))
#mesh3 <- inla.mesh.2d( loc=coords, max.edge = c(300,300))
#mesh4 <- inla.mesh.2d( loc=coords, max.edge = c(300,1000),cutoff=200)
#mesh5 <- inla.mesh.2d( loc=coords, max.edge = c(300,1000),offset=c(-0.3,-0.1))
#mesh6 <- inla.mesh.2d( loc=coords, max.edge = c(300,1000),offset=c(-0.1,-0.3))
plot(mesh1, asp=1);points(coords, col="red",pch=20,cex=0.7)
plot(mesh2, asp=1);points(coords, col="red",pch=20,cex=0.7)
plot(mesh3, asp=1);points(coords, col="red",pch=20,cex=0.7)
plot(mesh4, asp=1);points(coords, col="red",pch=20,cex=0.7)
plot(mesh5, asp=1);points(coords, col="red",pch=20,cex=0.7)
plot(mesh6, asp=1);points(coords, col="red",pch=20,cex=0.7)
##########################################################
##### Conventional geostatistical modeling ###############
##### Pre-processing
spde <- inla.spde2.matern(mesh=mesh2, alpha=0.5)
A <- inla.spde.make.A( mesh=mesh2, loc=coords )
stk <- inla.stack(data=list(y=y), A=list(1, A), tag="obs",
effects=list(data.frame(intercept=1, dist=x$dist, ffreq=x$ffreq), s=1:spde$n.spde))
A0 <- inla.spde.make.A(mesh=mesh2, loc=coords0)
stk0 <- inla.stack(data=list(y=NA), A=list(1, A0), tag="mis",
effects=list( data.frame(intercept=1, dist=x0$dist, ffreq=x0$ffreq),s=1:spde$n.spde))
stk.full<- inla.stack(stk, stk0)
stk.data<- inla.stack.data(stk.full)
stk.A <- inla.stack.A(stk.full)
##### Geostatistical modeling and prediction
mod <- inla( log(y) ~ 0 + intercept + dist + ffreq + f(s, model=spde), data=stk.data,
control.predictor=list( A=stk.A ), family="gaussian")
mis.ind <- inla.stack.index(stk.full, tag="mis")$data
pred <- mod$summary.fitted.values[mis.ind, "mean"]
pred_sd <- mod$summary.fitted.values[mis.ind, "sd"]
##### Plot the predicted values
meuse.grid_sf <- st_as_sf(meuse.grid, coords = c("x", "y"), crs = 28992)
meuse.grid_sf$pred <-pred
meuse.grid_sf$pred_sd<-pred_sd
plot(meuse.grid_sf[, "pred"], axes=TRUE, pch=15)
plot(meuse.grid_sf[, "pred_sd"], axes=TRUE, pch=15)
########################################################################
##### Geostatistical modeling considering physical barrier #############
##### [a] Constrained by the Meuse river
data(meuse.riv)
xlim <- c(min(meuse.grid[,1])-100, max(meuse.grid[,1])+100)
ylim <- c(min(meuse.grid[,2])-100, max(meuse.grid[,2])+100)
meuse.riv<- meuse.riv[meuse.riv[,1]>xlim[1] & meuse.riv[,1]<xlim[2] & meuse.riv[,2]>ylim[1] & meuse.riv[,2]<ylim[2],]
meuse.lst<- list(Polygons(list(Polygon(meuse.riv)),"meuse.riv"))
meuse.sr <- SpatialPolygons(meuse.lst)
segment <- inla.sp2segment(meuse.sr)
mesh_b1 <- fm_mesh_2d( loc=coords, max.edge = 300, interior =segment)
#or mesh_b1 <- inla.mesh.2d( loc=coords, max.edge = 300, interior =segment)
##### [b] Constrained by the study area
# This part is uodated because rgeos package is retired
meuse.grid_sp<-meuse.grid
coordinates(meuse.grid_sp) = c("x", "y")
gridded(meuse.grid_sp) <- TRUE
meuse.grid_sp <- as(meuse.grid_sp, "SpatialPolygons")
meuse.grid_sf <- st_as_sf(meuse.grid_sp)
meuse.area_sf <- st_union(meuse.grid_sf)
mesh_b2 <- fm_mesh_2d(loc = coords, max.edge = 300, boundary = fm_segm(meuse.area_sf))
##### Spatial prediction under [a] or [b] ##########
##### Pre-processing
mesh2 <-mesh_b2 ## or mesh_b2
plot(mesh2, asp=1);points(coords, col="red",pch=20,cex=0.7)
spde<- inla.spde2.matern(mesh=mesh2, alpha=1.5)
A <- inla.spde.make.A( mesh=mesh2, loc=coords)
stk <- inla.stack(data=list(y=y), A=list(1, A), tag="obs",
effects=list(data.frame(intercept=1,dist=x$dist, ffreq=x$ffreq), s=1:spde$n.spde))
A0 <- inla.spde.make.A(mesh=mesh2, loc=coords0)
stk0 <- inla.stack(data=list(y=NA), A=list(1, A0), tag="mis",
effects=list( data.frame(intercept=1,dist=x0$dist, ffreq=x0$ffreq), s=1:spde$n.spde))
stk.full <- inla.stack(stk, stk0)
stk.data <- inla.stack.data(stk.full)
stk.A <- inla.stack.A(stk.full)
##### Geostatistical modeling and prediction
mod <- inla( log(y) ~ 0 + intercept + dist + ffreq + f(s, model=spde), data=stk.data,
control.predictor=list( A=stk.A),family="gaussian")
mis.ind<- inla.stack.index(stk.full, tag="mis")$data
pred <- mod$summary.fitted.values[mis.ind, "mean"]
pred_sd<- mod$summary.fitted.values[mis.ind, "sd"]
meuse.grid_sf$pred2 <-pred
meuse.grid_sf$pred2_sd<-pred_sd
##### Plot the predicted values
plot(meuse.grid_sf[, "pred2"], axes=TRUE, pch=15,border=NA)
plot(meuse.grid_sf[, "pred2_sd"], axes=TRUE, pch=15,border=NA)