Announcement

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

  • access to a column (and a row) of matrix

    Dear all,

    Is there a way to get access to a column of matrix and display it? I see that there is a way to display a specific element by using the following code:

    Code:
        sysuse auto, clear
        qui: regress mpg trunk foreign turn
        matrix mytable= r(table)
        matrix list mytable
    
        di table1[1,1]
    But I haven't figured out how to display a column (and hopefully a row).


  • #2
    See this technical document, particularly 14.5 could be useful: https://www.stata.com/manuals/u14.pdf

    Comment


    • #3
      Code:
      matlist mytable[2, 2]
      matlist mytable[1, 1..4]
      matlist mytable[1..9, 1]

      Comment


      • #4
        Ken Chui Leonardo Guizzetti Thanks for your suggestions. Would there be a way to do so using column name? For example,
        Code:
        matlist mytable[1,"trunk"]
        It would be really useful if it's possible.

        Comment


        • #5
          Code:
          mytable[1,colnumb(tab1,"trunk")]
          mytable[1,1]
          I've just found that it can be achieved with -colnumb-, but hope that there is a direct way. Please let me know if anybody knows.

          Comment


          • #6
            On top of what Leonardo showed, you can extract 2nd row, or 3rd column like this:

            Code:
            . sysuse auto, clear
            (1978 Automobile Data)
            
            . qui regress mpg trunk foreign turn
            
            . mat table = r(table)
            
            . matlist table[2,1...]
            
                         |     trunk    foreign       turn      _cons 
            -------------+--------------------------------------------
                      se |  .1346554   1.289525   .1574997   5.782865 
            
            
            . matlist table[1...,3]
            
                         |      turn 
            -------------+-----------
                       b | -.8403808 
                      se |  .1574997 
                       t |  -5.33576 
                  pvalue |  1.11e-06 
                      ll | -1.154504 
                      ul | -.5262575 
                      df |        70 
                    crit |  1.994437 
                   eform |         0


            Comment


            • #7
              These are all equivalent:

              Code:
              matlist r(table)["b","trunk"]
              matlist r(table)[1,"trunk"]
              matlist r(table)["b",1]

              Comment


              • #8
                Leonardo Guizzetti Thanks, but it doesn't work for me. For your information, my state version is MP15.0.

                The below code works.
                Code:
                matlist r(table)
                The below code doesn't work.
                Code:
                matlist r(table)["b","trunk"]
                matlist r(table)[1,"trunk"]
                matlist r(table)["b",1]
                matlist r(table)[1,1]

                Comment


                • #9
                  HI Jae, you did not tell us what version you were using, so the code above works in Stata 16. To my understanding, the feature that allows code the code in #7 to work wasn't added until version 16, so you will have to to use -colnumb()- and -rownumb()- like you did in #5.

                  Edit:
                  On second thought, my example was somewhat misleading. The code I showed in #8 does work, but it uses r(table), which was enhanced in Stata 16. If you replace r(table) with a matrix (e.g., -mytable-, and not a result from r() or e()), then it works in Stata 15.

                  Code:
                  matlist mytable["b","trunk"]
                  matlist mytable[1,"trunk"]
                  matlist mytable["b",1]
                  matlist mytable[1,1]
                  Last edited by Leonardo Guizzetti; 03 Apr 2021, 16:09.

                  Comment


                  • #10
                    Yes, apparently matrix extraction of system matrices such as e(V) and r(table) was added in Stata 16. Before this one needs to firstly assign the system matrix to standard matrix, and then operate on the standard matrix.

                    Also one can extract rows and columns with reference to row and column names, or with reference to row and column number. Therefore all of the following work:

                    Code:
                    . sysuse auto, clear
                    (1978 Automobile Data)
                    
                    .     qui: regress mpg trunk foreign turn
                    
                    . mat table = r(table)
                    
                    
                    . matlist table[1,1...]
                    
                                 |     trunk    foreign       turn      _cons 
                    -------------+--------------------------------------------
                               b | -.3122702  -1.167662  -.8403808   59.26023 
                    
                    . matlist table["b",1...]
                    
                                 |     trunk    foreign       turn      _cons 
                    -------------+--------------------------------------------
                               b | -.3122702  -1.167662  -.8403808   59.26023 
                    
                    . matlist table["b","trunk"...]
                    
                                 |     trunk    foreign       turn      _cons 
                    -------------+--------------------------------------------
                               b | -.3122702  -1.167662  -.8403808   59.26023
                    and similarly with column extraction:

                    Code:
                    . matlist table["b"...,"trunk"]
                    
                                 |     trunk 
                    -------------+-----------
                               b | -.3122702 
                              se |  .1346554 
                               t | -2.319032 
                          pvalue |  .0233177 
                              ll | -.5808319 
                              ul | -.0437085 
                              df |        70 
                            crit |  1.994437 
                           eform |         0 
                    
                    . matlist table["b"...,1]
                    
                                 |     trunk 
                    -------------+-----------
                               b | -.3122702 
                              se |  .1346554 
                               t | -2.319032 
                          pvalue |  .0233177 
                              ll | -.5808319 
                              ul | -.0437085 
                              df |        70 
                            crit |  1.994437 
                           eform |         0

                    Originally posted by Leonardo Guizzetti View Post
                    HI Jae, you did not tell us what version you were using, so the code above works in Stata 16. To my understanding, the feature that allows code the code in #7 to work wasn't added until version 16, so you will have to to use -colnumb()- and -rownumb()- like you did in #5.

                    Edit:
                    On second thought, my example was somewhat misleading. The code I showed in #8 does work, but it uses r(table), which was enhanced in Stata 16. If you replace r(table) with a matrix (e.g., -mytable-, and not a result from r() or e()), then it works in Stata 15.

                    Code:
                    matlist mytable["b","trunk"]
                    matlist mytable[1,"trunk"]
                    matlist mytable["b",1]
                    matlist mytable[1,1]

                    Comment

                    Working...
                    X