Announcement

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

  • cross section regression analysis for each industry year

    Dear members,
    I am new to stata and I need to estimate a regression model for each industry and year for years from 2000 till 2016 and 42 industries, my variables are y, x1, x2 ,x3, industry and year, I need that the regression is estimated if there is a minimum of 15 observations. I used this code but it returned nothing but a new variable in the original data set for the number of observations (nobs),

    Code:
     bysort year industry: gen nobs = _N
    forval y=2000/2016 {
          forval  i= 1/42 {
            di "year = `y' and industry = `i'"
          reg y x1 x2 x3 if  Industry== `i' & year==`y' & nobs>15
    }
    Thanks,
    Your help is much appreciated

  • #2
    Well, Stata did exactly what you asked it to. -regress- does not store results in your data set: it displays them in the Results window (which, I imagine, you saw go scrolling before your eyes). It also stores results in e() and r(), and creates two matrices, _b[] and _se[] that contain the coefficients and their standard errors, respectively. (But in a loop like this, each regression will overwrite the results of the previous ones.)

    So if you want the results to be placed in your data set, you have to add code to the loop that will grab the particular results you are interested in and place them in new variables. You don't say what results you are interested in storing, so it's not possible to give you more specific advice.

    Also, there is a simpler way to do this without writing loops, and it has the additional advantage of being much faster if you have a large data set. Let's say you want the coefficients of x1, x2, x3, their standard errors, and the number of observations. Then you could do this:

    Code:
    capture program drop one_regression
    program define one_regression
            regress y x1 x2 x3
            if e(N) > 15 {
                gen nobs = e(N)
                forvalues i = 1/3 {
                    gen b_x`i' = _b[x`i']
                    gen se_x`i' = _se[x`i']
                }
            }
            exit
    end
    
    runby one_regression, by(industry year) status
    You can modify the code inside program one_regression to grab whatever particular results you actually need. You just need to know where to find them in e() or r(). See the PDF documentation for -regress- that is part of your Stata installation for details.

    Notes: Code not tested. Beware of typos, etc. The program -runby- is written by Robert Picard and me, and is available from SSC.

    Comment


    • #3
      Thank you very much for your time and support. The problem is that the code that I provided in the question did not give me anything in the results window not even an error message.

      Comment


      • #4
        I cannot explain why the code in #1 would simply produce no output in the Results window at all.

        There is an inconsistency in that code. In the first line, you have a variable called industry, and in the -reg- command you refer to it as Industry. Stata variable names are case sensitive. Now unless you have two variables, one called industry and the other called Industry, one of those references is an error. And whichever is an error should have produced an error message. If you really do have two such variables, then the code should have produced some output as well. (The output might be garbage, but it should have been there.)

        Every combination of Industry and year should have produced the message "year == # and industry = #" with # replaced by the corresponding numbers. In addition, each combination should have produced either regression output or an error message saying that there were no observations, or insufficient observations for the regression.

        In the future, when you want troubleshooting on code, you should copy/paste both the code concerned and the output Stata gave you directly from the Results window (or your log file) into the Forum. I can only assume that the code you are showing is in some way different from what you actually ran, or that there was some output that you are not describing accurately. But I simply cannot find any circumstance that would lead the code you showed to produce no output at all.

        Comment


        • #5
          Thank you sir very much for the code you provided. it worked fine, sorry for my late reply as Friday and Saturday are days off in Egypt and I have Stata installed at the University campus, so I just tried it now.

          Comment


          • #6

            #2
            HTML Code:
            capture program drop one_regression  program define one_regression         regress y x1 x2 x3         if e(N) > 15 {             gen nobs = e(N)             forvalues i = 1/3 {                 gen b_x`i' = _b[x`i']                 gen se_x`i' = _se[x`i']             }         }         exit end  runby one_regression, by(industry year) status
            Hi Clyde,
            With regard to my regress programme in #2, I was wondering if there is a way to insert/modify the command that would give me the intercept (beta_0) besides the betas of x1, x2 and x3. I appreciate your help.
            Best wises,
            D Vikas

            Comment


            • #7
              Easy.
              Code:
              capture program drop one_regression
              program define one_regression
                      regress y x1 x2 x3
                      if e(N) > 15 {
                          gen nobs = e(N)
                          forvalues i = 1/3 {
                              gen b_x`i' = _b[x`i']
                              gen se_x`i' = _se[x`i']
                          }
                          gen b_cons = _b[_cons]
                          gen se_cons = _se[_cons]
                      }
                      exit
              end
              
              runby one_regression, by(industry year) status

              Comment


              • #8
                Many thanks Clyde,
                This is very helpful.
                Best wishes,
                D Vikas

                Comment

                Working...
                X