Announcement

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

  • Column and Row labels go away upon sorting

    EDIT: I use gcollapse (ssc inst gtools, replace), but regular collapse works fine.




    When I sort a matrix like this
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long code float(premse postmse ratio rmspe_rank p)
    11  .05515488  2.771367    50.247 1 .16666667
    12  .14310792  .2025988 1.4157065 6         1
    26 .012931863  .5920703  45.78384 3        .5
    27  .02456784 1.1890676  48.39935 2  .3333333
    35 .029555114  .3298432 11.160275 5  .8333333
    51   .0257518   .880294 34.183784 4  .6666667
    end
    label values code code
    label def code 11 "Rondônia", modify
    label def code 12 "Acre", modify
    label def code 26 "Pernambuco", modify
    label def code 27 "Alagoas", modify
    label def code 35 "São Paulo", modify
    label def code 51 "Mato Grosso", modify
    
    gcollapse (firstnm) premse postmse ratio rmspe_rank p, by(code)
    
    decode code, g(panel)
    
    mkmat premse postmse ratio rmspe_rank p, rownames(panel) mat(errmat)
    
    matname errmat "Pre-MSE" "Post-MSE" "Ratio" "Rank" "p", columns(1..5) explicit
    
    mata : st_matrix("errmat", sort(st_matrix("errmat"), 4))
    
    
    mat l errmat
    it robs me of my precious row and column labels.
    Code:
    . mat l errmat
    
    errmat[6,5]
                   Pre-MSE   Post-MSE      Ratio       Rank          p
       Rondônia  .05515488  2.7713671  50.247002          1  .16666667
           Acre  .14310792   .2025988  1.4157065          6          1
     Pernambuco  .01293186  .59207028   45.78384          3         .5
        Alagoas  .02456784  1.1890676  48.399349          2  .33333331
      São_Paulo  .02955511  .32984319  11.160275          5  .83333331
    Mato_Grosso   .0257518  .88029402  34.183784          4  .66666669
    
    . mata : st_matrix("errmat", sort(st_matrix("errmat"), 4))
    
    .
    .
    . mat l errmat
    
    errmat[6,5]
               c1         c2         c3         c4         c5
    r1  .05515488  2.7713671  50.247002          1  .16666667
    r2  .02456784  1.1890676  48.399349          2  .33333331
    r3  .01293186  .59207028   45.78384          3         .5
    r4   .0257518  .88029402  34.183784          4  .66666669
    r5  .02955511  .32984319  11.160275          5  .83333331
    r6  .14310792   .2025988  1.4157065          6          1
    Why might this be?

  • #2
    The technical answer to "why" is that Mata matrices do not have row or column names. Mata can obtain and attach row and column names from and to Stata matrices but that is a separate step from obtaining the matrix contents, i.e., values. The conceptual answer to "why" is for StataCorp to answer.

    Anyway, I show a possible work around here. I refer to the wrong function name in the final code-snippet, which you should obviously not do.
    Last edited by daniel klein; 02 Jun 2022, 11:23.

    Comment

    Working...
    X