Announcement

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

  • how to calculate the hhi index for a list variables?

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(pr1_duration pr2_duration) byte(pr3_duration pr4_duration)
           6   . . .
        7.05   0 0 .
    6.466667   0 . .
         7.3   0 . .
    7.466667 .45 0 .
        5.25   . . .
    6.783333   0 . .
        10.6   0 . .
    8.516666   0 0 .
    7.016667   0 . .
    end
    There exists one to four presenters, I want to calculate the duration concentration for the presenters using hhi. How to do this?
    For observation 1, the calculation should be "(6/6)^2", for observaton 5, the calculation should be "(7.466667/(7.466667+.45))^2+(.45/(7.466667+.45))^2", do we have a user-command to do this?
    Thanks a ton!

  • #2
    Code:
    egen total = rowtotal(pr?_duration)
    
    forvalues i=1/4 {
        gen pr`i'_share_sq = (pr`i'_duration/total)^2
    }
    
    egen hhi = rowtotal(pr?_share_sq)
    drop pr?_share_sq total
    which produces:

    Code:
    . list, noobs sep(0)
      +------------------------------------------------------+
      | pr1_du~n   pr2_du~n   pr3_du~n   pr4_du~n        hhi |
      |------------------------------------------------------|
      |        6          .          .          .          1 |
      |     7.05          0          0          .          1 |
      | 6.466667          0          .          .          1 |
      |      7.3          0          .          .          1 |
      | 7.466667        .45          0          .   .8927779 |
      |     5.25          .          .          .          1 |
      | 6.783333          0          .          .          1 |
      |     10.6          0          .          .          1 |
      | 8.516666          0          0          .          1 |
      | 7.016667          0          .          .          1 |
      +------------------------------------------------------+

    Comment


    • #3
      Thanks!

      Comment


      • #4
        "hhi index" is a tautology as "hhi" already means Herfindahl-Hirschman index. As usual, this measure was not first invented by the people named for it, being known decades earlier than their work under various guises, including as one of many indexes invented by Gini. Hirschman raised a fuss about being a few years earlier than Herfindahl, modulo Hirschman using a square root, but he was in turn not sensitive to his predecessors.

        There are several community-contributed commands for measures of this kind, including one called hhi on SSC!

        But they each assume a particular data layout, and it is entirely arguable that no command is needed, as it is just school arithmetic, as in Hemanshu Kumar's nice answer, or done this way:

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(pr1_duration pr2_duration) byte(pr3_duration pr4_duration)
               6   . . .
            7.05   0 0 .
        6.466667   0 . .
             7.3   0 . .
        7.466667 .45 0 .
            5.25   . . .
        6.783333   0 . .
            10.6   0 . .
        8.516666   0 0 .
        7.016667   0 . .
        end
        
        gen wanted = . 
        mata : 
        pr = st_data(., "pr?_duration")
        st_store(., "wanted", rowsum((pr :/ rowsum(pr)):^2)) 
        end 
        
        list
        Last edited by Nick Cox; 12 Nov 2022, 03:18.

        Comment

        Working...
        X