{"id":207,"date":"2018-03-14T21:51:15","date_gmt":"2018-03-15T01:51:15","guid":{"rendered":"http:\/\/blog.uvm.edu\/tbplante\/?p=207"},"modified":"2024-11-03T13:08:26","modified_gmt":"2024-11-03T18:08:26","slug":"code-to-make-a-dot-and-95-confidence-interval-figure-in-stata","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2018\/03\/14\/code-to-make-a-dot-and-95-confidence-interval-figure-in-stata\/","title":{"rendered":"Code to make a dot and 95% confidence interval figure in Stata"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Dot and confidence interval figures in Stata<\/h2>\n\n\n\n<p>Stata has a pretty handy -twoway scatter- code that can be combined with -twoway rcap- to make the figure below. Example code at the bottom.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Horizontal version<\/h3>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\" style=\"grid-template-columns:85% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"745\" src=\"http:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/dot-and-95-percent-ci-figure-1-1024x745.png\" alt=\"\" class=\"wp-image-286 size-full\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/dot-and-95-percent-ci-figure-1-1024x745.png 1024w, https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/dot-and-95-percent-ci-figure-1-300x218.png 300w, https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/dot-and-95-percent-ci-figure-1-768x559.png 768w, https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/dot-and-95-percent-ci-figure-1.png 2000w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\"><\/p>\n<\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Vertical version<\/h3>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\" style=\"grid-template-columns:85% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"745\" src=\"http:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/dot-and-95-percent-ci-figure-verticle-1024x745.png\" alt=\"\" class=\"wp-image-289 size-full\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/dot-and-95-percent-ci-figure-verticle-1024x745.png 1024w, https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/dot-and-95-percent-ci-figure-verticle-300x218.png 300w, https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/dot-and-95-percent-ci-figure-verticle-768x559.png 768w, https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/dot-and-95-percent-ci-figure-verticle.png 2000w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\"><\/p>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">First step, make an Excel file or input directly in a do file<\/h2>\n\n\n\n<p>You might find it a bit easier to input data from an excel file than straight in a do file. Here&#8217;s an example of an excel file called &#8220;dot and 95 percent ci data.xlsx&#8221; saved in the same folder as my .do file. This figure will display row 1 at the top and row 14 at the bottom. The gaps in between the lines are the absent rows 3,6, 9, and 12. <em>Group<\/em> tells Stata if you want a red diamond or blue square. <em>Proportion<\/em> is the point estimate and&nbsp;<em>low95&nbsp;<\/em>and&nbsp;<em>high95<\/em> are the surrounding 95% confidence intervals. <\/p>\n\n\n\n<p><em>Note that the data here are made up and are not related to any actual ongoing clinical investigation.&nbsp;<\/em><\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\" style=\"grid-template-columns:84% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"447\" height=\"396\" src=\"http:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/updated-example-excel-file.png\" alt=\"\" class=\"wp-image-285 size-full\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/updated-example-excel-file.png 447w, https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/updated-example-excel-file-300x266.png 300w\" sizes=\"auto, (max-width: 447px) 100vw, 447px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\"><\/p>\n<\/div><\/div>\n\n\n\n<p>Alternatively, you can use Stata&#8217;s &#8211;input&#8211; functionality to input data from the top of your do file. I&#8217;ve been doing this step more often recently. Things to remember: (1) variables in Stata cannot start with a number, hence &#8216;low95&#8242; and not &#8217;95low&#8217; above, (2) remember to use &#8211;end&#8211; at the end of your input command, (3) if you are inputting a string variable, specify that the following variable is a string (e.g., &#8220;strL varname&#8221;) in your input loop, and (4) remember to use blanks (dots or &#8220;&#8221; for strings) for missing variables. Here&#8217;s what the above would look like using an &#8211;input&#8211; command in the do file, right above the following code. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>clear all \/\/ clear all data in memory\n\ninput \/\/\/\ngroup proportion low95 high95\n1 1.2 1.1 1.4\n2 1.3 1.2 1.4\n. . . .\n1 1.05 1.01 1.5\n2 1.3 1.2 1.4\n. . . .\n1 1.1 1.05 1.15\n2 1.2 1.14 1.25\n. . . .\n1 1.3 1.2 1.4\n2 1.3 1.2 1.4\n. . . .\n1 1.4 1.3 1.45\n2 1.4 1.2 1.5\nend\n\n\/\/ this makes a variable that's 1 through n for each row by group, called \"row\"\ngen row = _n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Next step, make a .do file<\/h2>\n\n\n\n<p>In the same folder as the Excel file, copy\/paste\/save the code below as a .do file.&nbsp;<strong>Close Excel and close Stata<\/strong> then find the .do file from Windows Explorer and double click it.<em> Doing this will force Stata to set the working directory as the folder containing the .do file (and the Excel file).<\/em><\/p>\n\n\n\n<p>If the code won&#8217;t work, you probably have Excel open. Close it and try again.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">That&#8217;s it! Code for both versions of the figure follows. <\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>********************************************************************************\n******************************IMPORT DATA HERE**********************************\n********************************************************************************\n\/\/ if importing from an excel file, do that here:\nclear all \/\/ clear all data in memory\nimport excel \"dot and 95 percent ci data.xlsx\", firstrow clear\ndestring, replace\n\n\/\/ if using Stata's --input-- command, plop that here (see example code above)\n\n******************************************************************************** \n******************************CODE STARTS HERE********************************** \n******************************************************************************** \nset scheme s1mono \/\/ black and white\n\ntwoway \/\/\/\n(rcap low95 high95 row, horizontal) \/\/\/ code for 95% CI\n(scatter row proportion if group ==1, mcolor(red)) \/\/\/ dot for group 1\n(scatter row proportion if group ==2, mcolor(blue)) \/\/\/ dot for group 2\n, \/\/\/\nlegend(row(1) order(2 \"legend 1\" 3 \"legend 2\") pos(6)) \/\/\/ legend at 6 o'clock position\nylabel(1.5 \"Model A\" 4.5 \"Model B\" 7.5 \"Model C\" 10.5 \"Model D\" 13.5 \"Model E\", angle(0) noticks) \/\/\/\n\/\/\/ note that the labels are 1.5, 4.5, etc so they are between rows 1&amp;2, 4&amp;5, etc.\n\/\/\/ also note that there is a space in between different rows by leaving out rows 3, 6, 9, and 12 \nxlabel(.95 \" \" 1 \"1.0\" 1.1 \"1.1\" 1.2 \"1.2\" 1.3 \"1.3\" 1.4 \"1.4\" 1.5 \"1.5\" 1.6 \" \", angle(0)) \/\/\/ no 1.6 label\ntitle(\"Title\") \/\/\/\nxtitle(\"X axis\") \/\/\/ \nytitle(\"Y axis\") \/\/\/ \nyscale(reverse) \/\/\/ y axis is flipped\nxline(1.0, lpattern(dash) lcolor(gs8)) \/\/\/\n\/\/\/ aspect (next line) is how tall or wide the figure is\naspect(.5)\n\ngraph export \"dot and 95 percent ci figure horiz.png\", replace width(2000)\n\/\/graph export \"dot and 95 percent ci figure horiz.tif\", replace width(2000)\n\n\n********************************************************************************\n******************************CODE STARTS HERE**********************************\n********************************************************************************\nset scheme s1mono \/\/ black and white\n\ntwoway \/\/\/\n(rcap low95 high95 row, vert) \/\/\/ code for 95% CI\n(scatter proportion row if group ==1, mcolor(red)) \/\/\/ dot for group 1\n(scatter proportion row if group ==2, mcolor(blue)) \/\/\/ dot for group 2\n, \/\/\/\nlegend(row(1) order(2 \"legend 1\" 3 \"legend 2\") pos(6)) \/\/\/ legend at 6 o\u2019clock position\nxlabel(1.5 \"Model A\" 4.5 \"Model B\" 7.5 \"Model C\" 10.5 \"Model D\" 13.5 \"Model E\", angle(0) noticks) \/\/\/\n\/\/\/ note that the labels are 1.5, 4.5, etc so they are between rows 1&amp;2, 4&amp;5, etc.\n\/\/\/ also note that there is a space in between different rows by leaving out rows 3, 6, 9, and 12\nylabel(0.9 \"0.9\" 1 \"1.0\" 1.1 \"1.1\" 1.2 \"1.2\" 1.3 \"1.3\" 1.4 \"1.4\" 1.5 \"1.5\" , angle(0)) \/\/\/ no 1.6 label\ntitle(\"Title\") \/\/\/\nxtitle(\"X axis\") \/\/\/\nytitle(\"Y axis\") \/\/\/\nyline(1.0, lpattern(dash) lcolor(gs8)) \/\/\/\n\/\/\/ aspect (next line) is how tall or wide the figure is\naspect(.5)\n\ngraph export \"dot and 95 percent ci figure vert.png\", replace width(2000)\n\/\/graph export \"dot and 95 percent ci figure vert.tif\", replace width(2000)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dot and confidence interval figures in Stata Stata has a pretty handy -twoway scatter- code that can be combined with -twoway rcap- to make the figure below. Example code at the bottom. Horizontal version Vertical version First step, make an Excel file or input directly in a do file You might find it a bit &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2018\/03\/14\/code-to-make-a-dot-and-95-confidence-interval-figure-in-stata\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code to make a dot and 95% confidence interval figure 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":[],"class_list":["post-207","post","type-post","status-publish","format-standard","hentry","category-stata-code"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/207","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=207"}],"version-history":[{"count":21,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/207\/revisions"}],"predecessor-version":[{"id":1915,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/207\/revisions\/1915"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}