-1

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.

1 Answer 1

3

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.