Announcement

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

  • egen mode() misleading warning?

    I have the following code to produce example data and to reproduce my problem:

    Code:
    clear
    input id year status target
    1 2001 0 2
    1 2002 1 2
    1 2003 1 2
    1 2004 1 4
    1 2005 0 3
    2 1999 1 4
    2 2000 1 4
    2 2001 1 5
    2 2002 0 1
    2 2003 1 5
    3 2004 0 3
    3 2005 1 .
    3 2006 1 .
    3 2007 1 .
    3 2008 1 .
    3 2009 0 .
    end
    
    bysort id: egen modeflag = mode(target) if status==1, minmode
    bysort id: egen modefinal = mode(modeflag)
    
    /*
    warning: Multiple modes encountered in one or more groups.  Generating missing values for the modes in these groups.  Use option minmode, maxmode, or nummode() to select one of the modes.
    (6 missing values generated)
    */

    When running the second mode() command

    Code:
    bysort id: egen modefinal = mode(modeflag)
    at least Stata 18 and Stata 19.5 throw the error specified in the comment:
    "Multiple modes encountered in one or more groups."

    However, as you can see if you run this example, there are absolutely no multiple modes encountered in any bysort group at variable modeflag. I believe this should be fixed, as it has caused me to do unnecessary verifications to find out there were no multiple modes. Am I missing something here?

    Best regards,
    Kasper

  • #2
    Your data does have multiple modes. Look at id == 2. Even after eliminating observation 9, which has status == 0, you are left with two observations (6 and 7) with target == 4 and two observations with target == 5, and no other observations. So 4 and 5 are both modes for id == 2 and status == 1.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Your data does have multiple modes.
      Clyde, I think the OP explicitly acknowledges that by specifying the option -minmode- in his first command.

      bysort id: egen modeflag = mode(target) if status==1, minmode
      His comment relates to the second command, which uses the generated variable "modeflag" and includes no -if- restriction.


      Code:
      . list id modeflag, sepby(id)
      
           +---------------+
           | id   modeflag |
           |---------------|
        1. |  1          . |
        2. |  1          2 |
        3. |  1          2 |
        4. |  1          2 |
        5. |  1          . |
           |---------------|
        6. |  2          4 |
        7. |  2          4 |
        8. |  2          4 |
        9. |  2          . |
       10. |  2          4 |
           |---------------|
       11. |  3          . |
       12. |  3          . |
       13. |  3          . |
       14. |  3          . |
       15. |  3          . |
       16. |  3          . |
           +---------------+
      I think his point is valid, and as far as I can tell, this behavior results from all values within a group being missing. However, this is an unconventional way of achieving what the OP wants. I would simply do this by sorting within groups and picking the first value, since missing values are always sorted last.

      Code:
      bys id (modeflag): gen wanted = modeflag[1]

      Comment


      • #4
        Andrew Musau is right. I misunderstood the original post. My apologies.

        Comment


        • #5
          Originally posted by Andrew Musau View Post

          I think his point is valid, and as far as I can tell, this behavior results from all values within a group being missing. However, this is an unconventional way of achieving what the OP wants. I would simply do this by sorting within groups and picking the first value, since missing values are always sorted last.

          Code:
          bys id (modeflag): gen wanted = modeflag[1]
          Thanks for this tip, this is useful!

          The point on the misleading warning of egen mode() still stands, and should perhaps be corrected. I thought of this code example because that way it should be absolutely clear that there is only one unique value + possibly missing values within each value of id, making multiple modes impossible if missing values are ignored.

          Comment


          • #6
            I would send this directly to StataCorp technical support. It doesn't seem to have yet caught the attention of the company.

            I was bemused to re-discover that I first published a mode() function for egen in 1999, which was founded into official Stata not long after. But it's been rewritten several times since then and this behaviour arises from some fairly recent revision.

            A perhaps quirky personal view of modes runs like this.

            If your variable is discrete, the mode is just the most commonly occurring value. If you have to try harder in the face of ties and use extra criteria to define a mode, the problem is elusive, not just the solution.

            If your variable is continuous, the most obvious answer is perhaps to use density estimation first and look for a peak. Otherwise direct algorithms exist and deserve to be better known. hsmode from SSC encapsulates one such.

            Comment

            Working...
            X