Announcement

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

  • using egen to generate average of specified cases

    As usual I feel that I'm missing something simple but haven't managed to work out what I need to do.

    So I've generated a variable:

    egen var_x = mean(var_y)

    however I don't want the mean of the whole variable, I want to exclude some which meet a certain condition (which I have a separate variable for). var_z=1

    I tried:

    egen var_x = mean(var_y) if var_z ==1

    however that generates the same mean value but only generates it for cases where var_z == 1 whereas I want to generate a var_x for all cases but taking the mean from a specific subgroup of var_y.

    Am I overlooking something incredibly obvious?




  • #2
    If you want to exclude according to a condition, you may use - != - after the if clause.
    Best regards,

    Marcos

    Comment


    • #3
      Thanks for your response Marcos - I probably needed to provide further information - I still can't work out how I would need to phrase this - so supposing I had the following:
      var_x var_y var_z
      14 1
      90 1
      12 1
      67 0
      95 1
      64 0
      so I want a value in every row of var_x, I want this value to be the mean of var_y, but only the mean of var_y when var_z =1, i.e. I want var_x to equal the mean value of 14, 90, 12 and 95, ignoring the other two values (67 and 64) - does that make sense?.

      Matt
      Last edited by Matthew Breckons; 05 Jul 2017, 04:03.

      Comment


      • #4
        Matthew:
        are you looking for something along the following lines?:
        Code:
        egen varx=mean( vary) if varz==1
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Thanks Carlo - this gives me the following:
          var_x var_y var_z
          57 14 1
          57 90 1
          57 12 1
          . 67 0
          57 95 1
          . 64 0
          which has been calculated from (14+90+12+67+95+64)/6 = 57. So var_x is the mean of var_y but if var_z is 0 then var_x is left missing.

          but I would like to get to the following:
          var_x var_y var_z
          52.75 14 1
          52.75 90 1
          52.75 12 1
          52.75 67 0
          52.75 95 1
          52.75 64 0
          So when the mean of var_y is calculated, I would like values of var_y where var_z = 0 to be ignored in the calculation. So var_x = (14+90+12+95)/4 = 52.75

          Does this make sense?

          Comment


          • #6

            Matthew:
            the best work-around that springs to my mind is the following one:
            Code:
            . egen varx=mean( vary) if varz==1
            (2 missing values generated)
            
            . su varx
            
                Variable |        Obs        Mean    Std. Dev.       Min        Max
            -------------+---------------------------------------------------------
                    varx |          4       52.75           0      52.75      52.75
            
            . replace varx=r(mean) if varx==.
            (2 real changes made)
            
            . list
            
                 +---------------------+
                 | vary   varz    varx |
                 |---------------------|
              1. |   14      1   52.75 |
              2. |   90      1   52.75 |
              3. |   12      1   52.75 |
              4. |   67      0   52.75 |
              5. |   95      1   52.75 |
                 |---------------------|
              6. |   64      0   52.75 |
                 +---------------------+
            
            .
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              Thank you Carlo and Marcos - much appreciated. Matt

              Comment


              • #8
                Search this forum for mentions of dm0055. Sections 9 and 10 of the paper so identified discuss this problem systematically and give direct one-line solutions.

                Comment

                Working...
                X