Announcement

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

  • Ordering regression results based on estimated coefficients

    I'm trying to order fixed-effects regression results from the smallest estimated coefficient to the greatest. For example:

    Code:
    sysuse auto, clear
    xtset foreign
    
    xtreg price mpg headroom trunk weight , fe r
    matrix results = r(table)'
    matrix list results
    mata : st_matrix("results", sort(st_matrix("results"), 1)) /* This gets rid of the row and column names*/
    matrix list results
    However, this gets rid of the row and column names, so I can't tell which results are for which variable without manually comparing the coefficients. Can anyone help me with this? I'd like to be able to bring the matrix back from Mata to Stata without losing the row and column names.

  • #2
    Hi Jeff,

    This is a bit inelegant but this works, I think.

    Code:
    sysuse auto, clear
    xtset foreign
    
    xtreg price mpg headroom trunk weight , fe r
    matrix results = r(table)'
    matrix list results
    local rownames: rownames results
    
    preserve
    clear 
    
    svmat results, names(col)
    
    gen rownames = ""
    
    local i 0
    foreach rown of local rownames {
    local ++i
    replace rowname = "`rown'" in `i'
    }
    
    sort b
    
    mkmat b-eform, matrix(results) rownames(rownames)
    
    restore
    
    matrix list results

    Comment


    • #3
      May be useful to note that you can manipulate row labels in Mata using st_matrixrowstripe(). After creating the Stata matrix results try
      Code:
      mata:
      R = st_matrix("results")
      rowlabs = st_matrixrowstripe("results")
      columnlabs = st_matrixcolstripe("results")
      i = order(R, 1)
      st_matrix("results", R[i,])
      st_matrixrowstripe("results", rowlabs[i,])
      st_matrixcolstripe("results", columnlabs)
      end
      mat list results
      I added st_matrixcolstripe() to preserve column labels. This is all assuming that in your application it makes sense to sort this way, i.e. the magnitudes of the coefficients are comparable.
      Last edited by German Rodriguez; 21 Nov 2017, 10:26.

      Comment


      • #4
        Nice! Very elegant. I learned something today

        Comment


        • #5
          Works perfectly. Thanks!

          Comment

          Working...
          X