# NOT RUN {
## Sample calls: fits pilot response data included with the package
# }
# NOT RUN {
data(hbbrPilotResp)
hbfit = hbbr.Fit(brdta=hbbrPilotResp$brdta, design=hbbrPilotResp$design,
mcmc=list(burnin=500, iter=10000, nc=2, thin=10))
hb = hbfit$bbar.mcmc
dgn = hbfit$design
mns = hbfit$bbar.means
sds = hbfit$bbar.sd # same as apply(hbfit$bbar.mcmc, 2, sd)
## Plots of MCMC draws ---------------------------------------
op=par(mfrow=c(1,2), mar = c(4,2,3,1),oma=c(.1,.1,2,.1))
matplot(hb,type="l",xlab="Iterations",ylab="",
main=paste("Average Part-Worths (beta-bars)"),
cex.main=.8, cex.lab=0.8, axes=FALSE)
axis(1, at=seq(0,dim(hb)[1],length.out = 6),
labels= paste(seq(0,5,1)*dim(hb)[1]/5 *hbfit$other.inputs$thin, sep=""),
cex.axis=0.8)
axis(2, cex.axis=0.8,las=1)
plot(hbfit$logL, type="l",main="Log Likelihood", axes=FALSE,xlab="Iterations",ylab="",
cex.main=.8,cex.lab=0.8)
axis(1, at=seq(0,dim(hb)[1],length.out = 6),
labels= paste(seq(0,5,1)*dim(hb)[1]/5 *hbfit$other.inputs$thin, sep=""),
cex.axis=0.8)
axis(2, cex.axis=0.8,las=1)
title(outer=TRUE, main = paste("MCMC draws plotted at every ",
hbfit$other.inputs$thin,"-th Iteration",sep=""),cex.main=.9)
par(op)
## Plots for mean estimated part-worth utilities ------------------
require(ggplot2)
require(gridExtra)
b.mns = c()
b.sds = c()
b.atr = c()
b.lvl = c()
j.now=1
for (j in 1:dgn$b) {
b.mns = c(b.mns,0, mns[j.now:(j.now-1+dgn$bl[j]-1)])
b.sds = c(b.sds,0, sds[j.now:(j.now-1+dgn$bl[j]-1)])
b.atr = c(b.atr, rep(dgn$blbls[j], dgn$bl[j]))
b.lvl = c(b.lvl, paste("E", 1:dgn$bl[j],sep=""))
j.now = j.now-1+dgn$bl[j]
}
r.mns = c()
r.sds = c()
r.atr = c()
r.lvl = c()
k.now=j.now
for (k in 1:dgn$r) {
r.mns = c(r.mns,0,mns[k.now:(k.now-1+dgn$rl[k]-1)])
r.sds = c(r.sds,0, sds[k.now:(k.now-1+dgn$rl[k]-1)])
r.atr = c(r.atr, rep(dgn$rlbls[k], dgn$rl[k]))
r.lvl = c(r.lvl, paste("H", 1:dgn$rl[k],sep=""))
k.now = k.now-1+dgn$rl[k]
}
d0.b = data.frame(Attributes =b.atr, lvl=b.lvl, util = b.mns, se = b.sds)
d0.r = data.frame(Attributes =r.atr, lvl=r.lvl, util = r.mns, se = r.sds)
y.max = max(abs(mns) + max(sds))
pd <- position_dodge(0.2) # move them .2 to the left and right
pb = ggplot(data = d0.b, aes(x=lvl, y=util, group=Attributes,color=Attributes)) +
ylim(0, y.max) +
geom_hline(yintercept = 0) +
geom_line(size=1.5, position=pd) +
geom_point(size=4, shape=22, fill="green",color="darkgreen", position=pd) +
geom_errorbar(aes(ymin=util-se, ymax=util+se), width=0.2, position=pd) +
xlab("Benefit-Attribute Levels") + ylab("Estimated Utility") +
ggtitle("Estimated Partworth Utilities of Benefits") +
scale_color_manual(values=c("deepskyblue3" , "#9999CC", "cyan3" )) +
theme(legend.position="bottom",plot.title = element_text(size = 10))
pr = ggplot( data = d0.r, aes(x=lvl, y=util, group=Attributes,color=Attributes)) +
ylim(-y.max,0)+
geom_hline(yintercept = 0) +
geom_line(size=1.5, position=pd) +
geom_point(size=4, shape=22, fill="pink",color="darkred", position=pd) +
geom_errorbar(aes(ymin=util-se, ymax=util+se), width=0.2, position=pd) +
xlab("Risk-Attribute Levels") + ylab("Estimated Utility") +
ggtitle("Estimated Partworth Utilities of Risks") +
scale_color_manual(values=c("orange" , "maroon" )) +
theme(legend.position="bottom",plot.title = element_text(size = 10))
grid.arrange(pb, pr, nrow = 1)
##------------------------------------------------------------------
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab