Announcement

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

  • How get rowname of max value using matsort, tabstatmat and st_matrixrowstripe

    Hi all,

    I am trying to get row name of a max value in a matrix. Previously i did install matsort and tabstatmat and mix both commands with st_matrixrowstripe but fail in the last step, my example code
    Code:
    sysuse auto, clear
    *previously install tabstatmat
    tabstat mpg, by(foreig) stat(mean sd) save
    tabstatmat x, nototal
    *previously install matsort
    matsort x 1 "down"
    mat li x
                       mean         sd
    Domestic:mpg  24.772727  6.6111869
     Foreign:mpg  19.826923  4.7432972
    
    local max = x[1,1]
    
    mata : st_matrixrowstripe("x")
                  1          2
        +-----------------------+
      1 |  Domestic        mpg  |
      2 |   Foreign        mpg  |
        +-----------------------+
    
    local rownames = x[1,1]
    di "`rownames'"
    Please any advice I will grateful.
    Best regards

  • #2
    My goal is get label: Domestic

    Comment


    • #3
      Actually, no Mata is needed here.
      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . *previously install tabstatmat
      . tabstat mpg, by(foreign) stat(mean sd) save
      
      Summary for variables: mpg
           by categories of: foreign (Car type)
      
       foreign |      mean        sd
      ---------+--------------------
      Domestic |  19.82692  4.743297
       Foreign |  24.77273  6.611187
      ---------+--------------------
         Total |   21.2973  5.785503
      ------------------------------
      
      . tabstatmat x, nototal
      
      x[2,2]
                         mean         sd
      Domestic:mpg  19.826923  4.7432972
       Foreign:mpg  24.772727  6.6111869
      
      . *previously install matsort
      . matsort x 1 "down"
      
      . mat li x
      
      x[2,2]
                         mean         sd
      Domestic:mpg  24.772727  6.6111869
       Foreign:mpg  19.826923  4.7432972
      
      . mat y = x[1..1,1...]
      
      . mat li y
      
      y[1,2]
                         mean         sd
      Domestic:mpg  24.772727  6.6111869
      
      . local varname : roweq y
      
      . display "`varname'"
      Domestic
      
      .

      Comment


      • #4
        Thanks William, you make may day!

        Regards!

        Comment


        • #5
          Or if there is an odd chance that you prefer Mata over Stata (You have to install my package -matrixtools- (ssc install matrixtools) in order to run -sumat-):
          Code:
          . sysuse auto, clear
          (1978 Automobile Data)
          
          . * Get table r(sumat)
          . sumat mpg, statistics(mean sd) rowby(foreign)
          -------------------------------
                               mean    sd
          -------------------------------
          Car type(Domestic)  19.83  4.74
          Car type(Foreign)   24.77  6.61
          -------------------------------
          
          . * Get a column vector (slct) telling which row has the highest mean
          . mata: slct = ((sumat=st_matrix("r(sumat)")) :== colmax(sumat))[.,1]
          
          . * Use slct to select the row name value of r(sumat)
          . mata: select(st_matrixrowstripe("r(sumat)"), slct)[1,2]
            Car type(Foreign)
          
          . * Or just to get the label value
          . mata: regexm(select(st_matrixrowstripe("r(sumat)"), slct)[1,2], "\((.+)\)") ? regexs(1) : "Regex not true"
            Foreign
          Enjoy
          Kind regards

          nhb

          Comment


          • #6
            Thanks Niels !! your code work great!,

            I made some modifications in your code to work with tabstat:

            Code:
            sysuse auto, clear
            tabstat mpg , by(foreign) stat(mean) save
            tabstatmat x, nototal
                                 mpg
            Domestic:mean  19.826923
             Foreign:mean  24.772727
            
            mata: slct = ((sumat=st_matrix("x")) :== colmax(sumat))[.,1]
            mata: select(st_matrixrowstripe("x"), slct)[1,1]
            Foreign
            Best
            Rodrigo

            Comment


            • #7
              Sorry Niels Henrik Bruun


              I am trying to get Foreign as local names, but probably I am making something wrong, could help me?

              Code:
              mata: namerow= select(st_matrixrowstripe("x"), slct)[1,1]
              
              local rowname: word 1 of `namerow'
              di "`rowname'"
              .

              Comment


              • #8
                Certainly:
                Code:
                mata: st_local("rowname", namerow)
                Kind regards

                nhb

                Comment


                • #9
                  Thanks Niels Henrik Bruun

                  Dont bother anynmore!
                  Regards!

                  Comment

                  Working...
                  X