{"id":370,"date":"2014-10-31T10:09:14","date_gmt":"2014-10-31T10:09:14","guid":{"rendered":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/?p=370"},"modified":"2025-02-26T13:21:38","modified_gmt":"2025-02-26T13:21:38","slug":"solutions-to-the-exercises-chapter-3-question-2","status":"publish","type":"post","link":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/2014\/10\/31\/solutions-to-the-exercises-chapter-3-question-2\/","title":{"rendered":"Solutions to the Exercises: Chapter 3 &#8211; Question 2"},"content":{"rendered":"<p>Last time I presented my solution to the first question at the end of chapter 3 of \u2018<em>Bayesian analysis with Stata<\/em>\u2019 and this time I want to consider question 2 from the same chapter.<\/p>\n<p><strong>Question 2<\/strong><\/p>\n<p>This question analyses some data on the prevalence of very pre-term births (VPT) in Europe using a small 1997 survey as the basis for creating the prior in the analysis of a larger study conducted in 2003. The calculations are simple but illustrate how Bayesian analyses can provided information that is simply unavailable using traditional methods.<\/p>\n<p>(i) <strong>Use a binomial model for the total number of VPT births in 2003. Base your prior for the binomial parameter, p, on the 1997 survey and approximate it by a suitable beta distribution. Use a Metropolis-Hastings algorithm to derive the posterior for p.<\/strong><\/p>\n<p>The earlier survey covered two regions and so produced two estimates of the proportion of VPT births; Nord-du-Calais 820\/54815 = 0.015 and Trent 1149\/59394 = 0.019.<\/p>\n<p>There are two priors that we might want to specify,<br \/>\n(a) the VPT probability in a randomly chosen region of Europe in 2003<br \/>\n(b) the VPT probability for the whole of Europe in 2003<br \/>\nA third possibility is to produce a prior for a specific region in 2003 but this would require more detailed knowledge of that region than I have and so I will not attempt that.<\/p>\n<p>We are asked for prior (b) but it is instructive to consider them both. Of course, these priors are subjective and here you are getting my opinion.<\/p>\n<p>I based my prior on the two earlier surveys together with the article<br \/>\nTucker &amp; McGuire<br \/>\n<strong>Epidemiology of preterm birth<\/strong><br \/>\n<em>BMJ<\/em>. 2004; 329(7467): 675\u2013678<\/p>\n<p>The BMJ article tells me that the proportion of pre-term births had increased slightly over the previous 20 years but that the VPT birth rate stayed fairly constant at between 1-2%. The rates being slightly higher in the USA (1.9%) than in Europe. Factors influencing the preterm rates include the frequency of multiple births, the impact of assisted reproduction, the trend towards more obstetric intervention and better measurement of the gestational age through ultrasound rather than menstrual period date.<br \/>\nFor a randomly chosen region of Europe, I decided that I believed that the proportion of VPT births will turn out to be between 0.010 and 0.025, perhaps Beta(34,1966) which has a mean of 0.0170 and a standard deviation of 0.0029.<\/p>\n<p>My prior for a random region is shown below with the earlier proportions for Nord-du-Calais and Trent shown in red,<\/p>\n<p><span style=\"color: #000080\">twoway function y= betaden(34,1966,x), range(0 0.035)\u00a0 <\/span><span style=\"color: #000080\">xlabel(0(0.005) 0.035) \/\/\/<\/span><br \/>\n<span style=\"color: #000080\"> xtitle(p) ytitle(Prior density) xline(0.016 0.019,lcol(red))<\/span><\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-373\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig6.png\" alt=\"solution_chap3_fig6\" width=\"716\" height=\"521\" srcset=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig6.png 716w, https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig6-300x218.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/a><\/p>\n<p>The sample that we want to analyse covers much more of Europe and\u00a0consists of\u00a0the combined results for 10 regions, so for consistency we ought to have a mean around 0.0170 and a standard deviation of about 0.0029\/sqrt(10)=0.0009. I decided on Beta(255,14745) which has a mean of 0.0170 and st dev of 0.0011.<\/p>\n<p>Here is my prior for all Europe together with my earlier prior for a single region shown as a dashed line.<br \/>\n<span style=\"color: #000080\">twoway (function y= betaden(255,14745,x), range(0 0.035) ) \/\/\/<\/span><br \/>\n<span style=\"color: #000080\"> (function y= betaden(34,1966,x), range(0 0.035) lpat(dash) ) , \/\/\/<\/span><br \/>\n<span style=\"color: #000080\"> xlabel(0(0.005)0.035) xtitle(p) ytitle(Prior density) leg(off)<\/span><\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-374\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig7.png\" alt=\"solution_chap3_fig7\" width=\"716\" height=\"521\" srcset=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig7.png 716w, https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig7-300x218.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/a><\/p>\n<p>The second survey gives us the observation of 6533 VPT births out of 497482 live births. First we need a program to calculate the log-posterior.<\/p>\n<p><span style=\"color: #000080\">program logpost<\/span><br \/>\n<span style=\"color: #000080\"> args logp b<\/span><\/p>\n<p><span style=\"color: #000080\">local p = `b'[1,1]<\/span><br \/>\n<span style=\"color: #000080\"> scalar `logp&#8217; = 6533*log(`p&#8217;)+490949*log(1-`p&#8217;) \/\/\/<\/span><br \/>\n<span style=\"color: #000080\"> + 254*log(`p&#8217;) + 14744*log(1-`p&#8217;)<\/span><br \/>\n<span style=\"color: #000080\"> end<\/span><\/p>\n<p>In this program I have chosen not to use logdensity but rather to program the calculations directly. You could of course use<br \/>\n<span style=\"color: #000080\">scalar `logp&#8217; = 0<\/span><br \/>\n<span style=\"color: #000080\"> logdensity binomial `logp&#8217; 6533 490949 `p&#8217;<\/span><br \/>\n<span style=\"color: #000080\"> logdensity beta `logp&#8217; `p&#8217; 255 14745<\/span><\/p>\n<p>A by-product of writing the log-posterior as a direct calculation is that it makes it obvious that the problem is conjugate and by simple addition we can see that the log-posterior is,<br \/>\n6787log(p)+505693log(1-p)<br \/>\nand so the posterior must be Beta(6788,505694) and the posterior mean is 6788\/(6788+505694)=0.01325 and the posterior st dev is 0.0001597.<\/p>\n<p>Despite knowing the exact form of the posterior, we are asked to obtain it using a Metropolis-Hastings algorithm.<\/p>\n<p><span style=\"color: #000080\">set seed 950274<\/span><br \/>\n<span style=\"color: #000080\"> matrix b = 0.017<\/span><br \/>\n<span style=\"color: #000080\"> mcmcrun logpost b using temp.csv, replace \/\/\/<\/span><br \/>\n<span style=\"color: #000080\"> sampler( mhslogn , sd(0.1) ) burnin(1000) update(5000) par(p) jpost<\/span><br \/>\n<span style=\"color: #000080\"> insheet using temp.csv, clear<\/span><br \/>\n<span style=\"color: #000080\"> gen pc = 100*p<\/span><br \/>\n<span style=\"color: #000080\"> mcmcstats pc<\/span><\/p>\n<pre>----------------------------------------------------------------------------\r\n Parameter n    mean     sd    sem median    95% CrI\r\n ----------------------------------------------------------------------------\r\n pc        5000 1.325 0.017 0.0007 1.324 ( 1.294, 1.354 )\r\n ----------------------------------------------------------------------------<\/pre>\n<p>I have given the results on a percentage scale so that we can see more of the significant figures. The accuracy of the MCMC algorithm is near perfect for the mean and only slightly high for the st dev.<\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-375\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig8.png\" alt=\"solution_chap3_fig8\" width=\"768\" height=\"558\" srcset=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig8.png 768w, https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig8-300x217.png 300w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/a><\/p>\n<p>Mixing looks reasonable and there does not appear to be any drift. In fact the plot is a little deceptive because out of 5000 simulations, 4197 were identical with the one before, showing that the chain rejects too many values. This suggests a smaller proposal standard deviation.<\/p>\n<p>Changing the proposal standard deviation to 0.025 reduces the standard error and the standard deviation is more accurate but in truth the improvement is unlikely to be of practical importance, which illustrates the robustness of the Metropolis-Hastings sampler to the choice of the proposal distribution.<\/p>\n<pre>----------------------------------------------------------------------------\r\nParameter n\u00a0\u00a0\u00a0 mean\u00a0\u00a0\u00a0\u00a0 sd\u00a0\u00a0\u00a0 sem median\u00a0\u00a0\u00a0 95% CrI\r\n ----------------------------------------------------------------------------\r\n pc       5000 1.325 0.016 0.0005 1.325 ( 1.294, 1.355 )\r\n ----------------------------------------------------------------------------<\/pre>\n<p>On a log-scale the posterior 95%CrI has a range of log(0.01355)-log(0.01294)=0.046, so we have confirmed that a good proposal standard deviation is about half of this range.<\/p>\n<p>You have probably noticed that my B(255,14745) prior was not such a good a guess. I thought that the level would be about 1.7% and it turned out to be closer to 1.3%. Fortunately the actual data come from a survey of 497,482 live births while my prior was the equivalent of the imagined results from a survey of 15,000 live births, so the prior was not very influential.<\/p>\n<p>(ii) <strong>Prepare a program that estimates within the same iteration of the Metropolis-Hastings algorithm all 10 region-specific estimates of p. Run an MCMC analysis using this program<\/strong><\/p>\n<p>For this question we do need a separate prior for each region but as I have no particular knowledge about the regions I will use my B(34,1966) prior for all of them.<\/p>\n<p><span style=\"color: #000080\">clear<\/span><br \/>\n<span style=\"color: #000080\"> set obs 10<\/span><br \/>\n<span style=\"color: #000080\"> input n y<\/span><br \/>\n<span style=\"color: #000080\"> 60444 743<\/span><br \/>\n<span style=\"color: #000080\"> 34065 381<\/span><br \/>\n<span style=\"color: #000080\"> 84867 1202<\/span><br \/>\n<span style=\"color: #000080\"> 52078 724<\/span><br \/>\n<span style=\"color: #000080\"> 51939 569<\/span><br \/>\n<span style=\"color: #000080\"> 48235 513<\/span><br \/>\n<span style=\"color: #000080\"> 43188 566<\/span><br \/>\n<span style=\"color: #000080\"> 35336 369<\/span><br \/>\n<span style=\"color: #000080\"> 56990 949<\/span><br \/>\n<span style=\"color: #000080\"> 30329 517<\/span><\/p>\n<p><span style=\"color: #000080\">program logpost<\/span><br \/>\n<span style=\"color: #000080\"> args logp b i<\/span><\/p>\n<p><span style=\"color: #000080\">local p = `b'[1,`i&#8217;]<\/span><br \/>\n<span style=\"color: #000080\"> scalar `logp&#8217; = y[`i&#8217;]*log(`p&#8217;)+(n[`i&#8217;]-y[`i&#8217;])*log(1-`p&#8217;) \/\/\/<\/span><br \/>\n<span style=\"color: #000080\"> + 33*log(`p&#8217;) + 1965*log(1-`p&#8217;)<\/span><br \/>\n<span style=\"color: #000080\"> end<\/span><\/p>\n<p><span style=\"color: #000080\">set seed 950274<\/span><br \/>\n<span style=\"color: #000080\"> matrix b = J(1,10,0.017)<\/span><br \/>\n<span style=\"color: #000080\"> mcmcrun logpost b using temp.csv, replace \/\/\/<\/span><br \/>\n<span style=\"color: #000080\"> sampler( 10(mhslogn , sd(0.025)) ) burnin(1000) update(5000) \/\/\/<\/span><br \/>\n<span style=\"color: #000080\"> par(p1-p10)<\/span><br \/>\n<span style=\"color: #000080\"> insheet using temp.csv, clear<\/span><br \/>\n<span style=\"color: #000080\"> forvalues i=1\/10 {<\/span><br \/>\n<span style=\"color: #000080\"> gen pc`i&#8217; = 100*p`i&#8217;<\/span><br \/>\n<span style=\"color: #000080\"> }<\/span><br \/>\n<span style=\"color: #000080\"> mcmcstats pc*<\/span><\/p>\n<pre>----------------------------------------------------------------------------\r\n Parameter n  mean    sd    sem median    95% CrI\r\n ----------------------------------------------------------------------------\r\n pc1    5000 1.243 0.044 0.0022 1.243 ( 1.159, 1.326 )\r\n pc2    5000 1.153 0.056 0.0037 1.152 ( 1.054, 1.268 )\r\n pc3    5000 1.424 0.040 0.0017 1.423 ( 1.349, 1.506 )\r\n pc4    5000 1.408 0.053 0.0028 1.406 ( 1.312, 1.514 )\r\n pc5    5000 1.120 0.044 0.0023 1.119 ( 1.037, 1.208 )\r\n pc6    5000 1.093 0.046 0.0027 1.093 ( 1.006, 1.185 )\r\n pc7    5000 1.331 0.054 0.0030 1.330 ( 1.224, 1.444 )\r\n pc8    5000 1.082 0.053 0.0035 1.080 ( 0.982, 1.194 )\r\n pc9    5000 1.666 0.052 0.0024 1.665 ( 1.568, 1.767 )\r\n pc10   5000 1.708 0.070 0.0038 1.709 ( 1.572, 1.841 )\r\n ----------------------------------------------------------------------------<\/pre>\n<p>(iii) <strong>Estimate the probability that the Trent region of the UK has the highest rate of VPT births out of the 10 regions<\/strong><\/p>\n<p>Trent is region 10 in the list above and does indeed have the largest posterior mean but the uncertainty is such that it is possible that region 9 or even one of the others has a higher probability of VPT births.<br \/>\n<span style=\"color: #000080\">egen mp = rmax(pc1-pc9)<\/span><br \/>\n<span style=\"color: #000080\"> count if pc10 &gt; mp<\/span><br \/>\nand we find that 3422 out of the 5000 simulations placed Trent with the highest VPT percentage. So the estimated probability that Trent has the highest probability is 0.68.<\/p>\n<p>Questions like this are of real interest but they simply cannot be answered by non-Bayesian methods that concentrate on P(Data|Model) rather than P(Model|Data).<\/p>\n<p>(iv) <strong>Estimate the probability that the parameter p for Flanders is higher than that for Trent<\/strong><\/p>\n<p>Flanders is region 1 and pc1 was never larger than pc10 so the estimated probability that Flanders has a higher rate than Trent is zero.<\/p>\n<p>(v) <strong>Estimate the range of values of p across the 10 regions. Plot a histogram of the ranges. Does this distribution demonstrate that the values of p are not the same in every region?<\/strong><\/p>\n<p><span style=\"color: #000080\">. egen rmx = rowmax(pc1-pc10)<\/span><br \/>\n<span style=\"color: #000080\"> . egen rmn = rowmin(pc1-pc10)<\/span><br \/>\n<span style=\"color: #000080\"> . gen range = rmx &#8211; rmn<\/span><\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-376\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig9.png\" alt=\"solution_chap3_fig9\" width=\"538\" height=\"394\" srcset=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig9.png 538w, https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/10\/solution_chap3_fig9-300x219.png 300w\" sizes=\"auto, (max-width: 538px) 100vw, 538px\" \/><\/a><\/p>\n<p>Gives an average range of about 0.65%. However, to choose properly between the two models (varying p and common p) we should use the methods of Chapter 10 where we address the question of model choice. Here we have only seen results under one of those models so we should not jump to conclusions. Model choice requires us to compute P(Data|Model) for different models, what we have calculated is just the marginal posterior distribution of the range of the pc&#8217;s.<\/p>\n<p>With hindsight this question is too simple and would benefit from the addition of a more difficult final part, perhaps replacing (iv).\u00a0Maybe the reader could be required to fit a hierarchical model in which the distribution of the region-specific probabilities of VPT birth follows a Beta distribution, B(a,b), and the prior is placed on a and b. Such a model is slightly more challenging to fit although the hardest part of the question would be to set an appropriate prior on (a,b)\u00a0since it would not make sense to treat a and b as independent.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time I presented my solution to the first question at the end of chapter 3 of \u2018Bayesian analysis with Stata\u2019 and this time I want to consider question 2 from the same chapter. Question 2 This question analyses some data on the prevalence of very pre-term births (VPT) in Europe using a small 1997 [&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":[40,28],"class_list":["post-370","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-metropolis-hastings","tag-solutions"],"_links":{"self":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/370","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=370"}],"version-history":[{"count":5,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/370\/revisions"}],"predecessor-version":[{"id":380,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/370\/revisions\/380"}],"wp:attachment":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/media?parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/categories?post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/tags?post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}