Wednesday, July 10, 2013
Chapter 4
BUGS CODE 4.1 Alternative
model;
{
# Bernoulli distribution for the observations
# theta is the probability of a heart attack
for (i in 1:400){y[i]~dbern(theta[i])}
# logistic regression of theta on age and systolic blood pressure
# with interaction
for (i in 1:400){ logit(theta[i])<-alpha+beta[1]*x1[i]+beta[2]*x2[i]+beta[3]*x1[i]*x2[i]}
# prior distributions for the regression coefficients
# uninformative priors
alpha~ dnorm(0.0000,.00001)
for( i in 1:3){beta[i]~dnorm(.0000,.00001)}
ORage<-exp(beta[1])
ORsbp<-exp(beta[2])}
# y is the occurence of a heart attack
# x1 is age
# x2 is systolic blood pressure
# age and blood pressure are coded with binary values
list( y=c(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,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,
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,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,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,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,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,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,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69,52,77,72,76,77,76,70,67,70,74,72,74,57,88,79,64,67,67,69,77,68,86,58,77,79,86,70,80,60,72,63,92,56,79,76,65,70,76,55,56,48,54,50,49,45,36,54,47,52,47,47,49,46,39,50,51,39,59,46,44,52,53,49,53,52,46,49,46,41,58,61,50,45,58,51,58,44,50,57,60,48,51,56,47,44,53,41,39,50,36,55,47,59,43,51,43,40,43,39,38,62,49,46,45,42,50,53,43,47,31,40,43,45,51,51,55,56,54,49,46,53,49,34,43,60,44,56,50,46,44,49,46,53,45,46,56,43,53,45,49,49,41,46,44,43,48,39,59,55,46,42,38,40,42,29,41,38,52,51,54,50,48,47,47,47,55,44,50,55,50,43,54,42,55,42,38,52,49,50,56,50,39,51,49,51,54,47,42,59,54,52,47,52,44,44,45,50,49,48,35,43,45,49,46,47,47,59,48,52,44,48,50,58,47,49,43,56,50,42,48,41,52,41,43,47,43,47,54,52,50,62,47,56,42,46,53,43,43,50,56,54,46,51,49,47,42,42,54,53,45,35,48,52,45,47,41,48,40,42,50,41,56,56,49,53,34,46,50,54,42,48,51,51,56,49,56,44,47,51,51,36,46,58,58,51,40,53,53,44,44,51,47,48,61,40,50,49,39,47,36,45,47,57,44,44,51,40,44,51,60,48,51,46,51,47,48,57,48,39,49,40,58,50,45,41,47,49,41,64,36,47,49,58,59,45,45,53,37,43,43,50,44,49,43,53,48,54,53,44,47,55,52,40,43,47,47,54,46,51,65,54,53,56,53,49,47,38,59,55,44,52,41,55,43,47,46,55,38,45,42,54,52,42,56,48,44,40,47,37,44,51,37,50,47,46,45,46,44,44,52,45,38,48,54,46,41,41,43,44,44,49,48,41,57,45,46,46,35,50,43,41,52,43,54,46,40,50,61,49,44,50,40,50,38,52,46,49,53),
x2=c(158,164,150,157,161,165,166,147,169,159,150,166,161,161,167,164,167,163,166,159,162,166,145,163,161,162,156,160,178,164,154,115,114,128,117,123,122,114,116,114,119,112,123,119,122,130,119,128,120,115,163,166,162,154,167,163,155,160,157,154,149,159,167,162,159,160,162,157,158,168,168,164,157,171,150,158,150,163,163,159,171,165,148,156,164,168,158,167,153,167,149,158,159,144,167,151,156,150,154,164,158,147,151,167,151,151,160,161,160,160,156,168,162,171,155,150,159,156,147,162,163,170,158,163,168,153,151,150,156,162,164,164,159,152,164,159,158,163,162,152,162,157,158,162,149,166,142,151,160,154,168,165,161,152,151,159,155,169,169,168,166,163,158,150,151,150,152,167,155,150,157,152,158,168,158,152,162,165,154,168,173,158,164,157,155,161,160,160,159,155,162,164,160,158,164,157,168,159,169,159,155,161,150,119,114,120,114,128,126,116,119,109,121,114,121,120,122,112,113,116,126,117,109,120,111,123,113,119,110,112,120,127,116,124,111,134,137,113,124,127,125,123,120,119,121,115,119,110,123,123,124,126,128,119,122,118,115,121,114,125,117,125,124,133,115,121,120,127,120,118,136,125,119,120,111,119,121,111,125,130,117,131,127,109,124,114,120,112,130,127,118,119,121,119,117,117,117,123,122,121,119,125,113,115,131,126,116,126,120,119,125,113,124,127,116,119,128,119,129,125,122,121,113,118,113,130,119,117,125,130,120,115,118,131,121,117,130,103,136,127,118,122,125,121,123,117,110,126,118,125,123,128,129,116,127,109,118,127,134,123,124,115,113,126,120,116,122,112,109,121,110,114,133,131,123,122,122,116,120,104,120,125,117,121,125,121,121,116,115,121,126,121,113,126,128,112,112,126,120,122,113,126,119,131,118,112,126,131,124,116,122,120,115,104,115,127,122,123,134,120,124,104,113,110,118,117,118,128,121,120,116,121,116,121,116,128,115,123,118,114,123,121,116,111,118,119,127,129,114,122))
list(alpha=0,beta=c(0,0,0))
BUGS CODE 4.1
model;
{
# Bernoulli distribution for the observations
# theta is the probability of a heart attack
for (i in 1:400){y[i]~dbern(theta[i])}
# logistic regression of theta on age and systolic blood pressure
for (i in 1:400){ logit(theta[i])<-alpha+beta[1]*x1[i]+beta[2]*x2[i]}
# prior distributions for the regression coefficients
# uninformative priors
alpha~ dnorm(0.0000,.00001)
beta[1]~dnorm(0.0000,.00001)
beta[2]~dnorm(0.0000,.00001)
# the odds ratio for age
ORage<-exp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,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,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,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,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,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,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,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
x1=c(77,85,87,74,56,76,69,71,74,83,67,69,52,77,72,76,77,76,70,67,70,74,72,74,57,88,79,64,67,67,69,77,68,86,58,77,79,86,70,80,60,72,63,92,56,79,76,65,70,76,55,56,48,54,50,49,45,36,54,47,52,47,47,49,46,39,50,51,39,59,46,44,52,53,49,53,52,46,49,46,41,58,61,50,45,58,51,58,44,50,57,60,48,51,56,47,44,53,41,39,50,36,55,47,59,43,51,43,40,43,39,38,62,49,46,45,42,50,53,43,47,31,40,43,45,51,51,55,56,54,49,46,53,49,34,43,60,44,56,50,46,44,49,46,53,45,46,56,43,53,45,49,49,41,46,44,43,48,39,59,55,46,42,38,40,42,29,41,38,52,51,54,50,48,47,47,47,55,44,50,55,50,43,54,42,55,42,38,52,49,50,56,50,39,51,49,51,54,47,42,59,54,52,47,52,44,44,45,50,49,48,35,43,45,49,46,47,47,59,48,52,44,48,50,58,47,49,43,56,50,42,48,41,52,41,43,47,43,47,54,52,50,62,47,56,42,46,53,43,43,50,56,54,46,51,49,47,42,42,54,53,45,35,48,52,45,47,41,48,40,42,50,41,56,56,49,53,34,46,50,54,42,48,51,51,56,49,56,44,47,51,51,36,46,58,58,51,40,53,53,44,44,51,47,48,61,40,50,49,39,47,36,45,47,57,44,44,51,40,44,51,60,48,51,46,51,47,48,57,48,39,49,40,58,50,45,41,47,49,41,64,36,47,49,58,59,45,45,53,37,43,43,50,44,49,43,53,48,54,53,44,47,55,52,40,43,47,47,54,46,51,65,54,53,56,53,49,47,38,59,55,44,52,41,55,43,47,46,55,38,45,42,54,52,42,56,48,44,40,47,37,44,51,37,50,47,46,45,46,44,44,52,45,38,48,54,46,41,41,43,44,44,49,48,41,57,45,46,46,35,50,43,41,52,43,54,46,40,50,61,49,44,50,40,50,38,52,46,49,53),
x2=c(158,164,150,157,161,165,166,147,169,159,150,166,161,161,167,164,167,163,166,159,162,166,145,163,161,162,156,160,178,164,154,115,114,128,117,123,122,114,116,114,119,112,123,119,122,130,119,128,120,115,163,166,162,154,167,163,155,160,157,154,149,159,167,162,159,160,162,157,158,168,168,164,157,171,150,158,150,163,163,159,171,165,148,156,164,168,158,167,153,167,149,158,159,144,167,151,156,150,154,164,158,147,151,167,151,151,160,161,160,160,156,168,162,171,155,150,159,156,147,162,163,170,158,163,168,153,151,150,156,162,164,164,159,152,164,159,158,163,162,152,162,157,158,162,149,166,142,151,160,154,168,165,161,152,151,159,155,169,169,168,166,163,158,150,151,150,152,167,155,150,157,152,158,168,158,152,162,165,154,168,173,158,164,157,155,161,160,160,159,155,162,164,160,158,164,157,168,159,169,159,155,161,150,119,114,120,114,128,126,116,119,109,121,114,121,120,122,112,113,116,126,117,109,120,111,123,113,119,110,112,120,127,116,124,111,134,137,113,124,127,125,123,120,119,121,115,119,110,123,123,124,126,128,119,122,118,115,121,114,125,117,125,124,133,115,121,120,127,120,118,136,125,119,120,111,119,121,111,125,130,117,131,127,109,124,114,120,112,130,127,118,119,121,119,117,117,117,123,122,121,119,125,113,115,131,126,116,126,120,119,125,113,124,127,116,119,128,119,129,125,122,121,113,118,113,130,119,117,125,130,120,115,118,131,121,117,130,103,136,127,118,122,125,121,123,117,110,126,118,125,123,128,129,116,127,109,118,127,134,123,124,115,113,126,120,116,122,112,109,121,110,114,133,131,123,122,122,116,120,104,120,125,117,121,125,121,121,116,115,121,126,121,113,126,128,112,112,126,120,122,113,126,119,131,118,112,126,131,124,116,122,120,115,104,115,127,122,123,134,120,124,104,113,110,118,117,118,128,121,120,116,121,116,121,116,128,115,123,118,114,123,121,116,111,118,119,127,129,114,122))
# Initial values
list(alpha=0,beta=c(0,0))
BUGS CODE 4.2
model;
{
# binomial distribution for the number of respondents
for( i in 1:4){y[i]~dbin(theta[i],n[i])}
# logistic regression of theta
for( i in 1:4){logit(theta[i])<-alpha+beta[1]*x1[i]+beta[2]*x2[i]+beta[3]*x3[i]}
# prior distriburions
alpha~dnorm(.0000,.0001)
for( i in 1:3){beta[i]~dnorm(.0000,.00001)}
# odds ratio of first group versus fourth
OR1<-exp(beta[1])
OR2<-exp(beta[2])
OR3<-exp(beta[3])
for( i in 1:4){z[i]~dbin(theta[i],n[i])}
}
# data from Hosmer and Lemeshow page 48
# below is the number with CHD
list(y=c(5,20,15,10),
# n is the number in each group
n=c(25,30,25,20),
x1=c(1,0,0,0),
x2=c(0,1,0,0),
x3=c(0,0,1,0))
# data below Table 4.1
list(y=c(2,1,4,5),
# n is the number in each group
n=c(31,19,153,247),
x1=c(1,0,0,0),
x2=c(0,1,0,0),
x3=c(0,0,1,0))
# initial values
list(alpha =0, beta=c(0,0,0))
BUGS CODE 4.3
model;
{
for( i in 1:n){ y[i]~dnorm(mu[i], tau)}
for( i in 1:n){ z[i]~dnorm(mu[i], tau)}
for(i in 1:n){ mu[i]<- alpha+beta*x[i]}
# alpha is intercept
# uninformative priors
alpha~dnorm(0,.0001)
# beta is slope
beta~dnorm(0,.0001)
tau~dgamma(.00001,.00001)
# sigsq is variance about regression line
sigsq<-1/tau
}
# hypothetical example Table 4.7
list(n=15,x=c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3),
y=c(3.2,2.9,3.0,3.3,2.8,5.3,4.7,4.9,5.2,5.1,7.4,6.9,
7.2,6.8,7.0))
# data from table 4.9
list(n=30,
x=c(14,12,16,13,11,21,17,15,17,20,21,17,23,12,16,15,19,24,24,12,23,23,20,13,18,18,19,24,25,22),
y=c(97,100,100,106,119,111,106,117,111,126,115,118,124,126,116,131,125,125,128,130,138,131,129,127,131,126,142,165,157,163))
# initial values
list( alpha= 0,beta=0,tau=1)
BUGS CODE 4.4.
model;
{
for( i in 1:n){ y[i]~dnorm(mu[i], tau)}
for( i in 1:n){ z[i]~dnorm(mu[i], tau)}
for(i in 1:n){ mu[i]<- alpha+beta[1]*x1[i]+beta[2]*x2[i]}
# alpha is intercept
# uninformative priors
alpha~dnorm(0,.0001)
# regresson coefficients for x1 and x2
beta[1]~dnorm(0,.0001)
beta[2]~dnorm(0,.0001)
# prior for precision
tau~dgamma(.00001,.00001)
# sigsq is variance about regression line
sigsq<-1/tau
}
# data from table 4.9
# y is systolic blood pressure
# x1 is age
#x2 is weight
list(n=30,
x1=c(14,12,16,13,11,21,17,15,17,20,21,17,23,12,16,15,19,24,24,12,23,23,20,13,18,18,19,24,25,22),
y=c(97,100,100,106,119,111,106,117,111,126,115,118,124,126,116,131,125,125,128,130,138,131,129,127,131,126,142,165,157,163),
x2=c(94,95,104,107,108,116,117,117,122,124,125,125,125,128,128,129,134,134,136,137,137,138,139,141,141,143,150,171,172,172))
# cigazrette data below
# x1 is income
# x2 is price per pack
y is consumption
list(n=51, x1=c(2948,4644,3665,2878,4493,3855,4917,4524,5079,3738,3354,4623,
3290,4507,3772,3751,3853,3112,3090,3302,4309,4340,4180,3859,
2626,3781,3500,3789,4563,3737,4701,3077,4712,3252,3086,4020,
3387,3719,3971,3959,2990,3123,3119,3606,3227,3468,3712,4053,
3061,3812,3815),
x2=c(42.70,41.80,38.50,38.80,39.70,31.10,45.50,41.30,32.60,43.80,
35.80,36.70,33.60,41.40,32.20,38.50,38.90,30.10,39.30,38.80,
34.20,41.00,39.20,40.10,37.50,36.80,43.70,34.70,44.00,34.10,
41.70,41.70,41.70,29.40,38.90,38.10,39.80,29.00,44.70,40.20,
34.30,38.50,41.60,42.00,36.60,39.50,30.20,40.30,41.60,40.20,
34.40),
y=c(90,121,115,100,123,125,120,155,200,124,110,82,102,125,135,109,
114,156,116,129,124,124,129,104,93,121,111,108,190,266,121,90,
119,172,94,122,108,157,107,124,104,93,100,106,66,123,124,97,
115,106,132))
# inital values
list( alpha= 0,beta=c(0,0), tau=1)
BUGS CODE 4.5
model;
{
for( i in 1:n){ y[i]~dnorm(mu[i], tau)}
for( i in 1:n){ z[i]~dnorm(mu[i], tau)}
for(i in 1:n){ mu[i]<-alpha+beta[1]*x1[i]+beta[2]*x2[i]+beta[3]*x3[i]}
# alpha is intercept
# uninformative priors
# y is the projected per capita expendure for education
#x1 is the number of residents per thousand living in urban areas
# x2 is the 1973 per capita income
# x3 is the number of residents per thousand under 18
# regression coefficients
beta[1]~dnorm(0,.0001)
beta[2]~dnorm(0,.0001)
beta[3]~dnorm(0,.0001)
alpha~dnorm(0.0,.0001)
# prior for precision
tau~dgamma(.000001,.000001)
# sigsq is variance about regression line
sigsq<-1/tau
}
# data for state education expenditure data
list(n=50,
# URB70
x1=c(508,564,322,846,871,774,856,889,715,753,649,830,738,659,664,
572,701,443,446,615,661,722,766,631,390,450,476,603,805,523,
588,584,445,500,661,680,797,534,541,605,785,698,796,804,809,
726,671,609,484,831),
# state expenditure education
y=c(235,231,270,261,300,317,387,285,300,221,264,308,379,342,378,
232,231,246,230,268,337,344,330,261,214,245,233,250,243,216,
212,208,215,221,244,234,269,302,268,323,304,317,332,315,291,
312,316,332,NA,311),
# PI 73
x2=c(
3944,4578,4011,5233,4780,5889,5663,5759,4894,5012,4908,5753,
5439,4634,4921,4869,4672,4782,4296,4827,5057,5540,5331,4715,
3828,4120,3817,4243,4647,3967,3946,3724,3448,3680,3825,4189,
4336,4418,4323,4813,5046,3764,4504,4005,5560,4989,4697,5438,
5613,5309),
# Y74
x3=c(325,323,328,305,303,307,301,310,300,324,329,320,337,328,330,
318,309,333,330,318,304,328,323,317,310,321,342,339,287,325,
315,332,358,320,355,306,335,335,344,331,324,366,340,378,330,
313,305,307,386,333))
# weighted observations
# log transformation
list(n=50,
# URB70
x1=c(508,564,322,846,871,774,856,889,715,753,649,830,738,659,664,
572,701,443,446,615,661,722,766,631,390,450,476,603,805,523,
588,584,445,500,661,680,797,534,541,605,785,698,796,804,809,
726,671,609,484,831),
# state expenditure education
y=c(5.46,5.44,5.60,5.56,5.70,5.76,5.96,5.65,5.70,5.40,5.58,5.73,5.94,5.83,5.93,5.45,5.44,5.51,5.44,5.59,5.82,5.84,5.80,5.56,5.37,5.50,5.45,5.52,5.49,5.38,5.36,5.34,5.37,5.40,5.50,5.46,5.59,5.71,5.59,5.78,5.72,5.76,5.81,5.75,5.67,5.74,5.76,5.81,NA,5.74),
# PI 73
x2=c(
3944,4578,4011,5233,4780,5889,5663,5759,4894,5012,4908,5753,
5439,4634,4921,4869,4672,4782,4296,4827,5057,5540,5331,4715,
3828,4120,3817,4243,4647,3967,3946,3724,3448,3680,3825,4189,
4336,4418,4323,4813,5046,3764,4504,4005,5560,4989,4697,5438,
5613,5309),
# Y74
x3=c(325,323,328,305,303,307,301,310,300,324,329,320,337,328,330,
318,309,333,330,318,304,328,323,317,310,321,342,339,287,325,
315,332,358,320,355,306,335,335,344,331,324,366,340,378,330,
313,305,307,386,333))
# square root transformation
# state expenditure education
# weighted observations
list(n=50,
# URB70
x1=c(508,564,322,846,871,774,856,889,715,753,649,830,738,659,664,
572,701,443,446,615,661,722,766,631,390,450,476,603,805,523,
588,584,445,500,661,680,797,534,541,605,785,698,796,804,809,
726,671,609,484,831),
y=c(15.33,15.20,16.43,16.16,17.32,17.80,19.67,16.88,17.32,14.87,
16.25,17.55,19.47,18.49,19.44,15.23,15.20,15.68,15.17,16.37,
18.36,18.55,18.17,16.16,14.63,15.65,15.26,15.81,15.59,14.70,
14.56,14.42,14.66,14.87,15.62,15.30,16.40,17.38,16.37,17.97,17.44,17.80,18.22,17.75,17.06,17.66,17.78,18.22,NA,17.64),
# PI 73
x2=c(
3944,4578,4011,5233,4780,5889,5663,5759,4894,5012,4908,5753,
5439,4634,4921,4869,4672,4782,4296,4827,5057,5540,5331,4715,
3828,4120,3817,4243,4647,3967,3946,3724,3448,3680,3825,4189,
4336,4418,4323,4813,5046,3764,4504,4005,5560,4989,4697,5438,
5613,5309),
# Y74
x3=c(325,323,328,305,303,307,301,310,300,324,329,320,337,328,330,
318,309,333,330,318,304,328,323,317,310,321,342,339,287,325,
315,332,358,320,355,306,335,335,344,331,324,366,340,378,330,
313,305,307,386,333))
# inital values
list(alpha=0,beta=c(0,0,0), tau =1)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment