Announcement

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

  • "Leave one out" Mode

    Dear all

    I'm trying to compute the mode socio-economic background (SES) in each high school (egen schoolses= mode (ses), minmode by (highschool), but leaving the individual student out (ie. the mode SES of the individual’s peers). I worked with the "leave-one-out" mean before but I struggle to apply something similar for the mode SES. The SES is a categorical variable from 0 to 4 and I basically want to assign to each school a value from 0 to 4 according to the SES of most of the other students (ie. excluding the individual). Any help on that will be very much appreciated. Thank you very much in advance.

  • #2
    I would like to think there is a better way to do this, but I haven't been able to come up with one yet:

    Code:
    sysuse auto, clear
    
    gen modal_value = .
    gen value = .
    
    
    forvalues i = 1/`=_N' {
        replace value = cond(foreign == foreign[`i'] & _n != `i', rep78, .)
        egen tempmode = mode(value), minmode
        replace modal_value = tempmode in `i'
        drop tempmode
    }
    As you did not supply example data, I have used the built-in auto.dta to develop and illustrate the approach. In the above code, foreign plays the role of school in your example, and rep78 plays the role of SES. At the end, modal_value will be what you are looking for.

    Comment


    • #3
      This seems to work. Thank you so much!

      Comment

      Working...
      X