Announcement

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

  • Referring to matrix columns and rows

    Dear All,
    It should be fairly simply, but I am very confused as to how I should refer to a location in a matrix. I want to store certain column's of the regression output in a new matrix. To do so I start by creating an empty matrix and try to save first column (all rows) of matrix m1 in first column of matrix m:

    Code:
    mat m=J(15,27, .)
    
    svy: regress y x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14
    mat m1=r(table)'
    mat m[1..,1]=m1[1..,1]
    But I get the following error:

    Code:
     mat m[1..,1]=m1[1..,1]
    1.. invalid name
    r(198);
    1 is supposed to be a location (r x c) and not a name. How does one specify a "location" within a matrix in Stata? Seems like such a basic thing but I have tried many versions and it seems I am missing something fundamental about row-column address spec in Stata matrices.

    I will be grateful for your help and also reference to a guide to using matrices in Stata please.
    Many thanks.
    Sincerely,
    Sumedha.

  • #2
    Stata's matrix language is much more restricted than Mata. I am open to correction but I think all you can do here is specify matrix matname[r, c] on the left-hand side where r and c are just constants that define the row and column indexes of a particular element. That still allows submatrices to be replaced.

    See


    Code:
    help matrix substitution

    Comment


    • #3
      From the Stata User's Guide 14.9:
      7. When defining an element of a matrix, use
      matrix matname[i,j] = expression
      where i and j are scalar expressions. The matrix matname must already exist.

      Example:
      matrix A = J(2,2,0)
      matrix A[1,2] = sqrt(2)

      8. To replace a submatrix within a matrix, use the same syntax. If the expression on the right evaluates
      to a scalar or 11 matrix, the element is replaced. If it evaluates to a matrix, the submatrix with
      top-left element at (i; j) is replaced. The matrix matname must already exist.

      Example:
      matrix A = J(4,4,0)
      matrix A[2,2] = C'*C

      Comment


      • #4
        Prof. Cox,
        Thank you for the suggestion. I thought I was doing something wrong... Following your suggestion I wrote a loop (a bit ridiculous probably) to make it work...

        Code:
        mat m1=r(table)'
        local rows=rowsof(m1)
        forvalues t=1(1)`rows'{
        mat m[`t',1]=m1[`t',1]
         mat m[`t',2]=m1[`t',5]
         mat m[`t',3]=m1[`t',6]
        }
        So not elegant...but it does what I need it to do..

        Thank you again.
        Sincerely,
        Sumedha.

        Comment

        Working...
        X