Announcement

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

  • How to store coefficients of the regression within a loop?

    Dear Stata users,

    Sorry for posting several posts related to one issue. However, I can not properly figure out the solution. What I need is the following. I need to run the pooled ols model below within the period 1995-2015. Then, I need to store the values of fixed effect (firmID – gvkey1) within each year using rolling 3-year window. For instance, to obtain the firm-specific “gvkey1” coefficient for year 2005, I run equation below pooling firm-year observations for 2005, 2004 and 2003. Here is my regression within the loop, but I don’t know how to store the coefficients.
    Code:
    sum fyear
    scalar minyr=r(min)
    scalar maxyr=r(max)
    local m=minyr
    local n=maxyr
    forvalues i=`m'(1)`n'{
    qui reg bm_w i.gvkey1 D b1D b2D b3D b4D b5D b6D cum_ret_w b1cum_ret_w b2cum_ret_w b3cum_ret_w b4cum_ret_w b5cum_ret_w b6cum_ret_w DR_w b1DR_w b2DR_w b3DR_w b4DR_w b5DR_w b6DR_w i.fyear if inrange(fyear, fyear==`i-1', fyear==`i+1'), vce(cluster gvkey1)
    di `i'
    }
    I tried “matrix gvkey_coeff=e(b)'” but it does not work within the loop. Please, advise me this issue.

    Best regards,
    Alberto

  • #2
    It is not clear what you mean by "it does not work within the loop". If your problem is that the matrix is overwritten with each iteration of the loop, you have to change the name of the matrix in the loop.
    Code:
    matrix gvkey_coeff`i' = e(b)
    Example:
    Code:
    sysuse auto, clear
    
    forval i = 0/1 {
      regress mpg weight if foreign == `i'
      matrix coeff`i' = e(b)
    }
    
    forval i = 0/1 {
      matrix list coeff`i'
    }

    Comment


    • #3
      Thank you very much for your response! Yes, I tried as you did in you example. However, it does not work in that way. The code produces a list of coefficients that do not correspond to gvkey1(firm)-year observation. However, what I need is to create a new variable that will contain coefficient of "i.gvkey" each year. Here is the code that I use. Please, let me know if you have any suggestions how to improve it:

      Code:
      sum fyear
      scalar minyr=r(min)
      scalar maxyr=r(max)
      local m=minyr
      local n=maxyr
      
      
       forvalues i=`m'(1)`n'{
                  qui xi: reg bm_w i.gvkey1 D b1D b2D b3D b4D b5D b6D cum_ret_w b1cum_ret_w b2cum_ret_w b3cum_ret_w b4cum_ret_w b5cum_ret_w b6cum_ret_w DR_w b1DR_w b2DR_w b3DR_w b4DR_w b5DR_w b6DR_w i.fyear if inrange(fyear,`i-1',`i+1'), vce(cluster gvkey1) 
                  matrix gvkey_coeff`i' = e(b)' 
                  svmat gvkey_coeff`i'
                  di `i'
      }

      Comment

      Working...
      X