Announcement

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

  • Use results store in matrices

    Dear Statalister,
    many commands automatically store results in r(), either scalar, local or matrices. I focus in the latter case.

    I'd like to access directly to some values in the matrix stored. See the example below :
    Code:
    sysuse auto.dta,clear
    
    corr mpg price trunk
    return list
    The matrix of the correlation coefficient is store under the name r(C).
    However any direct use of r(C) doesn't work. See :

    Code:
    matrix list r(C)
    local b1=r(C)[1,2]
    di `b1'
    However, I've get this information using an intermediate matrix m set equal to r(C). And now the commands that didn't work directly on r(C) work on m

    I've done :
    Code:
    matrix m=r(C)
    matrix list m
    local c1=m[1,2]
    di `c1'
    local c2=m[1,3]
    di `c2'
    Is there a way to get this info without creating m?

    Thanks,
    Charlie

    Ps : I know that concerning correlation coefficient I could have simply done two by two correlations, which directly store in the local r(rho) avoiding a matrix, but this was for illustrational purposes. Indeed I use corrci command, which returns only matrixes.

  • #2
    As far as I know, in order to retain a returned matrix, you have to create a new matrix as you have done in #1. This is also true for matrices in -ereturn-.

    Comment


    • #3
      Ok, thanks Clyde, this is all I wanted to know.
      It seemed strange to me, quite unusual compared to scalar stored (local b=r(rho) would work) and quite long, but I guess there is some good reason behind.

      Charlie

      Comment


      • #4
        You can copy a single number to a local macro, but how would you expect a matrix to be stored in a local macro? You could store the name of a matrix in a local macro, clearly a different matter.

        Remember, a local macro holds a string. The string can have numeric characters only, but all local macro definitions are variants on

        Code:
        local foo "bar"
        even when they are assignments.

        Comment


        • #5
          Dear Nick,
          I didn't want to store a matrix in a local macro, but only one element of the matrix (i.e. here the [1,2]), which I do (see my code local c1=m[1,2]), but only after copying the matrix named r(C) into the matrix named m (or rather, after creating a new matrix m equal to r(C).)

          What I didn't get is why I could store the element [1,2] of my matrix m into a local macro, but couldn't do it for the matrix r(C), whereas they are supposed to be the same (same type : matrix, and same values). But I guess a matrix automatically generated as is r(C) is not exactly the same as a matrix I create using matrix, this is the one thing I didn't know, and made this issue look strange to me.

          Best,
          Charlie

          Comment


          • #6
            Fair point. I suspect that it just is a complication too far for the syntax. There would have to be a check of whether an r(C) exists that can take subscripts.. Implementing that without creating other problems sounds like a medium-size nightmare compared with the small benefit you are missing now.

            Comment


            • #7
              Using the el() matrix function enables the program to store the value without creating an intermediate matrix.
              Code:
              local b1=el(r(C),1,2)

              Comment


              • #8
                Thanks William,
                This is what I was looking for !

                Best,
                Charlie

                Comment


                • #9
                  For those who search finds this topic, with Stata 16 it is now possible to use the syntaxCharlie Joyez hoped to use in his code in post #1.
                  Code:
                  . sysuse auto.dta,clear
                  (1978 Automobile Data)
                  
                  . quietly corr mpg price trunk
                  
                  . matrix list r(C)
                  
                  symmetric r(C)[3,3]
                                mpg       price       trunk
                    mpg           1
                  price  -.46859669           1
                  trunk  -.58158503   .31433161           1
                  
                  . local b1=r(C)[1,2]
                  
                  . di `b1'
                  -.46859669

                  Comment


                  • #10
                    Great news!
                    Thanks William for the update.

                    Comment

                    Working...
                    X