Announcement

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

  • Can I use Mata to calculate a median of the outcome in the exposed and unexposed groups following matching with teffects psmatch?

    Hi everyone,

    After running the following "teffects psmatch" command:
    Code:
    use http://www.stata-press.com/data/r13/cattaneo2
    teffects psmatch (bweight) (mbsmoke married mage medu fbaby), nneighbour(1) gen(stub)
    I am developing code to display descriptive statistics for the outcome (bweight) in the exposed and unexposed groups after the matching process. To do this I have used the following Mata code:

    Code:
    gen byte touse = e(sample)
    
    mata:
    i = st_data(.,"stub*","touse")
    n = cols(i):-rowmissing(i)
    t = st_data(.,"mbsmoke","touse")
    w = w1 = w0 = st_data(.,"bweight","touse")
    
    for (j=1; j<=rows(i); j++) {
            if (t[j]) {
                    w0[j] = mean(w[i[|j,1\j,n[j]|]])
            }
            else {
                    w1[j] = mean(w[i[|j,1\j,n[j]|]])
            }
    }
    mean(w1)-mean(w0)
    mean(w0)
    min(w0)
    max(w0)
    mean(w1)
    min(w1)
    max(w1)
    w0
    w1
    end

    This code denotes the post-matching exposed group as "w1" and the post-matching unexposed group as "w0" and extracts the minimum, maximum and mean in both the exposed and unexposed groups from the Mata matrix. I used a manual calculation of ATE [mean(w1)-mean(w0)] to verify my outputs against the automated Stata ATE output.

    However I cannot find a way to calculate the median value in the exposed and unexposed groups (I am aware the median is not particularly relevant for birthweights in this example but I am using this stata dataset for this post but the original problem relates to a separate dataset where the median is more appropriate). The only method I can find so far is using the final two Mata commands "w0" and "w1" which list the birthweights in the exposed and unexposed groups after matching as a column vector, allowing me to manually calculate the median from this column. I'm sure there must be an easier way similar to the "min", "max" or "mean" commands within Mata or by exporting the "w0" and "w1"columns to Stata und utilizing "centile" or "summarize, detail" in Stata code but I can't work out how to do it.

    If anyone has any suggestions or help it would be appreciated.

    Thanks,

    Adam

  • #2
    Is this a question of how to get the median of a vector? I am not aware of such a function in Mata, but it is easily implemented:

    Code:
    version 12.1
    
    mata :
    
    real scalar get_qtile(real colvector x, real scalar q)
    {
        real colvector tmp
        real scalar idx
        
        if ((q <= 0) | (q >= 1)) {
            _error(3300)
        }
        
        tmp = sort(x, 1)
        idx = rows(x)*q
        
        if (mod(idx, 1)) {
            return(tmp[ceil(idx)])
        }
        else {
            return(((tmp[idx] + tmp[idx + 1])/2))
        }
    }
    Then

    Code:
    get_qtile(w0, .5)
    get_qtile(w1, .5)
    Best
    Daniel

    Comment


    • #3
      Thanks Daniel,

      You've solved my problem and I can adapt this to easily calculate other quintiles such as 25th and 75th centiles for the matched groups too.

      Regards,

      Adam

      Comment


      • #4
        the user-written package moremata has functions to compute medians and quantiles.

        Comment


        • #5
          Thanks for the tip Christophe,

          I used "moremata" to count my binary outcomes after matching, which has been useful.

          Adam

          Comment

          Working...
          X