Dear All, I found this question here (http://bbs.pinggu.org/thread-6634832-1-1.html). The data set is
For each company (stkcd) and year (year), I'd like to calculate the following index, $H=1-\sum_{i=1}^n P_i^n$ (Can't I use LaTeX here?).
However, I wonder if there is more concise code for this situation? Thanks.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input long stkcd int year float gender 2 2015 1 2 2015 1 2 2015 1 2 2015 1 2 2015 1 2 2015 1 2 2015 1 2 2015 1 2 2015 1 2 2015 1 2 2015 0 2 2015 1 2 2015 1 2 2015 1 2 2014 0 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2014 0 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2014 1 2 2013 1 2 2013 1 2 2013 1 2 2013 1 2 2013 1 2 2013 0 2 2013 0 2 2013 1 2 2013 1 2 2013 1 end
- In this case, n=2, i.e., gender=1 (say male) or 0 (say, female).
- P_1 is the ratio of males to all persons, and P_2 is the ratio of females to all persons.
- In particular, H=1-P_1^2-P_2^2 in my case.
Code:
bys stkcd year: egen tem1 = total(gender) bys stkcd year: egen tem2 = count(gender) gen P1 = tem1/tem2 gen P2 = 1-P1 gen P1sq = P1^2 gen P2sq = P2^2 gen H = 1 - P1sq - P2sq
Comment