Announcement

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

  • Standard error syntax for matrix storing

    Hi everyone. I am looking to store iterative sets of standard errors in a matrix for a Monte Carlo Simulation. The problem I'm having is that I'm not sure where to put the matrix, and if there is specific syntax for the standard error matrix. I need the standard errors store in a matrix so I can perform some calculations for my econometrics assignment. Ideally, I would like to be able to replace the matrix just like I have done with my coefficients.
    Code:
    clear
    
    local mc = 1000
    set seed 368
    set obs `mc'
    gen data_n = .
    gen data_store_x = .
    gen data_store_cons = .
    
    mata: beta_10 = J(1000,1,.)
    mata: beta_100 = J(1000,1,.)
    mata: beta_1000 = J(1000,1,.)
    mata: beta_10000 = J(1000,1,.)
    foreach n of numlist 10 100 1000 10000 {
        quietly {
            forvalues i = 1(1) `mc' {
                if floor((`i'-1)/100) == (`i' -1)/100 {
                    noisily display "Working on `i' out of `mc' at $S_TIME" 
                }
                preserve
    
                clear
    
              set obs `n'
    
                gen x = rnormal() *3 + 6
    
                gen e = rnormal() - 0.5
    
                gen y = 3 + 4*x + e
                
                reg y x, vce(robust)
    
                local xcoef = _b[x]
                local const = _b[_cons]
                mata: beta_`n'[`i',1] = `xcoef'
                restore
             
                
                replace data_n = `n' in `i'
                replace data_store_x = `xcoef' in `i'
                replace data_store_cons = `const' in `i'
                    }
            }
        summ  data_n data_store_x data_store_cons, detail
        
    }

  • #2
    It's a very minor modification to the existing code. Indeed, the handling of the standard errors is mutatis mutandis just like you have done with your coefficients.

    Code:
    clear
    
    local mc = 1000
    set seed 368
    set obs `mc'
    gen data_n = .
    gen data_store_x = .
    gen data_store_cons = .
    gen data_store_std_err = .
    
    mata: beta_10 = J(1000,1,.)
    mata: beta_100 = J(1000,1,.)
    mata: beta_1000 = J(1000,1,.)
    mata: beta_10000 = J(1000,1,.)
    
    mata: std_err_10 = J(1000,1,.)
    mata: std_err_100 = J(1000,1,.)
    mata: std_err_1000 = J(1000,1,.)
    mata: std_err_10000 = J(1000,1,.)
    
    foreach n of numlist 10 100 1000 10000 {
        quietly {
            forvalues i = 1(1) `mc' {
                if floor((`i'-1)/100) == (`i' -1)/100 {
                    noisily display "Working on `i' out of `mc' at $S_TIME"
                }
                preserve
    
                clear
    
              set obs `n'
    
                gen x = rnormal() *3 + 6
    
                gen e = rnormal() - 0.5
    
                gen y = 3 + 4*x + e
                
                reg y x, vce(robust)
    
                local xcoef = _b[x]
                local const = _b[_cons]
                local std_err = _se[x]
                mata: beta_`n'[`i',1] = `xcoef'
                mata: std_err_`n'[`i', 1] = `std_err'
                restore
             
                
                replace data_n = `n' in `i'
                replace data_store_x = `xcoef' in `i'
                replace data_store_cons = `const' in `i'
                replace data_store_std_err = `std_err' in `i'
                    }
            }
        summ  data_n data_store_x data_store_std_err data_store_cons, detail
        
    }

    Comment


    • #3
      Thank you Clyde! I had made a mistake with the standard error variable syntax when I tried doing something similar.

      Comment


      • #4
        Hi Clyde, Apologies for yet another question! I am looking to store some percentile values in a matrix, since I will need these values to do some mean squared differences calculations. I have attempted to try this out for myself, following the format you kindly provided, but I am getting the error r(3000) (essentially mata does not understand what I am asking it to do). Here is the code where I am trying to generate a matrix to store the 50th percentile values:

        Code:
        clear
        
        local mc = 1000
        set seed 368
        set obs `mc'
        gen data_n = .
        gen data_store_x = .
        gen data_store_cons = .
        gen data_store_std_err = .
        
        mata: beta_10 = J(1000,1,.)
        mata: beta_100 = J(1000,1,.)
        mata: beta_1000 = J(1000,1,.)
        mata: beta_10000 = J(1000,1,.)
        
        mata: std_err_10 = J(1000,1,.)
        mata: std_err_100 = J(1000,1,.)
        mata: std_err_1000 = J(1000,1,.)
        mata: std_err_10000 = J(1000,1,.)
        
        foreach n of numlist 10 100 1000 10000 {
            quietly {
                forvalues i = 1(1) `mc' {
                    if floor((`i'-1)/100) == (`i' -1)/100 {
                        noisily display "Working on `i' out of `mc' at $S_TIME"
                    }
                    preserve
        
                    clear
        
                  set obs `n'
        
                    gen x = rnormal() *3 + 6
        
                    gen e = rnormal() - 0.5
        
                    gen y = 3 + 4*x + e
                    
                    reg y x, vce(robust)
        
                    local xcoef = _b[x]
                    local const = _b[_cons]
                    local std_err = _se[x]
                    local 50percentile = r(p50)
                    mata: 50percentile_`n'[`i',1] = `50percentile'
                    mata: beta_`n'[`i',1] = `xcoef'
                    mata: std_err_`n'[`i', 1] = `std_err'
                    
                    restore
                 
                    
                    replace data_n = `n' in `i'
                    replace data_store_x = `xcoef' in `i'
                    replace data_store_cons = `const' in `i'
                    replace data_store_std_err = `std_err' in `i'
                        }
                }
            summ  data_n data_store_x data_store_std_err data_store_cons 50percentile, detail
        Do you have any insights as to why this may be happening?

        Comment

        Working...
        X