loopsstatacoefplot

stata plotting coefficients from loop regression


I am a beginner in Stata and I am trying to create a loop regression, store the coefficients of the DVs and then plot these. Does this code make sense?

forvalues i = 1/100 {
regress y x1 x2 x3 if ID==`i'
matrix b1 = e(x1) 
matrix b2 = e(x2) 
matrix b3 = e(x3) 
}

I am using coefplot right after and it just does not work. Any help will be very highly appreciated.


Solution

  • Does this code make sense? As you say, it does not work.

    I see three bugs.

    1. After each regression, the coefficients are not stored in e(x1) and so on. Such references are not illegal, but they just return missing values.

    2. Similarly a command like

      matrix b1 = e(x1)

    just creates a 1 x 1 matrix with a single missing value.

    1. Each time around the loop you just overwrite the previous matrices. Nothing in the code accumulates results, even if #1 and #2 were what you want.

    Hence, a natural question is: where did this code come from?

    There are several ways to get coefficients stored from say 100 regressions. See for example statsby and the community-contributed rangestat (SSC).