Announcement

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

  • Mean deviation from the median

    Hello,

    As part of my research, I need to calculate the mean deviation from the median of some variables. I know that we can use the egen function to compute the mean deviation from the mean (mdev) and the median deviation from the median (mad). However, is there an option to compute directly the mean deviation from the median? If not, how can I compute it by hand?

    Thank you in advance for your answers.

  • #2
    Mathieu:
    I've never challenged myself with that.
    Annyway, I do hope that the following toy-example will be helpful:

    Code:
    . sysuse auto.dta
    
    . quietly su price, d
    
    . g median_diff= price-r(p50)
    
    . su median_diff
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
     median_diff |         74    1158.757    2949.496    -1715.5    10899.5
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Strictly egen is a command; its functions are known as functions, in an idiosyncratic sense (such functions are not general Stata functions or Mata functions).

      There is not much need here for lots of little one-off functions, as the existing functions can get you where you want to be. In fact I wrote the original mdev() and mad() code in 1999 and stopped short of this measure as being rare in my experience but not too difficult to get at.

      You didn't specify absolute values |deviation|, but I am guessing that may be part of the prescription.

      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . egen median = median(mpg), by(rep78)
      
      . egen meandevmed1 = mean(mpg - median), by(rep78)
      
      . egen meandevmed2 = mean(abs(mpg - median)), by(rep78)
      
      . tabdisp rep78, c(median meandev*)
      
      -------------------------------------------------
      Repair    |
      Record    |
      1978      |      median  meandevmed1  meandevmed2
      ----------+--------------------------------------
              1 |          21            0            3
              2 |          18        1.125        2.875
              3 |          19     .4333333     2.966667
              4 |        22.5    -.8333333            4
              5 |          30    -2.636364     7.363636
              . |          22          -.6          3.8
      -------------------------------------------------

      Comment


      • #4
        Perfect answers, thank you @Carlo and @Nick!

        Comment

        Working...
        X