Announcement

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

  • Which observation gets "tag"ged?

    This is not explicitly stated anywhere in the help files and it seems across the board, people refer to the tag(varlist) function of egen "tags just one observation in unique groups of varlist." Which observation? In practice, it seems like it is necessarily the first observation and that should be fine for how it is used. What if I want to tag a (pseudo)random observation within unique groups of varlist?

  • #2
    I wrote that function originally, although I am confident that it was just an implementation of an existing Bill Gould trick. Either way, I can speak on the design.

    In practice it is the first observation in each group.

    There is no "necessarily" about that. To work for groups that could be as small as 1, there are, so far as I can see, only two possible simple rules that are practical, to choose the first or the last. (A rule to choose the "middle" of a group would select the single observation out of a group of 1 but would need to be more complicated to cope with odd and even numbers of observations, in precisely the same way as is needed to calculate medians.)

    I chose to implement choice of the first. I can't remember a reason. I don't think anyone has asked this since the function code was published in 1999.

    As you say, it should not matter as the whole point is that you have groups of identical values, so for some purposes it is enough to see just one of them, and indeed for some purposes anything else would be too much.

    It could matter for looking at other variables.

    To pick an arbitrary observation, you would need something like this:

    Code:
    set seed 2803
    gen double random = runiform()
    bysort group (random) : gen tag = _n == 1
    but as said you could pick the last instead.

    Code:
    viewsource _gtag.ado
    shows you the code.

    See also Robert Picard's randomtag (SSC).
    Last edited by Nick Cox; 31 Mar 2016, 10:22.

    Comment


    • #3
      Thank you!

      Comment

      Working...
      X