{"id":2162,"date":"2023-08-01T20:39:53","date_gmt":"2023-08-02T00:39:53","guid":{"rendered":"https:\/\/www.kaichen.work\/?p=2162"},"modified":"2023-08-25T17:27:32","modified_gmt":"2023-08-25T21:27:32","slug":"stata-command-to-draw-a-graph-showing-the-average-of-a-variable-by-group","status":"publish","type":"post","link":"https:\/\/www.kaichen.work\/?p=2162","title":{"rendered":"Stata command to draw a graph showing the average of a variable by group"},"content":{"rendered":"<p>Suppose we have the following dataset that provides the life expectancy (<code>lexp<\/code>) of each country in each region:<\/p>\n<p><a href=\"https:\/\/www.kaichen.work\/wp-content\/uploads\/2023\/08\/lifeexp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2164\" src=\"https:\/\/www.kaichen.work\/wp-content\/uploads\/2023\/08\/lifeexp.png\" alt=\"\" width=\"2928\" height=\"1898\" srcset=\"https:\/\/www.kaichen.work\/wp-content\/uploads\/2023\/08\/lifeexp.png 2928w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2023\/08\/lifeexp-300x194.png 300w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2023\/08\/lifeexp-1024x664.png 1024w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2023\/08\/lifeexp-768x498.png 768w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2023\/08\/lifeexp-1536x996.png 1536w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2023\/08\/lifeexp-2048x1328.png 2048w\" sizes=\"(max-width: 2928px) 100vw, 2928px\" \/><\/a><\/p>\n<p>We can draw a bar graph to show the average (or other statistics) life expectancy of each region using two different methods:<\/p>\n<h4>Method 1: using the <code>collapse<\/code> command<\/h4>\n<pre class=\"lang:sas decode:true\">sysuse lifeexp, clear\r\n\r\ncollapse (mean) lexp, by(region)\r\ngraph twoway bar lexp region<\/pre>\n<h4>Method 2: using the <code>egen tag<\/code> command<\/h4>\n<pre class=\"lang:sas decode:true \">sysuse lifeexp, clear\r\n\r\nbysort region: egen mean_lexp=mean(lexp)\r\negen tag: tag=tag(region)\r\n\r\ngraph twoway bar mean_lexp region if tag==1<\/pre>\n<p>Method 1 will actually destroy the original data. If we need to keep the original data available for use afterward, Method 2 may be more convenient.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose we have the following dataset that provides the life expectancy (lexp) of each country in each region: We can draw a bar graph to show the average (or other statistics) life expectancy of each region using two different methods: &hellip; <a href=\"https:\/\/www.kaichen.work\/?p=2162\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[17],"_links":{"self":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/2162"}],"collection":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2162"}],"version-history":[{"count":7,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/2162\/revisions"}],"predecessor-version":[{"id":2179,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/2162\/revisions\/2179"}],"wp:attachment":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}