Announcement

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

  • Extracting a large submatrix with column and row names

    Hi,

    Variance-covariance matrix of the estimators has a dimension of 9000 x 9000. I am trying to find a way to extract a submatrix (32x32). It is not very easy to find the indices of the variables. The variables to be extracted from the matrix are sequential, so slicing may also work. I can also make a list of variable names to be extracted from the matrix, but I am not familiar with looping in Stata.
    What is a clean and fast way of handling this problem in Stata?

  • #2
    Is the indexing used to create msub and msub1 useful for your purposes?
    Code:
    set seed 2345
    mata st_matrix("mall",floor(runiform(5,5):*5))
    matrix list mall
    matrix msub=mall["r2".."r4","c2".."c4"]
    matrix list msub
    
    matrix mall1=mall
    
    matrix rownames mall1 = myrow1 myrow2 myrow3 myrow4 myrow5
    matrix colnames mall1 = mycol1 mycol2 mycol3 mycol4 mycol5
    matrix list mall1
    
    matrix msub1=mall1["myrow2".."myrow4","mycol2".."mycol4"]
    matrix list msub1
    Results:
    Code:
    . matrix list mall
    
    mall[5,5]
        c1  c2  c3  c4  c5
    r1   1   0   0   1   2
    r2   1   1   1   4   3
    r3   0   2   3   3   2
    r4   2   4   4   3   4
    r5   1   0   0   1   3
    
    . matrix msub=mall["r2".."r4","c2".."c4"]
    
    . matrix list msub
    
    msub[3,3]
        c2  c3  c4
    r2   1   1   4
    r3   2   3   3
    r4   4   4   3
    
    .
    . matrix mall1=mall
    
    .
    . matrix rownames mall1 = myrow1 myrow2 myrow3 myrow4 myrow5
    
    . matrix colnames mall1 = mycol1 mycol2 mycol3 mycol4 mycol5
    
    . matrix list mall1
    
    mall1[5,5]
            mycol1  mycol2  mycol3  mycol4  mycol5
    myrow1       1       0       0       1       2
    myrow2       1       1       1       4       3
    myrow3       0       2       3       3       2
    myrow4       2       4       4       3       4
    myrow5       1       0       0       1       3
    
    .
    . matrix msub1=mall1["myrow2".."myrow4","mycol2".."mycol4"]
    
    . matrix list msub1
    
    msub1[3,3]
            mycol2  mycol3  mycol4
    myrow2       1       1       4
    myrow3       2       3       3
    myrow4       4       4       3
    
    .

    Comment


    • #3
      Hi John,

      Thank you for the help. I saved the variance-covariance matrix. It has a size of [8646, 8646].

      Code:
      mata : st_matrix("Co", st_matrix("e(V)"))

      Unfortunately I cannot figure out what the indices of columns and rows I am trying to extract because the matrix is too large. There are 38 variables that I am trying to extract from matrix Co.

      Comment


      • #4
        Thanks John for the instructions. Here is what I did that worked for me.
        Code:
        *save variance-covariance matrix
        mat Co=e(V) 
        
        *save a submatrix by locating variables with their names
         matrix sub_co = co["tt0".."26.cat_t1", "tt0".."26.cat_t1"]

        Comment

        Working...
        X