{"id":1317,"date":"2022-10-26T15:13:00","date_gmt":"2022-10-26T19:13:00","guid":{"rendered":"https:\/\/blog.uvm.edu\/tbplante\/?p=1317"},"modified":"2022-10-27T10:37:33","modified_gmt":"2022-10-27T14:37:33","slug":"formatting-p-values-for-stata-output","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2022\/10\/26\/formatting-p-values-for-stata-output\/","title":{"rendered":"Formatting P-values for Stata output"},"content":{"rendered":"\n<p>If you like automating your Stata output, you have probably struggled with how to format P-values so they display in a format that is common in journals, namely:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If <strong>P&gt;0.05 and not close to 0.05<\/strong>, P has an equals sign and you round at the hundredth place <ul><li>E.g., P=0.2777 becomes P=0.28<\/li><\/ul><\/li><li>If <strong>P is close to 0.05 <\/strong>, P has an equals sign and you round at the ten thousandth place<ul><li>E.g., P=0.05033259 becomes P=0.0503<\/li><li>E.g., P=0.0492823 become P=0.0493<\/li><\/ul><\/li><li>If <strong>well below 0.05 but above 0.001<\/strong>, P has an equals sign and you round at the hundredth place<ul><li>E.g., P=0.0028832 becomes P=0.003<\/li><\/ul><\/li><li>if <strong>below 0.001<\/strong>, display as &#8220;P&lt;0.001&quot;<\/li><li>if <strong>below 0.0001<\/strong>, display as &#8220;P&lt;0.0001&quot;<\/li><\/ul>\n\n\n\n<p>Here&#8217;s a loop that will do that for you. <strong><em>This contains local macros so you need to run it all at once from a do file, not line by line. <\/em><\/strong>You&#8217;ll need to have grabbed the P-value as a from Stata as a local macro named <strong>pval.<\/strong> It will generate a new local macro called <strong>pvalue<\/strong> that you need to treat as a string. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>local pval 0.05535235 \/\/ change me to play around with this\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}\ndi &quot;original P is &quot; `pval&#039; &quot;, formatted is &quot; &quot;`pvalue&#039;&quot;<\/code><\/pre>\n\n\n\n<p>Here&#8217;s how you might use it to format the text of a regression coefficient that you put on a figure.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/10\/image-19.png\" alt=\"\" class=\"wp-image-1318\" width=\"407\" height=\"298\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/10\/image-19.png 787w, https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/10\/image-19-300x220.png 300w, https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/10\/image-19-768x562.png 768w\" sizes=\"auto, (max-width: 407px) 100vw, 407px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>sysuse auto, clear\nregress weight mpg\n\/\/ let's grab the P-value for the mpg\nmatrix list r(table) \/\/notice that it's at &#091;4,1]\nlocal pval = r(table)&#091;4,1] \n\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}\ndi &quot;original P is &quot; `pval&#039; &quot;, formatted is &quot; &quot;`pvalue&#039;&quot;\n\n\/\/ now make a scatterplot and print out that formatted p-value\ntwoway \/\/\/\n(scatter weight mpg) \/\/\/\n(lfit weight mpg) \/\/\/\n, \/\/\/\ntext(3500 35 &quot;`pvalue&#039;&quot;, size(large))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you like automating your Stata output, you have probably struggled with how to format P-values so they display in a format that is common in journals, namely: If P&gt;0.05 and not close to 0.05, P has an equals sign and you round at the hundredth place E.g., P=0.2777 becomes P=0.28 If P is close &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2022\/10\/26\/formatting-p-values-for-stata-output\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Formatting P-values for Stata output<\/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":[523032,703434,502556],"class_list":["post-1317","post","type-post","status-publish","format-standard","hentry","category-stata-code","tag-formatting","tag-p-value","tag-stata"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1317","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=1317"}],"version-history":[{"count":8,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1317\/revisions"}],"predecessor-version":[{"id":1329,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1317\/revisions\/1329"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=1317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=1317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=1317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}