Announcement

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

  • Calculate mean over ID

    Hi,

    I have a dataset looking like this:

    Date ID x_1 x_2 x_3

    However, there are some missing values.
    while x_1 has a value x_2 and x_3 have for the same ID no values (i. e. ".") (but this is not alway the case, sometimes they have, sometimes they don't)

    What I want is the mean over x_1, x_2 and x_3 within ID. How can I achieve this?
    Last edited by Beginner; 20 May 2014, 05:52.

  • #2
    if there is only one row per id, then "egen newvar=rowmean(x*)" (assuming these are the only variables whose names start with "x"; see -h egen-

    Comment


    • #3
      no unfortunately there are several rows per id

      example:
      id x_1 x_2 x_3 x_4
      1 16
      1 17 18
      2 15
      2 15
      2 15 23
      2 15
      2 15
      2 15 26
      2 15
      2 15
      2 15 10
      2 15
      2 15
      3 11 12
      3 7
      Last edited by Beginner; 20 May 2014, 06:10.

      Comment


      • #4
        This may be what you want: First, calculate the mean across variables for each observation.
        Next, calculate the mean-of-means within each id:

        Code:
        egen x_m1 = rowmean(x_1-x_4)
        bysort id: egen x_m2 = mean(x_m1)
        But you might want something else, for example for id=1 the mean of 16, 17, and 18 (= 17) rather than the mean of 16 + (17+18)/2 (= 16.75).
        In that case you could:

        Code:
        egen x_s1 = rowtotal(x_1-x_4)          // Sum within each observation
        egen x_n1 = rownonmiss(x_1-x_4)        // N of valid values within each observation
        bysort id: egen x_s2 = total(x_s1)     // Sum-of-sums within each id
        bysort id: egen x_n2 = total(x_n1)     // Sum of valid values within each id
        generate x_mean = x_s2/x_n2            // overall mean within id

        Comment


        • #5
          Thanks Svend.

          One final question for this thread.

          I need the row-wise absolute sum / number of entries.
          Example

          x_1 x_2 x_3
          1 - 1 .
          -2 0 0


          what i need is for the first value (abs(1)+abs(-1))/2=1
          second value abs(-2)/1=2

          Comment


          • #6
            Code:
            gen x_abs_sum = 0
            gen x_missing_count = 0
            
            forval j = 1/3 {
               replace x_abs_sum = x_abs_sum + abs(x_`j') if x_`j' < .
               replace x_missing_count = x_missing_count + missing(x_`j')
            }
            
            replace x_abs_sum = . if x_missing_count == 3
            Please register with a full real name, not "Beginner".

            Comment

            Working...
            X