{"id":1218,"date":"2019-04-18T10:19:33","date_gmt":"2019-04-18T14:19:33","guid":{"rendered":"http:\/\/www.kaikaichen.com\/?p=1218"},"modified":"2020-10-11T00:49:18","modified_gmt":"2020-10-11T04:49:18","slug":"display-mean-and-median-test-results-in-stata-conveniently","status":"publish","type":"post","link":"https:\/\/www.kaichen.work\/?p=1218","title":{"rendered":"Display mean and median test results in Stata"},"content":{"rendered":"<p>Sometimes we may want to produce the following table to compare the mean and median of two groups:<\/p>\n<p><a href=\"http:\/\/kaichen.work\/wp-content\/uploads\/2019\/04\/mean-and-median.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1221\" src=\"http:\/\/kaichen.work\/wp-content\/uploads\/2019\/04\/mean-and-median.png\" alt=\"\" width=\"2068\" height=\"672\" srcset=\"https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/04\/mean-and-median.png 2068w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/04\/mean-and-median-300x97.png 300w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/04\/mean-and-median-768x250.png 768w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/04\/mean-and-median-1024x333.png 1024w\" sizes=\"(max-width: 2068px) 100vw, 2068px\" \/><\/a><\/p>\n<p>First of all, please refer to <a href=\"http:\/\/www.kaichen.work\/?p=1095\" target=\"_blank\" rel=\"noopener noreferrer\">this post<\/a> to see Stata commands to test equality of mean and median.<\/p>\n<p>However, it is time-consuming to glean numbers from the output of these Stata commands and place them in a table. It is even more struggling that you have to repeat the tedious process every time you update your sample.<\/p>\n<p>I write Stata codes to streamline the process. The codes vary between unpaired (i.e., unmatched) data and paired data.<\/p>\n<p><em><span style=\"text-decoration: underline;\"><strong>Unpaired data<\/strong><\/span><\/em><\/p>\n<p>The above example is unpaired data, i.e., suspect firm-years and other firm-years are not 1-to-1 or 1-to-m matched. One usage of unpaired data is the first step of Heckman&#8217;s two-step procedure, in which two groups of observations (i.e., the group that will be selected into the second step and the group that will not be selected into the second step) <strong>are stacked vertically<\/strong> in the dataset. The following codes are used for unpaired data. You only need to modify the first two lines to suit your data. The codes will generate a table in Stata&#8217;s output window like this:<\/p>\n<p><a href=\"http:\/\/kaichen.work\/wp-content\/uploads\/2019\/05\/unpaired-mean-and-median.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1284\" src=\"http:\/\/kaichen.work\/wp-content\/uploads\/2019\/05\/unpaired-mean-and-median.png\" alt=\"\" width=\"1854\" height=\"356\" srcset=\"https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/05\/unpaired-mean-and-median.png 1854w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/05\/unpaired-mean-and-median-300x58.png 300w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/05\/unpaired-mean-and-median-768x147.png 768w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/05\/unpaired-mean-and-median-1024x197.png 1024w\" sizes=\"(max-width: 1854px) 100vw, 1854px\" \/><\/a><\/p>\n<p>You can then select the output and right-click &#8220;Copy as table&#8221; and paste in Excel for a quick edit. The codes use t-test for mean and Wilcoxon rank-sum test for median.<\/p>\n<pre class=\"lang:default decode:true\">local vars retsd cfosd lnilliq authpct   \/\/put your variables here\r\nlocal group grpid   \/\/\"grpid\" is your group indicator that takes 1 and 0\r\n \r\nforeach v in `vars' {\r\n  di \"`v'\"\r\n  ttest `v', by(`group')\r\n  local mean_`v'_mean_0=round(r(mu_1),.001)\r\n  local mean_`v'_mean_1=round(r(mu_2),.001)\r\n  local mean_`v'_diff=`mean_`v'_mean_1'-`mean_`v'_mean_0'\r\n  local mean_`v'_p=r(p)\r\n}\r\n \r\nforeach v in `vars' {\r\n  sum `v' if `group'==0, detail\r\n  local p50_`v'_p50_0=round(r(p50),.001)\r\n  sum `v' if `group'==1, detail\r\n  local p50_`v'_p50_1=round(r(p50),.001)\r\n  ranksum `v', by(`group')\r\n  local p50_`v'_n_0=r(N_1)\r\n  local p50_`v'_n_1=r(N_2)\r\n  local p50_`v'_diff=`p50_`v'_p50_1'-`p50_`v'_p50_0'\r\n  local p50_`v'_p=2*normprob(-abs(r(z)))\r\n}\r\n \r\nqui {\r\n  noi di _newline\r\n  noi di \"{hline 115}\"\r\n  noi di _col(15) \"{c |} `group' = 1\" \/\/\/\r\n         _col(45) \"{c |} `group' = 0\" \/\/\/\r\n         _col(75) \"{c |} Diff\"\r\n  noi di _col(16) \"{hline 100}\"\r\n  noi di _col(15) \"{c |} Mean\" \/\/\/\r\n         _col(25) \"{c |} Median\" \/\/\/\r\n\t\t _col(35) \"{c |} N\" \/\/\/\r\n\t\t _col(45) \"{c |} Mean\" \/\/\/\r\n\t\t _col(55) \"{c |} Median\" \/\/\/\r\n\t\t _col(65) \"{c |} N\" \/\/\/\r\n\t\t _col(75) \"{c |} Mean\" \/\/\/\r\n\t\t _col(85) \"{c |} P\" \/\/\/\r\n\t\t _col(95) \"{c |} Median\" \/\/\/\r\n\t\t _col(105) \"{c |} P\"\r\n  noi di \"{hline 115}\"\r\n  foreach v in `vars' {\r\n    noi di %12s abbrev(\"`v'\",12) \/\/\/\r\n\t       _col(15) \"{c |}\" %8.3f `mean_`v'_mean_1' \/\/\/\r\n\t\t   _col(25) \"{c |}\" %8.3f `p50_`v'_p50_1' \/\/\/\r\n\t\t   _col(35) \"{c |}\" %8.0f `p50_`v'_n_1' \/\/\/\r\n\t\t   _col(45) \"{c |}\" %8.3f `mean_`v'_mean_0' \/\/\/\r\n\t\t   _col(55) \"{c |}\" %8.3f `p50_`v'_p50_0' \/\/\/\r\n\t\t   _col(65) \"{c |}\" %8.0f `p50_`v'_n_0' \/\/\/\r\n\t\t   _col(75) \"{c |}\" %8.3f `mean_`v'_diff' \/\/\/\r\n\t\t   _col(85) \"{c |}\" %8.3f `mean_`v'_p' \/\/\/\r\n\t\t   _col(95) \"{c |}\" %8.3f `p50_`v'_diff' \/\/\/\r\n\t\t   _col(105) \"{c |}\" %8.3f `p50_`v'_p' \r\n  }\r\n  noi di \"{hline 115}\"\r\n}<\/pre>\n<p><em><span style=\"text-decoration: underline;\"><strong>Paired data<\/strong><\/span><\/em><\/p>\n<p>A typical usage of paired data is to identify a matched control group for the treatment group. For example, identify a matched firm-year for an event firm-year based on a set of characteristics (same industry, same year, similar size and book-to-market), or identify a matched firm for every event firm based on the closest propensity score (i.e., propensity score matching).<\/p>\n<p>The following table is an example that compares the mean and median of two matched groups\u2014restating firms and non-restating group. Each restating firm is matched with a non-restating firm.<\/p>\n<p><a href=\"http:\/\/kaichen.work\/wp-content\/uploads\/2019\/05\/paired-mean-and-median.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1289\" src=\"http:\/\/kaichen.work\/wp-content\/uploads\/2019\/05\/paired-mean-and-median.png\" alt=\"\" width=\"1484\" height=\"416\" srcset=\"https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/05\/paired-mean-and-median.png 1484w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/05\/paired-mean-and-median-300x84.png 300w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/05\/paired-mean-and-median-768x215.png 768w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/05\/paired-mean-and-median-1024x287.png 1024w\" sizes=\"(max-width: 1484px) 100vw, 1484px\" \/><\/a><\/p>\n<p>Because of this matching relationship, every event firm and its control firm will be placed in the same row in the dataset. In other words, event firms and control firms are <strong>aligned horizontally<\/strong>. The following codes are used for paired data. You only need to modify the first two lines to suit your data. You must specify the same number of variables in the matched order in the first two lines. In other words, the first variable in the first line must be paired with the first variable in the second line, and so on. The codes will generate a table in Stata&#8217;s output window like this:<\/p>\n<p><a href=\"http:\/\/kaichen.work\/wp-content\/uploads\/2019\/05\/paired-mean-and-median-result.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1333\" src=\"http:\/\/kaichen.work\/wp-content\/uploads\/2019\/05\/paired-mean-and-median-result.png\" alt=\"\" width=\"1934\" height=\"354\" srcset=\"https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/05\/paired-mean-and-median-result.png 1934w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/05\/paired-mean-and-median-result-300x55.png 300w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/05\/paired-mean-and-median-result-768x141.png 768w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2019\/05\/paired-mean-and-median-result-1024x187.png 1024w\" sizes=\"(max-width: 1934px) 100vw, 1934px\" \/><\/a><\/p>\n<p>The codes use paired t-test for mean and Wilcoxon rank-sign test for median.<\/p>\n<pre class=\"lang:default decode:true \">\/\/put your paired variables in the first two lines. 1-to-1 correspondece is must\r\nlocal agrp \"drpre4 drpre3 drpre2 drpre1\"\t\t\/\/e.g., treatment group\r\nlocal bgrp \"mdrpre4 mdrpre3 mdrpre2 mdrpre1\"\t\/\/e.g., control group\r\n\r\nlocal n : word count `agrp'\r\n\r\nforvalues i = 1\/`n' {\r\n  local a : word `i' of `agrp'\r\n  local b : word `i' of `bgrp'\r\n  ttest `a'=`b'\r\n  local mean_`a'=round(r(mu_1),.001)\r\n  local mean_`b'=round(r(mu_2),.001)\r\n  local mean_`a'_diff=`mean_`a''-`mean_`b''\r\n  local n_`a'=r(N_1)\r\n  local mean_p_`a'=r(p)\r\n\r\n  sum `a', detail\r\n  local p50_`a'=round(r(p50),.001)\r\n  sum `b', detail\r\n  local p50_`b'=round(r(p50),.001)\r\n  signrank `a'=`b'\r\n  local p50_`a'_diff=round(`p50_`a''-`p50_`b'',.001)\r\n  local p50_p_`a'=2*normprob(-abs(r(z)))\r\n}\r\n\r\nqui {\r\n  noi di _newline\r\n  noi di \"{hline 120}\"\r\n  noi di _col(30) \"{c |}\" \/\/\/\r\n         _col(40) \"{c |} Var1\" \/\/\/\r\n\t\t _col(60) \"{c |} Var2\" \/\/\/\r\n\t\t _col(80) \"{c |} Diff\"\r\n  noi di _col(41) \"{hline 80}\"\r\n  noi di %27s \"Paired Var1 and Var2\" \/\/\/\r\n         _col(30) \"{c |} N\" \/\/\/\r\n\t\t _col(40) \"{c |} Mean\" \/\/\/\r\n\t\t _col(50) \"{c |} Median\" \/\/\/\r\n\t\t _col(60) \"{c |} Mean\" \/\/\/\r\n\t\t _col(70) \"{c |} Median\" \/\/\/\r\n\t\t _col(80) \"{c |} Mean\" \/\/\/\r\n\t\t _col(90) \"{c |} P\" \/\/\/\r\n\t\t _col(100) \"{c |} Median\" \/\/\/\r\n\t\t _col(110) \"{c |} P\"\r\n  noi di \"{hline 120}\r\n  forvalues i = 1\/`n' {\r\n    local a : word `i' of `agrp'\r\n    local b : word `i' of `bgrp'\r\n    noi di %27s abbrev(\"`a' vs `b'\",27) \/\/\/\r\n\t       _col(30) \"{c |}\" %8.0f `n_`a'' \/\/\/\r\n\t\t   _col(40) \"{c |}\" %8.3f `mean_`a'' \/\/\/\r\n\t\t   _col(50) \"{c |}\" %8.3f `p50_`a'' \/\/\/\r\n\t\t   _col(60) \"{c |}\" %8.3f `mean_`b'' \/\/\/\r\n\t\t   _col(70) \"{c |}\" %8.3f `p50_`b'' \/\/\/\r\n\t\t   _col(80) \"{c |}\" %8.3f `mean_`a'_diff' \/\/\/\r\n\t\t   _col(90) \"{c |}\" %8.3f `mean_p_`a'' \/\/\/\r\n\t\t   _col(100) \"{c |}\" %8.3f `p50_`a'_diff' \/\/\/\r\n\t\t   _col(110) \"{c |}\" %8.3f `p50_p_`a''\r\n  }\r\n  noi di \"{hline 120}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes we may want to produce the following table to compare the mean and median of two groups: First of all, please refer to this post to see Stata commands to test equality of mean and median. However, it is &hellip; <a href=\"https:\/\/www.kaichen.work\/?p=1218\">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":[],"_links":{"self":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/1218"}],"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=1218"}],"version-history":[{"count":36,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/1218\/revisions"}],"predecessor-version":[{"id":1478,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/1218\/revisions\/1478"}],"wp:attachment":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}