forked from Albert-L-Phan/EpilepsyAdmissions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEpilepsy.Rmd
537 lines (445 loc) · 20.8 KB
/
Epilepsy.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
---
title: "Epilepsy - GP and Obesity"
author: "Albert"
date: "27/06/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r data}
library(leaflet)
library(SpatialEpi)
library(spdep)
library(spatialreg)
library(ggplot2)
library(tmap)
library(sf)
library(dplyr)
library(MASS)
library(INLA)
load('~/EpilepsyAdmissions/mydata1.rda')
LGASz <- LGASz[LGASz$State=="VIC",]
LGASz <- LGASz[!st_is_empty(LGASz),]
LGASz$SzPublic_Number <- (LGASz$SzPublic/100000)*LGASz$Population
#LGASz<- na.omit(LGASz, cols = c("SzPublic_Number", "Population")) #c
LGASz <- LGASz[!is.na(LGASz$SzPublic),]
LGASz <- LGASz[!is.na(LGASz$Population),]
LGASz <- LGASz[!is.na(LGASz$GP100000),]
LGASz <- LGASz[!is.na(LGASz$Obese),]
rate<-sum(LGASz$SzPublic_Number)/sum(LGASz$Population) #c
LGASz$Expected<-with(LGASz, Population*rate )
LGASz$SMR<-with(LGASz, SzPublic_Number/Expected)
LGA.nb<-poly2nb(LGASz)
is.symmetric.nb(LGA.nb)
```
###GP
``` {r Spatial Regression GP}
tm_shape(LGASz) + tm_polygons(col='SMR',title='Epilepsy')
ggplot(data=LGASz,aes(x=GP100000,y=SzPublic)) + geom_point() + geom_smooth(method='lm',col='darkblue',fill='blue') + geom_smooth(method='rlm',col='darkred',fill='red')
LGASz$resids <- rlm(SzPublic_Number~GP100000,data=LGASz)$res
#This shows a map of the residuals
tm_shape(LGASz) + tm_polygons(col='resids',title='Residuals')+tm_style("col_blind")
#Creates spatial weights for neighbour lists
r.id<-attr(LGASz,"region.id")
lw <- nb2listw(LGA.nb,zero.policy = TRUE) #W=row standardised
#We use the Moran I test to see if the pattern of Epilepsy Admissions across Victoria is clustered or random.
gm<-moran.test(LGASz$SMR,listw = lw , na.action = na.omit, zero.policy = T)
gm
#local test of autocorrelation
lm<-localmoran(LGASz$SMR,listw = nb2listw(LGA.nb, zero.policy = TRUE, style = "C") , na.action = na.omit, zero.policy = T)
#Ordinary Least Squares. This takes into account the explanatory variable values of the LGA.
fit.ols<-lm(SMR~GP100000, data=LGASz, listw=lw,zero.policy=T, type="lag", method="spam")
summary(fit.ols)
#SAR - The Spatial Lag Model. This takes into account the number of Epilepsy Admissions of neighbours and the explanatory variable values of the LGA itself.
fit.lag<-lagsarlm(SMR~GP100000,data=LGASz, listw=lw,zero.policy=T, type="lag", method="spam")
summary(fit.lag, Nagelkerke=T)
#The Spatial Durbin Model. This takes into account the number of Epilepsy Admissions of neighbours and explanatory variable values of neighbours and the LGA itself.
fit.durb<-lagsarlm(SMR~GP100000,data=LGASz, listw=lw,zero.policy=T, type="mixed", method="spam")
summary(fit.durb, Nagelkerke=T)
#The Spatial Error Model. This takes into account explanatory variable values of the LGA itself, as well as the residuals of the neighbours.
fit.errdurb<-errorsarlm(SMR~GP100000, data=LGASz, listw=lw,zero.policy=T,etype="emixed", method="spam")
summary(fit.errdurb, Nagelkerke=T)
#The SAC or Manski Model. This takes into account the number of Epilepsy Admissions of neighbours, explanatory variable values of neighbours and the LGA itself, as well as the residuals of the neighbours.
fit.sac<-sacsarlm(SMR~GP100000,data=LGASz, listw=lw,zero.policy=T, type="sac", method = "MC")
summary(fit.sac, Nagelkerke=T)
```
``` {r INLA GP}
nb2INLA("LGA.graph", LGA.nb)
#This create a file called ``LDN-INLA.adj'' with the graph for INLA
LGA.adj <- paste(getwd(),"/LGA.graph",sep="")
#xpoisson model with no random latent effect-ideal baseline model
#Only Fixed Effects
m1<-inla(SzPublic_Number~ 1+GP100000, data=LGASz, family="xpoisson",
E=LGASz$Expected,control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE), verbose=TRUE)
R1<-summary(m1)
LGASz$ID <- 1:nrow(LGASz)
#Both Fixed Effects and Random Effects
m2<-inla(SzPublic_Number~ 1+ GP100000 +f(ID, model = "iid"), data=LGASz, family="xpoisson",
E=LGASz$Expected,control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE) )
R2<-summary(m2)
LGASz$FIXED.EFF <- m1$summary.fitted[, "mean"]
LGASz$IID.EFF <- m2$summary.fitted[, "mean"]
#plot regression on map
tFIXED<-tm_shape(LGASz)+tm_polygons("FIXED.EFF")
tIID<-tm_shape(LGASz)+tm_polygons("IID.EFF")
# Create sparse adjacency matrix
LGA.mat <- as(nb2mat(LGA.nb, style = "B",zero.policy = TRUE), "Matrix") #S=variance stabilise
# Fit model
#Takes into account neighbouring values.
m.icar <- inla(SzPublic_Number ~ 1+GP100000+
f(ID, model = "besag", graph = LGA.mat),
data = LGASz, E = LGASz$Expected, family ="xpoisson",
control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE))
R3<-summary(m.icar)
#Extension of Besag. Takes into account both neighbouring values as well as random effects
m.bym = inla(SzPublic_Number ~ -1+ GP100000+
f(ID, model = "bym", graph = LGA.mat),
data = LGASz, E = LGASz$Expected, family ="xpoisson",
control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE))
R4<-summary(m.bym)
ICARmatrix <- Diagonal(nrow(LGA.mat), apply(LGA.mat, 1, sum)) - LGA.mat
Cmatrix <- Diagonal(nrow(LGASz), 1) - ICARmatrix
max(eigen(Cmatrix)$values)
m.ler = inla(SzPublic_Number ~ -1+GP100000+
f(ID, model = "generic1", Cmatrix = Cmatrix),
data = LGASz, E = LGASz$Expected, family ="xpoisson",
control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE))
R5<-summary(m.ler)
#X
mmatrix <- model.matrix(SzPublic_Number ~ 1, LGASz)
#W
W <- as(nb2mat(LGA.nb, style = "W", zero.policy = TRUE), "Matrix")
#Q
Q.beta = Diagonal(n = ncol(mmatrix), x = 0.001)
#Range of rho
rho.min<- -1
rho.max<- 1
#Arguments for 'slm'
args.slm = list(
rho.min = rho.min ,
rho.max = rho.max,
W = W,
X = mmatrix,
Q.beta = Q.beta
)
#Prior on rho
hyper.slm = list(
prec = list(
prior = "loggamma", param = c(0.01, 0.01)),
rho = list(initial=0, prior = "logitbeta", param = c(1,1))
)
#Spatial Lag Model.
m.slm <- inla( SzPublic_Number ~ -1+GP100000+
f(ID, model = "slm", args.slm = args.slm, hyper = hyper.slm),
data = LGASz, family = "xpoisson",
E = LGASz$Expected,
control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE)
)
R6<-summary(m.slm)
marg.rho.internal <- m.slm$marginals.hyperpar[["Rho for ID"]]
marg.rho <- inla.tmarginal( function(x) {
rho.min + x * (rho.max - rho.min)
}, marg.rho.internal)
inla.zmarginal(marg.rho, FALSE)
plot(marg.rho, type = "l", main = "Spatial autocorrelation")
LGASz$ICAR <- m.icar$summary.fitted.values[, "mean"]
LGASz$BYM <- m.bym$summary.fitted.values[, "mean"]
LGASz$LEROUX <- m.ler$summary.fitted.values[, "mean"]
LGASz$SLM <- m.slm$summary.fitted.values[, "mean"]
labels<-c("Fixed","IID", "ICAR","BYM","LEROUX","SLM")
Marginal_Likelihood<-c(R1$mlik[1],R2$mlik[1],R3$mlik[1],R4$mlik[1],R5$mlik[1],R6$mlik[1])
Marginal_Likelihood<-round(Marginal_Likelihood,2)
WAIC<-c(R1$waic[[1]],R2$waic[[1]],R3$waic[[1]],R4$waic[[1]],R5$waic[[1]],R6$waic[[1]])
WAIC<-round(WAIC,2)
DIC<-c(R1$dic[[1]],R2$dic[[1]],R3$dic[[1]],R4$dic[[1]],R5$dic[[1]],R6$dic[[1]])
DIC<-round(DIC,2)
Results<-data.frame(labels,Marginal_Likelihood,WAIC,DIC)
knitr::kable(Results)
#plot maps
tICAR<-tm_shape(LGASz)+tm_polygons("ICAR")
tBYM<-tm_shape(LGASz)+tm_polygons("BYM")
tLEROUX<-tm_shape(LGASz)+tm_polygons("LEROUX")
tSLM<-tm_shape(LGASz)+tm_polygons("SLM")
#arrange in grid using tmap arrange
current.mode <- tmap_mode("plot")
tmap_arrange(tFIXED,tIID,tICAR,tBYM,tLEROUX,tSLM)
tmap_mode(current.mode)
```
###Obesity
``` {r Spatial Regression Obesity}
tm_shape(LGASz) + tm_polygons(col='SMR',title='Epilepsy')
ggplot(data=LGASz,aes(x=Obese,y=SzPublic)) + geom_point() + geom_smooth(method='lm',col='darkblue',fill='blue') + geom_smooth(method='rlm',col='darkred',fill='red')
LGASz$resids <- rlm(SzPublic_Number~Obese,data=LGASz)$res
#This shows a map of the residuals
tm_shape(LGASz) + tm_polygons(col='resids',title='Residuals')+tm_style("col_blind")
#Creates spatial weights for neighbour lists
r.id<-attr(LGASz,"region.id")
lw <- nb2listw(LGA.nb,zero.policy = TRUE) #W=row standardised
#We use the Moran I test to see if the pattern of Epilepsy Admissions across Victoria is clustered or random.
gm<-moran.test(LGASz$SMR,listw = lw , na.action = na.omit, zero.policy = T)
gm
#local test of autocorrelation
lm<-localmoran(LGASz$SMR,listw = nb2listw(LGA.nb, zero.policy = TRUE, style = "C") , na.action = na.omit, zero.policy = T)
#Ordinary Least Squares. This takes into account the explanatory variable values of the LGA.
fit.ols<-lm(SMR~Obese, data=LGASz, listw=lw,zero.policy=T, type="lag", method="spam")
summary(fit.ols)
#SAR - The Spatial Lag Model. This takes into account the number of Epilepsy Admissions of neighbours and the explanatory variable values of the LGA itself.
fit.lag<-lagsarlm(SMR~Obese,data=LGASz, listw=lw,zero.policy=T, type="lag", method="spam")
summary(fit.lag, Nagelkerke=T)
#The Spatial Durbin Model. This takes into account the number of Epilepsy Admissions of neighbours and explanatory variable values of neighbours and the LGA itself.
fit.durb<-lagsarlm(SMR~Obese,data=LGASz, listw=lw,zero.policy=T, type="mixed", method="spam")
summary(fit.durb, Nagelkerke=T)
#The Spatial Error Model. This takes into account explanatory variable values of the LGA itself, as well as the residuals of the neighbours.
fit.errdurb<-errorsarlm(SMR~Obese, data=LGASz, listw=lw,zero.policy=T,etype="emixed", method="spam")
summary(fit.errdurb, Nagelkerke=T)
#The SAC or Manski Model. This takes into account the number of Epilepsy Admissions of neighbours, explanatory variable values of neighbours and the LGA itself, as well as the residuals of the neighbours.
fit.sac<-sacsarlm(SMR~Obese,data=LGASz, listw=lw,zero.policy=T, type="sac", method = "MC")
summary(fit.sac, Nagelkerke=T)
```
``` {r INLA Obesity}
nb2INLA("LGA.graph", LGA.nb)
#This create a file called ``LDN-INLA.adj'' with the graph for INLA
LGA.adj <- paste(getwd(),"/LGA.graph",sep="")
#xpoisson model with no random latent effect-ideal baseline model
m1<-inla(SzPublic_Number~ 1+Obese, data=LGASz, family="xpoisson",
E=LGASz$Expected,control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE), verbose=TRUE)
R1<-summary(m1) #
LGASz$ID <- 1:nrow(LGASz)
m2<-inla(SzPublic_Number~ 1+ Obese +f(ID, model = "iid"), data=LGASz, family="xpoisson",
E=LGASz$Expected,control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE) )
R2<-summary(m2)
LGASz$FIXED.EFF <- m1$summary.fitted[, "mean"]
LGASz$IID.EFF <- m2$summary.fitted[, "mean"]
#plot regression on map
tFIXED<-tm_shape(LGASz)+tm_polygons("FIXED.EFF")
tIID<-tm_shape(LGASz)+tm_polygons("IID.EFF")
# Create sparse adjacency matrix
LGA.mat <- as(nb2mat(LGA.nb, style = "B",zero.policy = TRUE), "Matrix") #S=variance stabilise
# Fit model
m.icar <- inla(SzPublic_Number ~ 1+Obese+
f(ID, model = "besag", graph = LGA.mat),
data = LGASz, E = LGASz$Expected, family ="xpoisson",
control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE))
R3<-summary(m.icar)
m.bym = inla(SzPublic_Number ~ -1+ Obese+
f(ID, model = "bym", graph = LGA.mat),
data = LGASz, E = LGASz$Expected, family ="xpoisson",
control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE))
R4<-summary(m.bym)
ICARmatrix <- Diagonal(nrow(LGA.mat), apply(LGA.mat, 1, sum)) - LGA.mat
Cmatrix <- Diagonal(nrow(LGASz), 1) - ICARmatrix
max(eigen(Cmatrix)$values)
m.ler = inla(SzPublic_Number ~ -1+Obese+
f(ID, model = "generic1", Cmatrix = Cmatrix),
data = LGASz, E = LGASz$Expected, family ="xpoisson",
control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE))
R5<-summary(m.ler)
#X
mmatrix <- model.matrix(SzPublic_Number ~ 1, LGASz)
#W
W <- as(nb2mat(LGA.nb, style = "W", zero.policy = TRUE), "Matrix")
#Q
Q.beta = Diagonal(n = ncol(mmatrix), x = 0.001)
#Range of rho
rho.min<- -1
rho.max<- 1
#Arguments for 'slm'
args.slm = list(
rho.min = rho.min ,
rho.max = rho.max,
W = W,
X = mmatrix,
Q.beta = Q.beta
)
#Prior on rho
hyper.slm = list(
prec = list(
prior = "loggamma", param = c(0.01, 0.01)),
rho = list(initial=0, prior = "logitbeta", param = c(1,1))
)
#SLM model
m.slm <- inla( SzPublic_Number ~ -1+Obese+
f(ID, model = "slm", args.slm = args.slm, hyper = hyper.slm),
data = LGASz, family = "xpoisson",
E = LGASz$Expected,
control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE)
)
R6<-summary(m.slm)
marg.rho.internal <- m.slm$marginals.hyperpar[["Rho for ID"]]
marg.rho <- inla.tmarginal( function(x) {
rho.min + x * (rho.max - rho.min)
}, marg.rho.internal)
inla.zmarginal(marg.rho, FALSE)
plot(marg.rho, type = "l", main = "Spatial autocorrelation")
LGASz$ICAR <- m.icar$summary.fitted.values[, "mean"]
LGASz$BYM <- m.bym$summary.fitted.values[, "mean"]
LGASz$LEROUX <- m.ler$summary.fitted.values[, "mean"]
LGASz$SLM <- m.slm$summary.fitted.values[, "mean"]
labels<-c("Fixed","IID", "ICAR","BYM","LEROUX","SLM")
Marginal_Likelihood<-c(R1$mlik[1],R2$mlik[1],R3$mlik[1],R4$mlik[1],R5$mlik[1],R6$mlik[1])
Marginal_Likelihood<-round(Marginal_Likelihood,2)
WAIC<-c(R1$waic[[1]],R2$waic[[1]],R3$waic[[1]],R4$waic[[1]],R5$waic[[1]],R6$waic[[1]])
WAIC<-round(WAIC,2)
DIC<-c(R1$dic[[1]],R2$dic[[1]],R3$dic[[1]],R4$dic[[1]],R5$dic[[1]],R6$dic[[1]])
DIC<-round(DIC,2)
Results<-data.frame(labels,Marginal_Likelihood,WAIC,DIC)
knitr::kable(Results)
#plot maps
tICAR<-tm_shape(LGASz)+tm_polygons("ICAR")
tBYM<-tm_shape(LGASz)+tm_polygons("BYM")
tLEROUX<-tm_shape(LGASz)+tm_polygons("LEROUX")
tSLM<-tm_shape(LGASz)+tm_polygons("SLM")
#arrange in grid using tmap arrange
current.mode <- tmap_mode("plot")
tmap_arrange(tFIXED,tIID,tICAR,tBYM,tLEROUX,tSLM)
tmap_mode(current.mode)
```
###GP and Obesity
``` {r Spatial Regression GP and Obesity}
LGASz$resids <- rlm(SzPublic_Number~GP100000+Obese,data=LGASz)$res
#This shows a map of the residuals
tm_shape(LGASz) + tm_polygons(col='resids',title='Residuals')+tm_style("col_blind")
#Creates spatial weights for neighbour lists
r.id<-attr(LGASz,"region.id")
lw <- nb2listw(LGA.nb,zero.policy = TRUE) #W=row standardised
#We use the Moran I test to see if the pattern of Epilepsy Admissions across Victoria is clustered or random.
gm<-moran.test(LGASz$SMR,listw = lw , na.action = na.omit, zero.policy = T)
gm
#local test of autocorrelation
lm<-localmoran(LGASz$SMR,listw = nb2listw(LGA.nb, zero.policy = TRUE, style = "C") , na.action = na.omit, zero.policy = T)
#Ordinary Least Squares. This takes into account the explanatory variable values of the LGA.
fit.ols<-lm(SMR~GP100000+Obese, data=LGASz, listw=lw,zero.policy=T, type="lag", method="spam")
summary(fit.ols)
#SAR - The Spatial Lag Model. This takes into account the number of Epilepsy Admissions of neighbours and the explanatory variable values of the LGA itself.
fit.lag<-lagsarlm(SMR~GP100000+Obese,data=LGASz, listw=lw,zero.policy=T, type="lag", method="spam")
summary(fit.lag, Nagelkerke=T)
#The Spatial Durbin Model. This takes into account the number of Epilepsy Admissions of neighbours and explanatory variable values of neighbours and the LGA itself.
fit.durb<-lagsarlm(SMR~GP100000+Obese,data=LGASz, listw=lw,zero.policy=T, type="mixed", method="spam")
summary(fit.durb, Nagelkerke=T)
#The Spatial Error Model. This takes into account explanatory variable values of the LGA itself, as well as the residuals of the neighbours.
fit.errdurb<-errorsarlm(SMR~GP100000+Obese, data=LGASz, listw=lw,zero.policy=T,etype="emixed", method="spam")
summary(fit.errdurb, Nagelkerke=T)
#The SAC or Manski Model. This takes into account the number of Epilepsy Admissions of neighbours, explanatory variable values of neighbours and the LGA itself, as well as the residuals of the neighbours.
fit.sac<-sacsarlm(SMR~GP100000+Obese,data=LGASz, listw=lw,zero.policy=T, type="sac", method = "MC")
summary(fit.sac, Nagelkerke=T)
```
``` {r INLA GP and Obesity}
nb2INLA("LGA.graph", LGA.nb)
#This create a file called ``LDN-INLA.adj'' with the graph for INLA
LGA.adj <- paste(getwd(),"/LGA.graph",sep="")
#xpoisson model with no random latent effect-ideal baseline model
m1<-inla(SzPublic_Number~ 1+GP100000+Obese, data=LGASz, family="xpoisson",
E=LGASz$Expected,control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE), verbose=TRUE)
R1<-summary(m1) #
LGASz$ID <- 1:nrow(LGASz)
m2<-inla(SzPublic_Number~ 1+ GP100000+Obese +f(ID, model = "iid"), data=LGASz, family="xpoisson",
E=LGASz$Expected,control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE) )
R2<-summary(m2)
LGASz$FIXED.EFF <- m1$summary.fitted[, "mean"]
LGASz$IID.EFF <- m2$summary.fitted[, "mean"]
#plot regression on map
tFIXED<-tm_shape(LGASz)+tm_polygons("FIXED.EFF")
tIID<-tm_shape(LGASz)+tm_polygons("IID.EFF")
# Create sparse adjacency matrix
LGA.mat <- as(nb2mat(LGA.nb, style = "B",zero.policy = TRUE), "Matrix") #S=variance stabilise
# Fit model
m.icar <- inla(SzPublic_Number ~ 1+GP100000+Obese+
f(ID, model = "besag", graph = LGA.mat),
data = LGASz, E = LGASz$Expected, family ="xpoisson",
control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE))
R3<-summary(m.icar)
m.bym = inla(SzPublic_Number ~ -1+ GP100000+Obese+
f(ID, model = "bym", graph = LGA.mat),
data = LGASz, E = LGASz$Expected, family ="xpoisson",
control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE))
R4<-summary(m.bym)
ICARmatrix <- Diagonal(nrow(LGA.mat), apply(LGA.mat, 1, sum)) - LGA.mat
Cmatrix <- Diagonal(nrow(LGASz), 1) - ICARmatrix
max(eigen(Cmatrix)$values)
m.ler = inla(SzPublic_Number ~ -1+GP100000+Obese+
f(ID, model = "generic1", Cmatrix = Cmatrix),
data = LGASz, E = LGASz$Expected, family ="xpoisson",
control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE))
R5<-summary(m.ler)
#X
mmatrix <- model.matrix(SzPublic_Number ~ 1, LGASz)
#W
W <- as(nb2mat(LGA.nb, style = "W", zero.policy = TRUE), "Matrix")
#Q
Q.beta = Diagonal(n = ncol(mmatrix), x = 0.001)
#Range of rho
rho.min<- -1
rho.max<- 1
#Arguments for 'slm'
args.slm = list(
rho.min = rho.min ,
rho.max = rho.max,
W = W,
X = mmatrix,
Q.beta = Q.beta
)
#Prior on rho
hyper.slm = list(
prec = list(
prior = "loggamma", param = c(0.01, 0.01)),
rho = list(initial=0, prior = "logitbeta", param = c(1,1))
)
#SLM model
m.slm <- inla( SzPublic_Number ~ -1+GP100000+Obese+
f(ID, model = "slm", args.slm = args.slm, hyper = hyper.slm),
data = LGASz, family = "xpoisson",
E = LGASz$Expected,
control.predictor = list(compute = TRUE),
control.compute = list(dic = TRUE, waic = TRUE)
)
R6<-summary(m.slm)
marg.rho.internal <- m.slm$marginals.hyperpar[["Rho for ID"]]
marg.rho <- inla.tmarginal( function(x) {
rho.min + x * (rho.max - rho.min)
}, marg.rho.internal)
inla.zmarginal(marg.rho, FALSE)
plot(marg.rho, type = "l", main = "Spatial autocorrelation")
LGASz$ICAR <- m.icar$summary.fitted.values[, "mean"]
LGASz$BYM <- m.bym$summary.fitted.values[, "mean"]
LGASz$LEROUX <- m.ler$summary.fitted.values[, "mean"]
LGASz$SLM <- m.slm$summary.fitted.values[, "mean"]
labels<-c("Fixed","IID", "ICAR","BYM","LEROUX","SLM")
Marginal_Likelihood<-c(R1$mlik[1],R2$mlik[1],R3$mlik[1],R4$mlik[1],R5$mlik[1],R6$mlik[1])
Marginal_Likelihood<-round(Marginal_Likelihood,2)
WAIC<-c(R1$waic[[1]],R2$waic[[1]],R3$waic[[1]],R4$waic[[1]],R5$waic[[1]],R6$waic[[1]])
WAIC<-round(WAIC,2)
DIC<-c(R1$dic[[1]],R2$dic[[1]],R3$dic[[1]],R4$dic[[1]],R5$dic[[1]],R6$dic[[1]])
DIC<-round(DIC,2)
Results<-data.frame(labels,Marginal_Likelihood,WAIC,DIC)
knitr::kable(Results)
#plot maps
tICAR<-tm_shape(LGASz)+tm_polygons("ICAR")
tBYM<-tm_shape(LGASz)+tm_polygons("BYM")
tLEROUX<-tm_shape(LGASz)+tm_polygons("LEROUX")
tSLM<-tm_shape(LGASz)+tm_polygons("SLM")
#arrange in grid using tmap arrange
current.mode <- tmap_mode("plot")
tmap_arrange(tFIXED,tIID,tICAR,tBYM,tLEROUX,tSLM)
tmap_mode(current.mode)
```