Announcement

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

  • Calculate average score difference

    I hope I can explain this.

    Say I have a dataset with n observations, and a numerical (discrete/continuous) variable y

    I would like, for each observation, to create a new variable as follows:

    - first calculate the difference between its own y score and all the other individual y scores in the sample (n-1 calculations, or n calculations if calculating difference with own score, which is 0)
    - and then calculate the average of all these differences

    How would I do that in Stata?

    Thank you in advance



  • #2
    I don't know exactly how you want differences (absolute or not), and for calculating the average it does matter whether you include the difference with an observation's own value, but maybe this will start you in a useful direction. I hope your n is not to large, otherwise this will create quite a lot of variables.
    Code:
    clear
    input y
    1 
    2
    4
    end
    
    forval i = 1/`=_N' {
        gen diff`i' = y - y[`i'] if _n!=`i'
    }
    
    egen avg_diff = rowmean(diff*)
    list, noobs
    
      +--------------------------------------+
      | y   diff1   diff2   diff3   avg_diff |
      |--------------------------------------|
      | 1       .      -1      -3         -2 |
      | 2       1       .      -2        -.5 |
      | 4       3       2       .        2.5 |
      +--------------------------------------+

    Comment


    • #3
      Thank you very much.
      I wonder if the new data frames in Stata 16 can streamline the process (I haven;t got 16 yet)

      Comment

      Working...
      X