Announcement

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

  • Matrix stored as lower triangular but getting invalid cstorage error

    Hi. I am trying to use the drawnorm command with a lower triangular matrix specified. However, I keep getting an error telling me that the matrix is square, even though it is not, as far as I can tell.

    Code:
    matrix list `cov_mat'
                c1          c2
    r1   .00001213
    r2  -2.457e-06   .00022127
    
    drawnorm "`sim_list'", means(m) cov(`cov_mat') cstorage(lower)
    
    invalid cstorage; matrix is found to be square

  • #2
    How did you create matrix `cov_mat'? If you created it as a square matrix (e.g. matrix `cov_mat' = (.00001213, -2.457e-06, -2.457e-06, .00022127)), then it is, in fact, a square matrix. The fat that -matrix list- shows you a lower triangular is just a feature (some might call it a bug) of -matrix list- that when the matrix is symmetric, it only lists the lower triangle. But the matrix itself has not been converted to a lower triangle. Try it with -cstorage(full)-.

    Note: If you want, you can force -matrix list- to print the full matrix by specifing the -nohalf- option.

    Comment


    • #3
      Interesting. When I convert to lower triangular in Mata, using:

      Code:
      mata: st_view(X=., .,.); X= select(X, colsum(abs(X))); X= lowertriangle(X); cov_mat = st_local("cov_mat"); cov_mat; st_matrix(cov_mat, X)
      I still get the same error:
      Code:
      matrix list `cov_mat'              
              c1                  c2
      r1    1.2343669     0
      r2   -.09383314    .00717265
      Code:
      drawnorm "`sim_list'", means(m) cov(`cov_mat') cstorage(lower)
      invalid cstorage; matrix is found to be square
      Last edited by Lane Pierce; 15 Aug 2017, 13:39.

      Comment


      • #4
        The same error occurs when I use the diag() command:

        Code:
        matrix `cov_mat' = diag(vecdiag(`cov_mat'))

        Comment


        • #5
          Although I actually can't use the diag command because that does not do what I thought it did anyways

          Comment


          • #6
            I think you have to transform the lower part of your matrix into a vector in order to pass it to drawnorm. The Mata function vech should help you do that.

            Code:
            clear
            set obs 10
            set seed 14893
            // mat cov_mat = (.00001213, -2.457e-06, -2.457e-06, .00022127))
            mat cov_mat = (.00001213, 0.0\ -2.457e-06, .00022127)
            
            mata: 
            m = st_matrix("cov_mat")
            m = vech(m)
            st_matrix("cov_mat",m)
            end
            
            mat list cov_mat
            mat m = (0,0)
            drawnorm x1 x2, means(m) cov(cov_mat) cstorage(lower)
            
            list

            Comment

            Working...
            X