forked from boboppie/kruschke-doing_bayesian_data_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathANOVAonewayNonhomogvarBrugs.R
255 lines (234 loc) · 8.54 KB
/
ANOVAonewayNonhomogvarBrugs.R
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
graphics.off()
rm(list=ls(all=TRUE))
fnroot = "ANOVAonewayNonhomogvarBrugs"
library(BRugs) # Kruschke, J. K. (2010). Doing Bayesian data analysis:
# A Tutorial with R and BUGS. Academic Press / Elsevier.
#------------------------------------------------------------------------------
# THE MODEL.
modelstring = "
# BUGS model specification begins here...
model {
for ( i in 1:Ntotal ) {
y[i] ~ dnorm( mu[i] , tau[x[i]] )
mu[i] <- a0 + a[x[i]]
}
a0 ~ dnorm(0,0.001)
for ( j in 1:NxLvl ) {
a[j] ~ dnorm( 0.0 , atau )
tau[j] ~ dgamma( sG , rG )
}
sG <- pow(m,2)/pow(d,2)
rG <- m/pow(d,2)
m ~ dgamma(1,1)
d ~ dgamma(1,1)
atau <- 1 / pow( aSD , 2 )
aSD <- abs( aSDunabs ) + .1
aSDunabs ~ dt( 0 , 0.001 , 2 )
}
# ... end BUGS model specification
" # close quote for modelstring
# Write model to a file, and send to BUGS:
writeLines(modelstring,con="model.txt")
modelCheck( "model.txt" )
#------------------------------------------------------------------------------
# THE DATA.
# Specify data source:
dataSource = c( "McDonaldSK1991" , "SolariLS2008" , "Random" )[1]
# Load the data:
if ( dataSource == "McDonaldSK1991" ) {
fnroot = paste( fnroot , dataSource , sep="" )
datarecord = read.table( "McDonaldSK1991data.txt", header=T ,
colClasses=c("factor","numeric") )
y = as.numeric(datarecord$Size)
Ntotal = length(datarecord$Size)
x = as.numeric(datarecord$Group)
xnames = levels(datarecord$Group)
NxLvl = length(unique(datarecord$Group))
contrastList = list( BIGvSMALL = c(-1/3,-1/3,1/2,-1/3,1/2) ,
ORE1vORE2 = c(1,-1,0,0,0) ,
ALAvORE = c(-1/2,-1/2,1,0,0) ,
NPACvORE = c(-1/2,-1/2,1/2,1/2,0) ,
USAvRUS = c(1/3,1/3,1/3,-1,0) ,
FINvPAC = c(-1/4,-1/4,-1/4,-1/4,1) ,
ENGvOTH = c(1/3,1/3,1/3,-1/2,-1/2) ,
FINvRUS = c(0,0,0,-1,1) )
}
if ( dataSource == "SolariLS2008" ) {
fnroot = paste( fnroot , dataSource , sep="" )
datarecord = read.table("SolariLS2008data.txt", header=T ,
colClasses=c("factor","numeric") )
y = as.numeric(datarecord$Acid)
Ntotal = length(datarecord$Acid)
x = as.numeric(datarecord$Type)
xnames = levels(datarecord$Type)
NxLvl = length(unique(datarecord$Type))
contrastList = list( G3vOTHER = c(-1/8,-1/8,1,-1/8,-1/8,-1/8,-1/8,-1/8,-1/8) )
}
if ( dataSource == "Random" ) {
fnroot = paste( fnroot , dataSource , sep="" )
#set.seed(47405)
ysdtrue = 4.0
a0true = 100
atrue = c( 2 , -2 ) # sum to zero
npercell = 8
datarecord = matrix( 0, ncol=2 , nrow=length(atrue)*npercell )
colnames(datarecord) = c("y","x")
rowidx = 0
for ( xidx in 1:length(atrue) ) {
for ( subjidx in 1:npercell ) {
rowidx = rowidx + 1
datarecord[rowidx,"x"] = xidx
datarecord[rowidx,"y"] = ( a0true + atrue[xidx] + rnorm(1,0,ysdtrue) )
}
}
datarecord = data.frame( y=datarecord[,"y"] , x=as.factor(datarecord[,"x"]) )
y = as.numeric(datarecord$y)
Ntotal = length(y)
x = as.numeric(datarecord$x)
xnames = levels(datarecord$x)
NxLvl = length(unique(x))
# Construct list of all pairwise comparisons, to compare with NHST TukeyHSD:
contrastList = NULL
for ( g1idx in 1:(NxLvl-1) ) {
for ( g2idx in (g1idx+1):NxLvl ) {
cmpVec = rep(0,NxLvl)
cmpVec[g1idx] = -1
cmpVec[g2idx] = 1
contrastList = c( contrastList , list( cmpVec ) )
}
}
}
# Specify the data in a form that is compatible with BRugs model, as a list:
ySDorig = sd(y)
yMorig = mean(y)
z = ( y - yMorig ) / ySDorig
datalist = list(
y = z ,
x = x ,
Ntotal = Ntotal ,
NxLvl = NxLvl
)
# Get the data into BRugs:
modelData( bugsData( datalist ) )
#------------------------------------------------------------------------------
# INTIALIZE THE CHAINS.
# Autocorrelation within chains is large, so use several chains to reduce
# degree of thinning. But we still have to burn-in all the chains, which takes
# more time with more chains (on serial CPUs).
nchain = 10
modelCompile( numChains = nchain )
if ( F ) {
modelGenInits() # often won't work for diffuse prior
} else {
# initialization based on data
theData = data.frame( y=datalist$y , x=factor(x,labels=xnames) )
a0 = mean( theData$y )
a = aggregate( theData$y , list( theData$x ) , mean )[,2] - a0
tau = 1/(aggregate( theData$y , list( theData$x ) , sd )[,2])^2
genInitList <- function() {
return(
list(
a0 = a0 ,
a = a ,
tau = tau ,
m = mean( tau ) ,
d = sd( tau ) ,
aSDunabs = sd(a)
)
)
}
for ( chainIdx in 1 : nchain ) {
modelInits( bugsInits( genInitList ) )
}
}
#------------------------------------------------------------------------------
# RUN THE CHAINS
# burn in
BurnInSteps = 10000
modelUpdate( BurnInSteps )
# actual samples
samplesSet( c( "a0" , "a" , "tau", "m", "d", "aSD" ) )
stepsPerChain = ceiling(2000/nchain)
thinStep = 500 # 750
modelUpdate( stepsPerChain , thin=thinStep )
#------------------------------------------------------------------------------
# EXAMINE THE RESULTS
source("plotChains.R")
source("plotPost.R")
checkConvergence = T
if ( checkConvergence ) {
sumInfo = plotChains( "a0" , saveplots=F , filenameroot=fnroot )
sumInfo = plotChains( "a" , saveplots=F , filenameroot=fnroot )
sumInfo = plotChains( "tau" , saveplots=F , filenameroot=fnroot )
sumInfo = plotChains( "m" , saveplots=F , filenameroot=fnroot )
sumInfo = plotChains( "d" , saveplots=F , filenameroot=fnroot )
sumInfo = plotChains( "aSD" , saveplots=F , filenameroot=fnroot )
}
# Extract and plot the SDs:
aSDSample = samplesSample("aSD")
windows()
layout( matrix(1:2,nrow=2) )
par( mar=c(3,1,2.5,0) , mgp=c(2,0.7,0) )
plotPost( aSDSample , xlab="aSD" , main="a SD" , breaks=30 )
dev.copy2eps(file=paste(fnroot,"SD.eps",sep=""))
# Extract a and tau values:
a0Sample = samplesSample( "a0" )
chainLength = length(a0Sample)
aSample = array( 0 , dim=c( datalist$NxLvl , chainLength ) )
for ( xidx in 1:datalist$NxLvl ) {
aSample[xidx,] = samplesSample( paste("a[",xidx,"]",sep="") )
}
tauSample = array( 0 , dim=c( datalist$NxLvl , chainLength ) )
for ( xidx in 1:datalist$NxLvl ) {
tauSample[xidx,] = samplesSample( paste("tau[",xidx,"]",sep="") )
}
# Convert a to zero-centered b values:
mSample = array( 0, dim=c( datalist$NxLvl , chainLength ) )
for ( stepIdx in 1:chainLength ) {
mSample[,stepIdx ] = ( a0Sample[stepIdx] + aSample[,stepIdx] )
}
b0Sample = apply( mSample , 2 , mean )
bSample = mSample - matrix(rep( b0Sample ,NxLvl),nrow=NxLvl,byrow=T)
# Convert from standardized b values to original scale b values:
b0Sample = b0Sample * ySDorig + yMorig
bSample = bSample * ySDorig
# Plot b values:
windows(datalist$NxLvl*2.75,2.5)
layout( matrix( 1:datalist$NxLvl , nrow=1 ) )
par( mar=c(3,1,2.5,0) , mgp=c(2,0.7,0) )
for ( xidx in 1:datalist$NxLvl ) {
plotPost( bSample[xidx,] , breaks=30 ,
xlab=bquote(beta*1[.(xidx)]) ,
main=paste("x:",xnames[xidx]) )
}
dev.copy2eps(file=paste(fnroot,"b.eps",sep=""))
# Plot tau values:
windows(datalist$NxLvl*2.75,2.5)
layout( matrix( 1:datalist$NxLvl , nrow=1 ) )
par( mar=c(3,1,2.5,0) , mgp=c(2,0.7,0) )
for ( xidx in 1:datalist$NxLvl ) {
plotPost( tauSample[xidx,] , breaks=30 ,
xlab=bquote(tau[.(xidx)]) ,
main=paste("x:",xnames[xidx]) )
}
dev.copy2eps(file=paste(fnroot,"tau.eps",sep=""))
# Display contrast analyses
nContrasts = length( contrastList )
if ( nContrasts > 0 ) {
nPlotPerRow = 5
nPlotRow = ceiling(nContrasts/nPlotPerRow)
nPlotCol = ceiling(nContrasts/nPlotRow)
windows(3.75*nPlotCol,2.5*nPlotRow)
layout( matrix(1:(nPlotRow*nPlotCol),nrow=nPlotRow,ncol=nPlotCol,byrow=T) )
par( mar=c(4,0.5,2.5,0.5) , mgp=c(2,0.7,0) )
for ( cIdx in 1:nContrasts ) {
contrast = matrix( contrastList[[cIdx]],nrow=1) # make it a row matrix
incIdx = contrast!=0
histInfo = plotPost( contrast %*% bSample , compVal=0 , breaks=30 ,
xlab=paste( round(contrast[incIdx],2) , xnames[incIdx] ,
c(rep("+",sum(incIdx)-1),"") , collapse=" " ) ,
cex.lab = 1.0 ,
main=paste( "X Contrast:", names(contrastList)[cIdx] ) )
}
dev.copy2eps(file=paste(fnroot,"xContrasts.eps",sep=""))
}