{"id":445,"date":"2014-12-16T10:41:53","date_gmt":"2014-12-16T10:41:53","guid":{"rendered":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/?p=445"},"modified":"2025-02-26T13:21:38","modified_gmt":"2025-02-26T13:21:38","slug":"jags-with-stata-iii","status":"publish","type":"post","link":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/2014\/12\/16\/jags-with-stata-iii\/","title":{"rendered":"JAGS with Stata III"},"content":{"rendered":"<h2><em>Merry Christmas<\/em><\/h2>\n<p>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\u2019ll try mix this with a few practical examples and some more solutions to the exercises from the book.<\/p>\n<p>Meanwhile I have a couple of comments on JAGS to finish off the discussion from last week<\/p>\n<h2><em>JAGS with Stata III<\/em><\/h2>\n<p>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\u00a0have experimented to see whether the JAGS approach can be made to work in WinBUGS and vice versa.<\/p>\n<p>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.<\/p>\n<p>The WinBUGS model file for the biopsy analysis is,<br \/>\n<span style=\"color: #0000ff\">model<\/span><br \/>\n<span style=\"color: #0000ff\"> {<\/span><br \/>\n<span style=\"color: #0000ff\"> for (i in 1:ns){<\/span><br \/>\n<span style=\"color: #0000ff\"> nbiops[i] &lt;- sum(biopsies[i, ])<\/span><br \/>\n<span style=\"color: #0000ff\"> true[i] ~ dcat(p[])<\/span><br \/>\n<span style=\"color: #0000ff\"> biopsies[i, 1:4] ~ dmulti(error[true[i], ], nbiops[i])<\/span><br \/>\n<span style=\"color: #0000ff\"> }<\/span><br \/>\n<span style=\"color: #0000ff\"> error[2,1:2] ~ ddirch(prior[1:2])<\/span><br \/>\n<span style=\"color: #0000ff\"> error[3,1:3] ~ ddirch(prior[1:3])<\/span><br \/>\n<span style=\"color: #0000ff\"> error[4,1:4] ~ ddirch(prior[1:4])<\/span><br \/>\n<span style=\"color: #0000ff\"> p[1:4] ~ ddirch(prior[]); # prior for p<\/span><br \/>\n<span style=\"color: #0000ff\"> }<\/span><br \/>\nOpenBUGS gives the same code except that in OpenBUGS the Dirichlet distribution is denoted by ddirich(). The JAGS file is,<br \/>\n<span style=\"color: #0000ff\">var<\/span><br \/>\n<span style=\"color: #0000ff\"> biopsies[ns,4], # grades observed in ith session (multinomial)<\/span><br \/>\n<span style=\"color: #0000ff\"> nbiops[ns], # total number of biopsies in ith session<\/span><br \/>\n<span style=\"color: #0000ff\"> true[ns], # true state in ith session<\/span><br \/>\n<span style=\"color: #0000ff\"> error[4,4], # error matrix in taking biopsies<\/span><br \/>\n<span style=\"color: #0000ff\"> prior[4,4], # prior parameters for rows of error[,]<\/span><br \/>\n<span style=\"color: #0000ff\"> p[4]; # underlying incidence of true states<\/span><br \/>\n<span style=\"color: #0000ff\"> model {<\/span><br \/>\n<span style=\"color: #0000ff\"> for (i in 1:ns){<\/span><br \/>\n<span style=\"color: #0000ff\"> true[i] ~ dcat(p[]);<\/span><br \/>\n<span style=\"color: #0000ff\"> biopsies[i,] ~ dmulti(error[true[i],],nbiops[i]);<\/span><br \/>\n<span style=\"color: #0000ff\"> }<\/span><\/p>\n<p><span style=\"color: #0000ff\">for (j in 1:4) {<\/span><br \/>\n<span style=\"color: #0000ff\"> error[j,] ~ ddirch(prior[j,]);<\/span><br \/>\n<span style=\"color: #0000ff\"> }<\/span><\/p>\n<p><span style=\"color: #0000ff\">p[] ~ ddirch(prior[4,]); # prior for p<\/span><br \/>\n<span style=\"color: #0000ff\"> }<\/span><\/p>\n<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.<\/p>\n<p>In the WinBUGS model file there is no <strong>var<\/strong> section to define the matrix dimensions and so WinBUGS needs to define the dimensions of the variables within the code, hence\u00a0statements 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,<br \/>\n<span style=\"color: #0000ff\">model {<\/span><br \/>\n<span style=\"color: #0000ff\"> for (i in 1:ns){<\/span><br \/>\n<span style=\"color: #0000ff\"> true[i] ~ dcat(p[])<\/span><br \/>\n<span style=\"color: #0000ff\"> biopsies[i,] ~ dmulti(error[true[i],],nbiops[i])<\/span><br \/>\n<span style=\"color: #0000ff\"> }<\/span><br \/>\n<span style=\"color: #0000ff\"> for (j in 1:4) {<\/span><br \/>\n<span style=\"color: #0000ff\"> error[j,1:4] ~ ddirch(prior[j,])<\/span><br \/>\n<span style=\"color: #0000ff\"> }<\/span><br \/>\n<span style=\"color: #0000ff\"> p[1:4] ~ ddirch(prior[4,]) # prior for p<\/span><br \/>\n<span style=\"color: #0000ff\"> }<\/span><\/p>\n<p>Curiously I could find no way of getting JAGS to calculate <strong>nbiops[]<\/strong> 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.<\/p>\n<p>Finally I tried the WinBUGS approach of specifying <strong>error[]<\/strong> 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.<\/p>\n<p>What have I learned from my experiments.<\/p>\n<ul>\n<li>It is a good practice to include the array declarations in JAGS<\/li>\n<li>It is best to make calculations in Stata before calling JAGS<\/li>\n<li>Most programs that work in WinBUGS can easily be\u00a0modified to run in JAGS<\/li>\n<li>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\u00a0it is hard for the manuals to describe every possible use.<\/li>\n<li>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.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":134,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[42,4,5],"class_list":["post-445","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-jags","tag-stata","tag-winbugs"],"_links":{"self":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/users\/134"}],"replies":[{"embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/comments?post=445"}],"version-history":[{"count":4,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":450,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/445\/revisions\/450"}],"wp:attachment":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/media?parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/categories?post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/tags?post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}