组变量的频率 table

Frequency table with group variable

我有一个包含公司级别数据的数据集。 我有一个变量employees(一个整数)和一个变量nace2(一个表示公司相关的行业或服务部门的整数)

我已经为员工分组创建了第三个变量:

gen employees_cat = .
replace employees_cat = 1 if employees >=0 & employees<10
replace employees_cat = 2 if employees >=10 & employees<20
replace employees_cat = 3 if employees >=20 & employees<49
replace employees_cat = 4 if employees >=49 & employees<249
replace employees_cat = 5 if employees >=249

我想创建一个频率 table,显示每个 employees_cat 每个 nace2 部门有多少员工工作。

作为一个可重现的例子

sysuse auto.dta

让我们尝试获得一个频率 table,显示所有后备箱 space 为 11、12、16 等的国内/国外汽车的总里程数 (mpg) .

Stata 中频率列表的起点是 tabulate,它可以显示单向和双向细分。与 by: 多路故障一起使用可以生成一系列双向 tables。另见 table

对于您在 auto 数据中提到的变量,mpg 有 21 个不同的值,trunk 有 18 个不同的值,因此双向 table 将是21 x 18 或 18 x 21,有许多空单元格,因为 74 处的观察次数比乘积 378 少得多。(这里要计算不同的值,安装命令 distinct:Stata 中的 search distinct参考文献和最新代码版本下载。)

. sysuse auto, clear
(1978 Automobile Data)

. distinct mpg trunk

------------------------------
       |     total   distinct
-------+----------------------
   mpg |        74         21
 trunk |        74         18
------------------------------

解决此问题的一种方法是将表格折叠成一个列表,其中包含典型的条目{行变量、列变量、频率信息}。这是由程序 groups 提供的,必须先安装它,如下所示:

. ssc inst groups 

. groups trunk mpg

  +-------------------------------+
  | trunk   mpg   Freq.   Percent |
  |-------------------------------|
  |     5    28       1      1.35 |
  |     6    23       1      1.35 |
  |     7    18       1      1.35 |
  |     7    24       2      2.70 |
  |     8    21       1      1.35 |
  |-------------------------------|
  |     8    24       1      1.35 |
  |     8    26       1      1.35 |
  |     8    30       1      1.35 |
  |     8    35       1      1.35 |
  |     9    22       1      1.35 |
  |-------------------------------|
  |     9    28       1      1.35 |
  |     9    29       1      1.35 |
  |     9    31       1      1.35 |
  |    10    21       1      1.35 |
  |    10    24       1      1.35 |
  |-------------------------------|
  |    10    25       1      1.35 |
  |    10    26       2      2.70 |
  |    11    17       1      1.35 |
  |    11    18       1      1.35 |
  |    11    22       1      1.35 |
  |-------------------------------|
  |    11    23       1      1.35 |
  |    11    28       1      1.35 |
  |    11    30       1      1.35 |
  |    11    34       1      1.35 |
  |    11    35       1      1.35 |
  |-------------------------------|
  |    12    22       1      1.35 |
  |    12    23       1      1.35 |
  |    12    25       1      1.35 |
  |    13    19       3      4.05 |
  |    13    21       1      1.35 |
  |-------------------------------|
  |    14    14       1      1.35 |
  |    14    17       1      1.35 |
  |    14    18       1      1.35 |
  |    14    19       1      1.35 |
  |    15    14       1      1.35 |
  |-------------------------------|
  |    15    17       1      1.35 |
  |    15    18       1      1.35 |
  |    15    25       1      1.35 |
  |    15    41       1      1.35 |
  |    16    14       3      4.05 |
  |-------------------------------|
  |    16    18       1      1.35 |
  |    16    19       3      4.05 |
  |    16    20       2      2.70 |
  |    16    21       1      1.35 |
  |    16    22       1      1.35 |
  |-------------------------------|
  |    16    25       1      1.35 |
  |    17    16       3      4.05 |
  |    17    18       1      1.35 |
  |    17    19       1      1.35 |
  |    17    20       1      1.35 |
  |-------------------------------|
  |    17    22       1      1.35 |
  |    17    25       1      1.35 |
  |    18    12       1      1.35 |
  |    20    14       1      1.35 |
  |    20    15       1      1.35 |
  |-------------------------------|
  |    20    16       1      1.35 |
  |    20    18       2      2.70 |
  |    20    21       1      1.35 |
  |    21    17       1      1.35 |
  |    21    18       1      1.35 |
  |-------------------------------|
  |    22    12       1      1.35 |
  |    23    15       1      1.35 |
  +-------------------------------+

