{"id":588,"date":"2015-05-26T10:25:26","date_gmt":"2015-05-26T10:25:26","guid":{"rendered":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/?p=588"},"modified":"2025-02-26T13:21:37","modified_gmt":"2025-02-26T13:21:37","slug":"stan-with-stata-part-iv-automation","status":"publish","type":"post","link":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/2015\/05\/26\/stan-with-stata-part-iv-automation\/","title":{"rendered":"Stan with Stata, Part IV: Automation"},"content":{"rendered":"<p>Last time\u00a0I presented\u00a0a very basic\u00a0do file for <strong>running\u00a0Stan from within Stata<\/strong> and although the program worked, there were places where we had to find work-arounds that meant that the process was not fully automatic. In particular, we had to write the script manually, we\u00a0used Stata&#8217;s shell command to run\u00a0the compiled code\u00a0and we had to copy and paste the results into Stata. This week\u00a0I will address those issues.<\/p>\n<p><strong>Executables.txt<\/strong><br \/>\nIn order to tell the wbs programs were to find WinBUGS, OpenBUGS etc., I recommend creating a file called executables.txt in your PERSONAL folder (you can find the location of your PERSONAL folder by typing the sysdir command into Stata). executables.txt is a simple text file, which in my case includes lines such as,<br \/>\n<span style=\"color: #0000ff\">WINBUGS,\u201dC:\\Software\\WinBUGS14\\WinBUGS14.exe\u201d<\/span><br \/>\n<span style=\"color: #0000ff\"> OPENBUGS,&#8221;C:\\Software\\OpenBUGS\\OpenBUGS.exe&#8221;<\/span><br \/>\n<span style=\"color: #0000ff\"> JAGS,&#8221;C:\\Software\\JAGS\\JAGS-3.4.0\\x64\\bin\\jags.bat&#8221;<\/span><br \/>\nNow we need to add a line for Stan. We will not give the name of the executable as that is not the way that Stan works, but rather we will give the <strong>location of the cmdstan folder<\/strong>. Notice that Stan needs linux-style slashes\u00a0\/ in the path.<br \/>\n<span style=\"color: #0000ff\">STAN,&#8221;C:\/Software\/cmdstan-v2.6.2&#8243;<\/span><\/p>\n<p><strong>wbsscript<\/strong><br \/>\nWhen the book\u00a0<em>Bayesian analysis with Stata<\/em> was published wbsscript only handled WinBUGS and OpenBUGS so when I added the facility to run JAGS, I created a new program called wbsscript_v2.ado, which was\u00a0downloadable through my webpage. The command was given a new name so as not to cause confusion with the version made available by Stata at the time when the book was published. When the\u00a0new version has\u00a0been thoroughly tested I\u00a0plan to\u00a0update wbsscript with the contents of wbsscript_v2, except that now I also want to add a stan option, so I have created a new program called <strong>wbsscript_v3.ado<\/strong> that updates wbsscript_v2.ado and which can handle WinBUGS, OpenBUGS, JAGS and Stan. This program can also be downloaded from my home page although\u00a0it should be treated as still being under test.<\/p>\n<p><strong>wbsrun &amp; wbscoda<\/strong><br \/>\nI have created\u00a0new versions of wbsrun and wbscoda called <strong>wbsrun_v3<\/strong> and <strong>wbscoda_v3<\/strong> that have a Stan option and which are able to run simple Stan programs and read the results into Stata.<\/p>\n<p>Putting these things together we can run Stan much as we have been running WinBUGS, OpenBUGS and JAGS. Here is an updated version of the program that I presented last week.<br \/>\n<span style=\"color: #0000ff\">clear<\/span><br \/>\n<span style=\"color: #0000ff\"> set obs 20<\/span><br \/>\n<span style=\"color: #0000ff\"> local TRUE_THETA 0.4<\/span><br \/>\n<span style=\"color: #0000ff\"> gen y = runiform() &lt; `TRUE_THETA&#8217;<\/span><\/p>\n<p><span style=\"color: #0000ff\">wbsmodel thisdofile.do BernModel.stan<\/span><br \/>\n<span style=\"color: #0000ff\"> \/*<\/span><br \/>\n<span style=\"color: #0000ff\"> data {<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 int N;<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 int y[N];<\/span><br \/>\n<span style=\"color: #0000ff\"> }<\/span><br \/>\n<span style=\"color: #0000ff\"> parameters {<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 real theta;<\/span><br \/>\n<span style=\"color: #0000ff\"> }<\/span><br \/>\n<span style=\"color: #0000ff\"> model {<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 theta ~ beta(1,1);<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 for( i in 1:N ) {<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 y[i] ~ bernoulli(theta);<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><br \/>\n<span style=\"color: #0000ff\"> }<\/span><br \/>\n<span style=\"color: #0000ff\"> *\/<\/span><\/p>\n<p><span style=\"color: #0000ff\">wbslist_v2 (N &lt;- 20) (vect y , format(%3.0f)) using BernData.txt, replace jags<\/span><br \/>\n<span style=\"color: #0000ff\"> wbsscript_v3 using BernScript.bat , replace \/\/\/<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 model(BernModel.stan) data(BernData.txt) stan<\/span><br \/>\n<span style=\"color: #0000ff\"> wbsrun_v3 using BernScript.bat , stan<\/span><br \/>\n<span style=\"color: #0000ff\"> type log.txt<\/span><br \/>\n<span style=\"color: #0000ff\"> wbscoda_v3 using output.csv , stan clear<\/span><\/p>\n<p><span style=\"color: #0000ff\">mcmctrace theta<\/span><br \/>\n<span style=\"color: #0000ff\"> mcmcstats theta<\/span><\/p>\n<p><strong>Options<\/strong><br \/>\nMany of the command line options for Stan control aspects of the samplers and we cannot really discuss those without first understanding the nature of Stan&#8217;s samplers. However, there are simple features such as the run length and the names of the output files that we can easily modify. I have implemented the following using existing options for wbsscript:<br \/>\n<strong>burnin<\/strong>=length of burnin (num_warmup in Stan)<br \/>\n<strong>updates<\/strong> =\u00a0length of the run\u00a0after the burnin\u00a0 (num_samples in Stan is updates+burnin)<br \/>\n<strong>thin<\/strong>=thinning constant<br \/>\nc<strong>odafile<\/strong>=output file name (defaults to output.csv for Stan. Of course, Stan creates\u00a0a .csv file\u00a0that is\u00a0not in coda format, so\u00a0ideally we should use a more generic name for the option)<br \/>\n<strong>seed<\/strong>= the random number seed. In Stan a negative value draws a seed from the time of day.<\/p>\n<p>So now we can write commands such as,<\/p>\n<p><span style=\"color: #0000ff\">wbsscript_v3 using BernScript.bat , replace \/\/\/<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 model(BernModel.stan) data(BernData.txt) \/\/\/<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 updates(3000) burnin(500) thin(2) \/\/\/<\/span><br \/>\n<span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 coda(BernOut.csv) stan<\/span><br \/>\n<span style=\"color: #0000ff\"> wbsrun_v3 using BernScript.bat , stan<\/span><br \/>\n<span style=\"color: #0000ff\"> wbscoda_v3 using BernOut.csv , stan clear<\/span><\/p>\n<p>To make any more progress we will need to know something about the samplers that Stan uses so that we can\u00a0call Stan in\u00a0a more intelligent way. I&#8217;ll start\u00a0to explain the Stan sampler\u00a0in my next posting. Unfortunately (for the flow of the blog) I will be working abroad for the next couple of weeks so the explanation of Hamiltonian Monte Carlo (also known as Hybrid Monte Carlo) will have to wait for my return.<\/p>\n<p>If you want to download any of the programs referred to in this posting they are available from my webpage at <a href=\"https:\/\/www2.le.ac.uk\/Members\/trj\"><span style=\"color: #205c90\">https:\/\/www2.le.ac.uk\/Members\/trj<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time\u00a0I presented\u00a0a very basic\u00a0do file for running\u00a0Stan from within Stata and although the program worked, there were places where we had to find work-arounds that meant that the process was not fully automatic. In particular, we had to write the script manually, we\u00a0used Stata&#8217;s shell command to run\u00a0the compiled code\u00a0and we had to copy [&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":[56,4],"class_list":["post-588","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-stan","tag-stata"],"_links":{"self":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/588","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=588"}],"version-history":[{"count":7,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/588\/revisions"}],"predecessor-version":[{"id":704,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/588\/revisions\/704"}],"wp:attachment":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/media?parent=588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/categories?post=588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/tags?post=588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}