-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsecond_stage.Rmd
546 lines (394 loc) · 21.5 KB
/
second_stage.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
538
539
540
541
542
543
544
545
---
title: "Second Stage--Community factors associated with local RSV epidemic timing: a spatiotemporal modeling study"
author: "ZHE ZHENG"
date: "6/8/2020"
output: html_document
---
```{r}
# INITIALISE TO LOCAL CONTEXT
rm(list=ls())
setwd("~/OneDrive - Yale University/RSV and Influenza/CAR/20000 iter/data") # set work directory
# READ DATA from the first stage
#NJ data
njtransmean<- readRDS("transnj.rds") # phase estimates from the first stage
varnj<- readRDS("varnj.rds") # estimated variations from the first stage
wj<- readRDS("wj.rds") # W matrix for spatial proximity model
njcommutematrix <- readRDS("njcommutematrix .rds") # W matrix for commuting model
NJcovariate <- readRDS("NJcovariate.rds") # demographic information, including normalized average family size, population density, population size, median income and school districts information
#NY data
nytransmean<- readRDS("transny.rds")
varny<- readRDS("varny.rds")
wy<- readRDS("wy.rds")
nycommutematrix <- readRDS("nycommutematrix .rds")
NYcovariate <- readRDS("NYcovariate.rds")
#CT data
cttransmean<- readRDS("transct.rds")
varct<- readRDS("varct.rds")
wc<- readRDS("wc.rds")
CTcovariate <- readRDS("CTcovariate.rds")
ctcommutematrix <- readRDS("ctcommutematrix .rds")
```
```{r}
#################################################################
#No Spatial Correlation Model
#################################################################
## calculate DIC scores
random.dic <- function(trans,nu2,n.zip,housesize,popden,popsize,income,district,N.district){
library(rjags) # LOAD LIBRARIES USED
library(coda)
model_string<-"
model{
for(i in 1:n.zip){
trans[i] ~ dnorm (tru[i], nu2[i])
# The first stage estimates reflect a true mean and observational errors
tru[i] ~ dnorm(dmean[i], tau[i])
# The true mean depends on fixed effects (socio-demographic factors) and spatial random effects
tau[i]~ dgamma(0.01, 0.01)
# This model assumes no spatial correlation
}
for(i in 1:n.zip){
dmean[i]<-beta0+beta1*housesize[i]+beta2*popden[i]+beta3*popsize[i]+beta4*income[i]+re[district[i]]}
# this equation accounts for household size, population density, population size, SES and school districts
beta0 ~ dnorm (0, 0.0001)
beta1 ~ dnorm (0, 0.0001)
beta2 ~ dnorm (0, 0.0001)
beta3 ~ dnorm (0, 0.0001)
beta4 ~ dnorm (0, 0.0001)
for(q in 1:N.district){
re[q] ~ dnorm (0, tau4)
}
tau4~ dgamma(0.01, 0.01)
}"
dataset <- list('trans' = trans,"nu2"=nu2,'housesize'=housesize,"popden"=popden,"popsize"=popsize,"income"=income,'district'=district, 'N.district'=max(district),n.zip=n.zip) # read in corresponding dataset
whole <- jags.model(textConnection(model_string),
data=dataset,
n.chains=2) # set two chains for convergence diagnostic and dic calculation
update(whole,
n.iter=5000) # burn-in iterations
wholeresult<-coda.samples(whole, variable.names=c("dmean","beta0","beta1","beta2","beta3","beta4","tru","re","tau4"),
thin=10,
n.iter=20000) # acquire posterior samples
random.dic <- dic.samples(whole,n.iter=20000,thin=10,type="pD") # calculate dic
return(list(random.dic))
}
random.dic(trans=njtransmean$trans.nj,nu2=1/varnj$var.nj,n.zip=length(njtransmean$var.nj),housesize = NJcovariate$scalehs,popden = NJcovariate$scalepopden,popsize = NJcovariate$scalepopsize,income=NJcovariate$scaleincome,district = NJcovariate$district) # New Jersey estimate
random.dic(trans=nytransmean$trans.ny,nu2=1/varny$var.ny,n.zip=length(nytransmean$var.ny),housesize = NYcovariate$scalehs,popden = NYcovariate$scalepopden,popsize = NYcovariate$scalepopsize,income=NYcovariate$scaleincome,district = NYcovariate$district) # New York estimate
random.dic(trans=cttransmean$trans.ct,nu2=1/varct$var.ct,n.zip=length(cttransmean$trans.ct),housesize = CTcovariate$scalehs,popden = CTcovariate$scalepopden,popsize = CTcovariate$scalepopsize,income=CTcovariate$scaleincome,district = CTcovariate$district) # Connecticut estimate
## save results
randomwhole <- function(trans,nu2,n.zip,housesize,popden,popsize,income,district,N.district){
library(rjags) # LOAD LIBRARIES USED
library(coda)
model_string<-"
model{
for(i in 1:n.zip){
trans[i] ~ dnorm (tru[i], nu2[i])
# The first stage estimates reflect a true mean and observational errors
tru[i] ~ dnorm(dmean[i], tau[i])
# The true mean depends on fixed effects (socio-demographic factors) and spatial random effects
tau[i]~ dgamma(0.01, 0.01)
# This model assumes no spatial correlation
}
for(i in 1:n.zip){
dmean[i]<-beta0+beta1*housesize[i]+beta2*popden[i]+beta3*popsize[i]+beta4*income[i]+re[district[i]]}
# this equation accounts for household size, population density, population size, SES and school districts
beta0 ~ dnorm (0, 0.0001)
beta1 ~ dnorm (0, 0.0001)
beta2 ~ dnorm (0, 0.0001)
beta3 ~ dnorm (0, 0.0001)
beta4 ~ dnorm (0, 0.0001)
for(q in 1:N.district){
re[q] ~ dnorm (0, tau4)
}
tau4~ dgamma(0.01, 0.01)
}"
dataset <- list('trans' = trans,"nu2"=nu2,'housesize'=housesize,"popden"=popden,"popsize"=popsize,"income"=income,'district'=district, 'N.district'=max(district),n.zip=n.zip) # read in corresponding dataset
whole <- jags.model(textConnection(model_string),
data=dataset,
n.chains=2) # set two chains for convergence diagnostic and dic calculation
update(whole,
n.iter=5000) # burn-in iterations
wholeresult<-coda.samples(whole, variable.names=c("dmean","beta0","beta1","beta2","beta3","beta4","tru","re","tau4"),
thin=10,
n.iter=20000) # acquire posterior samples
randomresult <<- as.data.frame(as.matrix(wholeresult)) }
randomwhole(trans=njtransmean$trans.nj,nu2=1/varnj$var.nj,n.zip=length(njtransmean$var.nj),housesize = NJcovariate$scalehs,popden = NJcovariate$scalepopden,popsize = NJcovariate$scalepopsize,income=NJcovariate$scaleincome,district = NJcovariate$district) # New Jersey estimate
randomwhole(trans=nytransmean$trans.ny,nu2=1/varny$var.ny,n.zip=length(nytransmean$var.ny),housesize = NYcovariate$scalehs,popden = NYcovariate$scalepopden,popsize = NYcovariate$scalepopsize,income=NYcovariate$scaleincome,district = NYcovariate$district) # New York estimate
randomwhole(trans=cttransmean$trans.ct,nu2=1/varct$var.ct,n.zip=length(cttransmean$trans.ct),housesize = CTcovariate$scalehs,popden = CTcovariate$scalepopden,popsize = CTcovariate$scalepopsize,income=CTcovariate$scaleincome,district = CTcovariate$district) # Connecticut estimate
```
```{r}
#################################################################
#Spatial Proximity Model
#################################################################
SP.dic <- function(trans,nu2,n.zip,housesize,popden,popsize,income,district,w.mat,Id.mat){
library(rjags)
library(coda)
model_string<-"
model{
for(i in 1:n.zip){
trans[i] ~ dnorm (tru[i], nu2[i])
}
tru[1:n.zip] ~ dmnorm(dmean, Leroux)
# this model assumes nearby locations have similar epidemic timing
for(i in 1:n.zip){
dmean[i]<-beta0+beta1*housesize[i]+beta2*popden[i]+beta3*popsize[i]+beta4*income[i]+re[district[i]]
}
beta0 ~ dnorm (0, 0.0001)
beta1 ~ dnorm (0, 0.0001)
beta2 ~ dnorm (0, 0.0001)
beta3 ~ dnorm (0, 0.0001)
beta4 ~ dnorm (0, 0.0001)
Leroux <- inv.tau*(rho*w.mat + (1 - rho)*Id.mat) # calculate spatial correlated random effects
inv.tau ~ dgamma(0.01, 0.01)
rho~ dunif(0,1)
for(q in 1:N.district){
re[q] ~ dnorm (0, tau4)
}
tau4~ dgamma(0.01, 0.01)
#############################################
#calculate variance
#############################################
randomef <- 1/inv.tau+1/tau4
#############################################
#Use to calculate percentage
#############################################
sch.percent <- (1/tau4)/(1/inv.tau+1/tau4)
sp.percent <- (1/inv.tau)/(1/inv.tau+1/tau4)
}"
dataset <- list('trans' = trans,"nu2"=nu2,'housesize'=housesize,"popden"=popden,"popsize"=popsize,"income"=income,'district'=district, 'N.district'=max(district),n.zip=n.zip,w.mat=w.mat,Id.mat=Id.mat)
whole <- jags.model(textConnection(model_string),
data=dataset,
n.chains=2)
update(whole,
n.iter=5000)
wholeresult<-coda.samples(whole, variable.names=c("beta1","beta0","beta2","tru","rho","beta3","beta4","dmean","sch.percent","sp.percent","randomef"),
thin=10,
n.iter=20000)
sp.dic <- dic.samples(whole,n.iter=20000,thin=10,type="pD")
return(sp.dic)
}
SP.dic(trans=njtransmean$trans.nj,nu2=1/varnj$var.nj,n.zip=length(njtransmean$var.nj),housesize = NJcovariate$scalehs,popden = NJcovariate$scalepopden,popsize = NJcovariate$scalepopsize,income=NJcovariate$scaleincome,district = NJcovariate$district,w.mat=wj,Id.mat=diag(ncol(wj))) # New Jersey estimate
SP.dic(trans=cttransmean$trans.ct,nu2=1/varct$var.ct,n.zip=length(cttransmean$trans.ct),housesize = CTcovariate$scalehs,popden = CTcovariate$scalepopden,popsize = CTcovariate$scalepopsize,income=CTcovariate$scaleincome,district = CTcovariate$district,w.mat=wc,Id.mat=diag(nrow(wc))) # Connecticut estimate
SP.dic(trans=nytransmean$trans.ny,nu2=1/varny$var.ny,n.zip=length(nytransmean$var.ny),housesize = NYcovariate$scalehs,popden = NYcovariate$scalepopden,popsize = NYcovariate$scalepopsize,income=NYcovariate$scaleincome,district = NYcovariate$district,w.mat=wy,Id.mat=diag(ncol(wy))) # New York estimate
SP <- function(trans,nu2,n.zip,housesize,popden,popsize,income,district,w.mat,Id.mat){
library(rjags)
library(coda)
model_string<-"
model{
for(i in 1:n.zip){
trans[i] ~ dnorm (tru[i], nu2[i])
}
tru[1:n.zip] ~ dmnorm(dmean, Leroux)
# this model assumes nearby locations have similar epidemic timing
for(i in 1:n.zip){
dmean[i]<-beta0+beta1*housesize[i]+beta2*popden[i]+beta3*popsize[i]+beta4*income[i]+re[district[i]]
}
beta0 ~ dnorm (0, 0.0001)
beta1 ~ dnorm (0, 0.0001)
beta2 ~ dnorm (0, 0.0001)
beta3 ~ dnorm (0, 0.0001)
beta4 ~ dnorm (0, 0.0001)
Leroux <- inv.tau*(rho*w.mat + (1 - rho)*Id.mat) # calculate spatial correlated random effects
inv.tau ~ dgamma(0.01, 0.01)
rho~ dunif(0,1)
for(q in 1:N.district){
re[q] ~ dnorm (0, tau4)
}
tau4~ dgamma(0.01, 0.01)
#############################################
#calculate variance
#############################################
randomef <- 1/inv.tau+1/tau4
#############################################
#Use to calculate percentage
#############################################
sch.percent <- (1/tau4)/(1/inv.tau+1/tau4)
sp.percent <- (1/inv.tau)/(1/inv.tau+1/tau4)
}"
dataset <- list('trans' = trans,"nu2"=nu2,'housesize'=housesize,"popden"=popden,"popsize"=popsize,"income"=income,'district'=district, 'N.district'=max(district),n.zip=n.zip,w.mat=w.mat,Id.mat=Id.mat)
whole <- jags.model(textConnection(model_string),
data=dataset,
n.chains=2)
update(whole,
n.iter=5000)
wholeresult<-coda.samples(whole, variable.names=c("beta1","beta0","beta2","tru","rho","beta3","beta4","dmean","sch.percent","sp.percent","randomef"),
thin=10,
n.iter=20000)
spresult <<- as.data.frame(as.matrix(wholeresult))
}
SP(trans=njtransmean$trans.nj,nu2=1/varnj$var.nj,n.zip=length(njtransmean$var.nj),housesize = NJcovariate$scalehs,popden = NJcovariate$scalepopden,popsize = NJcovariate$scalepopsize,income=NJcovariate$scaleincome,district = NJcovariate$district,w.mat=wj,Id.mat=diag(ncol(wj))) # New Jersey estimate
SP(trans=cttransmean$trans.ct,nu2=1/varct$var.ct,n.zip=length(cttransmean$trans.ct),housesize = CTcovariate$scalehs,popden = CTcovariate$scalepopden,popsize = CTcovariate$scalepopsize,income=CTcovariate$scaleincome,district = CTcovariate$district,w.mat=wc,Id.mat=diag(nrow(wc))) # Connecticut estimate
SP(trans=nytransmean$trans.ny,nu2=1/varny$var.ny,n.zip=length(nytransmean$var.ny),housesize = NYcovariate$scalehs,popden = NYcovariate$scalepopden,popsize = NYcovariate$scalepopsize,income=NYcovariate$scaleincome,district = NYcovariate$district,w.mat=wy,Id.mat=diag(ncol(wy))) # New York estimate
```
```{r}
#################################################################
#Commuting Flows Model
#################################################################
com.dic <- function(trans,nu2,n.zip,housesize,population,district,w.mat,Id.mat,N.district){
library(rjags)
library(coda)
model_string<-"
model{
for(i in 1:n.zip){
trans[i] ~ dnorm (tru[i], nu2[i])
}
tru[1:n.zip] ~ dmnorm(dmean, Leroux[1:n.zip,1:n.zip])
# this model assumes commuting networks help to seed RSV epidemics
for(i in 1:n.zip){
dmean[i]<-beta0+beta1*housesize[i]+beta2*popden[i]+beta3*popsize[i]+beta4*income[i]+re[district[i]]}
beta0 ~ dnorm (0, 0.0001)
beta1 ~ dnorm (0, 0.0001)
beta2 ~ dnorm (0, 0.0001)
beta3 ~ dnorm (0, 0.0001)
beta4 ~ dnorm (0, 0.0001)
Leroux <- inv.tau*(rho*w.mat + (1 - rho)*Id.mat)
inv.tau ~ dgamma(1.00, 0.01)
rho~ dunif(0,1)
for(q in 1:N.district){
re[q] ~ dnorm (0, tau4)
}
tau4~ dgamma(0.01, 0.01)
}"
dataset <- list('trans' = trans,"nu2"=nu2,'housesize'=housesize,"popden"=popden,"popsize"=popsize,"income"=income,'district'=district, 'N.district'=max(district),n.zip=n.zip,w.mat=w.mat,Id.mat=Id.mat)
whole <- jags.model(textConnection(model_string),
data=dataset,
n.chains=2)
update(whole,
n.iter=5000)
wholeresult<-coda.samples(whole, variable.names=c("beta1","beta0","beta2","tru","rho","re","beta3","beta4","dmean","SSE.ref","theta","sch.percent","sp.percent","randomef"),
thin=10,
n.iter=20000)
com.dic <- dic.samples(whole,n.iter=20000,thin=10,type="pD")
return(com.dic)
}
com.dic(trans=njtransmean$trans.nj,nu2=1/varnj$var.nj,n.zip=length(njtransmean$var.nj),housesize = NJcovariate$scalehs,popden = NJcovariate$scalepopden,popsize = NJcovariate$scalepopsize,income=NJcovariate$scaleincome,district = NJcovariate$district,w.mat=njcommutematrix,Id.mat=diag(ncol(njcommutematrix)))
com.dic(trans=nytransmean$trans.ny,nu2=1/varny$var.ny,n.zip=length(nytransmean$var.ny),housesize = NYcovariate$scalehs,popden = NYcovariate$scalepopden,popsize = NYcovariate$scalepopsize,income=NYcovariate$scaleincome,district = NYcovariate$district,w.mat=nycommutematrix,Id.mat=diag(ncol(nycommutematrix)))
com.dic(trans=cttransmean$trans.ct,nu2=1/varct$var.ct,n.zip=length(cttransmean$trans.ct),housesize = CTcovariate$scalehs,popden = CTcovariate$scalepopden,popsize = CTcovariate$scalepopsize,income=CTcovariate$scaleincome,district = CTcovariate$district,w.mat=ctcommutematrix,Id.mat=diag(ncol(ctcommutematrix)))
com <- function(trans,nu2,n.zip,housesize,population,district,w.mat,Id.mat,N.district){
library(rjags)
library(coda)
model_string<-"
model{
for(i in 1:n.zip){
trans[i] ~ dnorm (tru[i], nu2[i])
}
tru[1:n.zip] ~ dmnorm(dmean, Leroux[1:n.zip,1:n.zip])
# this model assumes commuting networks help to seed RSV epidemics
for(i in 1:n.zip){
dmean[i]<-beta0+beta1*housesize[i]+beta2*popden[i]+beta3*popsize[i]+beta4*income[i]+re[district[i]]}
beta0 ~ dnorm (0, 0.0001)
beta1 ~ dnorm (0, 0.0001)
beta2 ~ dnorm (0, 0.0001)
beta3 ~ dnorm (0, 0.0001)
beta4 ~ dnorm (0, 0.0001)
Leroux <- inv.tau*(rho*w.mat + (1 - rho)*Id.mat)
inv.tau ~ dgamma(1.00, 0.01)
rho~ dunif(0,1)
for(q in 1:N.district){
re[q] ~ dnorm (0, tau4)
}
tau4~ dgamma(0.01, 0.01)
}"
dataset <- list('trans' = trans,"nu2"=nu2,'housesize'=housesize,"popden"=popden,"popsize"=popsize,"income"=income,'district'=district, 'N.district'=max(district),n.zip=n.zip,w.mat=w.mat,Id.mat=Id.mat)
whole <- jags.model(textConnection(model_string),
data=dataset,
n.chains=2)
update(whole,
n.iter=5000)
wholeresult<-coda.samples(whole, variable.names=c("beta1","beta0","beta2","tru","rho","re","beta3","beta4","dmean","SSE.ref","theta","sch.percent","sp.percent","randomef"),
thin=10,
n.iter=20000)
comresult <<- as.data.frame(as.matrix(wholeresult))
}
com(trans=njtransmean$trans.nj,nu2=1/varnj$var.nj,n.zip=length(njtransmean$var.nj),housesize = NJcovariate$scalehs,popden = NJcovariate$scalepopden,popsize = NJcovariate$scalepopsize,income=NJcovariate$scaleincome,district = NJcovariate$district,w.mat=njcommutematrix,Id.mat=diag(ncol(njcommutematrix)))
com(trans=nytransmean$trans.ny,nu2=1/varny$var.ny,n.zip=length(nytransmean$var.ny),housesize = NYcovariate$scalehs,popden = NYcovariate$scalepopden,popsize = NYcovariate$scalepopsize,income=NYcovariate$scaleincome,district = NYcovariate$district,w.mat=nycommutematrix,Id.mat=diag(ncol(nycommutematrix)))
com(trans=cttransmean$trans.ct,nu2=1/varct$var.ct,n.zip=length(cttransmean$trans.ct),housesize = CTcovariate$scalehs,popden = CTcovariate$scalepopden,popsize = CTcovariate$scalepopsize,income=CTcovariate$scaleincome,district = CTcovariate$district,w.mat=ctcommutematrix,Id.mat=diag(ncol(ctcommutematrix)))
```
```{r}
#######################################################
#calculate the relative importance of covariates
#######################################################
dmean.nj <- spresult[,grep("dmean[",colnames(spresult),fixed = T)] # get posterior estimates
beta0 <- spresult[,grep("beta0[",colnames(spresult),fixed = T)]
beta1 <- spresult[,grep("beta1[",colnames(spresult),fixed = T)]
beta2 <- spresult[,grep("beta2[",colnames(spresult),fixed = T)]
beta3 <- spresult[,grep("beta3[",colnames(spresult),fixed = T)]
beta4 <- spresult[,grep("beta4[",colnames(spresult),fixed = T)]
betasq.hz <- c() #relative importance of family size
betasq.popden <- c()#relative importance of population density
betasq.popsize <- c()#relative importance of population size
betasq.income <- c()#relative importance of median income
for (i in 1:4000) {
betasq.hz[i] <- (beta1[i]*sd(NJcovariate$scalehs)/sd(dmean.nj[i,]))^2 #relative importance of family size
betasq.popden[i] <- (beta2[i]*sd(NJcovariate$scalepopden)/sd(dmean.nj[i,]))^2 #relative importance of population density
betasq.popsize[i] <- (beta3[i]*sd(NJcovariate$scalepopsize)/sd(dmean.nj[i,]))^2 #relative importance of population size
betasq.income[i] <- (beta4[i]*sd(NJcovariate$scaleincome)/sd(dmean.nj[i,]))^2 #relative importance of median income
}
```
```{r}
##########################################################################################
#calculate difference in epidemic timing between the top and bottom decile for variables
##########################################################################################
beta1.nj <- beta1/sd.nj.hs # convert back to unstandardized coefficients
hs.nj.90th <- quantile(NJcovariates$hs,0.9)-mean.nj.hs # get the top dencile of household size
hs.nj.10th <- quantile(NJcovariates$hs,0.1)-mean.nj.hs # get the bottom dencile of household size
beta0.nj <- beta0
monthnj90th.hs <- 12*exp(beta0.nj+beta1.nj*hs.nj.90th)/(1+exp(beta0.nj+beta1.nj*hs.nj.90th))
# epidemic timing estimates for the top dencile of household size
monthnj10th.hs <- 12*exp(beta0.nj+beta1.nj*hs.nj.10th)/(1+exp(beta0.nj+beta1.nj*hs.nj.10th))
# epidemic timing estimates for the bottom dencile of household size
mean(monthnj90th.hs-monthnj10th.hs) # the mean difference in epidemic timing
quantile(monthnj90th.hs-monthnj10th.hs,c(0.025,0.975)) # the 95% CrI of difference in epidemic timing
```
```{r}
#############################################
#calculate variance without fixed effects
#############################################
rv <- function(trans,nu2,n.zip,district,w.mat,Id.mat){
library(rjags)
library(coda)
model_string<-"
model{
for(i in 1:n.zip){
trans[i] ~ dnorm (tru[i], nu2[i])
}
tru[1:n.zip] ~ dmnorm(dmean, Leroux)
for(i in 1:n.zip){
dmean[i]<-beta0+re[district[i]]
phi_leroux[i] <- tru[i]-dmean[i]
theta[i] <- trans[i]-beta0-re[district[i]]
}
Leroux <- inv.tau*(rho*w.mat + (1 - rho)*Id.mat)
inv.tau ~ dgamma(0.01, 0.01)
rho~ dunif(0,1)
for(q in 1:N.district){
re[q] ~ dnorm (0, tau4)
}
tau4~ dgamma(0.01, 0.01)
#############################################
#calculate variance
#############################################
randomef <- 1/inv.tau+1/tau4
#############################################
#Use to calculate percentage
#############################################
sch.percent <- (1/tau4)/(1/inv.tau+1/tau4)
sp.percent <- (1/inv.tau)/(1/inv.tau+1/tau4)
}"
dataset <- list('trans' = trans,"nu2"=nu2,'district'=district, 'N.district'=max(district),n.zip=n.zip,w.mat=w.mat,Id.mat=Id.mat)
whole <- jags.model(textConnection(model_string),
data=dataset,
n.chains=2)
update(whole,
n.iter=5000)
wholeresult<-coda.samples(whole, variable.names=c("tru","rho","re","dmean","SSE.ref","theta","sch.percent","sp.percent","randomef"),
thin=10,
n.iter=20000)
rvresult <- as.data.frame(as.matrix(wholeresult))
return(rvresult)
}
rv(trans=njtransmean$trans.nj,nu2=1/varnj$var.nj,n.zip=length(njtransmean$var.nj),district = NJcovariate$district,w.mat=wj,Id.mat=diag(ncol(wj))) # New Jersey estimate
ranef.nj <- rvresult[,grep("randomef",colnames(rvresult),fixed = T)]
fixef.nj <- spresult[,grep("randomef",colnames(spresult),fixed = T)]
#calculate variance reduction
(mean(fixef.nj)-mean(ranef.nj))/mean(ranef.nj) # because it's reduction, this value is negative
```