{"id":1229,"date":"2022-10-11T11:33:56","date_gmt":"2022-10-11T15:33:56","guid":{"rendered":"https:\/\/blog.uvm.edu\/tbplante\/?p=1229"},"modified":"2024-12-11T09:57:48","modified_gmt":"2024-12-11T14:57:48","slug":"making-a-bland-altman-plot-with-printed-mean-and-sd-in-stata","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2022\/10\/11\/making-a-bland-altman-plot-with-printed-mean-and-sd-in-stata\/","title":{"rendered":"Making a Bland-Altman plot with printed mean and SD in Stata"},"content":{"rendered":"\n<p>The below code outputs this Bland-Altman plot figure, which prints the mean and SD and puts a solid line for mean difference and red dotted lines for mean difference +\/- 1.96*SD. (Note: the <a rel=\"noreferrer noopener\" href=\"https:\/\/pubmed.ncbi.nlm.nih.gov\/2868172\/\" target=\"_blank\">original BA paper<\/a> used +\/- 2*SD, but it&#8217;s reasonable to use +\/-1.96*SD given it&#8217;s commonality in estimating the bounds of 95% confidence intervals of a normally-distributed plot). By the way, see the related page on making a scatterplot <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2022\/10\/10\/making-a-scatterplot-with-r-squared-and-percent-coefficient-of-variation-in-stata\/\">here<\/a>, which should probably be shown alongside this figure in any publication. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"628\" height=\"606\" src=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/10\/image-23.png\" alt=\"\" class=\"wp-image-1993\" style=\"width:375px;height:auto\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/10\/image-23.png 628w, https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/10\/image-23-300x289.png 300w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/figure>\n\n\n\n<p>Here&#8217;s the code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ note this uses local macros so you need to run this entire thing\n\/\/ top to bottom in a do file, not line by line. \n\/\/\n\/\/ Step 1: input some fake data:\n\/\/ Note: for comparing tests (e.g., lab tests), I make x the \"gold standard\" \n\/\/       test and y the investigational test \nclear all\n\ninput id yvar xvar\n1 2 5\n2 3 5\n3 6 5\n4 7 7\n5 3 2\n6 10 12\n7 1 2\n8 11 12\n9 4 6\n10 5 5\nend\n\n\/\/ Step 2a: make difference (to go on y axis) and mean (x axis) variables\ngen diff= yvar - xvar\ngen mean = (yvar+xvar)\/2\n\n\/\/ Step 2b: Grab the difference (y axis) mean and sd\nsum diff, d\nlocal diffmean= r(mean)\nlocal diffsd = r(sd)\n\n\/\/ Step 3: graph!\nset scheme s1mono \/\/ I like this scheme\n\ntwoway \/\/\/\n(scatter diff mean, msymbol(O) msize(medium) mcolor(black)) \/\/\/\n, \/\/\/\n\/\/\/ black dotted line at zero:\nyline(0, lcolor(black) lpattern(dot) lwidth(medium)) \/\/\/\n\/\/\/ red solid line at mean:\nyline(`=`diffmean'',lcolor(red) lpattern(solid) lwidth(medium)) \/\/\/\n\/\/\/ red dashed lines at mean +\/- 1.96*sd\nyline(`=`diffmean'+1.96*`diffsd''  , lcolor(red) lpattern(dash) lwidth(medium)) \/\/\/\nyline(`=`diffmean'-1.96*`diffsd''  , lcolor(red) lpattern(dash) lwidth(medium)) \/\/\/\naspect(1) \/\/\/ force output to be square\n\/\/\/ here's the text box with mean and sd,\n\/\/\/ you'll need to tweak the first two numbers so the y,x\n\/\/\/ coordinates drop the text where you want it\ntext(6 0 \"{bf:Mean=`:di %3.1f `=`diffmean'''}\" \"{bf:SD=`:di %3.1f `=`diffsd'''}\", placement(se) justification(left)) \/\/\/ \nlegend(off) \/\/\/ Shut off the legend\n\/\/\/ Now tweak the ranges of the axes to your liking. \n\/\/\/ I like to make them the same width. In this example, the x axis is from \n\/\/\/ 0 to 12 so is 12 units wide. So I set the y axis to also be \n\/\/\/ 12 units wide, going from -6 to +6.\nxla(0(3)12) \/\/\/ you'll need to tweak range for your data\nyla(-6(3)6, angle(0)) \/\/\/ you'll need to tweak range for your data\nytitle(\"Difference, yvar minus xvar\") \/\/\/\nxtitle(\"Mean, yvar and xvar\") \/\/\/\ntitle(\"Title!\") <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The below code outputs this Bland-Altman plot figure, which prints the mean and SD and puts a solid line for mean difference and red dotted lines for mean difference +\/- 1.96*SD. (Note: the original BA paper used +\/- 2*SD, but it&#8217;s reasonable to use +\/-1.96*SD given it&#8217;s commonality in estimating the bounds of 95% confidence &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2022\/10\/11\/making-a-bland-altman-plot-with-printed-mean-and-sd-in-stata\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Making a Bland-Altman plot with printed mean and SD in Stata<\/span><\/a><\/p>\n","protected":false},"author":4473,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[477491],"tags":[703419,703420,703418,222041,502556],"class_list":["post-1229","post","type-post","status-publish","format-standard","hentry","category-stata-code","tag-ba-plot","tag-bland-altman","tag-bland-altman-plot","tag-epidemiology","tag-stata"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/users\/4473"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/comments?post=1229"}],"version-history":[{"count":4,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1229\/revisions"}],"predecessor-version":[{"id":1994,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1229\/revisions\/1994"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=1229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=1229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=1229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}