Announcement

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

  • Obtaining multiple modes by group when using Mode in Egen command

    Hello, I have been attempting to generate a new variable to store multiple modes (at least the top two) from an existing string variable using the mode function of -egen- command. Would be best to be able to keep all the modes (all repeating values, appearing more then once per group). I want to obtain the modes by groups defined by three other variables (address and datayear are stored as string, code is numeric). I am getting an error message. Below is my code segment:
    Code:
     bysort address datayear code: egen mostcommoncrime = mode(crime) if crime!="", miss num(2)
    option nummode() may not be combined with by
    r(190);
    I then do a -collapse- (by address datayear code) with keeping the first non-missing value of the variable 'mostcommoncrime' for each group.

  • #2
    The error message you are getting is self-explanatory. You can't use a -by:- prefix incombination with the nummode() option of -egen-. So you have to find another way to do this group-wise.

    But before we go there, I think you are mis-understanding what -nummode()- does. That code, even if it had run, would not give you two modes in each group. It would give you the second smallest mode for any group in the event that there is more than one mode. Actually, given your comment that what you really want is to keep all repeating values, suggests that -egen, mode()- is probably not what you need anyway.

    Try this:

    Code:
    by address datayear code crime, sort: gen frequency = _N
    keep if frequency > 1
    by address datayear code crime: keep if _n == 1
    by address datayear code (frequency crime), sort: gen _j = _n
    reshape wide crime frequency, i(address datayear code) j(_j)
    This will, I believe, do what you want. At the end your data set will have variables crime1, crime2,... and frequency1, frequency2,... which show all repeating crimes in decreasing order of frequency of occurrence (and alphabetically sorted in case of ties).

    Note: As you did not provide example data, this code is not tested. It may contain errors that you will have to troubleshoot.

    In the future, when asking for help with code, please show example data and use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      See also

      Code:
      SJ-9-4  sg113_2 . . . . . . . . . . . . . . . . . . . . .  Tabulation of modes
              (help modes if installed) . . . . . . . . . . . . . . . . .  N. J. Cox
              Q4/09   SJ 9(4):652
              update to allow the generate() option to record in an
              indicator variable of which observations contain values
              matching any of the modes displayed
      
      SJ-3-2  sg113_1 . . . . . . . . . . . . . . . . . .  Software update for modes
              (help modes if installed) . . . . . . . . . . . . . . . . .  N. J. Cox
              Q2/03   SJ 3(2):211
              provides new option for specifying the number of modes to
              be shown
      
      STB-50  sg113 . . . . . . . . . . . . . . . . . . . . . .  Tabulation of modes
              (help modes if installed) . . . . . . . . . . . . . . . . .  N. J. Cox
              7/99    pp.26--27; STB Reprints Vol 9, pp.180--181
              provides table of most frequent observations (modes)
      The original 1999 publication discusses principles. See pp.26-27 in https://www.stata.com/products/stb/journals/stb50.pdf

      The update in 2009 is the one to download if interested.

      Comment

      Working...
      X