{"id":403,"date":"2014-11-21T11:43:55","date_gmt":"2014-11-21T11:43:55","guid":{"rendered":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/?p=403"},"modified":"2025-02-26T13:21:38","modified_gmt":"2025-02-26T13:21:38","slug":"jags-with-stata","status":"publish","type":"post","link":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/2014\/11\/21\/jags-with-stata\/","title":{"rendered":"JAGS with Stata"},"content":{"rendered":"<p>WinBUGS and OpenBUGS are just two of a growing number of blackbox programs for performing Bayesian analysis. Others include, JAGS (<a href=\"http:\/\/mcmc-jags.sourceforge.net\/\">http:\/\/mcmc-jags.sourceforge.net\/<\/a>), Stan (<a href=\"http:\/\/mc-stan.org\/\">http:\/\/mc-stan.org\/<\/a>) and BiiPS(<a href=\"https:\/\/alea.bordeaux.inria.fr\/biips\/doku.php\">https:\/\/alea.bordeaux.inria.fr\/biips\/doku.php<\/a>). Of these, the program that is\u00a0closest in style to\u00a0WinBUGS and OpenBUGS is JAGS;\u00a0it has a similar structure and\u00a0it uses very similar samplers. So it should be easy to modify the wbs family of ado files so that JAGS\u00a0can be\u00a0called from\u00a0Stata.<\/p>\n<p>JAGS stands for <em>Just Another Gibbs Sampler<\/em>; this is a appropriate name because the program follows very much the same approach as the BUGS family of programs. The rationale for creating a new program was to provide open source code that could be used across many platforms and that would be easy for the user to extend, for example by adding their own samplers. JAGS is written in C++ and it can be downloaded from the web address\u00a0mentioned above and it is free. The original version was written by Martyn Plummer and was released in 2007.<\/p>\n<p>JAGS has several design features that make it particularly easy to use\u00a0with R\u00a0and there is \u00a0an R package called rjags that takes advantage of those features. My aim is to provide similar facilities for Stata users, as far as possible I will integrate them within the programs that already link Stata to WinBUGS and OpenBUGS.<\/p>\n<p>So far, my experience is with using JAGS under Windows so I will restrict this posting to that implementation and return to discuss JAGS under Linux on another occasion. The cross-platform design should mean that the extension presents few problems. For the present, I will also\u00a0limit myself to the base version of JAGS and not talk about writing one&#8217;s own extensions.<\/p>\n<p>JAGS is designed for running via a script file in just the same way as one runs WinBUGS from Stata, because of this, JAGS has only a very basic\/non-existent front-end and if the program is run interactively, one merely obtains a Windows terminal. However, this is not a problem for us as we will be running the program through Stata.<\/p>\n<p>In the book &#8216;<em>Bayesian analysis with Stata&#8217;<\/em> I describe how WinBUGS and OpenBUGS are run by providing four text files; a model file, a data file, an initial values file and a script file. JAGS is very similar except that these files have a slightly different format.<\/p>\n<p>Since anyone who uses JAGS via Stata is likely to already have tried using\u00a0WinBUGS or\u00a0OpenBUGS with Stata, I will concentrate on the differences. More details can be found in the JAGS User Manual that\u00a0is provided\u00a0with the program when it is installed.<\/p>\n<p><strong>The Script File<\/strong><\/p>\n<p>The scripting language for JAGS is different to either WinBUGS or OpenBUGS but my objective is to hide that complexity from the Stata user by adding a jags option to wbsscript so that it will automatically writes the jags script file. The JAGS user manual describes the scripting language for anyone who is interested.<\/p>\n<p><strong>The Data &amp; Initial Values File<\/strong><\/p>\n<p>One of the ways in which JAGS is geared to work with R is that JAGS formats its data files in the layout provided by R&#8217;s dump() function. Here is a piece of R code that demonstrates the format<\/p>\n<pre><strong><span style=\"color: #0000ff\">&gt; x &lt;- c(1,2,3,4)<\/span><\/strong><\/pre>\n<pre><strong><span style=\"color: #0000ff\">&gt; p &lt;- 5<\/span><\/strong><\/pre>\n<pre><strong><span style=\"color: #0000ff\">&gt; m &lt;- matrix(c(1,2,3,4,5,6),2,3)<\/span><\/strong><\/pre>\n<pre><strong><span style=\"color: #0000ff\"><strong>&gt; m\r\n<\/strong><span style=\"color: #008000\">         [,1] [,2] [,3]<\/span><\/span>\r\n<span style=\"color: #008000\">         [1,] 1 3 5\r\n         [2,] 2 4 6\r\n<\/span><span style=\"color: #0000ff\">&gt; dump(c(\"x\",\"p\",\"m\"),file=\"text.txt\")<\/span>\r\n<span style=\"color: #0000ff\">&gt; file.show(\"text.txt\")<\/span>\r\n<span style=\"color: #008000\">         x &lt;-\r\n         c(1, 2, 3, 4)\r\n         p &lt;-\r\n         5\r\n         m &lt;-\r\n         structure(c(1, 2, 3, 4, 5, 6), .Dim = 2:3)<\/span><\/strong><\/pre>\n<p>Results are indented and coloured green to distinguish them from the commands.<\/p>\n<p>The first three lines define a vector, x, a scalar, p, and a matrix, m. The matrix is displayed and we see that, unlike WinBUGS, an R matrix is filled by column and not by row.<\/p>\n<p>The dump() command sends the three data objects to a text file and R\u2019s file.show() command displays the file in a text window. As this is not the format used by WinBUGS, wbslist needs to be re-written and unfortunately we cannot use the WinBUGS array format as implemented in wbsarray. JAGS cannot parse an R data frame.<\/p>\n<p>In these dumped files, the variable names may optionally be included in quotes.<\/p>\n<p><strong>The Model File<\/strong><\/p>\n<p>There are small differences between the syntax of JAGS and OpenBUGS but they are too minor to bother us here. The most important of these differences are in the handling of censored data, otherwise an OpenBUGS model file is likely to run in JAGS without alteration.<\/p>\n<p><strong>Updating wbs<\/strong><\/p>\n<p>If you want to use JAGS with Stata then you will need to download new versions of some of the wbs programs. Eventually I will make these the versions that are supplied when you net install wbs into Stata, but for the moment I am treating them as experimental and under test. I have included the code for the new ado files in a pdf from where they can be copied and saved as ado files in the PLUS folder. As they are still being tested I have given them names such as wbsrun_v2.ado. In this way they do not replace the current release of the wbs programs but can sit alongside them. As yet there are no help files. The new ado files are listed in a downloadable pdf called \u00a0<a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/11\/v2_adofiles.pdf\">v2_adofiles<\/a> . The security restrictions on this blog do not allow me to attach the ado files themselves.<\/p>\n<p><strong>Executables.txt<\/strong><\/p>\n<p>Once you have downloaded and installed JAGS, you will need to tell Stata were to find it on your computer. I do this by creating a text file called executables.txt in my PERSONAL ado folder that contains a line such as<br \/>\n<span style=\"color: #0000ff\">JAGS,\u201dc:\/jags\/jags-3.4.0\/x64\/bin\/jags.bat\u201d<\/span><br \/>\nThe quotes contain the path to the Windows batch file jags.bat that is supplied when JAGS is installed.<\/p>\n<p><strong>An Example<\/strong><\/p>\n<p>Let us take a extremely simple example. Here is some Stata code that calls JAGS and estimates the mean, mu,\u00a0of some simulated normally distributed data.<\/p>\n<pre><strong><span style=\"color: #0000ff\">cd \"C:\\Temp\"<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *-----------------------------------<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> * Simulate the data &amp; write to file<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *-----------------------------------<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> clear<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> set seed 390173<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> set obs 5<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> gen x = round(rnormal(),0.01)<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> scalar n = 5<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> wbslist_v2 (scalar n) (vect x) using data.txt , replace jags<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *-----------------------------------<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> * Set initial value &amp; write to file<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *-----------------------------------<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> scalar mu = 1<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> wbslist_v2 (scalar mu) using init.txt , replace jags<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *-----------------------------------<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> * Write model file<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *-----------------------------------<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> wbsmodel thisfile.do model.txt<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> \/*<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> model {<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\">    for( i in 1:n ) {<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\">       x[i] ~ dnorm(mu,1)\r\n<\/span><\/strong><strong><span style=\"color: #0000ff\">    }<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\">    mu ~ dnorm(1,1)\r\n<\/span><\/strong><strong><span style=\"color: #0000ff\"> }<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *\/<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *-----------------------------------<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> * Write script file<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *-----------------------------------<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> wbsscript_v2 using script.txt, replace \/\/\/<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> model(model.txt) data(data.txt) init(init.txt) \/\/\/<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> set(mu) coda(cd) burn(500) update(1000) jags<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *-----------------------------------<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> * Run script &amp; inspect log<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *-----------------------------------<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> wbsrun_v2 using script.txt , jags<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> type jagslog.txt<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *-----------------------------------<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> * Read the data and plot<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> *-----------------------------------<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> wbscoda_v2 using cd, clear jags<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> mcmcstats mu<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff\"> mcmctrace mu<\/span><\/strong><\/pre>\n<p>Anyone who is familiar with running WinBUGS from Stata will recognise this pattern of code; the only substantial difference is that the wbs*_v2 commands allow a jags option.<\/p>\n<p>However the text files that are created are different. Let\u2019s look at each of them<\/p>\n<p><strong>data.txt<\/strong><br \/>\n<span style=\"color: #0000ff\">&#8220;n&#8221; &lt;- 5.000<\/span><br \/>\n<span style=\"color: #0000ff\"> &#8220;x&#8221; &lt;- c( 0.730, -0.050, 0.890, -0.860, 1.010)<\/span><\/p>\n<p><strong>init.txt<\/strong><br \/>\n<span style=\"color: #0000ff\">&#8220;mu&#8221; &lt;- 1.000<\/span><\/p>\n<p><strong>model.txt<\/strong><br \/>\n<span style=\"color: #0000ff\">model {<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 for( i in 1:n ) {<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 x[i] ~ dnorm(mu,1)<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 }<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 mu ~ dnorm(1,1)<\/span><br \/>\n<span style=\"color: #0000ff\"> }<\/span><\/p>\n<p><strong>script.txt<\/strong><br \/>\n<span style=\"color: #0000ff\">model in &#8216;C:\/Temp\/model.txt&#8217;<\/span><br \/>\n<span style=\"color: #0000ff\"> data in &#8216;C:\/Temp\/data.txt&#8217;<\/span><br \/>\n<span style=\"color: #0000ff\"> compile<\/span><br \/>\n<span style=\"color: #0000ff\"> parameters in &#8216;C:\/Temp\/init.txt&#8217;<\/span><br \/>\n<span style=\"color: #0000ff\"> initialize<\/span><br \/>\n<span style=\"color: #0000ff\"> update 500<\/span><br \/>\n<span style=\"color: #0000ff\"> monitor mu<\/span><br \/>\n<span style=\"color: #0000ff\"> update 1000<\/span><br \/>\n<span style=\"color: #0000ff\"> coda * , stem(&#8216;C:\/Temp\/cd&#8217;)<\/span><br \/>\n<span style=\"color: #0000ff\"> exit<\/span><\/p>\n<p>As currently written the wbsrun_v2.ado file writes the JAGS log to the file jagslog.txt. Eventually I will add an option that allows the filename to be specified by the user.\u00a0For our simple example\u00a0the log reads,<\/p>\n<pre><strong><span style=\"color: #0000ff\">Welcome to JAGS 3.4.0 on Mon Nov 10 15:27:22 2014<\/span>\r\n<span style=\"color: #0000ff\"> JAGS is free software and comes with ABSOLUTELY NO WARRANTY<\/span>\r\n<span style=\"color: #0000ff\"> Loading module: basemod: ok<\/span>\r\n<span style=\"color: #0000ff\"> Loading module: bugs: ok<\/span>\r\n<span style=\"color: #0000ff\"> Reading data file C:\/Temp\/data.txt<\/span>\r\n<span style=\"color: #0000ff\"> Compiling model graph<\/span>\r\n<span style=\"color: #0000ff\"> Resolving undeclared variables<\/span>\r\n<span style=\"color: #0000ff\"> Allocating nodes<\/span>\r\n<span style=\"color: #0000ff\"> Graph Size: 8<\/span>\r\n<span style=\"color: #0000ff\"> Reading parameter file C:\/Temp\/init.txt<\/span>\r\n<span style=\"color: #0000ff\"> Initializing model<\/span>\r\n<span style=\"color: #0000ff\"> Updating 500<\/span>\r\n<span style=\"color: #0000ff\"> Updating 1000<\/span><\/strong><\/pre>\n<p>Once we have read the results into Stata, we can inspect them as we would results from WinBUGS by using the mcmc commands. Here is the trace plot of mu. The example is very simple but everything works as one would expect.<\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/11\/jagstraceex1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-408\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/11\/jagstraceex1-300x218.png\" alt=\"jagstraceex1\" width=\"300\" height=\"218\" srcset=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/11\/jagstraceex1-300x218.png 300w, https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/11\/jagstraceex1.png 811w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Next time I will take a slightly more complex model and try fitting it in WinBUGS, OpenBUGS and JAGS so that we can see if there are any noticeable differences in performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WinBUGS and OpenBUGS are just two of a growing number of blackbox programs for performing Bayesian analysis. Others include, JAGS (http:\/\/mcmc-jags.sourceforge.net\/), Stan (http:\/\/mc-stan.org\/) and BiiPS(https:\/\/alea.bordeaux.inria.fr\/biips\/doku.php). Of these, the program that is\u00a0closest in style to\u00a0WinBUGS and OpenBUGS is JAGS;\u00a0it has a similar structure and\u00a0it uses very similar samplers. So it should be easy to modify the [&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],"class_list":["post-403","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-jags","tag-stata"],"_links":{"self":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/403","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=403"}],"version-history":[{"count":11,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/403\/revisions"}],"predecessor-version":[{"id":432,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/403\/revisions\/432"}],"wp:attachment":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/media?parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/categories?post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/tags?post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}