Announcement

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

  • How to use STATA to draw a bin-scatter plot ?

    Suppose I want to put x variable in a decile, and get the mean of y variable for x variable in each decile. Plot the mean y variable against the mean x variable in the decile.

    Thanks in advance.

  • #2
    Probably something along these lines.
    Code:
    version 16.0
    
    clear *
    
    set seed `=strreverse("1516573")'
    quietly set obs 100
    
    generate double x = runiform(0, 100)
    generate double y = runiform(0, 100)
    
    *
    * Begin here
    *
    egen byte d = cut(x), group(10)
    set type double
    collapse (mean) x = x y = y, by(d)
    
    graph twoway scatter y x, mcolor(black) msize(small) ylabel( , angle(horizontal) nogrid)
    
    exit
    A line graph might be more the look that you want.
    Code:
    help graph twoway line

    Comment


    • #3
      Thanks a lot.
      It works!!!
      Btw I found there is another way: just install binscatter demand.

      Comment


      • #4
        Originally posted by Joseph Coveney View Post
        Probably something along these lines.
        Code:
        version 16.0
        
        clear *
        
        set seed `=strreverse("1516573")'
        quietly set obs 100
        
        generate double x = runiform(0, 100)
        generate double y = runiform(0, 100)
        
        *
        * Begin here
        *
        egen byte d = cut(x), group(10)
        set type double
        collapse (mean) x = x y = y, by(d)
        
        graph twoway scatter y x, mcolor(black) msize(small) ylabel( , angle(horizontal) nogrid)
        
        exit
        A line graph might be more the look that you want.
        Code:
        help graph twoway line
        Any idea on how to cut the variables unevenly? for example 10th percentile and 50th percentile?

        Comment


        • #5
          Here is some further technique addressing #4 but also pertinent to earlier posts.


          Code:
          . sysuse auto, clear
          
          . _pctile weight, p(10, 50, 90)
          
          . return list
          
          scalars:
                           r(r1) =  2020
                           r(r2) =  3190
                           r(r3) =  4060
          
          . gen binned_weight = cond(weight <= r(r1), 1, cond(weight <= r(r2), 2, cond(weight <= r(r3), 3, 4))) if weight < .
          
          . tab weight binned_weight
          
              Weight |                binned_weight
              (lbs.) |         1          2          3          4 |     Total
          -----------+--------------------------------------------+----------
               1,760 |         1          0          0          0 |         1
               1,800 |         2          0          0          0 |         2
               1,830 |         1          0          0          0 |         1
               1,930 |         1          0          0          0 |         1
               1,980 |         1          0          0          0 |         1
               1,990 |         1          0          0          0 |         1
               2,020 |         1          0          0          0 |         1
               2,040 |         0          1          0          0 |         1
               2,050 |         0          1          0          0 |         1
               2,070 |         0          1          0          0 |         1
               2,110 |         0          1          0          0 |         1
               2,120 |         0          1          0          0 |         1
               2,130 |         0          1          0          0 |         1
               2,160 |         0          1          0          0 |         1
               2,200 |         0          2          0          0 |         2
               2,230 |         0          1          0          0 |         1
               2,240 |         0          1          0          0 |         1
               2,280 |         0          1          0          0 |         1
               2,370 |         0          1          0          0 |         1
               2,410 |         0          1          0          0 |         1
               2,520 |         0          1          0          0 |         1
               2,580 |         0          1          0          0 |         1
               2,640 |         0          1          0          0 |         1
               2,650 |         0          2          0          0 |         2
               2,670 |         0          1          0          0 |         1
               2,690 |         0          1          0          0 |         1
               2,730 |         0          1          0          0 |         1
               2,750 |         0          2          0          0 |         2
               2,830 |         0          2          0          0 |         2
               2,930 |         0          1          0          0 |         1
               3,170 |         0          1          0          0 |         1
               3,180 |         0          1          0          0 |         1
               3,200 |         0          0          1          0 |         1
               3,210 |         0          0          1          0 |         1
               3,220 |         0          0          1          0 |         1
               3,250 |         0          0          1          0 |         1
               3,260 |         0          0          1          0 |         1
               3,280 |         0          0          1          0 |         1
               3,300 |         0          0          1          0 |         1
               3,310 |         0          0          1          0 |         1
               3,330 |         0          0          1          0 |         1
               3,350 |         0          0          1          0 |         1
               3,370 |         0          0          2          0 |         2
               3,400 |         0          0          1          0 |         1
               3,420 |         0          0          2          0 |         2
               3,430 |         0          0          1          0 |         1
               3,470 |         0          0          1          0 |         1
               3,600 |         0          0          2          0 |         2
               3,670 |         0          0          1          0 |         1
               3,690 |         0          0          2          0 |         2
               3,700 |         0          0          1          0 |         1
               3,720 |         0          0          1          0 |         1
               3,740 |         0          0          1          0 |         1
               3,830 |         0          0          1          0 |         1
               3,880 |         0          0          1          0 |         1
               3,900 |         0          0          1          0 |         1
               4,030 |         0          0          1          0 |         1
               4,060 |         0          0          2          0 |         2
               4,080 |         0          0          0          1 |         1
               4,130 |         0          0          0          1 |         1
               4,290 |         0          0          0          1 |         1
               4,330 |         0          0          0          1 |         1
               4,720 |         0          0          0          1 |         1
               4,840 |         0          0          0          1 |         1
          -----------+--------------------------------------------+----------
               Total |         8         29         31          6 |        74
          
          . quantile weight, ms(none) mlabel(binned_weight) mlabpos(0)
          
          . egen mean = mean(mpg), by(binned_weight)
          
          . scatter mean binned_weight
          
          . scatter mean weight, ms(Oh) jitter(1)

          Comment

          Working...
          X