{"id":207,"date":"2014-08-03T09:10:33","date_gmt":"2014-08-03T09:10:33","guid":{"rendered":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/?p=207"},"modified":"2025-02-26T13:21:38","modified_gmt":"2025-02-26T13:21:38","slug":"solutions-to-the-exercises-chapter-2","status":"publish","type":"post","link":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/2014\/08\/03\/solutions-to-the-exercises-chapter-2\/","title":{"rendered":"Solutions to the Exercises: Chapter 2"},"content":{"rendered":"<p>This is the second of my occasional postings working through the exercises given at the end of the chapters in \u2018<em>Bayesian analysis with Stata<\/em>\u2019. Chapter 2 was perhaps the most difficult chapter to write because I wanted to cover the creation of Stata programs for the calculation of the log posterior before I had introduced the simulation algorithms that make use of those programs. As a result some of the applications are a little artificial.<\/p>\n<p>The exercises at the end of the chapter are relatively straight forward, none the less they raise some interesting issues. The code that is needed to\u00a0analyse the examples is given in a separate downloadable pdf available here (<a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/chapter2.pdf\">chapter2<\/a>\u00a0code)\u00a0and in this posting I restrict myself to commenting on the\u00a0answers and not the code. The questions, code and comments should really be viewed alongside one another.<\/p>\n<p><em><strong>Question 1<\/strong><\/em><\/p>\n<p>This question re-analyses some old data from an early breast cancer screening program. One of the trickiest aspects of the question is to assess your prior for the all-cause death rate per 100,000 women per year in unscreened women. The US Census Bureau data suggest a value around 600 deaths but there are uncertainties due to<br \/>\n\u2022 Not having national data for exactly the same years as HIPS<br \/>\n\u2022 Differences between HIPS and the National population both in geography and social class<br \/>\n\u2022 Uncertainty over the exact age structure of HIPS<\/p>\n<p>In the end I opted for G(60,10) giving a mean on 600 (60&#215;10) and a standard deviation of sqrt(60x10x10)=77, but of course you should rework the solution for your own priors and in a group exercise I would super-impose\u00a0all of the\u00a0priors on the same plot.<\/p>\n<p>The downloadable code includes three ways of calculating the posterior.<br \/>\n\u2022 Using Stata\u2019s integ command for numerical integration<br \/>\n\u2022 Using a program for calculating the log posterior<br \/>\n\u2022 Using the theoretical result that a Gamm distribution is conjugate for the Poisson mean<br \/>\nOf course, all three methods give the same result but the most generalizable approach is definitely the second of the three.<\/p>\n<p>Here is the posterior superimposed on my prior and the likelihood normalized so that it displays conveniently on the same axes.<\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-226\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig1.png\" alt=\"solution_chap2_fig1\" width=\"716\" height=\"521\" srcset=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig1.png 716w, https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig1-300x218.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/a><\/p>\n<p>We can see that the prior has had little impact on the posterior. In a group exercise I would also superimpose all of the posteriors on the same plot in order to emphasise that the posteriors are much more similar than the priors.<\/p>\n<p>For the screened group I opted for a G(55,10) prior to reflect my belief that screening would be beneficial. My prior reduces the anticipated mortality by just under 10%. The method for calculating the corresponding posterior is identical to that used for the control group.<\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-228\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig2.png\" alt=\"solution_chap2_fig2\" width=\"716\" height=\"521\" srcset=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig2.png 716w, https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig2-300x218.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/a><\/p>\n<p>Plotting the two posterior distributions on the same graph suggests a benefit due to screening much as I anticipated.<\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-229\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig3.png\" alt=\"solution_chap2_fig3\" width=\"716\" height=\"521\" \/><\/a><\/p>\n<p>It is interesting to distinguish two questions<br \/>\n(a) What is my best guess at the difference in mortality due to screening?<br \/>\n(b) Does HIPS provide evidence that screening makes a difference?<br \/>\nThis analysis has addressed the first of these questions and the answer rightly depends on the HIPS study data plus everything else I believe about breast cancer screening. The second question is different and would require us to strip away my anticipated benefit and to consider only the evidence from the HIPS data. This can be done within a Bayesian context but it requires the calculation of a Bayes Factor and that is not covered until Chapter 10.<\/p>\n<p>Is screening beneficial? \u2013 probably, but there are several reasons to hesitate<br \/>\n\u2022 The analysis conducted so far is influenced by my priors, which anticipated a difference? Perhaps we should conduct a sensitivity analysis with other priors<br \/>\n\u2022 The first few years of a screening program tend to pick up a backlog of late stage cancers that may be more difficult to treat \u2013 once established the screen should predominantly pick up early treatable cancers and show more benefit<br \/>\n\u2022 HIPS is not the whole world, so generalization is difficult \u2013 for instance, refusals might be much higher in another population and the benefit depends in part on the quality of treatment given to unscreened women.<br \/>\n\u2022 Remember that it is potentially misleading to assess a screening program based on cause-specific mortality, so don\u2019t be tempted to base your assessment on the breast cancer deaths (imagine a type of mammogram that gave women lung cancer)<\/p>\n<p><em><strong>Question 2<\/strong><\/em><\/p>\n<p>This question performs a meta-analysis of seven clinical trials. To give us a feel for the data here is forest plot based on frequentist estimates and confidence intervals.<\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-230\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig4.png\" alt=\"solution_chap2_fig4\" width=\"716\" height=\"521\" srcset=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig4.png 716w, https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig4-300x218.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/a><\/p>\n<p>The question requires us to start by setting a prior. My priors tend to be more informative than some people would be happy with (see my posting \u2013 <a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/2014\/03\/14\/why-i-dont-use-winbugs-priors\/\">Why I don\u2019t use WinBUGS priors<\/a>). In this case I felt that the treatment would do something between having no effect (log(1)=0) and halving the rate of contrast nephropathy (log(0.5)=-0.7). This led me to a rather conservative prior of N(-0.4,sd=0.3). We could also put priors on the sigma\u2019s but as we only want a distribution conditional on knowing the sigma\u2019s, there is no point.<\/p>\n<p>Calculation uses a program for calculating the log posterior and is shown in the pdf. My posterior and prior (dashed) for theta are<\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-231\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig5.png\" alt=\"solution_chap2_fig5\" width=\"716\" height=\"521\" srcset=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig5.png 716w, https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig5-300x218.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/a><\/p>\n<p>Clearly my prior is consistent with the data. Prior to seeing the data I gave a 9% chance that the drug would increase the risk of contrast nephropathy (lnRR&gt;0) \u2013 after seeing the data this falls to 0.2%.<\/p>\n<p>It is interesting to represent these distributions on the relative risk (RR) scale (note the Jacobian in the calculations)<\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-232\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig6.png\" alt=\"solution_chap2_fig6\" width=\"716\" height=\"521\" srcset=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig6.png 716w, https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig6-300x218.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/a><\/p>\n<p>The result for the random effects model is very similar, at least when we condition on phi=0.5.<\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-233\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig7.png\" alt=\"solution_chap2_fig7\" width=\"716\" height=\"521\" srcset=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig7.png 716w, https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig7-300x218.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/a><\/p>\n<p>The random effects model makes little difference as the posteriors for theta under the two models demonstrate. The random effects posterior (solid line) is only slightly wider.<\/p>\n<p><a href=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-234\" src=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig8.png\" alt=\"solution_chap2_fig8\" width=\"716\" height=\"521\" srcset=\"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig8.png 716w, https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/files\/2014\/07\/solution_chap2_fig8-300x218.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/a><\/p>\n<p>In this case it would be better to average over the distribution of phi and not to condition on a specific value but that requires the methods of chapter 3.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the second of my occasional postings working through the exercises given at the end of the chapters in \u2018Bayesian analysis with Stata\u2019. Chapter 2 was perhaps the most difficult chapter to write because I wanted to cover the creation of Stata programs for the calculation of the log posterior before I had introduced [&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":[26,27,28,4],"class_list":["post-207","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-bayesian-analysis","tag-exercises","tag-solutions","tag-stata"],"_links":{"self":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/207","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=207"}],"version-history":[{"count":6,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/207\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/posts\/207\/revisions\/241"}],"wp:attachment":[{"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/media?parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/categories?post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staffblogs.le.ac.uk\/bayeswithstata\/wp-json\/wp\/v2\/tags?post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}