Announcement

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

  • Quintiles with conditions

    Hello, team.

    I am using stata 13. I have to do this: Define 11 quantiles of surp, where quantile 6
    contains all observations with a (exactly) zero surprise, quantiles 7-11
    contain positive, and quantiles 1-5 negative surprises.

    (on one set of data I do not have exact zero, on the other I do?
    I tried sort + xtile, but I do not know how to include a conditional statement.

    Could you be so kind to help?
    Thank you,
    Milena

  • #2
    The general term is quantiles: the specific term quintiles refers to 20(20)80 % points in the distribution and by extension bins or intervals defined by those points. What you seek are definitely not quintiles in any sense.

    What you seem to be after is here is part quantile-based binning. We really can't clarify your instructions, but this code shows some technique:

    Code:
     
    clear
    set obs 100
    set seed 2803 
    gen surprise = cond(runiform() < .2, 0, 5 * rnormal())
    gen q_surprise = 6 if surprise == 0
    xtile work1 = surprise if surprise > 0, n(5)
    xtile work2 = surprise if surprise < 0, n(5)
    replace q_surprise = cond(surprise < 0, work2, work1 + 6) if missing(q_surprise)
    tab q_surprise, su(surprise)
    
    . tab q_surprise, su(surprise)
    
                |         Summary of surprise
     q_surprise |        Mean   Std. Dev.       Freq.
    ------------+------------------------------------
              1 |  -9.3381763   2.1105088           9
              2 |   -5.927442   .92163629           9
              3 |  -3.7252931   .52873894           8
              4 |   -2.261361   .51764729           9
              5 |   -.9430002   .48662739           8
              6 |           0           0          19
              7 |   .66234776   .52959399           8
              8 |   2.1290522   .56167487           8
              9 |   3.5425976   .47737573           7
             10 |   5.5762498   .91617885           8
             11 |   9.9275887   2.4558474           7
    ------------+------------------------------------
          Total |  -.33856659   4.9398298         100

    Comment


    • #3
      Thank you, Nick. Very much appreciate it. This is exactly what i needed.
      Best,
      Milena

      Comment

      Working...
      X