Merry Christmas
This will be my last posting before Christmas and in January I will be abroad for a few weeks so I will not be posting again until February. I have several topics in mind for the new year, including more on JAGS, a discussion of the use of Stan and more on using Stata and R in combination. I’ll try mix this with a few practical examples and some more solutions to the exercises from the book.
Meanwhile I have a couple of comments on JAGS to finish off the discussion from last week
JAGS with Stata III
Last time I compared JAGS and WinBUGS for the analysis of some heart biopsy data. Both programs supply model files for the analysis of this problem but on the surface they look very different. There are further differences in the contents of the data and initial values files. The manuals for the two programs are not detailed enough to enable one to say with certainty whether these differences are essential or merely a matter of programming style, so I have experimented to see whether the JAGS approach can be made to work in WinBUGS and vice versa.
This posting really needs to be read in sequence with my other recent blogs on running JAGS from within Stata, in particular in my recent postings I have given the Stata commands needed for running the WinBUGS and JAGS programs for analysing the biopsy data.
The WinBUGS model file for the biopsy analysis is,
model
{
for (i in 1:ns){
nbiops[i] <- sum(biopsies[i, ])
true[i] ~ dcat(p[])
biopsies[i, 1:4] ~ dmulti(error[true[i], ], nbiops[i])
}
error[2,1:2] ~ ddirch(prior[1:2])
error[3,1:3] ~ ddirch(prior[1:3])
error[4,1:4] ~ ddirch(prior[1:4])
p[1:4] ~ ddirch(prior[]); # prior for p
}
OpenBUGS gives the same code except that in OpenBUGS the Dirichlet distribution is denoted by ddirich(). The JAGS file is,
var
biopsies[ns,4], # grades observed in ith session (multinomial)
nbiops[ns], # total number of biopsies in ith session
true[ns], # true state in ith session
error[4,4], # error matrix in taking biopsies
prior[4,4], # prior parameters for rows of error[,]
p[4]; # underlying incidence of true states
model {
for (i in 1:ns){
true[i] ~ dcat(p[]);
biopsies[i,] ~ dmulti(error[true[i],],nbiops[i]);
}
for (j in 1:4) {
error[j,] ~ ddirch(prior[j,]);
}
p[] ~ ddirch(prior[4,]); # prior for p
}
JAGS places colons at the end of its command lines while on the whole WinBUGS does not, although in this example there is an unnecessary stray semi-colon at the end of the last line of the WinBUGS model. Both programs will work with or without the semi-colons.
In the WinBUGS model file there is no var section to define the matrix dimensions and so WinBUGS needs to define the dimensions of the variables within the code, hence statements such as p[1:4]. In JAGS we can also remove the definitions but if we do then we need to give JAGS more information in the model definition so that it can deduce the sizes of the matrices. This means that we would need something like,
model {
for (i in 1:ns){
true[i] ~ dcat(p[])
biopsies[i,] ~ dmulti(error[true[i],],nbiops[i])
}
for (j in 1:4) {
error[j,1:4] ~ ddirch(prior[j,])
}
p[1:4] ~ ddirch(prior[4,]) # prior for p
}
Curiously I could find no way of getting JAGS to calculate nbiops[] within the model file. So I had to calculate nbiops[] in Stata and supply the result as data. This is a logical way to work but it is surprising that there is no alternative.
Finally I tried the WinBUGS approach of specifying error[] in both the data and the initial values files but in JAGS this caused the program to fail. It is possible to specify initial values for error[] but an attempt to update terms in a structure that is defined as data seems to cause a problem.
What have I learned from my experiments.
- It is a good practice to include the array declarations in JAGS
- It is best to make calculations in Stata before calling JAGS
- Most programs that work in WinBUGS can easily be modified to run in JAGS
- As with WinBUGS there is an element of trial and error needed to find a way to program a model in JAGS because the language is both complex and very flexible, so it is hard for the manuals to describe every possible use.
- In its basic form JAGS is no better that WinBUGS. The real advantage lies in the potential to add modules to JAGS. I hope to try this and report on my experience in the new year.
Comments are closed, but trackbacks and pingbacks are open.