{"id":419,"date":"2014-12-08T09:00:42","date_gmt":"2014-12-08T09:00:42","guid":{"rendered":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/?p=419"},"modified":"2025-02-26T13:21:38","modified_gmt":"2025-02-26T13:21:38","slug":"jags-with-stata-ii","status":"publish","type":"post","link":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/2014\/12\/08\/jags-with-stata-ii\/","title":{"rendered":"JAGS with Stata II"},"content":{"rendered":"<p>This week I want to take a more detailed look at the use of JAGS with Stata and in particular I want to contrast JAGS with WinBUGS by analysing the biopsy data that I described last time. This posting really needs to be read in sequence with the previous two.<\/p>\n<p>Before I\u00a0start on the comparison,\u00a0let me say a little more about the JAGS syntax. I explained when I introduced JAGS that a WinBUGS program is likely to run in JAGS without alteration and this is more or less true, but there are a few potentially useful features of the JAGS language that derive from the original, sometimes called classic, version of BUGS that preceded WinBUGS.<\/p>\n<p><strong>Some JAGS syntax<\/strong><\/p>\n<p>In JAGS we have the option of declaring the dimensions of vectors and matrices in the model file. This is done by having a <strong>var<\/strong> block before the model starts. If the declaration is omitted, JAGS tries to work out the dimensions of the structures and it is usually able to do this successfully.<\/p>\n<p>Second we can end an assignment or a distributional command with a semi-colon. In this way we can place multiple commands on a single line.<\/p>\n<p>Finally we are allowed to implicitly define a calculated quantity (logical node) within a distributional statement so we could replace the WinBUGS commands<\/p>\n<pre><strong><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 mu[i] &lt;- a + b*x[i]<\/span><\/strong><\/pre>\n<pre><strong><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 y[i] ~ dnorm(mu[i],tau)<\/span><\/strong><\/pre>\n<p>with a single JAGS command,<\/p>\n<pre><strong><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 y[i] ~ dnorm(a+b*x[i],tau)<\/span><\/strong><\/pre>\n<p>The only reason for using the former syntax in JAGS would be if we wanted to save the values of mu.<\/p>\n<p><strong>A JAGS program for analysing the biopsies<\/strong><\/p>\n<p>When JAGS is installed a folder is created called <strong>classic-bugs<\/strong> that contains the files needed for fitting all of the WinBUGS examples in JAGS. The model file for the biopsy example is called <strong>biops.bug<\/strong> and contains<\/p>\n<pre><span style=\"color: #0000ff\"><strong>var\r\n\u00a0\u00a0 biopsies[ns,4], # grades observed in ith session (multinomial)\r\n\u00a0\u00a0 nbiops[ns],\u00a0\u00a0\u00a0\u00a0 # total number of biopsies in ith session\r\n\u00a0\u00a0 true[ns],\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # true state in ith \r\n\u00a0\u00a0 error[4,4],\u00a0\u00a0\u00a0\u00a0 # error matrix in taking biopsies\r\n\u00a0\u00a0 prior[4,4],\u00a0\u00a0\u00a0\u00a0 # prior parameters for rows of error[,]\r\n\u00a0\u00a0 p[4];\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # underlying\u00a0\u00a0 incidence of true states\r\nmodel {\r\n\u00a0\u00a0 for (i in 1:ns)\r\n\u00a0\u00a0\u00a0\u00a0 true[i]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ~ dcat(p[]);\r\n\u00a0\u00a0\u00a0\u00a0 biopsies[i,] ~ dmulti(error[true[i],],nbiops[i]); \r\n\u00a0\u00a0 }\r\n\u00a0\u00a0 for (j in 1:4)\r\n       error[j,]\u00a0\u00a0\u00a0\u00a0 ~ ddirch(prior[j,]);\r\n   }\r\n\u00a0\u00a0 p[]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ~ ddirch(prior[4,]);\u00a0\u00a0\u00a0\u00a0 # prior for p\r\n}<\/strong><\/span><\/pre>\n<p>If this file is contrasted with the WinBUGS model file for the same problem you will see that here <strong>prior<\/strong> refers to a 4&#215;4 matrix, which is defined in the data file to be,<\/p>\n<p>1\u00a0\u00a0 0\u00a0\u00a0 0\u00a0\u00a0 0<br \/>\n1\u00a0\u00a0 1\u00a0\u00a0 0\u00a0\u00a0 0<br \/>\n1\u00a0\u00a0 1\u00a0\u00a0 1\u00a0\u00a0 0<br \/>\n1\u00a0\u00a0 1\u00a0\u00a0 1\u00a0\u00a0 1<\/p>\n<p>while previously prior was a vector of length 4 that was filled with 1\u2019s i.e. (1,1,1,1)<\/p>\n<p>Another difference is that in JAGS, <strong>nbiops<\/strong>, the total number of biopsies for each heart, is read in as data while in the WinBUGS code it was calculated.<\/p>\n<p>Other differences in the model file are that this JAGS model file defines the sizes of all of the vectors and matrices in a <strong>var<\/strong> block and the lines are ended with semi-colons.<\/p>\n<p>The initial values are different in that JAGS does not give initial values for <strong>error[]<\/strong> but does give them for <strong>p<\/strong>[] and for <strong>true<\/strong>[]. In fact the way in which the structured zeros are handled is\u00a0quite different to the method used by WinBUGS. In JAGS it is not necessary to define <strong>error<\/strong>[] in both the data and the initial values files with NA in the appropriate places. In JAGS we can define <strong>error<\/strong>[]\u00a0implicitly and still estimate some of its terms as parameters. This seems to me to be convenient but a little illogical.<\/p>\n<p><strong>Running JAGS from Stata<\/strong><\/p>\n<p>We can now prepare a Stata program that calls JAGS and fits the biopsy model.<\/p>\n<pre><strong><span style=\"color: #0000ff\">*---------------------------------\r\n* write data file\r\n*---------------------------------\r\nuse biopsy.dta, clear\r\nmatrix prior = J(4,4,1)\r\nforvalues r=1\/4 {\r\n\u00a0\u00a0 forvalues c=2\/4 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if `c' &gt; `r' matrix prior[`r',`c'] = 0\r\n}\r\n }\r\n egen nbiops = rsum(grade*)\r\n gen true = 4\r\n scalar ns = 157\r\n wbslist_v2 (scalar ns , format(%5.0f)) (matrix prior , format(%4.0f)) \/\/\/\r\n (structure grade1 grade2 grade3 grade4 , name(biopsies) format(%3.0f)) \/\/\/\r\n (vector nbiops , format(%3.0f) ) \/\/\/\r\n using dataj.txt , replace jags\r\n *---------------------------------\r\n * write initial values file\r\n *---------------------------------\r\n matrix p = J(1,4,0.25)\r\n wbslist_v2 (matrix p) (vec true, format(%3.0f) ) \/\/\/\r\n using initj.txt , replace jags\r\n *---------------------------------\r\n * write model file\r\n *---------------------------------\r\n wbsmodel biopsy_jags.do modelj.txt\r\n \/*\r\n var\r\n biopsies[ns,4], # grades observed in ith session (multinomial)\r\n nbiops[ns], # total number of biopsies in ith session\r\n true[ns], # true state in ith session\r\n error[4,4], # error matrix in taking biopsies\r\n prior[4,4], # prior parameters for rows of error[,]\r\n p[4]; # underlying incidence of true states\r\n model {\r\n for (i in 1:ns){\r\n true[i] ~ dcat(p[]);\r\n biopsies[i,] ~ dmulti(error[true[i],],nbiops[i]);\r\n }\r\nfor (j in 1:4) {\r\n error[j,] ~ ddirch(prior[j,]);\r\n }\r\np[] ~ ddirch(prior[4,]); # prior for p\r\n }\r\n *\/\r\n *---------------------------------\r\n * write script file\r\n *---------------------------------\r\n wbsscript_v2 using scriptj.txt , replace \/\/\/\r\n model(modelj.txt) data(dataj.txt) init(initj.txt) \/\/\/\r\n burnin(1000) update(10000) \/\/\/\r\n set(p error) coda(bsj) jags\r\n *---------------------------------\r\n * run and read the results\r\n *---------------------------------\r\n wbsrun_v2 using scriptj.txt , jags\r\n type jagslog.txt\r\n wbscoda_v2 using bsj , clear jags\r\n *---------------------------------\r\n * inspect the results\r\n *---------------------------------\r\n mcmcstats *\r\n mcmctrace p*<\/span><\/strong><\/pre>\n<p>Notice that I am using the modified versions of some of the <strong>wbs<\/strong> programs, for example, <strong>wbsrun_v2<\/strong>. These were introduced in an earlier posting\u00a0\u00a0 <a title=\"JAGS with Stata\" href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/2014\/11\/21\/jags-with-stata\/\">https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/2014\/11\/21\/jags-with-stata\/<\/a> and can be downloaded from there.<\/p>\n<p>You might like to look back and compare this program with the version that called WinBUGS that was given in last week\u2019s posting. The only real difference in structure is the use of the <strong>jags<\/strong> option in <strong>wbslist<\/strong>,<strong> sbsscript<\/strong>,<strong> wbsrun <\/strong>and<strong> wbscoda<\/strong>. All other differences relate to the model file itself and the data and initial values that are supplied and stem from the small differences between WinBUGS and JAGS.<\/p>\n<p>Here are the results calculated by JAGS<\/p>\n<pre>----------------------------------------------------------------------------\r\n Parameter     n  mean    sd    sem median    95% CrI\r\n ----------------------------------------------------------------------------\r\n p_1       10000 0.152 0.051 0.0012 0.154 ( 0.046, 0.247 )\r\n p_2       10000 0.313 0.057 0.0011 0.308 ( 0.213, 0.435 )\r\n p_3       10000 0.388 0.043 0.0005 0.387 ( 0.306, 0.475 )\r\n p_4       10000 0.147 0.030 0.0003 0.145 ( 0.093, 0.210 )\r\n error_1_1 10000 1.000 0.000      . 1.000 ( 1.000, 1.000 )\r\n error_2_1 10000 0.588 0.067 0.0013 0.589 ( 0.456, 0.715 )\r\n error_3_1 10000 0.341 0.045 0.0006 0.340 ( 0.258, 0.432 )\r\n error_4_1 10000 0.099 0.042 0.0005 0.094 ( 0.033, 0.196 )\r\n error_1_2 10000 0.000 0.000      . 0.000 ( 0.000, 0.000 )\r\n error_2_2 10000 0.412 0.067 0.0013 0.411 ( 0.285, 0.544 )\r\n error_3_2 10000 0.036 0.018 0.0002 0.034 ( 0.009, 0.077 )\r\n error_4_2 10000 0.023 0.023 0.0003 0.015 ( 0.001, 0.087 )\r\n error_1_3 10000 0.000 0.000      . 0.000 ( 0.000, 0.000 )\r\n error_2_3 10000 0.000 0.000      . 0.000 ( 0.000, 0.000 )\r\n error_3_3 10000 0.622 0.047 0.0007 0.624 ( 0.527, 0.710 )\r\n error_4_3 10000 0.206 0.060 0.0008 0.201 ( 0.103, 0.337 )\r\n error_1_4 10000 0.000 0.000      . 0.000 ( 0.000, 0.000 )\r\n error_2_4 10000 0.000 0.000      . 0.000 ( 0.000, 0.000 )\r\n error_3_4 10000 0.000 0.000      . 0.000 ( 0.000, 0.000 )\r\n error_4_4 10000 0.672 0.073 0.0010 0.676 ( 0.522, 0.805 )\r\n ----------------------------------------------------------------------------<\/pre>\n<p>Notice that, unlike WinBUGS, JAGS allows us to have items in the error matrix that are not updated. So the results file will contain items like error_1_4 that do not change. In fact, if you look back you will see that the error matrix is not defined in either the data file or the initial values file and so takes it initial values of zero by default when it is listed in the var block.<\/p>\n<p>Both this week\u2019s JAGS code and last week\u2019s WinBUGS code were run for a burn-in of 1,000 followed by 10,000 updates so ought to give comparable results. To make comparison easier I have collected together some of the summary statistics. There is no systematic benefit in using either program.<\/p>\n<pre> --------------------------------------------------\r\n                 WinBUGS              JAGS\r\n Parameter  mean    sd    sem  mean    sd    sem\r\n --------------------------------------------------\r\n error_2_1 0.587 0.066 0.0013 0.588 0.067 0.0013\r\n error_2_2 0.413 0.066 0.0013 0.412 0.067 0.0013\r\n error_3_1 0.342 0.046 0.0007 0.341 0.045 0.0006\r\n error_3_2 0.037 0.018 0.0002 0.036 0.018 0.0002\r\n error_3_3 0.621 0.048 0.0007 0.622 0.047 0.0007\r\n error_4_1 0.099 0.042 0.0005 0.099 0.042 0.0005\r\n error_4_2 0.022 0.023 0.0003 0.023 0.023 0.0003\r\n error_4_3 0.204 0.061 0.0008 0.206 0.060 0.0008\r\n error_4_4 0.675 0.073 0.0010 0.672 0.073 0.0010\r\n p_1       0.153 0.050 0.0011 0.152 0.051 0.0012\r\n p_2       0.311 0.055 0.0010 0.313 0.057 0.0011\r\n p_3       0.389 0.044 0.0006 0.388 0.043 0.0005\r\n p_4       0.147 0.030 0.0003 0.147 0.030 0.0003\r\n ---------------------------------------------------<\/pre>\n<p>Next time I will return to this example and experiment to see how many of the differences between the WinBUGS and JAGS models files are actually essential if we are to get the JAGS program to run.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week I want to take a more detailed look at the use of JAGS with Stata and in particular I want to contrast JAGS with WinBUGS by analysing the biopsy data that I described last time. This posting really needs to be read in sequence with the previous two. Before I\u00a0start on the comparison,\u00a0let [&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":[45,42,4,5],"class_list":["post-419","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-biopsies","tag-jags","tag-stata","tag-winbugs"],"_links":{"self":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/419","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=419"}],"version-history":[{"count":8,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/419\/revisions"}],"predecessor-version":[{"id":444,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/419\/revisions\/444"}],"wp:attachment":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/media?parent=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/categories?post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/tags?post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}