Announcement

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

  • Subscripting r(table) using row and column names

    I am having problems with a seemingly simple issue. I am trying to take a specific subset of the r(table) matrix which is stored in Stata after running a logistic regression. In doing so, I would like to use row and column names to subscript r(table). My code is like the following:

    Code:
    logistic y var1 var2 var3
    matrix s1=r(table)
    matrix s2=s1["pvalue","var3"]
    However, when I run this code, Stata reports the error

    Code:
    var3 not found 
    r(111)
    despite the fact that when I list r(table) I see var3 as the name of a column. Is anyone able to help me solve this issue?

    Many thanks in advance.

  • #2
    you want the "el" function and you want to tell it the actual row and column numbers; see "h el"

    Comment


    • #3
      Hi Rich, thanks so much for your answer. If I use row and column numbers also my code above works - though (I think) it extracts a matrix and not a scalar as instead your code does. The point is that I'd need to use row and column names rather than numbers. Do your think that is possible somehow?

      Comment


      • #4
        The fourth item of help matrix indicates that:
        Subscripting by row/column name may be used only in a matrix context.
        but that this is not a real constraint as there are functions to obtain row and column numbers. Here's an example:

        Code:
        sysuse auto, clear
        logistic foreign price mpg trunk
        matrix s1=r(table)
        mat list s1
        
        * subscripting by row/column name is only available in matrix context
        matrix s2=s1["pvalue","foreign:trunk"]
        scalar pv = el(s2,1,1)
        dis pv
        local pv = el(s2,1,1)
        dis `pv'
        
        * you can workaround this limit using functions to get row/col numbers
        scalar pv2 = s1[rownumb(s1,"pvalue"),colnumb(s1,"foreign:trunk")]
        dis pv2
        local pv2 = s1[rownumb(s1,"pvalue"),colnumb(s1,"foreign:trunk")]
        dis `pv2'

        Comment


        • #5
          Robert Picard , thanks a lot. Your answer is enlightening. By reading your code I realized I was making some mistakes I was not aware of. Rich Goldstein , thank you again as well. The el function was greatly useful in doing what I was trying to do.

          Comment

          Working...
          X