{"id":1220,"date":"2022-10-10T14:51:46","date_gmt":"2022-10-10T18:51:46","guid":{"rendered":"https:\/\/blog.uvm.edu\/tbplante\/?p=1220"},"modified":"2024-12-11T10:00:24","modified_gmt":"2024-12-11T15:00:24","slug":"making-a-scatterplot-with-r-squared-and-percent-coefficient-of-variation-in-stata","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2022\/10\/10\/making-a-scatterplot-with-r-squared-and-percent-coefficient-of-variation-in-stata\/","title":{"rendered":"Making a scatterplot with R squared and percent coefficient of variation in Stata"},"content":{"rendered":"\n<p>I recently had to make some scatterplots for Figure 3 of <a href=\"https:\/\/pubmed.ncbi.nlm.nih.gov\/36865583\/\" data-type=\"link\" data-id=\"https:\/\/pubmed.ncbi.nlm.nih.gov\/36865583\/\" target=\"_blank\" rel=\"noreferrer noopener\">this paper<\/a>. I decided to clean up the code in case it might be helpful to others. <\/p>\n\n\n\n<p>The below code outputs a scatterplot with R-squared and %CV. I grabbed the %CV-from-a-regression code from Mehmet Mehmetoglu&#8217;s CV program. (Type &#8211;ssc install cv&#8211; to grab that one. It&#8217;s not needed for below.) By the way, you might also be interested in how to make a related Bland-Altman plot <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2022\/10\/11\/making-a-bland-altman-plot-with-printed-mean-and-sd-in-stata\/\" target=\"_blank\" rel=\"noreferrer noopener\">here.<\/a> <\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"604\" height=\"600\" src=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/10\/image-22.png\" alt=\"\" class=\"wp-image-1991\" style=\"width:379px;height:auto\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/10\/image-22.png 604w, https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/10\/image-22-300x298.png 300w, https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/10\/image-22-150x150.png 150w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/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:\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: Grab the R2 from a regresssion\nregress yvar xvar\nlocal rsq = e(r2)\n\/\/ Step 2b, from the same regression, calculate the percent CV\nlocal  rmse= e(rmse)\nlocal depvar = e(depvar)\nsum `depvar' if e(sample), meanonly\nlocal depvarmean = r(mean)\nlocal cv = `rmse'\/`depvarmean'*100\n\n\/\/ Step 3: graph!\nset scheme s1mono \/\/ I like this scheme\n\ntwoway \/\/\/\n\/\/\/ Line of unity, you'll need to tweak the range for your data.\n\/\/\/ note that 'y' and 'x' are not the same as 'yvar' and 'xvar'\n\/\/\/ but are instead what's required here to print the line of\n\/\/\/ unity:\n(function y=x , range(0 12) lcolor(red) lpattern(solid) lwidth(medium)) \/\/\/\n\/\/\/ line of fit:\n(lfit yvar xvar,  lcolor(red) lpattern(dash) lwidth(medium)) \/\/\/\n\/\/\/ Scatter plot dots:\n(scatter yvar xvar, msymbol(O) msize(medium) mcolor(black)) \/\/\/\n, \/\/\/\naspect(1) \/\/\/ force output to be square\n\/\/\/ here's the text box with R2 and CV,\n\/\/\/ you'll need to tweak the first two numbers so the y,x\n\/\/\/ coordinates drop the text where you want it\ntext(12 0 \"{bf:R\u00b2=`:di %3.1f `=`rsq'''}\" \"{bf:CV=`:di %4.1f `=`cv'''%}\", placement(se) justification(left)) \/\/\/ \nlegend(off) \/\/\/ Shut off the legend\nyla(0(3)12, angle(0)) \/\/\/ you'll need to tweak range for your data\nxla(0(3)12) \/\/\/ you'll need to tweak range for your data\nytitle(\"yvar!\") \/\/\/\nxtitle(\"xvar!\") \/\/\/\ntitle(\"Title!\") <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I recently had to make some scatterplots for Figure 3 of this paper. I decided to clean up the code in case it might be helpful to others. The below code outputs a scatterplot with R-squared and %CV. I grabbed the %CV-from-a-regression code from Mehmet Mehmetoglu&#8217;s CV program. (Type &#8211;ssc install cv&#8211; to grab that &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2022\/10\/10\/making-a-scatterplot-with-r-squared-and-percent-coefficient-of-variation-in-stata\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Making a scatterplot with R squared and percent coefficient of variation 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":[394639,703412,393478,222041,703415,703411,703410,53641,579,703417,502556],"class_list":["post-1220","post","type-post","status-publish","format-standard","hentry","category-stata-code","tag-cv-2","tag-coefficient-of-variation","tag-cv","tag-epidemiology","tag-percent-coefficient-of-variation","tag-r-squared","tag-r2","tag-regression","tag-research","tag-scatterplot","tag-stata"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1220","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=1220"}],"version-history":[{"count":10,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1220\/revisions"}],"predecessor-version":[{"id":1996,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1220\/revisions\/1996"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=1220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=1220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=1220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}