Wednesday, July 10, 2013
Chapter 3
Bugs Code 3.1 Alternative
model;
{
for(i in 1:6){p1[i]<-m1[i]-y1[i]}
for(i in 1:6){p2[i]<-m2[i]-y2[i]}
# beta distributions for the thetas assuming improper prior
for(i in 1:6){ theta1[i]~dbeta(y1[i],p1[i])}
for(i in 1:6){ theta2[i]~dbeta(y2[i],p2[i])}
numds1<-theta1[1]*mr[1]+theta1[2]*mr[2]+theta1[3]*mr[3]+
theta1[4]*mr[4]+theta1[5]*mr[5]+theta1[6]*mr[6]
denoms1<-sum(mr[])
# directly standardized mortality for area 1
s1<-numds1/denoms1
numds2<-theta2[1]*mr[1]+theta2[2]*mr[2]+theta2[3]*mr[3]+
theta2[4]*mr[4]+theta2[5]*mr[5]+theta2[6]*mr[6]
denoms2<-sum(mr[])
#standardized mortality for area 2
s2<-numds2/denoms2
r<-s1/s2
d<-s1-s2
}
#deaths for area 2
list( y2=c(1987,1940,3993,9823,12092,5225),
# deaths for area 1
y1=c(3446,4137,10038,29511,31918,16332),
#population for are 1
m1=c(2531333,10686658,10500587,9288864,3645546,600968),
# population for area 2
m2=c(1073506,4668242,4720799,5079161,2825477,434125),
# population for reference standard
mr=c(20201362,84652193,82134554,81489445,34774551,5493433))
# initial values for the thetas
list( theta1=c(.5,.5,.5,.5,.5,.5),theta2=c(.5,.5,.5,.5,.5,.5))
Bugs Code 3.1
model;
{
for(i in 1:6){ y1[i]~dbin(theta1[i],m1[i])}
for(i in 1:6){ y2[i]~dbin(theta2[i],m2[i])}
# prior distribution for theta1 and theta2
for(i in 1:6){theta1[i]~dbeta(1,1)}
for(i in 1:6){theta2[i]~dbeta(1,1)}
numds1<-theta1[1]*mr[1]+theta1[2]*mr[2]+theta1[3]*mr[3]+
theta1[4]*mr[4]+theta1[5]*mr[5]+theta1[6]*mr[6]
denoms1<-sum(mr[])
# directly standardized mortality for area 1
s1<-numds1/denoms1
numds2<-theta2[1]*mr[1]+theta2[2]*mr[2]+theta2[3]*mr[3]+
theta2[4]*mr[4]+theta2[5]*mr[5]+theta2[6]*mr[6]
denoms2<-sum(mr[])
#standardized mortality for area 2
s2<-numds2/denoms2
r<-s1/s2
d<-s1-s2
}
#deaths for area 2
list( y2=c(1987,1940,3993,9823,12092,5225),
# deaths for area 1
y1=c(3446,4137,10038,29511,31918,16332),
#population for are 1
m1=c(2531333,10686658,10500587,9288864,3645546,600968),
# population for area 2
m2=c(1073506,4668242,4720799,5079161,2825477,434125),
# population for reference standard
mr=c(20201362,84652193,82134554,81489445,34774551,5493433))
# initial values for the thetas
list( theta1=c(.5,.5,.5,.5,.5,.5),theta2=c(.5,.5,.5,.5,.5,.5))
Bugs Code 3.2
model;
# estimated standardized rates based on the inverse of the variance of the difference
{
for(i in 1:6){ y1[i]~dbin(theta1[i],m1[i])}
for(i in 1:6){ y2[i]~dbin(theta2[i],m2[i])}
for(i in 1:6){nw1[i]<-y1[i]*(m1[i]-y1[i])/m1[i]*m1[i]*(m1[i]+1)}
for(i in 1:6){nw2[i]<-y2[i]*(m2[i]-y2[i])/m2[i]*m2[i]*(m2[i]+1)}
# the variance of the difference is the next statement
for( i in 1:6){v[i]<-nw1[i]+nw2[i]}
# below are the computed weights
for( i in 1:6){w[i]<-1/v[i]}
numds1<-theta1[1]*w[1]+theta1[2]*w[2]+theta1[3]*w[3]+
theta1[4]*w[4]+theta1[5]*w[5]+theta1[6]*w[6]
denoms1<-sum(w[])
# directly standardized mortality for area 1
s1<-numds1/denoms1
numds2<-theta2[1]*w[1]+theta2[2]*w[2]+theta2[3]*w[3]+
theta2[4]*w[4]+theta2[5]*w[5]+theta2[6]*w[6]
denoms2<-sum(w[])
#standardized mortality for area 2
s2<-numds2/denoms2
# uniform prior for thetas
for( i in 1:6){theta1[i]~dbeta(1,1)}
for( i in 1:6){theta2[i]~dbeta(1,1)}
r<-s1/s2
d<-s1-s2
}
#deaths for area 2
list(y2=c(1987,1940,3993,9823,12092,5225),
# deaths for area 1
y1=c(3446,4137,10038,29511,31918,16332),
#population for area 1
m1=c(2531333,10686658,10500587,9288864,3645546,600968),
# population for area 2
m2=c(1073506,4668242,4720799,5079161,2825477,434125))
# initial values for the thetas
list( theta1=c(.5,.5,.5,.5,.5,.5),theta2=c(.5,.5,.5,.5,.5,.5))
Bugs Code 3.3
model;
{
for (i in 1:6){yr[i]~dbin(thetar[i],mr[i])}
# prior distribution of the thetar
for (i in 1:6){thetar[i]~dbeta(.5,.5)}
y1~dbin(theta1,m1)
y2~dbin(theta2,m2)
ys~dbin(thetas, ms)
# prior distribution of theta1, theta2, and thetas
theta1~dbeta(.5,.5)
theta2~dbeta(.5,.5)
thetas~dbeta(.5,.5)
# numerator of SMR1
nums1<-theta1*m1
denoms1<- thetas*m1
# numerator for SMR2
nums2<-theta2*m2
denoms2<-thetas*m2
# crude rate for the standard
# Numerator of crude rate for standard
numst<-thetar[1]*mr[1]+thetar[2]*mr[2]+thetar[3]*mr[3]+thetar[4]*mr[4]+
thetar[5]*mr[5]+thetar[6]*mr[6]
CRST<-numst/ms
ISR1<-(nums1/denoms1)*CRST
ISR2<-(nums2/denoms2)*CRST
#difference in the indirectly standardized rates
disr<-ISR1-ISR2
SMR1<-nums1/denoms1
SMR2<-nums2/denoms2
# difference in the standard mortality rates
dsmr<-SMR1-SMR2
# ratio of the standard mortality rates
rsmr<-SMR1/SMR2
# ratio of the indirectly standardized rates
risr<-ISR1/ISR2
}
# data from Tables 3.1 and 3.2
list(mr=c(20201362,84652193,82134554,81489445,34774551,5493433),
yr=c(28869,34790,112178,493566,1031816,764582), m1=37253956, m2=18801310,y1=95383, y2=35060, ms=308745538,ys=2465801))
# initial values
list(theta1=.5,theta2=.5))
Bugs Code 3.4
model;
# posterior distribution of the odds ratio for two strata
{
# stratum 1
# assume improper prior (2.5)
#posterior distribution of thetapp1
thetapp1~dbeta(npp1,nmp1)
# posterior distribution of thetapm1
thetapm1~dbeta(npm1,nmm1)
# postrior distribution of thetamp1
thetamp1~dbeta(nmp1,npp1)
thetamm1~dbeta(nmm1,npm1)
# OR1 is the odds ratio for stratum 1
OR1<-(thetapp1*thetamm1)/(thetapm1*thetamp1)
# AR1 is the attributable risk for stratum 1
AR1<-(OR1-1)/OR1
# stratum 2
# assume improper prior (2.5)
#posterior distribution of thetapp2
thetapp2~dbeta(npp2,nmp2)
# posterior distribution of thetapm2
thetapm2~dbeta(npm2,nmm2)
# posterior distribution of thetamp2
thetamp2~dbeta(nmp2,npp2)
thetamm2~dbeta(nmm2,npm2)
# OR2 is the odds ratio for stratum 2
OR2<-(thetapp2*thetamm2)/(thetapm2*thetamp2)
# AR2 is the attributable risk
AR2<-(OR2-1)/OR2
DOR<-OR1-OR2
ROR<-OR1/OR2
RAR<-AR1/AR2
DAR<-AR1-AR2
}
#Israeli Heart Study Strata 1 and 2 list(npp1=9,nmp1=6,npm1=115,nmm1=73,npp2=20,nmp2=21,npm2=596,nmm2=1171)
# initial value of thetas
list(thetapp1=.5, thetapm1=.5, thetamp1=.5,thetamm1=.5,
thetapp2=.5, thetapm2=.5, thetamp2=.5,thetamm2=.5)
BUGS CODE 3.5
Model;
{
gpp~dgamma(npp,2)
gpq~dgamma(npq,2)
gqp~dgamma(nqp,2)
gqq~dgamma(nqq,2)
hpp~dgamma(mpp,2)
hpq~dgamma(mpq,2)
hqp~dgamma(mqp,2)
hqq~dgamma(mqq,2)
sgh<- gpp+gpq+gqp+gqq+hpp+hpq+hqp+hqq
# Dirichlet distribution for the thetas and phis
thetapp<-gpp/sgh
thetapq<-gpq/sgh
thetaqp<-gqp/sgh
thetaqq<-gqq/sgh
phipp<-hpp/sgh
phipq<-hpq/sgh
phiqp<-hqp/sgh
phiqq<-hqq/sgh
# the interaction
I<-thetapp-phipp-thetaqp+phiqp
MH<- MHn/MHd
MHn<- (thetapp*thetaqq/n1)+(phipp*phiqq/n2)
MHd<- (thetapq*thetaqp/n1)+(phipq*phiqp/n2)
}
# data from Table 3.11
list( npp=9,npq=115,nqp=6,nqq=73,mpp=20,mpq=596,mqp=21,mqq=1171,
n1=203,n2=1808)
# initial values
list( gpp=1,gpq=1,gqp=1,gqq=1,hpp=1,hpq=1,hqp=1,hqq=1)
BUGS CODE 3.6
model;
{
gpp~dgamma(npp,2)
gpq~dgamma(npq,2)
gqp~dgamma(nqp,2)
gqq~dgamma(nqq,2)
hpp~dgamma(mpp,2)
hpq~dgamma(mpq,2)
hqp~dgamma(mqp,2)
hqq~dgamma(mqq,2)
sgh<- gpp+gpq+gqp+gqq+hpp+hpq+hqp+hqq
# Dirichlet distribution for the thetas and phis
thetapp<-gpp/sgh
thetapq<-gpq/sgh
thetaqp<-gqp/sgh
thetaqq<-gqq/sgh
phipp<-hpp/sgh
phipq<-hpq/sgh
phiqp<-hqp/sgh
phiqq<-hqq/sgh
# odds ratio first stratum
OR1<-(thetapp*thetaqq)/(thetapq*thetaqp)
#Odds ratio second stratum
OR2<-(phipp*phiqq)/(phipq*phiqp)
V1<-(1/npp)+(1/npq)+(1/npq)+(1/nqq)
V2<-(1/mpp)+(1/mpq)+(1/mpq)+(1/mqq)
# weight for first stratum
w1<-1/V1
w2<-1/V2
# weighted average of log odds ratios
lodds<-(w1*log(OR1)+w2*log(OR2))/(w1+w2)
# overall odds ratio
OR<-exp(lodds)
}
# data from Table 3.11
list( npp=9,npq=115,nqp=6,nqq=73,mpp=20,mpq=596,mqp=21,mqq=1171,
n1=203,n2=1808)
# initial values
list( gpp=1,gpq=1,gqp=1,gqq=1,hpp=1,hpq=1,hqp=1,hqq=1)
BUGS CODE 3.7
# Analysis for matched case-control study
model;
{
gpp~dgamma(npp,2)
gpm~dgamma(npm,2)
gmp~dgamma(nmp,2)
gmm~dgamma(nmm,2)
sgh<- gpp+gpm+gmp+gmm
# Dirichlet distribution for the thetas and phis
thetapp<-gpp/sgh
thetapm<-gpm/sgh
thetamp<-gmp/sgh
thetamm<-gmm/sgh
# Mantel-Hanszel for a matched case-control study
MH<- thetapm/thetamp
}
# data from Table 3.16
list( npp=34,npm=21,nmp=17,nmm=59)
# initial values
list( gpp=1,gpm=1,gmp=1,gmm=1)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment