Announcement

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

  • Saving coefficients and residuals of cross sectional regressions based on industry and year

    Hi forum members,

    I was searching for a code to run regression based on industry (SIC) and year, and save residuals and coefficients of these regression as variables to be used in next level analysis.
    Plus I want the regression to run if at least 20 observations are there per each SIC-year
    I found a useful code from the statalist,quoted below
    HTML Code:
    levelsof SIC, local(codes)  
    foreach code in `codes' {          
    count if SIC == `code'        
     if r(N) >= 20 {                  
    reg DV IV1 if SIC == `code'        
    predict res_`code' if SIC == `code', residuals          
        }  
    }
    But it generated residuals for each SIC. But I wanted coefficients and residuals for each SIC-year (2000-2014).
    I would be really grateful if someone could help me with that, I have around 50 SICs spanning over 10 years (Each SIC will have many firms), approximately need around 50*10 sic-year regressions and generate residual and coefficients as separate variables.
    Last edited by Priyesh VP; 21 Jun 2017, 01:42.

  • #2
    Seems to me you need an outer loop over years.
    Code:
    forvalues y=2000/2014 {
    levelsof SIC if year==`y', local(codes)  
    foreach code in `codes' {          
    count if SIC == `code' & year==`y'       
     if r(N) >= 20 {                  
    reg DV IV1 if SIC == `code' & year==`y'      
    predict res_`code'_`y' if SIC == `code' & year==`y', residuals          
        }  
    }
    }

    Comment


    • #3
      Thanks Lisowski,
      But the above code generated residuals only for the year 2000 for different SICs. Could you please help me to sort it out?? Kindly give a code to generate coefficients also since I want both residuals and coefficients to be used in subsequent analysis

      Comment


      • #4
        So on doing a little research, it turns out that this topic is a restatement of an earlier topic you posted.

        https://www.statalist.org/forums/for...r-and-industry

        I have posted a response there.

        Comment

        Working...
        X