groups 有更多选项,在它的帮助中有记录。但它很容易扩展到多路 tables 也折叠成列表,就像这里的第三个分组变量一样:

. groups foreign trunk mpg, sepby(foreign trunk)

  +------------------------------------------+
  |  foreign   trunk   mpg   Freq.   Percent |
  |------------------------------------------|
  | Domestic       7    18       1      1.35 |
  | Domestic       7    24       2      2.70 |
  |------------------------------------------|
  | Domestic       8    26       1      1.35 |
  | Domestic       8    30       1      1.35 |
  |------------------------------------------|
  | Domestic       9    22       1      1.35 |
  | Domestic       9    28       1      1.35 |
  | Domestic       9    29       1      1.35 |
  |------------------------------------------|
  | Domestic      10    21       1      1.35 |
  | Domestic      10    24       1      1.35 |
  | Domestic      10    26       1      1.35 |
  |------------------------------------------|
  | Domestic      11    17       1      1.35 |
  | Domestic      11    22       1      1.35 |
  | Domestic      11    28       1      1.35 |
  | Domestic      11    34       1      1.35 |
  |------------------------------------------|
  | Domestic      12    22       1      1.35 |
  |------------------------------------------|
  | Domestic      13    19       3      4.05 |
  | Domestic      13    21       1      1.35 |
  |------------------------------------------|
  | Domestic      14    19       1      1.35 |
  |------------------------------------------|
  | Domestic      15    14       1      1.35 |
  | Domestic      15    18       1      1.35 |
  |------------------------------------------|
  | Domestic      16    14       3      4.05 |
  | Domestic      16    18       1      1.35 |
  | Domestic      16    19       3      4.05 |
  | Domestic      16    20       2      2.70 |
  | Domestic      16    22       1      1.35 |
  |------------------------------------------|
  | Domestic      17    16       3      4.05 |
  | Domestic      17    18       1      1.35 |
  | Domestic      17    19       1      1.35 |
  | Domestic      17    20       1      1.35 |
  | Domestic      17    22       1      1.35 |
  | Domestic      17    25       1      1.35 |
  |------------------------------------------|
  | Domestic      18    12       1      1.35 |
  |------------------------------------------|
  | Domestic      20    14       1      1.35 |
  | Domestic      20    15       1      1.35 |
  | Domestic      20    16       1      1.35 |
  | Domestic      20    18       2      2.70 |
  | Domestic      20    21       1      1.35 |
  |------------------------------------------|
  | Domestic      21    17       1      1.35 |
  | Domestic      21    18       1      1.35 |
  |------------------------------------------|
  | Domestic      22    12       1      1.35 |
  |------------------------------------------|
  | Domestic      23    15       1      1.35 |
  |------------------------------------------|
  |  Foreign       5    28       1      1.35 |
  |------------------------------------------|
  |  Foreign       6    23       1      1.35 |
  |------------------------------------------|
  |  Foreign       8    21       1      1.35 |
  |  Foreign       8    24       1      1.35 |
  |  Foreign       8    35       1      1.35 |
  |------------------------------------------|
  |  Foreign       9    31       1      1.35 |
  |------------------------------------------|
  |  Foreign      10    25       1      1.35 |
  |  Foreign      10    26       1      1.35 |
  |------------------------------------------|
  |  Foreign      11    18       1      1.35 |
  |  Foreign      11    23       1      1.35 |
  |  Foreign      11    30       1      1.35 |
  |  Foreign      11    35       1      1.35 |
  |------------------------------------------|
  |  Foreign      12    23       1      1.35 |
  |  Foreign      12    25       1      1.35 |
  |------------------------------------------|
  |  Foreign      14    14       1      1.35 |
  |  Foreign      14    17       1      1.35 |
  |  Foreign      14    18       1      1.35 |
  |------------------------------------------|
  |  Foreign      15    17       1      1.35 |
  |  Foreign      15    25       1      1.35 |
  |  Foreign      15    41       1      1.35 |
  |------------------------------------------|
  |  Foreign      16    21       1      1.35 |
  |  Foreign      16    25       1      1.35 |
  +------------------------------------------+