Announcement

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

  • Appending Matrices of Scalars Together

    I'm doing my first simulation study, and I'd like to see how sensitive my estimator is to various levels of Gaussian noise. To do so, I'd like to get all of the pre-post Mean Squared Errors in a matrix. My question is, how may I loop over (or a user written command) multiple matrices and append them to one another? The code below does exactly as I wish...
    Code:
    sysuse auto, clear
    
    matrix drop _all
    
    
    loc list weight mpg trunk headroom gear foreign turn
    
    loc n: word count `list'
    cls
    
    qui forv i = 1/`n' {
        
        loc a: word `i' of `list'
        
        reg price `a'
    
        tempname err_`i'
        
        matrix `err_`i'' = e(rmse) , e(r2)
        
        matrix rownames `err_`i'' = "Noise of `i'"
        matrix colnames `err_`i'' = "RMSE" "R2"
    
    }
    
    mat A = (`err_1' \\`err_2' \\`err_3')
    mat l A
    However, what if I had 20 levels of noise? What if I simulated it from 0.01 to 1, increasing it by 0.001 each time? Clearly, it doesn't make sense to keep doing
    Code:
    mat A = (`err_1' \\`err_2' \\`err_3') // ............ and so on
    until I'm at err_1000. How might I append all of these together at once, where "Noise of 1" comes first all the way to "Noise of `n'"?

  • #2
    I find exercises like this easier to do in Mata. In this example (meant to capture the essence of your case) Mata would define a void matrix (A), the Stata loop does the appending to that void matrix at each iteration, and then Mata returns the resulting matrix (B) back to Stata. st_matrixrowstripe() gives the row names to B.

    Code:
    mata A=J(0,2,.)
    mata R=J(0,2,"")
    
    tempname e12
    
    forval j=1/20 {
    loc e1=rnormal()
    loc e2=rnormal()
    matrix `e12'=`e1',`e2'
    mata A=A\st_matrix("`e12'")
    mata R=R\(("Noise of "+strofreal(`j'))," ")
    }
    
    mata st_matrix("B",A)
    mata st_matrixrowstripe("B",R)
    matrix list B
    Last edited by John Mullahy; 21 Apr 2022, 08:26.

    Comment


    • #3
      One day, I shall read William Gould's book, and understand it! Thanks so much.

      Here is the full code, (minus the rownames), for completeness.
      Code:
      sysuse auto, clear
      
      mata A=J(0,2,.)
      tempname e123
      
      
      loc list weight mpg trunk headroom gear foreign turn
      
      loc n: word count `list'
      cls
      
      qui forv i = 1/`n' {
          
          loc a: word `i' of `list'
          
          reg price `a'
      
       matrix `e123'=e(rmse),e(r2)
       mata: A=A\st_matrix("`e123'")
      
      }
      
      mata: st_matrix("B",A)
      
      matrix colnames B = "RMSE" "R2"
      
      mat l B

      Comment


      • #4
        To put RMSE and R2 as the column names you can use st_matrixcolstripe
        Code:
        mata C="RMSE"\"R2"
        mata C=C,J(2,1," ")
        mata st_matrix("B",A)
        mata st_matrixrowstripe("B",R)
        mata st_matrixcolstripe("B",C)
        matrix list B

        Comment


        • #5
          In Stata:

          Code:
          sysuse auto, clear
          clear matrix
          matrix drop _all
          
          
          loc list weight mpg trunk headroom gear foreign turn
          
          loc n: word count `list'
          
          
          qui forv i = 1/`n' {
              
              loc a: word `i' of `list'
              
              reg price `a'
          
              tempname err_`i'
              
              matrix `err_`i'' = e(rmse) , e(r2)
              
              matrix rownames `err_`i'' = "Noise of `i'"
              matrix colnames `err_`i'' = "RMSE" "R2"
              mat A= nullmat(A)\ `err_`i''
          }
          
          mat l A

          Comment

          Working...
          X