Wednesday, July 10, 2013
Chapter 8
BUGS CODE 8.1
model;
{
# 4 readers
# ROC area
# melanoma example
# code is from Congdon
# non diseased
for(i in 1:30){for(j in 1:5){logit(ndgamma[i,j])<-ndtheta[j]-ndmu[i]}}
for(i in 1:30){ndp[i,1]<-ndgamma[i,1]}
for(i in 1:30){ndp[i,2]<-ndgamma[i,2]-ndgamma[i,1]}
for(i in 1:30){ndp[i,3]<-ndgamma[i,3]-ndgamma[i,2]}
for(i in 1:30){ndp[i,4]<-ndgamma[i,4]-
ndgamma[i,3]}
for(i in 1:30){ndy[i]~dcat(ndp[i,1:5])}
for(i in 1:30){ndp[i,5]<-1-ndgamma[i,4]}
# intercept depends on y
for(i in 1:30){
ndmu[i]<-ndb0[ndy[i]]+ ndx1[i]*ndb[1]+ndx2[i]*ndb[2]+ndx3[i]*ndb[3]}
for(i in 1:3){ndb[i]~dnorm(0,.001)}
for(i in 1:5){ndb0[i]~dnorm(0,.001)}
ndtheta[1]~dnorm(0,1)
ndtheta[2]~dnorm(0,1)
ndtheta[3]~dnorm(0,1)
ndtheta[4]~dnorm(0,1)
ndtheta[5]~dnorm(0,1)I(ndtheta[4],)
for( i in 1:5){ndq[i]<-mean(ndp[,i])}
# diseased population
for(i in 1:50){for(j in 1:5){logit(dgamma[i,j])<-dtheta[j]-dmu[i]}}
for(i in 1:50){dp[i,1]<-dgamma[i,1]}
for(i in 1:50){dp[i,2]<-dgamma[i,2]-
dgamma[i,1]}
for(i in 1:50){dp[i,3]<-dgamma[i,3]-
dgamma[i,2]}
for(i in 1:50){dp[i,4]<-dgamma[i,4]-
dgamma[i,3]}
for(i in 1:50){dy[i]~dcat(dp[i,1:5])}
for(i in 1:50){dp[i,5]<-1-dgamma[i,4]}
# intercept depends on y
for(i in 1:50){
dmu[i]<-db0[dy[i]]+ dx1[i]*db[1]+dx2[i]*db[2]+dx3[i]*db[3]}
for(i in 1:3){db[i]~dnorm(0,.001)}
for(i in 1:5){db0[i]~dnorm(0,.001)}
dthe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c(0,0,0,0,0))
BUGS CODE 8.2
model;
{
# nonlinear regression of PCB on cube root of age
for( i in 1:N){ y[i]~dnorm(vu[i], tauy)}
for( i in 1:N){vu[i]<-exp(beta[1]+beta[2]*x[i])}
# predicted values of PCB
for( i in 1:N){ u[i]~dnorm(vu[i], tauy)}
# linear regression of log PCB on cube root of age
for( i in 1:N){ z[i]~dnorm(mu[i], tau)}
# z is natural log of y
for( i in 1:N){ z[i]<-log(y[i])}
for( i in 1:N){mu[i]<-theta[1]+theta[2]*x[i]}
# x is the cube root of age
for( i in 1:N){x[i]<-pow(age[i],.333)}
# prior distribution of the beta
for( i in 1:2){beta[i]~dnorm(0.000,0.0001)}
for( i in 1:2){theta[i]~dnorm(0.000,0.0001)}
# prior distribution for tau
tau~dgamma(.0001,.0001)
tauy~dgamma(.0001,.0001)
# predicted values of log PCB
for( i in 1:N){ w[i]~dnorm(mu[i], tau)}
# sigma is the inverse of the precision tau
sigma<-1/tau
}
# PCB data
list(N=28,
y =c(.6,1.6,.5,1.2,2,1.3,2.5,2.2,2.4,1.2,3.5,4.1,5.1,5.7,3.4,9.7,8.6,
4.0,5.5,10.5,17.5,13.4,4.5,30.4,12.4,13.4,26.2,7.4),
age=c(1,1,1,1,2,2,2,3,3,3,4,4,4,5,6,6,6,7,7,7,8,8,8,9,11,12,12,12))
# initial values
list( beta=c(0,0), theta=c(0,0), tauy=1,tau=1)
BUGS CODE 8.3
model;
{
# nonlinear regression of BOD on time
#for( i in 1:N){ y[i]~dnorm(mu[i], tau)}
#for( i in 1:N){mu[i]<-theta[1]*(1-exp(-theta[2]*x[i]))}
# predicted values of BOD
#for( i in 1:N){ w[i]~dnorm(mu[i], tau)}
# prior distributions
#theta[1]~ dnorm(1,.001)
#theta[2]~ dnorm(1, .001)
# tau~dgamma(.001,.001)
# regression of chlorine concentration on time
for( i in 1:N){y[i]~dnorm(mu[i], tau)}
for( i in 1:N){mu[i]<-theta[1]*(1-exp(-theta[2]*x[i]))}
# predicted chlorine values
for( i in 1:N){ w[i]~dnorm(mu[i], tau)}
# prior distributions
theta[1]~ dnorm( 1, .001)
theta[2]~ dnorm( 1, .001)
tau~dgamma(.01,.01)
}
# BOD data
list( y=c(8.3,10.3,19,16,15.6,19.8),
x=c(1,2,3,4,5,7),N= 6)
# initial values BOD
list(tau=1, theta=c(20,.24))
# chlorine concentration
list( N=54,
y=c(17.3,17.6,17.9,18.3,18.5,18.9,19,19.3,19.8,19.9,20.2,
20.5,20.6,21.1,21.5,21.9,22,22.3,22.6,22.8,23.0,23.2,
23.4,23.7,24,24.2,24.5,25,25.4,25.5,25.9,25.9,26.3,26.2,
26.5,26.5,26.6,27,27,27,27,27.3,27.8,28.1,28.1,28.1,28.4,
28.6,29,29.2,29.3,29.4,29.4, 29.4),
x=c(2.45,2.55,2.65,2.75,2.85,2.95,3.05,3.15,3.25,3.35,3.45,3.55,
3.65,3.75,3.85,3.95,4.05,4.15,4.25,4.35,4.45,4.55,4.65,4.75,
4,85,4.95,5.05,5.15,5.25,5.35,5.45,5.55,5.65,5.75,5.85,5.95,
6.05,6.15,6.25,6.35,6.45,6.55,6.65,6.75,6.85,6.95,7.05,
7.15,7.25,7.35,7.45,7.55,7.65,7.75))
# initial values chlorine
list( theta=c(0,0), tau=1)
BUGS CODE 8.4
model;
{
for(i in 1:n){for(j in 1:p){y[i,j]~dnorm(mu[i,j],tau)}}
for(i in 1:n){for(j in 1:p){mu[i,j]<- theta+alpha[i]+beta[j]}}
for(i in 1:n){alpha[i]~dnorm(0,tau.alpha)}
for(j in 1:p){beta[j]~dnorm(0,tau.beta)}
for(i in 1:n){for(j in 1:p){z[i,j]~dnorm(mu[i,j],tau)}}
# prior distributions
theta~dnorm(0,.0001)
tau.alpha~dgamma(.0001,.0001)
tau.beta~dgamma(.0001,.0001)
tau~dgamma(.0001,.0001)
sigma.alpha<-1/tau.alpha
sigma.beta<-1/tau.beta
sigma<-1/tau
}
# example with placebo group only
list(n=26, p=5, y=structure(.Data=
c(20,19,20,20,18,
14,15,16,9,6,
7,5,8,8,5,
6,10,9,10,10,
9,7,9,4,6,
9,10,9,11,11,
7,3,7,6,3,
18,20,20,23,21,
6,10,10,13,14,
10,15,15,15,14,
5,9,7,3,12,
11,11,8,10,9,
10,2,9,3,2,
17,12,14,15,13,
16,15,13,7,9,
7,10,4,10,5,
5,0,5,0,0,
16,7,7,6,10,
5,6,9,5,6,
2,1,1,2,2,
7,11,7,5,11,
9,16,17,10,6,
2,5,6,7,6,
7,3,5,5,5,
19,13,19,17,17,
7,5,8,8,6),.Dim=c(26,5)))
# initial values
list(alpha=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
beta=c(0,0,0,0,0), tau.alpha=1, tau.beta=1, tau=1, theta=0)
BUGS CODE 8.6
model;
{
for(j in 1 : N) {
for(k in 1 : T) {
log(mu[j, k]) <- a0 + alpha.Base * (Base4[j] - Base4.bar)
+ alpha.Trt * (Trt[j] - Trt.bar)
+ alpha.Age * (Age[j] - Age.bar) +b1[j]+b2[k]
# the following is the Poisson regression model
y[j,k] ~ dpois(mu[j,k])
# z are the predicted values of y
z[j,k]~dpois(mu[j,k])
}
# the basline measurement is divide by 4
Base4[j] <-(Base[j] / 4)
}
# covariate means:
Age.bar <- mean(Age[])
Trt.bar <- mean(Trt[])
Base4.bar <- mean(Base4[])
# priors:
for(j in 1:N){b1[j]~dnorm(0,taub1) }
for(k in 1:T){b2[k]~dnorm(0,taub2) }
a0 ~ dnorm(0.0,1.0E-4)
alpha.Base ~ dnorm(0.0,1.0E-4)
alpha.Trt ~ dnorm(0.0,1.0E-4);
taub1~dgamma(.0001,.0001)
taub2~dgamma(.0001,.0001)
alpha.Age ~ dnorm(0.0,1.0E-4)
sigmab1<-1/taub1
sigmab2<-1/taub2
}
# Epileptic information
list(N = 59, T = 4,
y = structure(.Data = c( 5, 3, 3, 3,
3, 5, 3, 3,
2, 4, 0, 5,
4, 4, 1, 4,
7, 18, 9, 21,
5, 2, 8, 7,
6, 4, 0, 2,
40, 20, 21, 12,
5, 6, 6, 5,
14, 13, 6, 0,
26, 12, 6, 22,
12, 6, 8, 4,
4, 4, 6, 2,
7, 9, 12, 14,
16, 24, 10, 9,
11, 0, 0, 5,
0, 0, 3, 3,
37, 29, 28, 29,
3, 5, 2, 5,
3, 0, 6, 7,
3, 4, 3, 4,
3, 4, 3, 4,
2, 3, 3, 5,
8, 12, 2, 8,
18, 24, 76, 25,
2, 1, 2, 1,
3, 1, 4, 2,
13, 15, 13, 12,
11, 14, 9, 8,
8, 7, 9, 4,
0, 4, 3, 0,
3, 6, 1, 3,
2, 6, 7, 4,
4, 3, 1, 3,
22, 17, 19, 16,
5, 4, 7, 4,
2, 4, 0, 4,
3, 7, 7, 7,
4, 18, 2, 5,
2, 1, 1, 0,
0, 2, 4, 0,
5, 4, 0, 3,
11, 14, 25, 15,
10, 5, 3, 8,
19, 7, 6, 7,
1, 1, 2, 3,
6, 10, 8, 8,
2, 1, 0, 0,
102, 65, 72, 63,
4, 3, 2, 4,
8, 6, 5, 7,
1, 3, 1, 5,
18, 11, 28, 13,
6, 3, 4, 0,
3, 5, 4, 3,
1, 23, 19, 8,
2, 3, 0, 1,
0, 0, 0, 0,
1, 4, 3, 2), .Dim = c(59, 4)),
Trt = c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1),
Base = c( 11, 11, 6, 8, 66, 27, 12, 52, 23, 10,
52, 33, 18, 42, 87, 50, 18,111, 18, 20,
12, 9, 17, 28, 55, 9, 10, 47, 76, 38,
19, 10, 19, 24, 31, 14, 11, 67, 41, 7,
22, 13, 46, 36, 38, 7, 36, 11,151, 22,
41, 32, 56, 24, 16, 22, 25, 13, 12),
Age = c(31,30,25,36,22,29,31,42,37,28,
36,24,23,36,26,26,28,31,32,21,
29,21,32,25,30,40,19,22,18,32,
20,30,18,24,30,35,27,20,22,28,
23,40,33,21,35,25,26,25,22,32,
25,35,21,41,32,26,21,36,37))
list(a0 = 1, alpha.Base = 0, alpha.Trt = 0,
alpha.Age = 0, taub1=1,taub2=1)
BUGS CODE 8.7
model {
# Likelihood
for (i in 1 : N) {
O[i] ~ dpois(mu[i])
log(mu[i]) <- log(E[i]) + alpha0 + alpha1 * X[i]/10 + b[i]
# predicted cases u[i] ~ dpois(mu[i])
# Area-specific relative risk (for maps)
RR[i] <- exp(alpha0 + alpha1 * X[i]/10 + b[i])
}
# CAR prior distribution for random effects:
b[1:N] ~ car.normal(adj[], weights[], num[], tau)
for(k in 1:sumNumNeigh) {
weights[k] <- 1
}
# Other priors:
alpha0 ~ dflat()
alpha1 ~ dnorm(0.0, 1.0E-5)
tau ~ dgamma(0.5, 0.0005) # prior on precision
sigma <- sqrt(1 / tau) # standard deviation
b.mean <- sum(b[])
}
# lip cancer
list(N = 56,
O = c( 9, 39, 11, 9, 15, 8, 26, 7, 6, 20,
13, 5, 3, 8, 17, 9, 2, 7, 9, 7,
16, 31, 11, 7, 19, 15, 7, 10, 16, 11,
5, 3, 7, 8, 11, 9, 11, 8, 6, 4,
10, 8, 2, 6, 19, 3, 2, 3, 28, 6,
1, 1, 1, 1, 0, 0),
E = c( 1.4, 8.7, 3.0, 2.5, 4.3, 2.4, 8.1, 2.3, 2.0, 6.6,
4.4, 1.8, 1.1, 3.3, 7.8, 4.6, 1.1, 4.2, 5.5, 4.4,
10.5,22.7, 8.8, 5.6,15.5,12.5, 6.0, 9.0,14.4,10.2,
4.8, 2.9, 7.0, 8.5,12.3,10.1,12.7, 9.4, 7.2, 5.3,
18.8,15.8, 4.3,14.6,50.7, 8.2, 5.6, 9.3,88.7,19.6,
3.4, 3.6, 5.7, 7.0, 4.2, 1.8),
X = c(16,16,10,24,10,24,10, 7, 7,16,
7,16,10,24, 7,16,10, 7, 7,10,
7,16,10, 7, 1, 1, 7, 7,10,10,
7,24,10, 7, 7, 0,10, 1,16, 0,
1,16,16, 0, 1, 7, 1, 1, 0, 1,
1, 0, 1, 1,16,10),
num = c(3, 2, 1, 3, 3, 0, 5, 0, 5, 4,
0, 2, 3, 3, 2, 6, 6, 6, 5, 3,
3, 2, 4, 8, 3, 3, 4, 4, 11, 6,
7, 3, 4, 9, 4, 2, 4, 6, 3, 4,
5, 5, 4, 5, 4, 6, 6, 4, 9, 2,
4, 4, 4, 5, 6, 5
),
adj = c(
19, 9, 5,
10, 7,
12,
28, 20, 18,
19, 12, 1,
17, 16, 13, 10, 2,
29, 23, 19, 17, 1,
22, 16, 7, 2,
5, 3,
19, 17, 7,
35, 32, 31,
29, 25,
29, 22, 21, 17, 10, 7,
29, 19, 16, 13, 9, 7,
56, 55, 33, 28, 20, 4,
17, 13, 9, 5, 1,
56, 18, 4,
50, 29, 16,
16, 10,
39, 34, 29, 9,
56, 55, 48, 47, 44, 31, 30, 27,
29, 26, 15,
43, 29, 25,
56, 32, 31, 24,
45, 33, 18, 4,
50, 43, 34, 26, 25, 23, 21, 17, 16, 15, 9,
55, 45, 44, 42, 38, 24,
47, 46, 35, 32, 27, 24, 14,
31, 27, 14,
55, 45, 28, 18,
54, 52, 51, 43, 42, 40, 39, 29, 23,
46, 37, 31, 14,
41, 37,
46, 41, 36, 35,
54, 51, 49, 44, 42, 30,
40, 34, 23,
52, 49, 39, 34,
53, 49, 46, 37, 36,
51, 43, 38, 34, 30,
42, 34, 29, 26,
49, 48, 38, 30, 24,
55, 33, 30, 28,
53, 47, 41, 37, 35, 31,
53, 49, 48, 46, 31, 24,
49, 47, 44, 24,
54, 53, 52, 48, 47, 44, 41, 40, 38,
29, 21,
54, 42, 38, 34,
54, 49, 40, 34,
49, 47, 46, 41,
52, 51, 49, 38, 34,
56, 45, 33, 30, 24, 18,
55, 27, 24, 20, 18
),
sumNumNeigh = 234)
# initial values
list(tau = 1, alpha0 = 0, alpha1 = 0,
b=c(0,0,0,0,0,NA,0,NA,0,0,
NA,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0),
u=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1))
Chapter 7
BUGS CODE 7.1
# Measures of accuracy
# Binary Scores
Model;
{
# Dirichlet distribution for cell probabilities
g00~dgamma(a00,2)
g01~dgamma(a01,2)
g10~dgamma(a10,2)
g11~dgamma(a11,2)
h<-g00+g01+g10+g11
# the theta have a Dirichlet distribution
theta00<-g00/h
theta01<-g01/h
theta10<-g10/h
theta11<-g11/h
# the basic test accuracies are below
tpf<-theta11/(theta11+theta01)
se<-tpf
sp<-1-fpf
fpf<-theta10/(theta10+theta00)
tnf<-theta00/(theta00+theta10)
fnf<-theta01/(theta01+theta11)
ppv<-theta11/(theta10+theta11)
npv<-theta00/(theta00+theta01)
pdlr<-tpf/fpf
ndlr<-fnf/tnf
# sensitivity mammography for Chamberlain et al.
tpfm<-(theta11+theta10)/(theta11+theta10+theta01+theta00)
# sensitivity of physical opinion
tpfpe<-(theta01+theta11)/(theta11+theta10+theta01+theta00)
}
# Exercise Stress Test Pepe
# Uniform Prior (add one to each cell of the table frequencies !)
list(a00=328,a01=209,a10=116,a11=819)
# Table 7.13, Chamberlain et al.
# Uniform prior
list(a00=30051,a01=14,a10=1959,a11=181)
# Chamberlain et al. uniform prior
# add one to each cell
list(a00=3,a01=9,a10=50,a11=123)
# Problem 7, improper prior
list(a00=70205,a01=25,a10=2908,a11=262)
# Problem 8, improper prior
list(a00=67116,a01=41,a10=3216,a11=76)
# initial values
list(g00=1,g01=1,g10=1,g11=1)
BUGS CODE 7.2
# Area under the curve
# Ordinal values
# Five values
Model;
{
# generate Dirichlet distribution
g11~dgamma(a11,2)
g12~dgamma(a12,2)
g13~dgamma(a13,2)
g14~dgamma(a14,2)
g15~dgamma(a15,2)
g01~dgamma(a01,2)
g02~dgamma(a02,2)
g03~dgamma(a03,2)
g04~dgamma(a04,2)
g05~dgamma(a05,2)
g1<-g11+g12+g13+g14+g15
g0<-g01+g02+g03+g04+g05
# posterior distribution of probabilities for response of diseased patients
theta1<-g11/g1
theta2<-g12/g1
theta3<-g13/g1
theta4<-g14/g1
theta5<-g15/g1
# posterior distribution for probabilities of response of non-diseased patients
ph1<-g01/g0
ph2<-g02/g0
ph3<-g03/g0
ph4<-g04/g0
ph5<-g05/g0
# auc is area under ROC curve
#A1 is the P[Y>X]
#A2 is the P[Y=X]
auc<- A1+A2/2
A1<-theta2*ph1+theta3*(ph1+ph2)+theta4*(ph1+ph2+ph3)+
theta5*(ph1+ph2+ph3+ph4)
A2<- theta1*ph1+theta2*ph2+theta3*ph3+theta4*ph4
+theta5*ph5
}
# Mammography Example Zhou et al.
# Uniform Prior
# see Table 7.7
list(a11=2,a12=1,a13=7,a14=12,a15=13,a01=10,a02=3,a03=12,
a04=9,a05=1)
# initial values
list(g11=1,g12=1,g13=1,g14=1,g15=1,g01=1,g02=1,g03=1,
g04=1,g05=1)
BUGS CODE 7.3
model;
# weibull distribution for time of entry to diagnosis
# study group
{ for( i in 1:n1){ x1[i]~dweib(v1,lamda1)}
v1~dgamma(.001,.001)
lamda1~dgamma(.001,.001)
lme1<--(1/v1)*log(lamda1)+loggam(1+1/v1)
# me1 is the mean of time from entry to diagnosis for the study group
me1<-exp(lme1)
# weibull distribution for time to diagnosis
# control group
for( i in 1:n2){ x2[i]~dweib(v2,lamda2)}
v2~dgamma(.001,.001)
lamda2~dgamma(.001,.001)
lme2<--(1/v2)*log(lamda2)+loggam(1+1/v2)
# me2 is the mean of time from entry to diagnosis for the control group
me2<-exp(lme2)
# diff is the estimate of the lead time
diff<-me2-me1
}
# study and control groups HIP
# x1 is the vector of times from entry to diagnosis for the study group
# x2 is the vector of times from entry to diagnosis for the control group
list(n1=132, x1=c(81,118,428,1265,530,468,1200,496,127,29,
599,48,751,1197,1375,1343,1279,575,37,595,406,27,412,
126,75,568,993,1277,192,123,12,236,1358,79,384,64,439,
47,29,845,743,1473,414,152,13,1315,549,1320,1244,1191,
1063,448,67,392,23,959,34,739,18,808,1341,1101,345,378,
875,1398,1177,53,34,1383,343,21,14,386,927,439,544,
600,347,88,987,495,792,160,16,1274,750,420,147,38,730,
110,1051,81,1335,939,871,183,490,79,404,79,404,971,123,
1260,298,59,182,964,1520,1308,1097,16,85,300,517,22,
1456,69,16,881,73,41,1265,1322,358,673,470,217,561,
409,433,29), n2=303,
x2=c(1468,1779,601,29,1617,1227,1470,141,751,1297,1109,1562,
1285,345,1429,972,345,1853,1554,490,1478,1799,1791,614,
118,50,1446,1353,510,1466,1177,146,1623,1715,70,1098,
1352,1470,728,534,193,1446,1818,1551,14,1474,197,1037,
1728,230,696,440,491,576,1771,1539,1025,1616,575,330,604,
1576,528,1118,1038,1527,77,1413,1240,358,110,349,214,1096, 1495,1143,1543,729,412,1674,105,16,684,434,7,330,1707,1433,
87,1028,1237,1729,274,682,679,703,755,505,966,1595,209,1273,
1361,495,1368,258,800,1519,532,878,1333,802,390,1482,647,
1730,1537,609,622,1472,1041,1343,1754,1505,1765,600,1632,
16,1149,415,309,1745,1797,164,68,1734,919,619,1185,1044,
1335,546,93,832,1681,1742,168,989,550,1295,99,366,1806,559,
756,1734,822,139,724,446,511,1553,1304,388,1328,1467,1526, 810,1616,252,392,1684,331,1820,1746,789,22,1562,1525,53,1507,
426,1125,719,255,1098,1567,1261,981,44,1568,1590,1810,601,
973,1539,549,1237,274,1309,1513,520,1478,1778,572,985,1665,797,
513,568,1590,1631,912,1815,185,764,1320,1633,1619,1407,779,
313,1322,1623,896,1734,151,1427,463,511,296,1362,1131,1175,
807,1154,79,415,921,659,588,1727,163,198,1829,417,839,1306,
1192,1747,778,670,1066,1630,1098,1270,133,1119,423,912,379,
734,155,1732,1460,287,1812,555,41,1081,811,1155,1559,1147,
1635,1766,722,1011,1189,1534,558,236,1301,1405,266,932,407,
428,1216,208,657,518,379,864,1051,1037,510,77,1105,319,
83,169,464))
# initial values for the Weibull distribution
list(v1=1,lamda1=1,v2=1,lamda2=1)
BUGS Code 7.4.
Model;
{
# group 1
for( i in 1:a){ m1[i]~dbin(q1[i], Op1[i])}
# prior distribution of mortality
for( i in 1:a){q1[i]~dbeta(1,1)}
for( i in 1:a){Op1[i]<-O1[i]-w1[i]}
# below is applied when only half the withdrawals are used
# for( i in 1:a){Op1[i]<-O1[i]-w1[i]/2}
# survival probabilities
for( i in 2:a){L1[i]<-L1[i-1]+w1[i]}
L1[1]<-w1[1]
# sr1 survival rate for group 1
for( i in 1:a){s1[i]~dbin(sr1[i], O1[1])}
# prior distribution for survival rate
for( i in 1:a){sr1[i]~dbeta(1,1)}
for( i in 2:a){s1[i]<- O1[i]+L1[i-1]}
# group 2
for( i in 1:a){ m2[i]~dbin(q2[i], Op2[i])}
for( i in 1:a){q2[i]~dbeta(1,1)}
for( i in 1:a){Op2[i]<-O2[i]-w2[i]}
# below is applied when only half the withdrawals are used
# for( i in 1:a){Op2[i]<-O2[i]-w2[i]/2}
# survival probabilities
for( i in 2:a){L2[i]<-L2[i-1]+w2[i]}
L2[1]<-w2[1]
# sr2 survival rate for group 2
for( i in 1:a){s2[i]~dbin(sr2[i], O2[1])}
for( i in 1:a){sr2[i]~dbeta(1,1)}
for( i in 2:a){s2[i]<- O2[i]+L2[i-1]}
}
# data for HIP study and control cohorts
# group 1 is study cohort
# group 2 is control
list(a=40, m1=c(11,8,12,11,5,9,8,3,5,7,7,7,5,2,12,3,6,2,4,10,1,7,3,2,
2,3,0,2,3,1,3,3,2,2,1,1,0,0,0,0), O1=c(304,293,285,273,262,257,248,240,237,232,225,218,211,206,
204,192,189,183,181,177,167,166,159,156,154,152,149,145,138,129,116,96,77,64,
49,34,26,14,5,1),w1=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,5,6,
12,17,16,11,13,14,7,12,9,4,1), m2=c(8,14,21,18,14,9,14,8,9,4,3,4,6,4,4,5,5,4,
3,1,1,2,6,1,2,2,2,2,0,3,2,1,3,.1,0,0,0,0,0,0),
O2=c(295,287,273,252,234,220,211,197,189,180,176,173,169,163,159,155,150,145,
141,138,137,136,134,128,127,125,123,119,112,103,84,69,63,54,45,37,23,11,4,1),
w2=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,5,9,16,13,5,6,9,8,14,12,
7,3,1))
# data for HIP screened cohort and control cohort
# group 1 is screened cohort
# group 2 is control
list(a=40, m1=c(3,6,8,6,4,6,5,3,5,6,7,6,2,1,9,2,6,1,3,8,0,3,3,2,2,3,0,2,2,1,2,1,2,2,1,0,0,
0,0,0), O1=c(225,222,216,208,202,198,192,187,184,179,173,166,160,158,157,148,146,140,139,136,128,128,125,122,120,118,115,112,107,100,
89,77,62,51,39,27,20,10,3,1),
w1=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,5,10,10,14,9,10,11,7,10,7,2,1), m2=c(8,14,21,18,14,9,14,8,9,4,3,4,6,4,4,5,5,4,
3,1,1,2,6,1,2,2,2,2,0,3,2,1,3,.1,0,0,0,0,0,0),
O2=c(295,287,273,252,234,220,211,197,189,180,176,173,169,163,159,155,150,145,
141,138,137,136,134,128,127,125,123,119,112,103,84,69,63,54,45,37,23,11,4,1),
w2=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,5,9,16,13,5,6,9,8,14,12,
7,3,1))
# initial values
list(q1=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,
.5,.5,.5,.5,.5,.5,.5,.5,.5,.5),q2=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5),
sr1=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,
.5,.5,.5,.5,.5,.5,.5,.5,.5,.5),
sr2=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,
.5,.5,.5,.5,.5,.5,.5,.5,.5,.5))
BUGS CODE 7.5
model;
# log-rank Bayesian approach
{
# for group 1
# distribution of the number of deaths
for ( i in 1:m1){ d1[i]~dbin(q1[i],r1[i])}
# distribution of the number of censored
for ( i in 1:m1){ c1[i]~dbin(qc1[i],r1[i])}
# prior distribution of q1[i]
for ( i in 1:m1){ q1[i]~dbeta (.1,.1)}
for ( i in 1:m1){ qc1[i]~dbeta (.1,.1)}
# number at risk
r1[1]<- n1
for( i in 2:m1){ r1[i]<-r1[i-1]-d1[i-1]-c1[i-1]}
# for group 2
# distribution of the number of deaths
for (i in 1:m2){ d2[i]~dbin(q2[i],r2[i])}
# distribution of the number of censored
for (i in 1:m2){ c2[i]~dbin(qc2[i],r2[i])}
# prior distribution of q2[i]
for ( i in 1:m2){ q2[i]~dbeta (.1,.1)}
for ( i in 1:m2){ qc2[i]~dbeta (.1,.1)}
# number at risk
r2[1]<- n2
for( i in 2:m2){ r2[i]<-r2[i-1]-d2[i-1]-c2[i-1]}
# mortality group1
for( i in 1:m1){ s1[i]<- q1[i]*r1[i]}
# mortality group 2
for( i in 1:m2){ s2[i]<- q2[i]*r2[i]}
# Expected number of deaths
for( i in 1:m1){e1[i]<- e11[i]*e12[i]}
for( i in 1:m2){ e11[i]<- r1[i]/(r1[i]+r2[i])}
for( i in 1:m2){ e12[i]<- s1[i]+s2[i]}
for( i in 1:m1){e2[i]<- e21[i]*e22[i]}
for( i in 1:m2){ e21[i]<- r2[i]/(r1[i]+r2[i])}
for( i in 1:m2){ e22[i]<- s1[i]+s2[i]}
# variance of difference
for( i in 1:m2){ t1[i]<- r1[i]*r2[i]*(s1[i]+s2[i])*(r1[i]+r2[i]- s1[i]-s2[i])}
for( i in 1:m2){ t2[i]<-(r1[i]+r2[i])*(r1[i]+r2[i])*(r1[i]+r2[i]-1)}
# difference observed minus expected
for( i in 1:m1){ome1[i]<- d1[i]-e1[i]}
for( i in 1:m1){ vlr1[i]<-t1[i]/t2[i]}
for( i in 1:m2){ome2[i]<-d2[i]-e2[i]}
# sum of observed minus expected group 1
some1<- sum(ome1[])
# sum of expected group 1
se1<-sum(e1[])
# sum of expected group 2
se2<-sum(e2[])
# sum of observed minus expected group 2
some2<- sum(ome2[])
# likelihood ratio
LR<- some1*some1/se1+some2*some2/se2
}
# HIP study
list(n1=304,n2=295, m1= 40, d1=c(11,8,12,11,5,9,8,3,5,7,7,7,5,2,12,
3,6,2,4,10,1,7,3,2,2,3,0,2,3,1,3,3,2,2,1,1,0,0,0,0), c1=c(0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,5,6,12,17,16,11,13,14,7,12,9,4,1),
m2=40, d2=c(8,14,21,18,14,9,14,8,9,4,3,4,6,4,4,5,5,4,3,1,1,2,6,1,2,2,2,2,0,3,2,1,3,
0,0,0,0,0,0,0 ),
c2=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,5,9,16,13,5,6,9,
8,14,12,7,3,1))
# initial values study versus control
list( q1=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5), q2=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5),
qc1=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5),
qc2=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5))
# Screened versus control
list(n1=225,n2=295, m1= 20, d1=c(9,14,10,8,11,13,3,11,7,11,3,5,5,2,3,3,4,1,0,0), c1=c(0,0,0,0,0,0,0,0,0,0,0,0,0,6,15,24,19,18,17,3),
m2=20, d2=c(22,39,23,22,13,7,10,9,9,4,3,7,4,4,3,3,3,0,0,0 ),
c2=c(0,0,0,0,0,0,0,0,0,0,0,0,0,7,24,18,15,22,19,4))
# initial values screened vs control
list( q1=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5),
q2=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5),
qc1=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5),
qc2=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5))
BUGS CODE 7.6
model;
{
# Set up data
for(i in 1:N) {
for(j in 1:T) {
# risk set = 1 if obs.t >= t
Y[i,j] <- step(obs.t[i] - t[j] + eps)
# counting process jump = 1 if obs.t in [ t[j], t[j+1] )
# i.e. if t[j] <= obs.t < t[j+1]
dN[i, j] <- Y[i, j] * step(t[j + 1] - obs.t[i] - eps) * fail[i]
}
}
# Model
for(j in 1:T) {
for(i in 1:N) {
dN[i, j] ~ dpois(Idt[i, j]) # Likelihood
Idt[i, j] <- Y[i, j] * exp(beta * x1[i]) * dL0[j] # Intensity
}
dL0[j] ~ dgamma(mu[j], c)
mu[j] <- dL0.star[j] * c # prior mean hazard
# Survivor function = exp(-Integral{l0(u)du})^exp(beta*z)
S.screened[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta * -0.5));
S.control[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta * 0.5));
}
c <- 0.1
r <- .1
for (j in 1 : T) {
dL0.star[j] <- r * (t[j + 1] - t[j])
}
beta ~ dnorm(0.0,0.000001)
# hazard ratio for group 1 versus group 2
HR<-exp(beta)
}
# HIP information
# screened group table 2 of appendix
# control group table 11 of appendix
# see Shapiro, et al.
list( N=520,T=19, eps=1.0E-10,
obs.t=c(6,6,6,6,6,6,6,6,6,
18,18,18,18,18,18,18,18,18,18,18,18,18,18,
30,30,30,30,30,30,30,30,30,30,
42,42,42,42,42,42,42,42,
54,54,54,54,54,54,54,54,54,54,54,
66,66,66,66,66,66,66,66,66,66,66,66,66,
78,78,78,
90,90,90,90,90,90,90,90,90,90,90,
102,102,102,102,102,102,102,
114,114,114,114,114,114,114,114,114,114,114,
126,126,126,
138,138,138,138,138,
150,150,150,150,150,
162,162,
162,162,162,162,162,162,
174,174,174,
174,174,174,174,174,174,174,174,174,174,174,174,174,174,174,
186,186,186,
186,186,186,186,186, 186,186,186,186,186, 186,186,186,186,186, 186,186,186,186,186,
186,186,186,186,
198,198,198,198,
198,198,198,198,198, 198,198,198,198,198, 198,198,198,198,198, 198,198,198,198,
210,
210,210,210,210,210, 210,210,210,210,210, 210,210,210,210,210, 210,210,210,
222,222,222,222,222, 222,222,222,222,222, 222,222,222,222,222,222,222,
234,234,234,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18, 18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,
18,
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,
54,54,54,54,54,54,54,54,54,54,54,54,54,
66,66,66,66,66,66,66,
78,78,78,78,78,78,78,78,78,78,
90,90,90,90,90,90,90,90,99,
102,102,102,102,102,102,102,102,102,
114,114,114,114,
126,126,126,
138,138,138,138,138,138,138,
150,150,150,150,
162,162,162,162,
162,162,162,162,162,162,162,
174,174,174,
174,174,174,174,174, 174,174,174,174,174, 174,174,174,174,174, 174,174,174,174,174,
174,174,174,174,174,
186,186,186,
186,186,186,186,186, 186,186,186,186,186,
186,186,186,186,186, 186,186,186,
198,198,198,
198,198,198,198,198, 198,198,198,198,198, 198,198,198,198,198,
210,210,210,210,210, 210,210,210,210,210, 210,210,210,210,210, 210,210,210,210,210,
210,210,
222,222,222,222,222, 222,222,222,222,222, 222,222,222,222,222, 222,222,222,222,
234,234,234,234),
fail=c(
1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,
1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,
1,1,1,
1,1,1,1,1,
1,1,1,1,1,
1,1,0,0,0,0,0,0,
1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5,
-.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5,
-.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5,
-.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5,
-.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5,
-.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5,
-.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5,
-.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5, -.5,-.5,-.5,-.5,-.5),
t=c(6,18,30,42,54,66,78,90,102,114,126,138,150,162,174,186,198,210,222, 234 ))
#initial values
list( beta = 0.0,
dL0 = c(1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0, 1.0,1.0,1.0,1.0))
Chapter 6
BUGS CODE 6.1
model;
# leukemia patients two groups
{
# for group 1
# distribution of the number of recurrences
for ( i in 1:m1){ d1[i]~dbin(q1[i],r1[i])}
# distribution of the number of censored
for ( i in 1:m1){ ce1
[i]~dbin(qc1[i],r1[i])}
# prior distribution of q1[i]
for ( i in 1:m1){ q1[i]~dbeta (.01,.01)}
for ( i in 1:m1){ qc1[i]~dbeta (.01,.01)}
# conditional probabilities
#for(i in 1:m1){ p[i]<-(r[i]-d[i])/r[i]}
for ( i in 1:m1){ p1[i]<-1-q1[i]}
# number at risk
r1[1]<- 21
for( i in 2:m1){ r1[i]<-r1[i-1]-d1[i-1]-ce1[i-1]}
# product-limit survival probabilities
for ( i in 2:m1){ s1[i]<-s1[i-1]*p1[i]}
s1[1]<-p1[1]
# for group 2
# distribution of the number of recurrences
for (i in 1:m2){ d2[i]~dbin(q2[i],r2[i])}
# distribution of the number of censored
for (i in 1:m2){ ce2[i]~dbin(qc2[i],r2[i])}
# prior distribution of q2[i]
for ( i in 1:m2){ q2[i]~dbeta (.01,.01)}
for ( i in 1:m2){ qc2[i]~dbeta (.01,.01)}
# conditional probabilities
#for(i in 1:m2){ p2[i]<-(r2[i]-d2[i])/r2[i]}
for ( i in 1:m2){ p2[i]<-1-q2[i]}
# number at risk
r2[1]<- 21
for( i in 2:m2){ r2[i]<-r2[i-1]-d2[i-1]-ce2[i-1]}
# product-limit survival probabilities
for ( i in 2:m2){ s2[i]<-s2[i-1]*p2[i]}
s2[1]<-p2[1]
}
# data for Freirich et al.
list( m1= 7, d1=c(3,1,1,1,1,1,1), ce1=c(1,1,2,0,3,0,5),
m2= 12, d2=c(2,2,1,2,2,4,2,2,1,1,1,1),
ce2=c(0,0,0,0,0,0,0,0,0,0,0,0))
# initial values
list( q1=c(.5,.5,.5,.5,.5,.5,.5),
q2=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5))
BUGS CODE 6.2
model;
# log-rank Bayesian approach
{
# for group 1
# distribution of the number of recurrences
for ( i in 1:m1){ d1[i]~dbin(q1[i],r1[i])}
# distribution of the number of censored
for ( i in 1:m1){ c1[i]~dbin(qc1[i],r1[i])}
# prior distribution of q1[i]
for ( i in 1:m1){ q1[i]~dbeta (.01,.01)}
for ( i in 1:m1){ qc1[i]~dbeta (.01,.01)}
# number at risk
r1[1]<- n1
for( i in 2:m1){ r1[i]<-r1[i-1]-d1[i-1]-c1[i-1]}
# for group 2
# distribution of the number of recurrences
for (i in 1:m2){ d2[i]~dbin(q2[i],r2[i])}
# distribution of the number of censored
for (i in 1:m2){ c2[i]~dbin(qc2[i],r2[i])}
# prior distribution of q2[i]
for ( i in 1:m2){ q2[i]~dbeta (.01,.01)}
for ( i in 1:m2){ qc2[i]~dbeta (.01,.01)}
# number at risk
r2[1]<- n2
for( i in 2:m2){ r2[i]<-r2[i-1]-d2[i-1]-c2[i-1]}
# mortality group1
for( i in 1:m1){ s1[i]<- q1[i]*r1[i]}
# mortality group 2
for( i in 1:m2){ s2[i]<- q2[i]*r2[i]}
# Expected number of events
for( i in 1:m1){e1[i]<- e11[i]*e12[i]}
for( i in 1:m2){ e11[i]<- r1[i]/(r1[i]+r2[i])}
for( i in 1:m2){ e12[i]<- s1[i]+s2[i]}
for( i in 1:m1){e2[i]<- e21[i]*e22[i]}
for( i in 1:m2){ e21[i]<- r2[i]/(r1[i]+r2[i])}
for( i in 1:m2){ e22[i]<- s1[i]+s2[i]}
# variance of difference
for( i in 1:m2){ t1[i]<- r1[i]*r2[i]*(s1[i]+s2[i])*(r1[i]+r2[i]- s1[i]-s2[i])}
for( i in 1:m2){ t2[i]<-(r1[i]+r2[i])*(r1[i]+r2[i])*(r1[i]+r2[i]-1)}
# difference observed minus expected
for( i in 1:m1){ome1[i]<- d1[i]-e1[i]}
for( i in 1:m1){ vlr1[i]<-t1[i]/t2[i]}
for( i in 1:m2){ome2[i]<-d2[i]-e2[i]}
# sum of observed minus expected group 1
some1<- sum(ome1[])
# sum of expected group 1
se1<-sum(e1[])
# sum of expected group 2
se2<-sum(e2[])
# sum of observed minus expected group 2
some2<- sum(ome2[])
# likelihood ratio<- some1*some1/se1+some2*some2/se2
}
# data for Freirich et al. Table 6.6
list( n1=21, n2=21,m1= 17, d1=c(0,0,0,0,0,3,1,0,1,0,0,1,0,1,0,1,1),
# the number of censored observations group 1
c1=c(0,0,0,0,0,1,0,1,1,1,0,0,0,0,3,0,0),
m2= 17, d2=c(2,2,1,2,2,0,0,4,0,2,2,0,1,0,1,1,1),
# the number of censored observations group 2
c2=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
# initial values Table 6.6
list( q1=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5),
q2=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5))
# data exercise 5
list(n1=25,n2=25, m1= 44,d1=c(0,0,1,1,0,1,1,0,0,1,0,1,0,1,0,0,0,0,0,0,1,1,1,0,0,0,1,0,1,0,0,1,0,0,1,1,0,1,1,1,1,1,0,1), c1=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
m2=44, d2=c(1,1,1,0,1,0,0,1,1,0,1,1,1,0,1,1,1,1,1,1,0,0,0,1,1,1,0,1,0,1,1,0,1,1,0,0,1,0,0,0,0,0,1,0),
c2=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
# initial values Exercise 5
list( q1=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5),
q2=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5))
BUGS CODE 6.3
model
{
# Weibull distribution for survival times
# right censored
for(i in 1 : M) {
for(j in 1 : N) {
t[i, j] ~ dweib(k[i], lamda[i])I(t.cen[i, j],)
#culmative.t[i, j] <- culmative(t[i, j], t[i, j])
}
lamda[i] <- exp(beta[i])
beta[i] ~ dnorm(0.0, 0.001)
# median survival times
median[i] <- pow(log(2) * exp(-beta[i]), 1/k[i])
}
for( i in 1:M){
k[i] ~ dexp(0.01)}
# difference in median survival
# note the number of betas is the number of groups
diff<- median[1] - median[2]
}
# leukemia data
# note there are two betas corresponding to two groups
list(t = structure(.Data =
c(6,6,6,7,10,13,16,22,23,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
1,1,2,2,3,4,4,5,5,8,8,8,8,11,11,12,12,15,17,22,23),
.Dim = c(2, 21)),
t.cen = structure(.Data =
c(0,0,0,0,0,0,0,0,0,6,9,10,11,17,19,20,25,32,32,34,35,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
.Dim = c(2, 21)),
M = 2, N = 21)
# initial values
list(beta = c(-1, -1))
BUGS CODE 6.4
model
{
# Weibull distribution for time to events
# right censored
for(i in 1 : M) {
for(j in 1 : N) {
t[i, j] ~ dweib(k[i], lamda[i])I(t.cen[i, j],)
#culmative.t[i, j] <- culmative(t[i, j], t[i, j])
}
lamda[i] <- exp(beta[i])
beta[i] ~ dnorm(0.0, 0.001)
# median survival times
median[i] <- pow(log(2) * exp(-beta[i]), 1/k[i])
}
for( i in 1:M){
k[i] ~ dexp(0.01)}
}
# McGilchrist and Aisbett
# time to infection
list(t = structure(.Data =
c(8,15,447,13,7,NA,15,2,96,NA,17,119,NA,33,30,511,7,NA,185,
NA,22,402,24,12,53,34,141,27,536,190,292,63,152,39,132,130,152,NA),
.Dim = c(1, 38)),
t.cen = structure(.Data =
c( 0,0,0,0,0,113,0,0,0,5,0,0,6,0,0,0,0,149,0,
22,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,54),
.Dim = c(1,38)),
M =1, N = 38)
# initial values
list(beta = c(-1))
BUGS CODE 6.5
model
{
for (i in 1 : N) {
for (j in 1 : M) {
# Survival times bounded below by censoring times:
t[i,j] ~ dweib(r, mu[i,j]) I(t.cen[i, j], );
log(mu[i,j ]) <- alpha + beta.age * age[i, j]
+ beta.sex *sex[i]
+ beta.dis[disease[i]] + b[i];
#culmative.t[i,j] <- culmative(t[i,j], t[i,j])
}
# Random effects:
b[i] ~ dnorm(0.0, tau)
}
# Priors:
alpha ~ dnorm(0.0, 0.0001);
beta.age ~ dnorm(0.0, 0.0001);
beta.sex ~ dnorm(0.0, 0.0001);
# beta.dis[1] <- 0; # corner-point constraint
for(k in 2 : 4) {
beta.dis[k] ~ dnorm(0.0, 0.0001);
}
tau ~ dgamma(1.0E-3, 1.0E-3);
r ~ dgamma(1.0, 1.0E-3);
sigma <- 1 / sqrt(tau); # s.d. of random effects
}
# kidney data
list(N = 38, M = 2,
t = structure(
.Data = c( 8, 16,
23, NA,
22, 28,
447, 318,
30, 12,
24, 245,
7, 9,
511, 30,
53, 196,
15, 154,
7, 333,
141, NA,
96, 38,
NA, NA,
536, NA,
17, NA,
185, 177,
292, 114,
NA, NA,
15, NA,
152, 562,
402, NA,
13, 66,
39, NA,
12, 40,
NA, 201,
132, 156,
34, 30,
2, 25,
130, 26,
27, 58,
NA, 43,
152, 30,
190, NA,
119, 8,
NA, NA,
NA, 78,
63, NA), .Dim = c(38, 2)),
t.cen = structure(
.Data = c( 0, 0,
0, 13,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 8,
0, 0,
149, 70,
0, 25,
0, 4,
0, 0,
0, 0,
22, 159,
0, 108,
0, 0,
0, 24,
0, 0,
0, 46,
0, 0,
113, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
5, 0,
0, 0,
0, 5,
0, 0,
54, 16,
6, 0,
0, 8), .Dim = c(38, 2)),
age = structure(
.Data = c(28, 28,
48, 48,
32, 32,
31, 32,
10, 10,
16, 17,
51, 51,
55, 56,
69, 69,
51, 52,
44, 44,
34, 34,
35, 35,
42, 42,
17, 17,
60, 60,
60, 60,
43, 44,
53, 53,
44, 44,
46, 47,
30, 30,
62, 63,
42, 43,
43, 43,
57, 58,
10, 10,
52, 52,
53, 53,
54, 54,
56, 56,
50, 51,
57, 57,
44, 45,
22, 22,
42, 42,
52, 52,
60, 60), .Dim = c(38, 2)),
beta.dis = c(0, NA, NA, NA),
sex = c(0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0),
disease = c(1, 2, 1, 1, 1, 1, 2, 2, 3, 2, 3, 1, 3, 3, 1, 3, 1, 1,2, 1, 4, 1, 3, 3,
3, 3, 2, 3, 2, 2, 3, 3, 4, 2, 1, 1, 4, 4))
# initial values
list(beta.age = 0, beta.sex = 0, beta.dis=c(NA,0,0,0),
alpha = 0, r=1, tau=0.3)
BUGS CODE 6.6
model
{
# Set up data
for(i in 1:N) {
for(j in 1:T) {
# risk set = 1 if obs.t >= t
Y[i,j] <- step(obs.t[i] - t[j] + eps)
# counting process jump = 1 if obs.t in [ t[j], t[j+1] )
# i.e. if t[j] <= obs.t < t[j+1]
dN[i, j] <- Y[i, j] * step(t[j + 1] - obs.t[i] - eps) * fail[i]
}
}
# Model
for(j in 1:T) {
for(i in 1:N) {
dN[i, j] ~ dpois(Idt[i, j]) # Likelihood
Idt[i, j] <- Y[i, j] * exp(beta * x1[i]) * dL0[j] # Intensity
}
dL0[j] ~ dgamma(mu[j], c)
mu[j] <- dL0.star[j] * c # prior mean hazard
# Survivor function = exp(-Integral{l0(u)du})^exp(beta*z)
S.treat[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta * -0.5));
S.placebo[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta * 0.5));
}
c <- 0.001
r <- 0.1
for (j in 1 : T) {
dL0.star[j] <- r * (t[j + 1] - t[j])
}
beta ~ dnorm(0.0,0.000001)
# hazard ratio for group 1 versus group 2
HR<-exp(beta)
}
# x1 is the group indicator
list(N = 42, T = 17, eps = 1.0E-10,
obs.t = c(1, 1, 2, 2, 3, 4, 4, 5, 5, 8, 8, 8, 8, 11, 11, 12, 12, 15, 17, 22, 23, 6,
6, 6, 6, 7, 9, 10, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 32, 34, 35),
fail = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0),
x1 = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
-0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5),
t = c(1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 15, 16, 17, 22, 23, 35))
# initial values
list( beta = 0.0,
dL0 = c(1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,
1.0,1.0,1.0,1.0,1.0,1.0, 1.0,1.0))
BUGS CODE 6.7
model
{
# Set up data
for(i in 1:N) {
for(j in 1:T) {
# risk set = 1 if obs.t >= t
Y[i,j] <- step(obs.t[i] - t[j] + eps)
# counting process jump = 1 if obs.t in [ t[j], t[j+1] )
# i.e. if t[j] <= obs.t < t[j+1]
dN[i, j] <- Y[i, j] * step(t[j + 1] - obs.t[i] - eps) * fail[i]
}
}
# Model
for(j in 1:T) {
for(i in 1:N) {
dN[i, j] ~ dpois(Idt[i, j]) # Likelihood
# With covariate log white blood cell count
Idt[i, j] <- Y[i, j] * exp(beta[1] * x1[i]+beta[2]*x2[i]) * dL0[j] # Intensity
}
dL0[j] ~ dgamma(mu[j], c)
mu[j] <- dL0.star[j] * c # prior mean hazard
# Survivor function = exp(-Integral{l0(u)du})^exp(beta*x1)
# adjusted for covariates at mean value of covariate
S.treat[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta[1]* -0.5+beta[2]*2.63));
S.placebo[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta[1]* 0.5+beta[2]*3.22));
}
c <- 0.001
r <- 0.1
for (j in 1 : T) {
dL0.star[j] <- r * (t[j + 1] - t[j])
}
beta[1] ~ dnorm(0.0,0.000001)
beta[2] ~ dnorm(0.0,0.000001)
# hazard ratio for group 1 versus group 2
HR.beta1<-exp(beta[1])
# HR for log wbc
HR.beta2<-exp(beta[2])
}
# x1 is the group indicator
# x2 is the log wbc count
list(N = 42, T = 17, eps = 1.0E-10,
x2=c(2,8,5,4.91,4.48,4.01,4.36,2.42,3.49,3.97,3.52,3.05,2.32,3.26,3.49,2.12,1.5,3.06,2.30,2.95,2.73,1.97,
2.31,4.06,3.28,4.43,2.96,2.88,3.60,2.32,2.57,3.20,2.80,2.70,2.60,2.16,2.05,2.01,1.78,2.20,2.53,1.47,1.45),
obs.t = c(1, 1, 2, 2, 3, 4, 4, 5, 5, 8, 8, 8, 8, 11, 11, 12, 12, 15, 17, 22, 23, 6,
6, 6, 6, 7, 9, 10, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 32, 34, 35),
fail = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0),
x1= c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
-0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5),
t = c(1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 15, 16, 17, 22, 23, 35))
# initial values
list( beta = c(0,0),
dL0 = c(1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,
1.0,1.0,1.0,1.0,1.0,1.0, 1.0,1.0))
BUGS CODE 6.7 Ext
model
{
# Set up data
for(i in 1:N) {
for(j in 1:T) {
# risk set = 1 if obs.t >= t
Y[i,j] <- step(obs.t[i] - t[j] + eps)
# counting process jump = 1 if obs.t in [ t[j], t[j+1] )
# i.e. if t[j] <= obs.t < t[j+1]
dN[i, j] <- Y[i, j] * step(t[j + 1] - obs.t[i] - eps) * fail[i]
}
}
# Model
for(j in 1:T) {
for(i in 1:N) {
dN[i, j] ~ dpois(Idt[i, j]) # Likelihood
# With covariate log white blood cell count
Idt[i, j] <- Y[i, j] * exp(beta[1] * x1[i]+beta[2]*x2[i]+beta[3]*x3[i]) * dL0[j] # Intensity
}
dL0[j] ~ dgamma(mu[j], c)
mu[j] <- dL0.star[j] * c # prior mean hazard
# Survivor function = exp(-Integral{l0(u)du})^exp(beta*z)
# adjusted for covariates at mean value of covariate
S.treat[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta[1] * -0.5+beta[2]*2.63+beta[3]*.47));
S.placebo[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta[1] * 0.5+beta[2]* 3.22+beta[3]*.52));
}
c <- 0.001
r <- 0.1
for (j in 1 : T) {
dL0.star[j] <- r * (t[j + 1] - t[j])
}
beta[1] ~ dnorm(0.0,0.000001)
beta[2] ~ dnorm(0.0,0.000001)
beta[3] ~ dnorm(0.0,0.000001)
# hazard ratio for group 1 versus group 2
HR.beta1<-exp(beta[1])
# hazard ratio for log wbc
HR.beta2<-exp(beta[2])
# HR for gender
HR.beta3<-exp(beta[3])
}
# x1 is the group indicator
# x2 is log wbc
# x3 is gender ( 1 male , 0 female)
list(N = 42, T = 17, eps = 1.0E-10,
x3=c(1,1,1,1,1,1,0,1,0,0,0,1,1,1,0,0,0,0,0,0,1,
0,1,0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1),
x2=c(2,8,5,4.91,4.48,4.01,4.36,2.32,3.49,3.97,3.52,3.05,2.42,3.26,3.49,2.12,1.5,3.06,2.30,2.95,2.73,1.97,
2.31,4.06,3.28,4.43,2.96,2.88,3.60,2.32,2.57,3.20,2.80,2.70,2.60,2.16,2.05,2.01,1.78,2.20,2.53,1.47,1.45),
obs.t = c(1, 1, 2, 2, 3, 4, 4, 5, 5, 8, 8, 8, 8, 11, 11, 12, 12, 15, 17, 22, 23, 6,
6, 6, 6, 7, 9, 10, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 32, 34, 35),
fail = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0),
x1 = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
-0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5),
t = c(1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 15, 16, 17, 22, 23, 35))
# initial values
list( beta = c(0,0,0),
dL0 = c(1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,
1.0,1.0,1.0,1.0,1.0,1.0, 1.0,1.0))
BUGS CODE 6.8
model
{
# Set up data
for(i in 1:N) {
for(j in 1:T) {
# risk set = 1 if obs.t >= t
Y[i,j] <- step(obs.t[i] - t[j] + eps)
# counting process jump = 1 if obs.t in [ t[j], t[j+1] )
# i.e. if t[j] <= obs.t < t[j+1]
dN[i, j] <- Y[i, j] * step(t[j + 1] - obs.t[i] - eps) * fail[i]
}
}
# Model
for(j in 1:T) {
for(i in 1:N) {
dN[i, j] ~ dpois(Idt[i, j]) # Likelihood
# With covariate log white blood cell count
Idt[i, j] <- Y[i, j] * exp(beta[1] * x1[i]+beta[2]*x2[i]) * dL0[j] # Intensity
}
dL0[j] ~ dgamma(mu[j], c)
mu[j] <- dL0.star[j] * c # prior mean hazard
# Survivor function = exp(-Integral{l0(u)du})^exp(beta*z)
# adjusted for covariates at mean value of covariate
# test for PH of group adjusted for wbc
S.treat[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta[1]* -0.5+beta[2]*2.63 +delta*-.5*t[j]));
S.placebo[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta[1]* 0.5+beta[2]*3.22+delta*.5*t[j]));
}
c <- 0.001
r <- 0.1
for (j in 1 : T) {
dL0.star[j] <- r * (t[j + 1] - t[j])
}
beta[1] ~ dnorm(0.0,0.000001)
delta ~ dnorm(0.0,0.000001)
beta[2] ~ dnorm(0.0,0.000001)
# hazard ratio for group 1 versus group 2
HR.beta1<-exp(beta[1])
HR.beta2<-exp(beta[2])
# x1 is the group indicator ( placebo .5 and treatment -.5)
# x2 is the log wbc
}
list(N = 42, T = 17, eps = 1.0E-10,
x2=c(2,8,5,4.91,4.48,4.01,4.36,2.42,3.49,3.97,3.52,3.05,2.32,3.26,3.49,2.12,1.5,3.06,2.30,2.95,2.73,1.97,
2.31,4.06,3.28,4.43,2.96,2.88,3.60,2.32,2.57,3.20,2.80,2.70,2.60,2.16,2.05,2.01,1.78,2.20,2.53,1.47,1.45),
obs.t = c(1, 1, 2, 2, 3, 4, 4, 5, 5, 8, 8, 8, 8, 11, 11, 12, 12, 15, 17, 22, 23, 6,
6, 6, 6, 7, 9, 10, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 32, 34, 35),
fail = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0),
x1 = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
-0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5),
t = c(1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 15, 16, 17, 22, 23, 35))
# initial values
list( beta =c( 0,.0), delta=0,
dL0 = c(1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,
1.0,1.0,1.0,1.0,1.0,1.0, 1.0,1.0))
BUGS CODE 6.9
model
{
# Set up data
for(i in 1:N) {
for(j in 1:T) {
# risk set = 1 if obs.t >= t
Y[i,j] <- step(obs.t[i] - t[j] + eps)
# counting process jump = 1 if obs.t in [ t[j], t[j+1] )
# i.e. if t[j] <= obs.t < t[j+1]
dN[i, j] <- Y[i, j] * step(t[j + 1] - obs.t[i] - eps) * fail[i]
}
}
# Model
for(j in 1:T) {
for(i in 1:N) {
dN[i, j] ~ dpois(Idt[i, j]) # Likelihood
# With covariate log white blood cell count
Idt[i, j] <- Y[i, j] * exp(beta[1] * x1[i]+beta[2]*x2[i]+beta[3]*x3[i]) * dL0[j] # Intensity
}
dL0[j] ~ dgamma(mu[j], c)
mu[j] <- dL0.star[j] * c # prior mean hazard
# Survivor function = exp(-Integral{l0(u)du})^exp(beta*z)
# adjusted for covariates at mean value of covariate
S.treat[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta[1] * -0.5+beta[2]*2.63+beta[3]*.47+delta*.47*t[j]));
S.placebo[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta[1] * 0.5+beta[2]* 3.22+beta[3]*.52+delta*.52*t[j]));
}
c <- 0.001
r <- 0.1
for (j in 1 : T) {
dL0.star[j] <- r * (t[j + 1] - t[j])
}
beta[1] ~ dnorm(0.0,0.000001)
beta[2] ~ dnorm(0.0,0.000001)
beta[3] ~ dnorm(0.0,0.000001)
delta ~ dnorm(0.0,0.000001)
# hazard ratio for group 1 versus group 2
HR.beta1<-exp(beta[1])
# hazard ratio for log wbc
HR.beta2<-exp(beta[2])
# HR for gender
HR.beta3<-exp(beta[3]) }
# x1 is the group indicator
# x2 is log wbc
# x3 is gender ( 1 male , 0 female)
list(N = 42, T = 17, eps = 1.0E-10,
x3=c(1,1,1,1,1,1,0,1,0,0,0,1,1,1,0,0,0,0,0,0,1,
0,1,0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1),
x2=c(2,8,5,4.91,4.48,4.01,4.36,2.32,3.49,3.97,3.52,3.05,2.42,3.26,3.49,2.12,1.5,3.06,2.30,2.95,2.73,1.97,
2.31,4.06,3.28,4.43,2.96,2.88,3.60,2.32,2.57,3.20,2.80,2.70,2.60,2.16,2.05,2.01,1.78,2.20,2.53,1.47,1.45),
obs.t = c(1, 1, 2, 2, 3, 4, 4, 5, 5, 8, 8, 8, 8, 11, 11, 12, 12, 15, 17, 22, 23, 6,
6, 6, 6, 7, 9, 10, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 32, 34, 35),
fail = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0),
x1 = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
-0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5),
t = c(1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 15, 16, 17, 22, 23, 35))
# initial values
list( beta = c(0,0,0), delta=0,
dL0 = c(1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,
1.0,1.0,1.0,1.0,1.0,1.0, 1.0,1.0))
Chapter 5
BUGS CODE 5.1
model;
{
# posterior distribution of mortality probabilities
for( i in 1:a){ q[i]~dbeta(m[i], b[i])}
for( i in 1:a){b[i]<- O[i]-m[i]}
# survival probabilities
for( i in 1:a){p[i]<-1-q[i]}
# overall survival
s <- p[1]*p[2]*p[3]*p[4]*p[5]*p[6]*p[7]*p[8]*p[9]*p[10]
}
# data from table 5.2
# the O vector is the number available at the beginning of the period
list(a=10, m=c(6,5,5,5,5,4,5,5,5,5),
O=c(1000,994,989,984,979, 974,970,965,960,955,950))
# initial values
list(q=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5))
BUGS CODE 5.2
Model;
{
# posterior distribution of mortality probabilities
for( i in 1:a){ q[i]~dbeta(m[i], b[i])}
for( i in 1:a){b[i]<-Op[i]-m[i]}
# the below statement applies to Table 5.4
for( i in 1:a){Op[i]<-O[i]-w[i]}
# below is applied when only half the withdrawals are used
# Applies to Table 5.6
#for( i in 1:a){Op[i]<-O[i]-w[i]/2}
# cause specific net data
#for( i in 1:a){Op[i]<-O[i]-w[i]/2-do[i]}
# survival probabilities
for( i in 1:a){p[i]<-1-q[i]}
# overall survival
#s <- p[1]*p[2]*p[3]*p[4]*p[5]*p[6]
# Overall probability of response problem 4
P<- p[1]*p[2]*p[3]*p[4]*p[5]
}
# data from table 5.4
# the O vector is the number available at the beginning of the period
# w vector is the number of withdrawals
# m vector is the number of deaths
list(a=6, m=c(207,41,9,7,18,10),
O=c(450,235,184,166,148,118),
w=c(8,10,9,11,12,9))
# data for crude cause specific information
list(a=6, m=c(51,9,5,6,11,5),
O=c(450,235,184,166,148,118),
w=c(8,10,9,11,12,9))
# data for net cause specific information
list(a=6, m=c(51,9,5,6,11,5),
O=c(450,235,184,166,148,118),
w=c(8,10,9,11,12,9),
do=c(156,32,4,1,7,5))
# data for clinical trial problem 4
list(a=5, m=c(1,1,3,4,5),
O=c(34,30,27,20,12),
w=c(3,2,4,5,1))
# initial values
list(q=c(.5,.5,.5,.5,.5,.5))
# initial values for problem 4
list(q=c(.5,.5,.5,.5,.5))
BUGS CODE 5.3
model;
{
#Dirichlet Distribution of those that do not withdraw in interval
# i is the year number
for ( i in 1:21) {for (j in 1:5){x[i,j]~dgamma(m[i,j],2)}}
for( i in 1:21){ s[i]<-sum(x[i,])}
for ( i in 1:21) {for ( j in 1:5){q[i,j]<-x[i,j]/s[i]}}
# q[i,j] have a dirichlet distribution
#q[1,1] is the probability of survival for year 1 interval (0,1)
# q[i,j] the probability of category j of year i
# Dirichlet Distribution of those that do withdraw in interval
# i is the year number
for ( i in 1:21) {for (j in 1:5){y[i,j]~dgamma(n[i,j],2)}}
for( i in 1:21){ sw[i]<-sum(y[i,])}
for ( i in 1:21) {for ( j in 1:5){qw[i,j]<-y[i,j]/sw[i]}}
# qw[i,j] have a dirichlet distribution
#qw[1,1] is the probability of survival for year 1 interval (0,1)
# qw[i,j] the probability of category j of year i
}
list(n=structure(.Data=c(
746,60,2,6,153,
727,45,5,12,134,
611,32,1,9,146,
597,27,4,12,145,
505,18,1,10,129,
462,18,2,7,122,
439, 21,2,6,131,
393,9,1,4,95,
322,8,1,7,74,
314,4,1,8,73,
257,16,1,4,78,
229,5,1,6,63,
158, 1,1,2,51,
146,2,1,3,30,
119,4,1,2,44,
112,3,1,4,24,
83,2,1,2,24,
70,1,1,4,11,
44,2,1,4,16,
46,1,2,2,16,
41,1,1,2,9),.Dim=c(21,5)),
m=structure(.Data=c(
17203,2382,57,650,571,
14053,1690,36,351,113,
11564,1162,29,283,101,
9522,864,30,258,100,
7882,534,34,188,108,
6487,433,34,185,89,
5393,271,18,132,71,
4386,196,25,129,67,
3528,178,14,101,72,
2866,107,18,86,49,
2294,85,11,51,34,
1799,45,12,60,31,
1377, 48,6,47,26,
1072,26,13,40,22,
818,22,3,37,19,
600,14,2,26,15,
420,17,1,14,13,
285,10,2,10,10,
193,2,1,7,4,
118, 1,4,7,5,
50,3,2,3,1),.Dim=c(21,5)))
# activate initial values from specification tool
BUGS CODE 5.4
Model;
{
# Study 1
# posterior distribution of mortality probabilities
for( i in 1:a){ q1[i]~dbeta(m1[i], b1[i])}
for( i in 1:a){b1[i]<-Op1[i]-m1[i]}
for( i in 1:a){Op1[i]<-O1[i]-w1[i]}
# below is applied when only half the withdrawals are used
# for( i in 1:a){Op1[i]<-O1[i]-w1[i]/2}
# survival probabilities
for( i in 1:a){p1[i]<-1-q1[i]}
# overall survival
s1 <- p1[1]*p1[2]*p1[3]*p1[4]*p1[5]*p1[6]
# Study 2
# posterior distribution of mortality probabilities
for( i in 1:a){ q2[i]~dbeta(m2[i], b2[i])}
for( i in 1:a){b2[i]<-Op2[i]-m2[i]}
for( i in 1:a){Op2[i]<-O2[i]-w2[i]}
# below is applied when only half the withdrawals are used
# for( i in 1:a){Op2[i]<-O2[i]-w2[i]/2}
# survival probabilities
for( i in 1:a){p2[i]<-1-q2[i]}
# overall survival
s2 <- p2[1]*p2[2]*p2[3]*p2[4]*p2[5]*p2[6]
d<-s1-s2
}
# data from table 5.4
# the O vector is the number available at the beginning of the period
# w vector is the number of withdrawals
# m vector is the number of deaths
list(a=6, m1=c(207,41,9,7,18,10),
O1=c(450,235,184,166,148,118),
w1=c(8,10,9,11,12,9),
m2=c(200,39,10,16,24,15),
O2=c(410,202,154,137,114,87),
w2=c(8,9,7,7,3,6))
# data for crude cause specific information Table 5.8
list(a=6, m=c(51,9,5,6,11,5),
O=c(450,235,184,166,148,118),
w=c(8,10,9,11,12,9))
# data for net cause specific information Table 5.8
list(a=6, m=c(51,9,5,6,11,5),
O=c(450,235,184,166,148,118),
w=c(8,10,9,11,12,9),
do=c(156,32,4,1,7,5))
# initial values
list(q=c(.5,.5,.5,.5,.5,.5))
BUGS CODE 5.5
model;
{
# probabilities of death first study
q1[1]~dbeta(207,243)
q1[2]~dbeta(41,194)
q1[3]~dbeta(9,175)
q1[4]~dbeta(7,159)
q1[5]~dbeta(18,130)
q1[6]~dbeta(10,108)
# probabilites of death second study
q2[1]~dbeta(200,200)
q2[2]~dbeta(39,163)
q2[3]~dbeta(10,149)
q2[4]~dbeta(16,121)
q2[5]~dbeta(24,99)
q2[6]~dbeta(15,72)
# survival probabilities first study
p1[1]~dbeta(243,207)
p1[2]~dbeta(194,41)
p1[3]~dbeta(175,9)
p1[4]~dbeta(159,7)
p1[5]~dbeta(130,18)
p1[6]~dbeta(108,10)
# survival probabilities second study
p2[1]~dbeta(200,200)
p2[2]~dbeta(163,39)
p2[3]~dbeta(149,10)
p2[4]~dbeta(121,16)
p2[5]~dbeta(99,24)
p2[6]~dbeta(72,15)
# posterior distribution of the number of deaths and survivals
for ( i in 1:6){m1[i]<-q1[i]*T1[i]}
for ( i in 1:6){m2[i]<-q2[i]*T2[i]}
for ( i in 1:6){s1[i]<-p1[i]*T1[i]}
for ( i in 1:6){s2[i]<-p2[i]*T2[i]}
# numerator of odds ratio
ORN<- m1[1]*s2[1]/850+m1[2]*s2[2]/437+m1[3]*s2[3]/343+m1[4]*s2[4]/303+m1[5]*s2[5]/271+m1[6]*s2[6]/205
# denominator of odds ratio
ORD<- m2[1]*s1[1]/850+m2[2]*s1[2]/437+m2[3]*s1[3]/343+m2[4]*s1[4]/303+m2[5]*s1[5]/271+m2[6]*s1[6]/205
# odds ratio
OR<-ORN/ORD
}
# see Table 5.16
list(T1=c(450,235,184,166,148,118),
T2=c(400,202,159,137,123,87))
# initial values
list(p1=c(.5,.5,.5,.5,.5,.5),p2=c(.5,.5,.5,.5,.5,.5),
q1=c(.5,.5,.5,.5,.5,.5),q2=c(.5,.5,.5,.5,.5,.5))
BUGS CODE 5.6
model;
{
# binomial distribution of the number of deaths
for ( i in 1:a){d[i]~dbin(q[i],O[i])}
# prior distribution for probability of death
for ( i in 1:a){ q[i]~dbeta(.01,.01)}
# survial probabilities for each event
for ( i in 1:a){ p[i]<-1-q[i]}
# product limit survival probabilities
s[1]<-p[1]
for ( i in 2:a){s[i]<-s[i-1]*p[i]}
for( i in 1:a){ m[i]<-q[i]*O[i]}
# avg numberof deaths
mm<-mean(m[])
# total number of deaths
tm<-a*mm
# average survival time
mt<-mean(t[])
# total survival time for a patients
tt<-a*mt
# average hazard
ah<-tm/tt
}
# data from table 5.18
list(a=26, O=c(26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1), d=c(1,1,0,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,0,1,1,0,1,1,1,1),
t=c(2,5,8,9,14,17,26,28,35,39,41,45,49,58,62,63,74,78,82,88,
91,94,95,99,100,104))
# data from table 5.20
list( a=23,
O=c(23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1),
d=c(1,1,0,0,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,1),
t=c(2,5,7,11,12,25,31,32,37,40,51,61,63,68,75,78,92,
97,99,105,110,110,133))
# initial values table 5.18
list(q=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5))
# inital values table 5.20
list(q=c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,
.5,.5,.5,.5,.5,.5))
Subscribe to:
Posts (Atom)