Announcement

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

  • gen vs egen - How do they work? Why is it wrong?

    Hi everyone,

    I don't really get why the first one does not work while the second does.
    Can you explain me the different uses of gen and egen?
    I also don't get mean [var] vs mean (var) .

    Thank you so much and sorry for the very dummy question,
    I have just started to study Stata autonomously and I get lost on very little and stupid stuffs..
    .
    .
    .
    .
    So,
    I've generated the variable "wpop" in this way:

    gen wpop = populism*votes

    Which is the level of populism for every national party ""weighted"" on its consensus at last election
    Than I want to calculate the average wpop for each country.

    1) bys country_id: gen pop_country = mean wpop //// why it does not work? error: invalid 'wpop' ///

    2) mean wpop, over (country_id)
    by country_id: egen pop_country = mean(wpop) //// this works but I do not actually get why and how does it work.




    Thank you!
    Silvia


    P.S.
    I know this is not a good way to measure countries' level of populism dw

  • #2
    Silvia:
    welcome to this forum.
    1) mean(wpop), over(country_id) gives an estimate, but does not create a variable;
    2) the -egen- function -mean- creates the variable you want.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      I am not sure what kind of explanation you want here.

      There is a separate command mean but, surprisingly or not, there is no connection between that and generate or egen.

      It is empty but true to say that

      Code:
      gen pop_country = mean wpop
      is illegal, and that is seen by looking at the help for generate except that you need to know that mean wpop is not an expression in Stata's sense.

      The differences between generate and egen start with

      1. egen is at root an elaborate wrapper for generate, but only works with calls to its own functions (*).

      2. generate is a very general framework for creating new variables, but it knows nothing about egen functions.

      I am happy to agree that that is confusing until you are used to it. I am just the messenger here.

      egen arose very early in Stata and arose (I understand) because of constraints on what could be coded how at the time. But it's stuck as a kind of Swiss army knife. I've seen posts asking for the equivalent in R, which misses the point that there is no real rationale to it any more than there is a rationale to what you keep together in a box or case or drawer.

      (*) Stata uses the term function in three senses: its own functions (whatever is documented at help functions); egen functions; and Mata functions. That can be confusing, but so would using three different words for what might seem broadly the same idea.

      Comment

      Working...
      X