Stata - 分年度-行业计算销售额前四名的行业占比

论坛 期权论坛 期权     
Stata连享会   2019-3-20 09:56   11644   0
作者:连玉君 (知乎 | 简书 | 码云)
Stata连享会 精彩推文1   || 精彩推文2
[h2]一起学空间计量……[/h2]
空间计量专题-海报.jpg[h2]1. 问题[/h2]在 Stata 中如何分年度计算一个行业当中排名前四位的公司的总收入占整个行业的收入比重?
Note: 如果是算一般的赫芬达尔指数,可以使用连玉君老师编写的
  1. hhi5
复制代码
命令,很便捷。安装命令的方法为:在 Stata 命令窗口中执行
  1. ssc install hhi5, replace
复制代码
即可。
本例的情况稍微特殊一些,需要自己写命令,但核心思路与
  1. hhi5
复制代码
是一样的。有兴趣的读者可以使用
  1. adoedit hhi5.ado
复制代码
打开
  1. hhi5
复制代码
命令的原始 ado 文件,以便知晓内部计算思路和过程。
[h2]2. 解决方法[/h2]我们先自行模拟生成一份 Panel data 数据。自己操作时,换用自己的数据即可。
[h3]2.1 自行模拟生成一份数据[/h3]
    1. *---
    复制代码
    1. *-A: Data Generation: simulation
    复制代码

    1. clear
    复制代码
    1. set obs 50  // N=50 家
    复制代码
    1. gen id = _n
    复制代码
    1. egen industry = cut(id), at(1(10)55)  // 行业
    复制代码
    1. expand 4    // T=4 年
    复制代码
    1. bysort id: gen year = _n + 2010
    复制代码
    1. xtset id year
    复制代码
    1. gen sale = int(100+rnormal()*20)
    复制代码
生成的数据长这样:
    1. . gsort year industry -sale
    复制代码
    1. . list in 1/20, sepby(year industry) noobs
    复制代码

    1.   +-----------------------------+
    复制代码
    1.   | id   industry   year   sale |
    复制代码
    1.   |-----------------------------|
    复制代码
    1.   | 10          1   2011    137 |
    复制代码
    1.   |  1          1   2011    119 |
    复制代码
    1.   |  6          1   2011    102 |
    复制代码
    1.   |  4          1   2011     99 |
    复制代码
    1.   |  3          1   2011     97 |
    复制代码
    1.   |  2          1   2011     97 |
    复制代码
    1.   |  9          1   2011     96 |
    复制代码
    1.   |  7          1   2011     85 |
    复制代码
    1.   |  8          1   2011     84 |
    复制代码
    1.   |  5          1   2011     82 |
    复制代码
    1.   |-----------------------------|
    复制代码
    1.   | 11         11   2011    135 |
    复制代码
    1.   | 15         11   2011    134 |
    复制代码
    1.   | 20         11   2011    133 |
    复制代码
    1.   | 12         11   2011    119 |
    复制代码
    1.   | 13         11   2011    117 |
    复制代码
    1.   | 16         11   2011    104 |
    复制代码
    1.   | 17         11   2011    101 |
    复制代码
    1.   | 19         11   2011     95 |
    复制代码
    1.   | 14         11   2011     93 |
    复制代码
    1.   | 18         11   2011     88 |
    复制代码
    1.   +-----------------------------+
    复制代码
[h3]2.2 计算目标变量[/h3]
    1. *---
    复制代码
    1. *-B: The percentage
    复制代码

    1. gsort year industry -sale
    复制代码
    1. bysort year industry: gen sale123 = _n  //排序
    复制代码
    1. list in 1/20, sepby(year industry) noobs
    复制代码

    1. *-行业总收入
    复制代码
    1. bysort year industry: egen sale_ind = total(sale)
    复制代码
    1. *-Top 4
    复制代码
  • [code]bysort year industry: egen sale_top4 = total(sale) if sale123
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:190
帖子:39
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP