Announcement

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

  • Appending to a matrix in a loop

    Hi,

    I am looping over several independent variables, and I am trying to end up with a matrix that combines results for each regression. With one empty row separating results.
    This is the code I have written so far, which overwrites instead of appends:

    Code:
    local indicators employment degree
    local invars X Y
    
    foreach depvar of local indicators{
        foreach invar of local invars{
    
                matrix result_`invar'=J(1, 3, .)            
    
                svy: regress `depvar' dummy if Year==2010 & invars==`invar'
                 matrix temp1_`invar'`=r(table)
    
                matselrc temp1_`invar' temp2_`invar', c(1) r(1 2 4)
                                            
                matrix temp2_`invar' = temp2_`invar''
                    
    
                matrix result_`invar'= result_`invar'\temp2_`invar'
    
                }
    }
    So basically I would like to end up with a matrix that has 3 rows (one for X, an empty one, and one for Y). And three columns, where the coefficient, standard error, and pvalue are printed.

    And I know things like outreg or esttab would be easier, but unfortunately I need my results in matrix format.

    Can anyone help me with this?

    Thank you.
    Last edited by Anja Heimann; 02 Nov 2020, 07:09.

  • #2
    I figure to add an empty row, I could write something like this?
    Code:
     matrix empty=J(1,3,.)
     matrix result= result_`invar'\empty
     matrix result=result\result_`invar'

    Comment


    • #3
      The results are overwritten because you loop over the dependent variables. You change your code to something like this
      Code:
      foreach depvar of local indicators{
          foreach invar of local invars{
      
                  matrix result_`invar'_`depvar'=J(1, 3, .)            
      
                  svy: regress `depvar' dummy if Year==2010 & invars==`invar'
                   matrix temp1_`invar'_`depvar'=r(table)
      
                  matselrc temp1_`invar'_`depvar' temp2_`invar'_`depvar', c(1) r(1 2 4)
                                              
                  matrix temp2_`invar' = temp2_`invar'_`depvar''
                      
      
                  matrix result_`invar'= result_`invar'_`depvar'\temp2_`invar'_`depvar'
      
                  }
      }

      Comment


      • #4
        Thanks Sven-Kristjan, that solved my problem.

        Comment

        Working...
        X