Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to compute weighted mean (HHI index) by group and year using a loop or maybe without using a loop?

    Hi, HHI index measures the degree of concentration of firms in each industry.
    Stata allows me to compute the HHI index using a built-in command hhi5.
    Here I got the HHI index for sales of firms in each industry.

    That is,

    Code:
      hhi5 sales, by(ksic2c year)
    where ksic2c denotes industry.

    Now I want to generate the weighted HHI index where the weight is determined by the sum of sales in each industry.
    So if the sum of sales in industry A is greater than that of industry B, HHI of A industry will be much higher than that of B industry as of weighted HHI than general HHI.
    I was wondering if there's a way to generate this weighted HHI index using or without using a loop for each industry so that I may be able to compare the yearly trend of each HHI index.

    Thank you.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str6 firmid float(year sales) byte ksic2c float hhi_sales
    "016090" 1992  8.9881e+10 12 .20193757
    "002070" 1992 1.18718e+11 12 .20193757
    "009270" 1992 1.67807e+11 12 .20193757
    "007700" 1992  1.8668e+10 13 .20193757
    "005800" 1992  1.0758e+11 13 .20193757
    "014990" 1992 1.05812e+11 13 .20193757
    "001460" 1992 3.13182e+11 13 .20193757
    "001460" 1993 3.23294e+11 13  .2012516
    "005800" 1993  9.7535e+10 13  .2012516
    "014990" 1993 1.25405e+11 13  .2012516
    "002070" 1993 1.19428e+11 13  .2012516
    "016090" 1993  9.8385e+10 13  .2012516
    "007700" 1993  1.9236e+10 13  .2012516
    "009270" 1993 2.22712e+11 13  .2012516
    "001460" 1994 3.56683e+11 13 .20662676
    "007700" 1994  1.7686e+10 14 .20662676
    "014990" 1994 1.74042e+11 14 .20662676
    "005800" 1994   9.832e+10 14 .20662676
    "016090" 1994 1.30714e+11 14 .20662676
    "002070" 1994 1.26674e+11 14 .20662676
    "009270" 1994 3.59815e+11 14 .20662676
    "009270" 1995 4.77822e+11 14   .192759
    "002070" 1995 1.29806e+11 14   .192759
    "016090" 1995 1.98731e+11 14   .192759
    "007980" 1995  5.6522e+10 14   .192759
    "014990" 1995  2.5471e+11 14   .192759
    "005800" 1995 1.08177e+11 14   .192759
    "007700" 1995  1.8387e+10 14   .192759
    "001460" 1995 3.74637e+11 12  .192759
    "016090" 1996 2.34864e+11 12 .19206028
    "007980" 1996  8.7393e+10 13 .19206028
    "002070" 1996 1.31513e+11 13 .19206028
    "001460" 1996 3.38225e+11 13 .19206028
    "009270" 1996 5.78684e+11 13 .19206028
    "014990" 1996  3.4062e+11 13 .19206028
    "007700" 1996  2.8805e+10 13 .19206028
    "005800" 1996 1.11875e+11 13 .19206028
    "009270" 1997 8.24554e+11 14 .21814816
    "001460" 1997 3.03513e+11 14 .21814816
    "014990" 1997 2.36455e+11 14 .21814816
    "020000" 1997  7.8887e+10 14 .21814816
    "016090" 1997 2.41609e+11 14 .21814816
    "002070" 1997 1.40157e+11 14 .21814816
    "007980" 1997  8.5183e+10 14 .21814816
    "005800" 1997 1.21896e+11 14 .21814816
    "007700" 1997  3.8906e+10 14 .21814816
    "007980" 1998 1.37216e+11 14  .1808953
    "002070" 1998 1.26851e+11 14  .1808953
    "007700" 1998  3.2976e+10 14  .1808953
    "001460" 1998 2.41391e+11 14  .1808953
    "005800" 1998 1.01217e+11 14  .1808953
    "020000" 1998  6.0407e+10 14  .1808953
    "016090" 1998 1.45922e+11 14  .1808953
    "002070" 1999 1.25423e+11 14  .1682532
    "001460" 1999 2.39071e+11 14  .1682532
    "014990" 1999 1.01291e+11 14  .1682532
    "007980" 1999 1.44961e+11 14  .1682532
    "007700" 1999  6.4863e+10 14  .1682532
    "020000" 1999  9.3415e+10 14  .1682532
    "005800" 1999 1.00301e+11 14  .1682532
    "001460" 2000 2.06335e+11 12 .13863963
    "016090" 2000 1.47421e+11 12 .13863963
    "007700" 2000 1.24714e+11 12 .13863963
    "016450" 2000 2.07755e+11 12 .13863963
    "002070" 2000 1.41952e+11 13 .13863963
    "005800" 2000 1.20502e+11 13 .13863963
    "009270" 2000 5.22911e+11 13 .13863963
    "014990" 2000 1.38454e+11 13 .13863963
    "007980" 2000 1.43221e+11 14 .13863963
    "020000" 2000 1.15441e+11 14 .13863963
    "007980" 2001 1.76096e+11 14 .12719487
    "014990" 2001 1.61301e+11 14 .12719487
    "005800" 2001 1.34766e+11 14 .12719487
    "016450" 2001 2.55723e+11 14 .12719487
    "009270" 2001 5.01673e+11 13 .12719487
    "002070" 2001  1.4833e+11 13 .12719487
    "020000" 2001 1.36191e+11 12 .12719487
    "001460" 2001 1.97297e+11 13 .12719487
    "007700" 2001 1.48484e+11 14 .12719487
    "016090" 2001 1.62339e+11 14 .12719487
    "014990" 2002 1.77731e+11 14 .11663017
    "005800" 2002 1.54556e+11 14 .11663017
    "016450" 2002  2.9978e+11 14 .11663017
    "016090" 2002 1.72862e+11 14 .11663017
    "002070" 2002 1.81459e+11 14 .11663017
    "001460" 2002 1.82104e+11 14 .11663017
    "020000" 2002 1.48557e+11 14 .11663017
    "007980" 2002 1.92906e+11 14 .11663017
    "007700" 2002 1.77439e+11 14 .11663017
    "005800" 2003 1.56194e+11 14 .12396794
    "001460" 2003 1.75923e+11 14 .12396794
    "007700" 2003 1.87728e+11 12 .12396794
    "002070" 2003 2.23069e+11 12 .12396794
    "020000" 2003 2.23081e+11 12 .12396794
    "014990" 2003 1.54634e+11 12 .12396794
    "016090" 2003 1.51643e+11 12 .12396794
    "009270" 2003 3.81501e+11 12 .12396794
    "007980" 2003 1.62639e+11 14 .12396794
    "009270" 2004 3.67371e+11 14 .11219126
    "020000" 2004 2.60041e+11 14 .11219126
    end
    Last edited by Chul-Kyoo Jung; 04 Jan 2022, 21:59.

  • #2
    I don't understand how this differs from the usual flavour, but most weighted means boil down to

    numerator IS sum of weights * whatever

    denominator IS sum of weights

    wanted IS numerator / denominator

    and egen together with by: lets you work step by step avoid loops (or, in effect, loops for you)

    Detail on English writing: HHI = Herfindahl-Hirschman index

    The expression HHI index is like saying Herfindahl-Hirschman index index

    Hirschman wrote a rather angry little paper emphasising that he had more or less the same idea earlier than Herfindahl, but so did Gini 30 or so years before Hirschman.. And so did many others.
    Last edited by Nick Cox; 05 Jan 2022, 05:22.

    Comment


    • #3
      Nick Cox thanks for the clarification!

      Comment

      Working...
      X