Announcement

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

  • mean absolute deviation of the median

    Hi, does anyone know how to compute mean absolute deviation of the median in stata 17?

  • #2
    I have no idea what the mean absolute deviation of the median would be. Perhaps you have in mind the mean absolute deviation from the median, which could be a two-step:


    .
    Code:
     sysuse auto, clear
    (1978 automobile data)
    
    . egen median = median(mpg), by(foreign)
    
    . egen wanted = mean(abs(mpg - median)), by(foreign)

    Comment


    • #3
      I don't know of a program that does that, but it is easy enough to write one:

      Code:
      clear all
      program define mad, rclass
          syntax varname [if] [in] [fweight]
          marksample touse
          
          if "`weight'" != "" {
              local wgt = "[`weight' `exp']"
          }
          
          _pctile price if `touse' `wgt'
          tempvar dif
          gen double `dif' = abs(price-r(r1))
          sum `dif' if `touse' `wgt', meanonly
          di as txt "mean absolute deviation from median is: " as result %9.3g r(mean)
          return scalar mad = r(mean)
      end
          
      sysuse auto, clear
      mad price
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Dear Jimmy,

        This question was asked before here and Nick Cox noted that Stata's '-egen- has had a -mad()- function since Stata 7'.
        Here is a trivial example (do select all code for a proper run):

        Code:
        sysuse auto, clear
        qui sum price, detail
        local median = r(p50)
        gen deviation = abs(price - `median')
        qui sum deviation, detail
        di "Median absolute deviation of the variable price is " r(p50)
        
        egen mad_price=mad(price)
        
        gen deviationD = price - `median'
        gen d_score = deviationD / mad_price
        
        qui sum price, detail
        local pmedian = r(p50)
        local pmin = r(min)
        local pmax = r(max)
        
        scatter price d_score , name(g1) xscale(range(-2 12)) xlabel(-2(1)12, grid glpattern(line))
        kdensity price , name(g2) xscale(range(3000 16000)) xlabel(3000(1000)16000, grid glpattern(line))    ///
            plotregion(margin(l-5.9 r-6.4 t-0 b-0))    ///
            xline(`pmedian', lwidth(.4) lcolor(maroon))    ///
            xline(`pmin', lwidth(.4) lcolor(navy))    ///
            xline(`pmax', lwidth(.4) lcolor(navy))
        graph combine g1 g2 , colfirst c(1)
        Which results in the following figure that illustrates how the kernel density estimate can be matched against the d-scores of the cases that is computed using the mean absolute deviation of the median:

        Click image for larger version

Name:	Median_absolute_deviation_of_the_variable_price.png
Views:	1
Size:	74.5 KB
ID:	1619949


        Note that I did not 'polish' my code so that the tails of the kernel distribution trace line do not run over the plot area edges (sorry).
        http://publicationslist.org/eric.melse

        Comment


        • #5
          Eric Melse Thanks for the citation.

          The mean absolute deviation from the median is not in general the median absolute deviation from the median. The latter seems to be better motivated, as a resistant version of what was long called the "probable error" and as a quantity often expected to be close to IQR/2.

          Comment


          • #6
            Thank you very much. I really appreciate.

            Comment

            Working...
            X