-
Notifications
You must be signed in to change notification settings - Fork 14
/
Stan-Ybinom-Xnom1fac-Mlogistic.R
executable file
·340 lines (321 loc) · 14 KB
/
Stan-Ybinom-Xnom1fac-Mlogistic.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
# 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=myData , zName="Hits", NName="AtBats", sName="Player", cName="PriPos",
datFrm , yName , NName , xName ,
numSavedSteps=50000 , thinSteps=1 , saveName=NULL ,
runjagsMethod=runjagsMethodDefault ,
nChains=nChainsDefault ) {
#------------------------------------------------------------------------------
# THE DATA.
# Convert data file columns to generic x,y variable names for model:
y = as.numeric(datFrm[,yName])
x = as.numeric(as.factor(datFrm[,xName]))
N = as.numeric(datFrm[,NName])
xlevels = levels(as.factor(datFrm[,xName]))
Ntotal = length(y)
NxLvl = length(unique(x))
# Specify the data in a list for sending to JAGS:
dataList = list(
y = y ,
N = N ,
x = x ,
Ntotal = Ntotal ,
NxLvl = NxLvl
)
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# INTIALIZE THE CHAINS.
initsList = NA
#------------------------------------------------------------------------------
# RUN THE CHAINS
require(rstan)
parameters = c( "mu" , "omega" , "kappa" , "b0" , "b" )
adaptSteps = 500
burnInSteps = 1000
# Translate to C++ and compile to DSO:
stanDso <- stan_model( file="Ybinom-Xnom1fac-Mlogistic.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 = "random" ) # 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 )
}
#===============================================================================
smryMCMC = function( codaSamples , datFrm=NULL , xName=NULL ,
contrasts=NULL , saveName=NULL ) {
# All single parameters:
parameterNames = varnames(codaSamples)
if ( !is.null(datFrm) & !is.null(xName) ) {
xlevels = levels(as.factor(datFrm[,xName]))
}
summaryInfo = NULL
mcmcMat = as.matrix(codaSamples,chains=TRUE)
for ( parName in parameterNames ) {
summaryInfo = rbind( summaryInfo , summarizePost( mcmcMat[,parName] ) )
thisRowName = parName
if ( !is.null(datFrm) & !is.null(xName) ) {
# For row name, extract numeric digits from parameter name. E.g., if
# parameter name is "beta[12,34]" then pull out 12 and 34:
levelVal = as.numeric(
grep( "^[1-9]" , # grep only substrings that begin with digits.
# Return sll substrings split by "[" or "," or "]":
unlist( strsplit( parName , "\\[|,|\\]" ) ) ,
value=TRUE ) )
if ( length(levelVal) > 0 ) {
# Assumes there is only a single factor, i.e., levelVal has only entry:
thisRowName = paste(thisRowName,xlevels[levelVal])
}
}
rownames(summaryInfo)[NROW(summaryInfo)] = thisRowName
}
# All contrasts:
if ( !is.null(contrasts) ) {
if ( is.null(datFrm) | is.null(xName) ) {
show(" *** YOU MUST SPECIFY THE DATA FILE AND FACTOR NAMES TO DO CONTRASTS. ***\n")
} else {
# contrasts:
if ( !is.null(contrasts) ) {
for ( cIdx in 1:length(contrasts) ) {
thisContrast = contrasts[[cIdx]]
left = right = rep(FALSE,length(xlevels))
for ( nIdx in 1:length( thisContrast[[1]] ) ) {
left = left | xlevels==thisContrast[[1]][nIdx]
}
left = normalize(left)
for ( nIdx in 1:length( thisContrast[[2]] ) ) {
right = right | xlevels==thisContrast[[2]][nIdx]
}
right = normalize(right)
contrastCoef = matrix( left-right , ncol=1 )
# contrast on b[j]:
postContrast = ( mcmcMat[,paste("b[",1:length(xlevels),"]",sep="")]
%*% contrastCoef )
summaryInfo = rbind( summaryInfo ,
summarizePost( postContrast ,
compVal=thisContrast$compVal ,
ROPE=thisContrast$ROPE ) )
rownames(summaryInfo)[NROW(summaryInfo)] = (
paste( "b:",paste(thisContrast[[1]],collapse=""), ".v.",
paste(thisContrast[[2]],collapse=""),sep="") )
# contrast on omega[j]:
postContrast = ( mcmcMat[,paste("omega[",1:length(xlevels),"]",sep="")]
%*% contrastCoef )
summaryInfo = rbind( summaryInfo ,
summarizePost( postContrast ,
compVal=thisContrast$compVal ,
ROPE=thisContrast$ROPE ) )
rownames(summaryInfo)[NROW(summaryInfo)] = (
paste( "omega:",paste(thisContrast[[1]],collapse=""), ".v.",
paste(thisContrast[[2]],collapse=""),sep="") )
}
}
}
}
# Save results:
if ( !is.null(saveName) ) {
write.csv( summaryInfo , file=paste(saveName,"SummaryInfo.csv",sep="") )
}
return( summaryInfo )
}
#===============================================================================
plotMCMC = function( codaSamples ,
datFrm , yName , xName , NName, contrasts=NULL ,
saveName=NULL , saveType="jpg" ) {
mcmcMat = as.matrix(codaSamples,chains=TRUE)
chainLength = NROW( mcmcMat )
y = datFrm[,yName]
x = as.numeric(as.factor(datFrm[,xName]))
xlevels = levels(as.factor(datFrm[,xName]))
N = datFrm[,NName]
# Display data with posterior predictive distributions
openGraph(width=min(10,1.25*length(xlevels)),height=5)
par(mar=c(3,3,2,0.5)) # number of margin lines: bottom,left,top,right
par(mgp=c(1.75,0.5,0)) # which margin lines to use for labels
par(las=3) # labels vertical
par(mai=c(1.25,0.75,0.75,0.25))
plot(-1,0,
xlim=c(0.1,length(xlevels)+0.1) ,
xlab=xName , xaxt="n" , ylab=paste(yName,"/",NName) ,
ylim=c(0,1) ,
main="Data with Posterior Predictive Distrib.")
axis( 1 , at=1:length(xlevels) , tick=FALSE , lab=xlevels )
for ( xidx in 1:length(xlevels) ) {
xPlotVal = xidx
yVals = y[ x==xidx ]
nVals = N[ x==xidx ]
points( rep(xPlotVal,length(yVals))+runif(length(yVals),-0.05,0.05) ,
yVals/nVals , pch=1 , cex=(0.25+1.75*nVals/max(nVals)) , col="red" )
chainSub = round(seq(1,chainLength,length=20))
for ( chnIdx in chainSub ) {
o = mcmcMat[chnIdx,paste("omega[",xidx,"]",sep="")]
k = mcmcMat[chnIdx,paste("kappa",sep="")]
a = o*(k-2)+1
b = (1-o)*(k-2)+1
blim = qbeta( c(0.025,0.975) , a , b )
yl = blim[1]
yh = blim[2]
ycomb=seq(yl,yh,length=201)
yb = dbeta( ycomb , a,b )
yb = 0.67*yb/max(yb)
lines( xPlotVal-yb , ycomb , col="skyblue" )
}
}
if ( !is.null(saveName) ) {
saveGraph( file=paste(saveName,"PostPred",sep=""), type=saveType)
}
if ( !is.null(contrasts) ) {
if ( is.null(datFrm) | is.null(xName) ) {
show(" *** YOU MUST SPECIFY THE DATA FILE AND FACTOR NAMES TO DO CONTRASTS. ***\n")
} else {
for ( cIdx in 1:length(contrasts) ) {
thisContrast = contrasts[[cIdx]]
left = right = rep(FALSE,length(xlevels))
for ( nIdx in 1:length( thisContrast[[1]] ) ) {
left = left | xlevels==thisContrast[[1]][nIdx]
}
left = normalize(left)
for ( nIdx in 1:length( thisContrast[[2]] ) ) {
right = right | xlevels==thisContrast[[2]][nIdx]
}
right = normalize(right)
contrastCoef = matrix( left-right , ncol=1 )
openGraph(height=8,width=4)
layout(matrix(1:2,ncol=1))
# b contrast:
postContrast = ( mcmcMat[,paste("b[",1:length(xlevels),"]",sep="")]
%*% contrastCoef )
plotPost( postContrast , xlab="Difference (in b)" ,
main=paste0( "b: " ,
paste(thisContrast[[1]],collapse="."),
"\nvs\n",
paste(thisContrast[[2]],collapse=".") ) ,
compVal=thisContrast$compVal , ROPE=thisContrast$ROPE )
# omega contrast:
postContrast = ( mcmcMat[,paste("omega[",1:length(xlevels),"]",sep="")]
%*% contrastCoef )
plotPost( postContrast , xlab="Difference (in omega)" ,
main=paste0( "omega: " ,
paste(thisContrast[[1]],collapse="."),
"\nvs\n",
paste(thisContrast[[2]],collapse=".") ) ,
compVal=thisContrast$compVal , ROPE=thisContrast$ROPE )
if ( !is.null(saveName) ) {
saveGraph( file=paste0(saveName, paste0(
paste(thisContrast[[1]],collapse=""),
".v.",
paste(thisContrast[[2]],collapse="") ) ),
type=saveType )
}
}
}
} # end if ( !is.null(contrasts) )
}
# plotMCMC = function( codaSamples ,
# datFrm , yName="y" , xName="x" , contrasts=NULL ,
# saveName=NULL , saveType="jpg" ) {
# mcmcMat = as.matrix(codaSamples,chains=TRUE)
# chainLength = NROW( mcmcMat )
# y = datFrm[,yName]
# x = as.numeric(as.factor(datFrm[,xName]))
# xlevels = levels(as.factor(datFrm[,xName]))
# # Display data with posterior predictive distributions
# openGraph(width=min(10,1.25*length(xlevels)),height=5)
# par(mar=c(3,3,2,0.5)) # number of margin lines: bottom,left,top,right
# par(mgp=c(1.75,0.5,0)) # which margin lines to use for labels
# plot(-1,0,
# xlim=c(0.1,length(xlevels)+0.1) ,
# xlab=xName , xaxt="n" , ylab=yName ,
# ylim=c(min(y)-0.2*(max(y)-min(y)),max(y)+0.2*(max(y)-min(y))) ,
# main="Data with Posterior Predictive Distrib.")
# axis( 1 , at=1:length(xlevels) , tick=FALSE , lab=xlevels )
# for ( xidx in 1:length(xlevels) ) {
# xPlotVal = xidx
# yVals = y[ x==xidx ]
# points( rep(xPlotVal,length(yVals))+runif(length(yVals),-0.05,0.05) ,
# yVals , pch=1 , cex=1.5 , col="red" )
# chainSub = round(seq(1,chainLength,length=20))
# for ( chnIdx in chainSub ) {
# m = mcmcMat[chnIdx,paste("m[",xidx,"]",sep="")]
# s = mcmcMat[chnIdx,paste("ySigma",sep="")]
# nu = 1000 # effectively normal instead of mcmcMat[chnIdx,"nu"]
# tlim = qt( c(0.025,0.975) , df=nu )
# yl = m+tlim[1]*s
# yh = m+tlim[2]*s
# ycomb=seq(yl,yh,length=201)
# #ynorm = dnorm(ycomb,mean=m,sd=s)
# #ynorm = 0.67*ynorm/max(ynorm)
# yt = dt( (ycomb-m)/s , df=nu )
# yt = 0.67*yt/max(yt)
# lines( xPlotVal-yt , ycomb , col="skyblue" )
# }
# }
# if ( !is.null(saveName) ) {
# saveGraph( file=paste(saveName,"PostPred",sep=""), type=saveType)
# }
# if ( !is.null(contrasts) ) {
# if ( is.null(datFrm) | is.null(xName) ) {
# show(" *** YOU MUST SPECIFY THE DATA FILE AND FACTOR NAMES TO DO CONTRASTS. ***\n")
# } else {
# for ( cIdx in 1:length(contrasts) ) {
# thisContrast = contrasts[[cIdx]]
# left = right = rep(FALSE,length(xlevels))
# for ( nIdx in 1:length( thisContrast[[1]] ) ) {
# left = left | xlevels==thisContrast[[1]][nIdx]
# }
# left = normalize(left)
# for ( nIdx in 1:length( thisContrast[[2]] ) ) {
# right = right | xlevels==thisContrast[[2]][nIdx]
# }
# right = normalize(right)
# contrastCoef = matrix( left-right , ncol=1 )
# postContrast = ( mcmcMat[,paste("b[",1:length(xlevels),"]",sep="")]
# %*% contrastCoef )
# openGraph(height=8,width=4)
# layout(matrix(1:2,ncol=1))
# plotPost( postContrast , xlab="Difference" ,
# main=paste0(
# paste(thisContrast[[1]],collapse="."),
# "\nvs\n",
# paste(thisContrast[[2]],collapse=".") ) ,
# compVal=thisContrast$compVal , ROPE=thisContrast$ROPE )
# plotPost( postContrast/mcmcMat[,"ySigma"] , xlab="Effect Size" ,
# main=paste0(
# paste(thisContrast[[1]],collapse="."),
# "\nvs\n",
# paste(thisContrast[[2]],collapse=".") ) ,
# compVal=0.0 ,
# ROPE=c(-0.1,0.1) )
# if ( !is.null(saveName) ) {
# saveGraph( file=paste0(saveName, paste0(
# paste(thisContrast[[1]],collapse=""),
# ".v.",
# paste(thisContrast[[2]],collapse="") ) ),
# type=saveType )
# }
# }
# }
# } # end if ( !is.null(contrasts) )
# }