-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathStan-Ybinom-XnomSsubjCcat-MbinomBetaOmegaKappa.R
350 lines (338 loc) · 15 KB
/
Stan-Ybinom-XnomSsubjCcat-MbinomBetaOmegaKappa.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
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
# Stan-Ydich-XnomSsubj-MbernBetaOmegaKappa.R
# Accompanies the book:
# Kruschke, J. K. (2014). Doing Bayesian Data Analysis:
# A Tutorial with R, JAGS, and Stan. 2nd Edition. Academic Press / Elsevier.
# Adapted for Stan by Joe Houpt
source("DBDA2E-utilities.R")
#===============================================================================
genMCMC = function( data , zName="z" , NName="N" , sName="s" , cName="c" ,
numSavedSteps=50000 , saveName=NULL , thinSteps=1 ,
nChains=nChainsDefault ) {
require(rstan)
#-----------------------------------------------------------------------------
# THE DATA.
# N.B.: This function expects the data to be a data frame,
# with one component named y being a vector of integer 0,1 values,
# and one component named s being a factor of subject identifiers.
z = data[[zName]]
N = data[[NName]]
s = data[[sName]]
c = data[[cName]]
n_subj = length(unique(s))
n_cat = length(unique(c))
# Specify the data in a list, for later shipment to JAGS:
dataList = list(
z = z ,
N = N ,
c = as.numeric(c) , # c in STAN is numeric, in R is possibly factor
n_subj = n_subj,
n_cat = n_cat
)
#-----------------------------------------------------------------------------
# INTIALIZE THE CHAINS.
# Initial values of MCMC chains based on data:
initsList = function() {
thetaInit = rep(NA,n_subj)
for ( sIdx in 1:n_subj ) { # for each subject
resampledZ = rbinom(1, size=N[sIdx] , prob=z[sIdx]/N[sIdx] )
thetaInit[sIdx] = resampledZ/N[sIdx]
}
thetaInit = 0.001+0.998*thetaInit # keep away from 0,1
kappaInit = 100 # lazy, start high and let burn-in find better value
return( list( theta=thetaInit ,
omega=aggregate(thetaInit,by=list(c),FUN=mean)$x ,
omegaO=mean(thetaInit) ,
kappaMinusTwo=rep(kappaInit-2,n_cat) ,
kappaMinusTwoO=kappaInit-2 ) )
}
#-----------------------------------------------------------------------------
# RUN THE CHAINS
parameters = c( "theta","omega","kappa", "omega0", "kappa0") # The parameters to be monitored
burnInSteps = 500 # Number of steps to burn-in the chains
nChains = 4 # nChains should be 2 or more for diagnostics
# Translate to C++ and compile to DSO:
stanDso <- stan_model( file="Ybinom-XnomSsubjCcat-MbinomBetaOmegaKappa.stan" )
# Get MC sample of posterior:
stanFit <- sampling( object=stanDso ,
data = dataList ,
#pars = parameters , # optional
chains = nChains ,
iter = ( ceiling(numSavedSteps/nChains)*thinSteps
+burnInSteps ) ,
warmup = burnInSteps ,
thin = thinSteps ,
init = initsList ) # optional
# Or, accomplish above in one "stan" command; note stanDso is not separate.
# For consistency with JAGS-oriented functions in DBDA2E collection,
# convert stan format to coda format:
codaSamples = mcmc.list( lapply( 1:ncol(stanFit) ,
function(x) { mcmc(as.array(stanFit)[,x,]) } ) )
# resulting codaSamples object has these indices:
# codaSamples[[ chainIdx ]][ stepIdx , paramIdx ]
if ( !is.null(saveName) ) {
save( codaSamples , file=paste(saveName,"Mcmc.Rdata",sep="") )
save( stanFit , file=paste(saveName,"StanFit.Rdata",sep="") )
save( stanDso , file=paste(saveName,"StanDso.Rdata",sep="") )
}
return( codaSamples )
} # end function
#===============================================================================
smryMCMC = function( codaSamples , compVal=0.5 , rope=NULL ,
diffSVec=NULL , diffCVec=NULL ,
compValDiff=0.0 , ropeDiff=NULL ,
saveName=NULL ) {
mcmcMat = as.matrix(codaSamples,chains=TRUE)
summaryInfo = NULL
rowIdx = 0
# omega:
for ( parName in grep("omega",colnames(mcmcMat),value=TRUE) ) {
summaryInfo = rbind( summaryInfo ,
summarizePost( mcmcMat[,parName] ,
compVal=compVal , ROPE=rope ) )
rowIdx = rowIdx+1
rownames(summaryInfo)[rowIdx] = parName
}
# kappa:
for ( parName in grep("kappa",colnames(mcmcMat),value=TRUE) ) {
summaryInfo = rbind( summaryInfo ,
summarizePost( mcmcMat[,parName] ,
compVal=NULL , ROPE=NULL ) )
rowIdx = rowIdx+1
rownames(summaryInfo)[rowIdx] = parName
}
# theta:
for ( parName in grep("theta",colnames(mcmcMat),value=TRUE) ) {
summaryInfo = rbind( summaryInfo ,
summarizePost( mcmcMat[,parName] ,
compVal=compVal , ROPE=rope ) )
rowIdx = rowIdx+1
rownames(summaryInfo)[rowIdx] = parName
}
# differences of theta's:
if ( !is.null(diffSVec) ) {
Nidx = length(diffSVec)
for ( t1Idx in 1:(Nidx-1) ) {
for ( t2Idx in (t1Idx+1):Nidx ) {
parName1 = paste0("theta[",diffSVec[t1Idx],"]")
parName2 = paste0("theta[",diffSVec[t2Idx],"]")
summaryInfo = rbind( summaryInfo ,
summarizePost( mcmcMat[,parName1]-mcmcMat[,parName2] ,
compVal=compValDiff , ROPE=ropeDiff ) )
rowIdx = rowIdx+1
rownames(summaryInfo)[rowIdx] = paste0(parName1,"-",parName2)
}
}
}
# differences of omega's:
if ( !is.null(diffCVec) ) {
Nidx = length(diffCVec)
for ( t1Idx in 1:(Nidx-1) ) {
for ( t2Idx in (t1Idx+1):Nidx ) {
parName1 = paste0("omega[",diffCVec[t1Idx],"]")
parName2 = paste0("omega[",diffCVec[t2Idx],"]")
summaryInfo = rbind( summaryInfo ,
summarizePost( mcmcMat[,parName1]-mcmcMat[,parName2] ,
compVal=compValDiff , ROPE=ropeDiff ) )
rowIdx = rowIdx+1
rownames(summaryInfo)[rowIdx] = paste0(parName1,"-",parName2)
}
}
}
# save:
if ( !is.null(saveName) ) {
write.csv( summaryInfo , file=paste(saveName,"SummaryInfo.csv",sep="") )
}
show( summaryInfo )
return( summaryInfo )
}
#===============================================================================
plotMCMC = function( codaSamples ,
data , zName="z" , NName="N" , sName="s" , cName="c" ,
compVal=0.5 , rope=NULL ,
diffSList=NULL , diffCList=NULL ,
compValDiff=0.0 , ropeDiff=NULL ,
saveName=NULL , saveType="jpg" ) {
#-----------------------------------------------------------------------------
# N.B.: This function expects the data to be a data frame,
# with one component z being a vector of integer # successes,
# one component N being a vector of integer # attempts,
# one component s being a factor of subject identifiers,
# and one component c being a factor of category identifiers, with
# subjects nested in categories.
z = data[[zName]]
N = data[[NName]]
s = data[[sName]]
c = data[[cName]]
n_subj = length(unique(s))
n_cat = length(unique(c))
# Now plot the posterior:
mcmcMat = as.matrix(codaSamples,chains=TRUE)
chainLength = NROW( mcmcMat )
# kappa:
parNames = sort(grep("kappa",colnames(mcmcMat),value=TRUE))
nPanels = length(parNames)
nCols = 4
nRows = ceiling(nPanels/nCols)
openGraph(width=2.5*nCols,height=2.0*nRows)
par( mfcol=c(nRows,nCols) )
par( mar=c(3.5,1,3.5,1) , mgp=c(2.0,0.7,0) )
#xLim = range( mcmcMat[,parNames] )
xLim=quantile(mcmcMat[,parNames],probs=c(0.000,0.995))
mainLab = c(levels(myData[[cName]]),"Overall")
mainIdx = 0
for ( parName in parNames ) {
mainIdx = mainIdx+1
postInfo = plotPost( mcmcMat[,parName] , compVal=compVal , ROPE=rope ,
xlab=bquote(.(parName)) , cex.lab=1.25 ,
main=mainLab[mainIdx] , cex.main=1.5 ,
xlim=xLim , border="skyblue" )
}
if ( !is.null(saveName) ) {
saveGraph( file=paste(saveName,"Kappa",sep=""), type=saveType)
}
# omega:
parNames = sort(grep("omega",colnames(mcmcMat),value=TRUE))
nPanels = length(parNames)
nCols = 4
nRows = ceiling(nPanels/nCols)
openGraph(width=2.5*nCols,height=2.0*nRows)
par( mfcol=c(nRows,nCols) )
par( mar=c(3.5,1,3.5,1) , mgp=c(2.0,0.7,0) )
#xLim = range( mcmcMat[,parNames] )
xLim=quantile(mcmcMat[,parNames],probs=c(0.001,0.999))
mainLab = c(levels(myData[[cName]]),"Overall")
mainIdx = 0
for ( parName in parNames ) {
mainIdx = mainIdx+1
postInfo = plotPost( mcmcMat[,parName] , compVal=compVal , ROPE=rope ,
xlab=bquote(.(parName)) , cex.lab=1.25 ,
main=mainLab[mainIdx] , cex.main=1.5 ,
xlim=xLim , border="skyblue" )
}
if ( !is.null(saveName) ) {
saveGraph( file=paste(saveName,"Omega",sep=""), type=saveType)
}
# Plot individual omega's and differences:
if ( !is.null(diffCList) ) {
for ( compIdx in 1:length(diffCList) ) {
diffCVec = diffCList[[compIdx]]
Nidx = length(diffCVec)
temp=NULL
for ( i in 1:Nidx ) {
temp = c( temp , which(levels(myData[[cName]])==diffCVec[i]) )
}
diffCVec = temp
openGraph(width=2.5*Nidx,height=2.0*Nidx)
par( mfrow=c(Nidx,Nidx) )
xLim = range(c( compVal, rope,
mcmcMat[,paste0("omega[",diffCVec,"]")] ))
for ( t1Idx in 1:Nidx ) {
for ( t2Idx in 1:Nidx ) {
parName1 = paste0("omega[",diffCVec[t1Idx],"]")
parName2 = paste0("omega[",diffCVec[t2Idx],"]")
if ( t1Idx > t2Idx) {
# plot.new() # empty plot, advance to next
par( mar=c(3,3,3,1) , mgp=c(2.0,0.7,0) , pty="s" )
nToPlot = 700
ptIdx = round(seq(1,chainLength,length=nToPlot))
plot ( mcmcMat[ptIdx,parName2] , mcmcMat[ptIdx,parName1] ,
cex.main=1.25 , cex.lab=1.25 ,
xlab=levels(myData[[cName]])[diffCVec[t2Idx]] ,
ylab=levels(myData[[cName]])[diffCVec[t1Idx]] ,
col="skyblue" )
abline(0,1,lty="dotted")
} else if ( t1Idx == t2Idx ) {
par( mar=c(3,1.5,3,1.5) , mgp=c(2.0,0.7,0) , pty="m" )
postInfo = plotPost( mcmcMat[,parName1] ,
compVal=compVal , ROPE=rope ,
cex.main=1.25 , cex.lab=1.25 ,
xlab=bquote(.(parName1)) ,
main=levels(myData[[cName]])[diffCVec[t1Idx]] ,
xlim=xLim )
} else if ( t1Idx < t2Idx ) {
par( mar=c(3,1.5,3,1.5) , mgp=c(2.0,0.7,0) , pty="m" )
postInfo = plotPost( mcmcMat[,parName1]-mcmcMat[,parName2] ,
compVal=compValDiff , ROPE=ropeDiff ,
cex.main=1.25 , cex.lab=1.25 ,
xlab=bquote("Difference of "*omega*"'s") ,
main=paste(
levels(myData[[cName]])[diffCVec[t1Idx]] ,
"-",
levels(myData[[cName]])[diffCVec[t2Idx]] ) )
}
}
}
if ( !is.null(saveName) ) {
saveGraph( file=paste0(saveName,"OmegaDiff",compIdx), type=saveType)
}
}
}
# Plot individual theta's and differences:
if ( !is.null(diffSList) ) {
for ( compIdx in 1:length(diffSList) ) {
diffSVec = diffSList[[compIdx]]
Nidx = length(diffSVec)
temp=NULL
for ( i in 1:Nidx ) {
temp = c( temp , which(myData[[sName]]==diffSVec[i]) )
}
diffSVec = temp
openGraph(width=2.5*Nidx,height=2.0*Nidx)
par( mfrow=c(Nidx,Nidx) )
xLim = range(c(compVal,rope,mcmcMat[,paste0("theta[",diffSVec,"]")],
z[diffSVec]/N[diffSVec]))
for ( t1Idx in 1:Nidx ) {
for ( t2Idx in 1:Nidx ) {
parName1 = paste0("theta[",diffSVec[t1Idx],"]")
parName2 = paste0("theta[",diffSVec[t2Idx],"]")
if ( t1Idx > t2Idx) {
# plot.new() # empty plot, advance to next
par( mar=c(3,3,3,1) , mgp=c(2.0,0.7,0) , pty="s" )
nToPlot = 700
ptIdx = round(seq(1,chainLength,length=nToPlot))
plot ( mcmcMat[ptIdx,parName2] , mcmcMat[ptIdx,parName1] , cex.lab=1.25 ,
xlab=s[diffSVec[t2Idx]] ,
ylab=s[diffSVec[t1Idx]] ,
col="skyblue" )
abline(0,1,lty="dotted")
} else if ( t1Idx == t2Idx ) {
par( mar=c(3,1.5,3,1.5) , mgp=c(2.0,0.7,0) , pty="m" )
postInfo = plotPost( mcmcMat[,parName1] ,
compVal=compVal , ROPE=rope ,
cex.main=1.25 , cex.lab=1.25 ,
xlab=bquote(.(parName1)) ,
main=paste0( s[diffSVec[t1Idx]] ,
" (",c[diffSVec[t1Idx]],")") ,
xlim=xLim )
points( z[diffSVec[t1Idx]]/N[diffSVec[t1Idx]] , 0 ,
pch="+" , col="red" , cex=3 )
text( z[diffSVec[t1Idx]]/N[diffSVec[t1Idx]] , 0 ,
bquote(list( z==.(z[diffSVec[t1Idx]]) ,
N==.(N[diffSVec[t1Idx]]) )) ,
adj=c( (z[diffSVec[t1Idx]]/N[diffSVec[t1Idx]]-xLim[1])/
(xLim[2]-xLim[1]),-3.25) , col="red" )
} else if ( t1Idx < t2Idx ) {
par( mar=c(3,1.5,3,1.5) , mgp=c(2.0,0.7,0) , pty="m" )
postInfo = plotPost( mcmcMat[,parName1]-mcmcMat[,parName2] ,
compVal=compValDiff , ROPE=ropeDiff ,
cex.main=0.67 , cex.lab=1.25 ,
xlab=bquote("Difference of "*theta*"'s") ,
main=paste(
s[diffSVec[t1Idx]] ,
" (",c[diffSVec[t1Idx]],")" ,
"\n -",
s[diffSVec[t2Idx]] ,
" (",c[diffSVec[t2Idx]],")" ) )
points( z[diffSVec[t1Idx]]/N[diffSVec[t1Idx]]
- z[diffSVec[t2Idx]]/N[diffSVec[t2Idx]] , 0 ,
pch="+" , col="red" , cex=3 )
}
}
}
if ( !is.null(saveName) ) {
saveGraph( file=paste0(saveName,"ThetaDiff",compIdx), type=saveType)
}
}
}
}
#===============================================================================