{"id":1669,"date":"2024-01-09T11:59:48","date_gmt":"2024-01-09T16:59:48","guid":{"rendered":"https:\/\/blog.uvm.edu\/tbplante\/?p=1669"},"modified":"2024-01-09T12:18:27","modified_gmt":"2024-01-09T17:18:27","slug":"printing-hazard-ratio-on-kaplan-meier-curve-in-stata","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2024\/01\/09\/printing-hazard-ratio-on-kaplan-meier-curve-in-stata\/","title":{"rendered":"Printing hazard ratio on Kaplan Meier curve in Stata"},"content":{"rendered":"\n<p>I recently made a figure that estimates a hazard ratio and renders it right on top of a Kaplan Meier curve in Stata. Here&#8217;s some example code to make this.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"612\" src=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2024\/01\/image-23-1024x612.png\" alt=\"\" class=\"wp-image-1694\" style=\"width:464px;height:auto\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2024\/01\/image-23-1024x612.png 1024w, https:\/\/blog.uvm.edu\/tbplante\/files\/2024\/01\/image-23-300x179.png 300w, https:\/\/blog.uvm.edu\/tbplante\/files\/2024\/01\/image-23-768x459.png 768w, https:\/\/blog.uvm.edu\/tbplante\/files\/2024\/01\/image-23.png 1122w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Good luck!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ Load example dataset. I got this from the --help stset-- file\nwebuse diet, clear\n\n\/\/ First, stset the data. \nstset dox \/\/\/ dox is the event or censor date\n, \/\/\/\nfailure(fail) \/\/\/ \"fail\" is the failure vs censor variable\nscale(365.25)\n\n\n\/\/ Next, estimate a cox ph model by \"hienergy\"\nstcox hienergy\n\/\/ now grab the bits from output of this\nlocal hrb=r(table)&#091;1,1]\nlocal hrlo=r(table)&#091;5,1]\nlocal hrhi=r(table)&#091;6,1]\nlocal pval = r(table)&#091;4,1]\n\/\/ now format the p-value so it's pretty\nif `pval'&gt;=0.056 {\n\tlocal pvalue \"P=`: display %3.2f `pval''\"\n}\nif `pval'&gt;=0.044 &amp; `pval'&lt;0.056 {\n\tlocal pvalue &quot;P=`: display %5.4f `pval&#039;&#039;&quot;\n}\nif `pval&#039; &lt;0.044 {\n\tlocal pvalue &quot;P=`: display %4.3f `pval&#039;&#039;&quot;\n}\nif `pval&#039; &lt;0.001 {\n\tlocal pvalue &quot;P&lt;0.001&quot;\n}\nif `pval&#039; &lt;0.0001 {\n\tlocal pvalue &quot;P&lt;0.0001&quot;\n}\n\ndi &quot;original P is &quot; `pval&#039; &quot;, formatted is &quot; &quot;`pvalue&#039;&quot;\ndi &quot;HR &quot; %4.2f `hrb&#039; &quot; (95% CI &quot; %4.2f `hrlo&#039; &quot;-&quot; %4.2f `hrhi&#039; &quot;; `pvalue&#039;)&quot;\n\n\/\/ Now make a km plot. this example uses CIs\nsts graph \/\/\/\n, \/\/\/\nsurvival \/\/\/ \nby(hienergy) \/\/\/\nplot1opts(lpattern(dash) lcolor(red)) \/\/\/ options for line 1\nplot2opts(lpattern(solid) lcolor(blue)) \/\/\/ options for line 2\nci \/\/\/ add CIs\nci1opts(color(red%20)) \/\/\/ options for CI 1\nci2opts(color(blue%20)) \/\/\/ options for CI 2\n\/\/\/ Following this is the legend, placed in the 6 O&#039;clock position. \n\/\/\/ Only graphics 5 and 6 are needed, but all 6 are shown so you \n\/\/\/ see that other bits that can show up in the legend. Delete \n\/\/\/ everything except for 5 and 6 to hide the rest of the legend components\nlegend(order(1 &quot;&#091;one]&quot; 2 &quot;&#091;two]&quot; 3 &quot;&#091;three]&quot; 4 &quot;&#091;four]&quot; 5 &quot;First group&quot; 6 &quot;Second group&quot;) position(6)) \/\/\/\n\/\/\/ Risk table to print at the bottom:\nrisktable(0(5)20 , size(small) order(1 &quot;First group&quot; 2 &quot;Second group&quot;)) \/\/\/\ntitle(&quot;Title&quot;) \/\/\/\nt1title(&quot;Subtitle&quot;) \/\/\/\nxtitle(&quot;Year of Follow-Up&quot;) \/\/\/\nytitle(&quot;Event-Free Survival&quot;) \/\/\/\n\/\/\/ Here&#039;s how you render the HR. Change the first 2 numbers to move it:\ntext(0 0 &quot;`: display &quot;HR &quot; %4.2f `hrb&#039; &quot; (95% CI &quot; %4.2f `hrlo&#039; &quot;-&quot; %4.2f `hrhi&#039; &quot;; `pvalue&#039;)&quot;&#039;&quot;, placement(e) size(medsmall)) \/\/\/\nyla(0(0.2)1) <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I recently made a figure that estimates a hazard ratio and renders it right on top of a Kaplan Meier curve in Stata. Here&#8217;s some example code to make this. Good luck!<\/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":[703490,703489,502556,703492,703491],"class_list":["post-1669","post","type-post","status-publish","format-standard","hentry","category-stata-code","tag-cox-proportional-hazards-models","tag-hazard-ratio","tag-stata","tag-stcox","tag-stset"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1669","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=1669"}],"version-history":[{"count":8,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1669\/revisions"}],"predecessor-version":[{"id":1696,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1669\/revisions\/1696"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=1669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=1669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=1669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}