Announcement

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

  • Percentiles and data trimming

    Good morning
    I tried to find an answer to my (apparently) easy question on the forum, but probably it is so easy that nobody else asked for that in the past...
    Forgive me.
    Anyway, I have a dataset with a lot of potential outliers; I managed that issue winsorizing, but a referee asked me to show the results convenienttly "trimming" the dataset instead of winsorizing.
    I did it manually, but I'd like to use a routine for several reasons.
    What I really need is something that gives me (for a long list of variables) new variables where only the values between the 5th and the 95th percentile have the original values, while the other observations are transformed into missing values.
    I am using STATA11
    Thanks a lot for any suggestions
    Simone

  • #2
    The following should work for a variable called varname:

    Code:
    clonevar newvar=varname
    sum newvar, detail
    replace newvar=. if newvar < `r(p5)' | newvar > `r(p95)'
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Thank you very much, Carole... As usual, problem solved perfectly within 20 minutes.
      I love this forum!

      Comment


      • #4
        The following should do the trick, where you replace the varlist price mpg head trunk by the varlist of the variable on which you want to apply the trimming.

        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . foreach var of varlist price mpg head trunk {
          2. summ `var', d
          3. replace `var' = . if `var'>r(p95) | `var'<r(p5)
          4. }

        Comment


        • #5
          Thanks Joro! This adds the "routine" part that i mentioned.
          I'll enjoy both the methods in different parts of the .do file.
          Thanks again

          Comment


          • #6
            Code:
            . search trimming 
            
            SJ-13-3 st0313  . . . . . . . . . . . . . .  Speaking Stata: Trimming to taste
                    (help trimmean, trimplot if installed)  . . . . . . . . . .  N. J. Cox
                    Q3/13   SJ 13(3):640--666
                    tutorial review of trimmed means, emphasizing the scope for
                    trimming to varying degrees in describing and exploring data

            Comment

            Working...
            X