Announcement

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

  • Get rownames from tabstat matrix

    Hi all,

    I am trying to get rownames from a tabstat matrix, example code:
    Code:
    sysuse auto, clear
    tabstat mpg, by(foreign) stat(mean sd) save
    mat li x
    local max = x[1,1]
    mat li x
    x[2,2]
                       mean         sd
    Domestic:mpg  24.772727  6.6111869
     Foreign:mpg  19.826923  4.7432972
    My goal its get Domestic but lamentably I only get mpg:

    Code:
    local rnames : rownames x
    di "`rnames'"
    mpg mpg
    PLease any comment I will grateful
    Thanks in advance

  • #2
    Originally posted by Rodrigo Badilla View Post
    My goal its get Domestic but lamentably I only get mpg:
    Code:
    sysuse auto, clear
    tabstat mpg, by(foreign) stat(mean sd) save
    display in smcl as text r(name1)

    Comment


    • #3
      Sorry my code have an error. I did install previously tabstatmat ado.
      Code:
      sysuse auto, clear
      tabstat mpg, by(foreign) stat(mean sd) save
      
      tabstatmat x, nototal
      
                         mean         sd
      Domestic:mpg  19.826923  4.7432972
       Foreign:mpg  24.772727  6.6111869
      
      local max = x[1,1]
      mat li x
      
      
      My goal its get Domestic but lamentably I only get mpg:
      
      
      local rnames : rownames x
      di "`rnames'"
      mpg mpg

      Comment


      • #4
        A variant of this was also posted in the Mata forum and answered there.

        https://www.statalist.org/forums/for...atrixrowstripe

        The short answer to the statement of the problem given here is to replace three line block of code in post #1 with
        Code:
        mat y = x[1..1,1...]
        mat li y
        local varname : roweq y
        display "`varname'"

        Comment


        • #5
          Yes, thanks again!

          Comment


          • #6
            William Your code works great to get the first name but lamentably the name is not agree with correct level. display 11-12, when the correct level is 13-15 in my real data.
            Code:
            clear all
            input  str10  edad    Pjetotal
            10-12    5.52
            10-12    5.04
            10-12    6.26
            13-15    4.39
            10-12    5.43
            10-12    6.48
            10-12    2.96
            10-12    3.30
            13-15    3.61
            10-12    6.43
            10-12    5.74
            10-12    6.87
            13-15    6.65
            13-15    4.78
            13-15    6.30
            10-12    4.70
            10-12    5.26
            10-12    6.00
            10-12    4.52
            13-15    5.26
            13-15    6.39
            end
            
            tabstat Pjetotal , by(edad) stat(mean) save
            
            
                  edad |      mean
            -----------+----------
                 10-12 |  5.322143
                 13-15 |      5.34
            -----------+----------
                 Total |  5.328095
            ----------------------
            
             tabstatmat x, nototal
             matsort x 1 "down"
            mat li x
            mat y = x[1..1,1...]
            mat li y
            
            symmetric y[1,1]
                         Pjetotal
            10-12:mean  5.3400001
            
            
            local varname : roweq y
            display "`varname'"
            10-12
            Do you know could be the reason?
            Last edited by Rodrigo Badilla; 11 Dec 2018, 21:21.

            Comment


            • #7
              Originally posted by Rodrigo Badilla View Post
              the correct level is 13-15 in my real data. . . . Do you know could be the reason?
              Code:
              mat y = x[1..1,1...]
              a
              a

              Wouldn't you be all-around better off with just
              Code:
              tabstat Pjetotal , by(edad) stat(mean) save
              display in smcl as text r(name2)

              Comment


              • #8
                Joseph
                The reason is that I use:
                Code:
                matsort x 1 "down"
                To sort the first column, then, the first row is the max mean, then my goal is get the max value and the category name. I priori I dont know the name for max mean value. The problem is that
                Code:
                matsort x 1 "down"
                Dont sort name label variable in my data.

                Comment


                • #9
                  Hi I used a command from Niels Henrik Bruun that work great, probably my problem was in use matsort to get label value.

                  Code:
                  tabstat Pjetotal , by(edad) stat(mean) save
                  
                  tabstatmat x, nototal
                               Pjetotal
                  10-12:mean  5.3221428
                  13-15:mean  5.3400001
                  
                  mata: slct = ((sumat=st_matrix("x")) :== colmax(sumat))[.,1]
                  mata: select(st_matrixrowstripe("x"), slct)[1,1]
                  13-15

                  Comment

                  Working...
                  X