创建三向百分比 table

Create 3-way percentages table

我想要一个 3 向 table 使用三个分类变量显示列或行百分比。下面的命令给出了计数,但我找不到如何获取百分比。

sysuse nlsw88

table married race collgrad, col

--------------------------------------------------------------------
          |                college graduate and race                
          | ---- not college grad ----    ------ college grad ------
  married | white  black  other  Total    white  black  other  Total
----------+---------------------------------------------------------
   single |   355    256      5    616      132     53      3    188
  married |   862    224     12  1,098      288     50      6    344
--------------------------------------------------------------------

我怎样才能得到百分比?

这个答案将展示各种技巧。缺点是我不知道一种简单的方法来准确地得到你的要求。好处是所有这些技巧都很容易理解并且通常很有用。

让我们使用您的示例,非常适合此目的。

. sysuse nlsw88, clear 
(NLSW, 1988 extract)

提示 #1 您可以自己计算一个百分比变量。我专注于 % single。在这个数据集中 married 是二进制的,所以我不会显示互补百分比。 一旦你计算了它,你就可以 (a) 依靠它在你用来定义它的组内是恒定的这一事实 (b) 直接将它制成表格。我发现 tabdisp 被用户低估了。它被标榜为程序员的命令,但一点也不难用。 tabdisp 允许您即时设置显示格式;它没有害处,并且可能对其他命令有用,直接使用 format 分配一个。

. egen pcsingle = mean(100 * (1 - married)), by(collgrad race)

. tabdisp collgrad race, c(pcsingle) format(%2.1f)

--------------------------------------
                 |        race        
college graduate | white  black  other
-----------------+--------------------
not college grad |  29.2   53.3   29.4
    college grad |  31.4   51.5   33.3
--------------------------------------

. format pcsingle %2.1f 

提示 #2 user-written 命令 groups 提供不同的灵活性。 groups 可以从 SSC 安装(严格来说,必须安装后才能使用)。它是各种表格的包装器,但使用 list 作为显示引擎。

. * do this installation just once 
. ssc inst groups 

. groups collgrad race pcsingle 

  +-------------------------------------------------------+
  |         collgrad    race   pcsingle   Freq.   Percent |
  |-------------------------------------------------------|
  | not college grad   white       29.2    1217     54.19 |
  | not college grad   black       53.3     480     21.37 |
  | not college grad   other       29.4      17      0.76 |
  |     college grad   white       31.4     420     18.70 |
  |     college grad   black       51.5     103      4.59 |
  |-------------------------------------------------------|
  |     college grad   other       33.3       9      0.40 |
  +-------------------------------------------------------+

我们可以对此进行改进。我们可以使用特性设置更好的header文本。 (在实践中,这些可以比变量名更受限制,但通常需要比变量标签更短。)我们可以通过调用标准 list 选项来使用分隔符。

. char pcsingle[varname] "% single"

. char collgrad[varname] "college?"

. groups collgrad race pcsingle , subvarname sepby(collgrad) 

  +-------------------------------------------------------+
  |         college?    race   % single   Freq.   Percent |
  |-------------------------------------------------------|
  | not college grad   white       29.2    1217     54.19 |
  | not college grad   black       53.3     480     21.37 |
  | not college grad   other       29.4      17      0.76 |
  |-------------------------------------------------------|
  |     college grad   white       31.4     420     18.70 |
  |     college grad   black       51.5     103      4.59 |
  |     college grad   other       33.3       9      0.40 |
  +-------------------------------------------------------+

提示 #3 通过使字符串等价将显示格式连接到变量中。我没有充分说明这一点,但是当我想组合显示时我经常使用它具有 tabdisp 中小数位的数值结果的计数。 format(%2.1f)format(%3.2f) 可能对大多数变量都适用(顺便说一下,重要的细节是小数位数)但它们会导致计数 42 显示为 42.0 或 42.00,看起来很傻。 tabdispformat() 选项没有进入字符串并更改内容;它甚至不知道字符串变量包含什么或它来自哪里。因此,字符串只是在出现时由 tabdisp 显示,这就是您想要的。

. gen s_pcsingle = string(pcsingle, "%2.1f") 

. char s_pcsingle[varname] "% single"

groups 有一个选项可以将表格中的内容保存为新数据集。

提示 #4 要获得总类别,请暂时将数据加倍。 原始数据的克隆被重新标记为总类别。您可能需要做一些额外的计算,但没有什么是高深莫测的:聪明的高中生都能算出来。这里有一个 line-by-line 研究的具体例子胜过冗长的解释。

. preserve 

. local Np1 = _N + 1 

. expand 2 
(2,246 observations created)

. replace race = 4 in `Np1'/L 
(2,246 real changes made)

. label def racelbl 4 "Total", modify  

. drop pcsingle 

. egen pcsingle = mean(100 * (1 - married)), by(collgrad race)

. char pcsingle[varname] "% single"

. format pcsingle %2.1f 

. gen istotal = race == 4 

. bysort collgrad istotal: gen total = _N 

. * for percents of the global total, we need to correct for doubling up     
. scalar alltotal = _N/2 

. * the table shows percents for college & race | collgrad and for collgrad | total 
. bysort collgrad race : gen pc = 100 * cond(istotal, total/alltotal, _N/total)  
. format pc %2.1f
. char pc[varname] "Percent" 

. groups collgrad race pcsingle pc , show(f) subvarname sepby(collgrad istotal) 

  +-------------------------------------------------------+
  |         college?    race   % single   Percent   Freq. |
  |-------------------------------------------------------|
  | not college grad   white       29.2      71.0    1217 |
  | not college grad   black       53.3      28.0     480 |
  | not college grad   other       29.4       1.0      17 |
  |-------------------------------------------------------|
  | not college grad   Total       35.9      76.3    1714 |
  |-------------------------------------------------------|
  |     college grad   white       31.4      78.9     420 |
  |     college grad   black       51.5      19.4     103 |
  |     college grad   other       33.3       1.7       9 |
  |-------------------------------------------------------|
  |     college grad   Total       35.3      23.7     532 |
  +-------------------------------------------------------+

请注意使用未明确显示的变量来添加分隔线的额外技巧。