Announcement

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

  • Use eststo for only subset of models in a for loop

    Hello,

    I need to run many regressions and store estimates to be plotted in graphs - specifically 3 x 12 x 14 regressions, which is more than what eststo can handle. For this reason, I stored my results using postfile.

    Some sample code for that is below (I omit some intermediate parts)
    Code:
    clear 
    forvalues spec = 1/3 {
        
        clear 
        postutil clear
        use "data", replace
    
        postfile results ///
            CODE HERE
    
        forvalues j = 1999/2010{
            forvalues i = 1/14 {
                local a : word `i' of $variables
                
    
                     reghdfe dlog_`a'_`j' var1 var2 if ${spec`spec'}, vce(robust)
                    
                local coefficient_exp = _b[var1]
                local coefficient_imp = _b[var2]
                local std_error_exp = _se[var1]
                local std_error_imp = _se[var2]
    
                
                post results CODE HERE
                }
            }
        postclose results
    }
    However, I also need regression tables for some of these models. Specifically, I need tables for all models with j = 2007 and j = 2010.

    Is there a way for me to use eststo and esttab for only this subset of models in this loop?

    For now I was just estimating those again separately, but storing with eststo and then using esttab. Is there some more elegant way of doing so?

    Thanks

  • #2
    Try this:
    Code:
    clear
    forvalues spec = 1/3 {
        
        clear
        postutil clear
        use "data", replace
    
        postfile results ///
            CODE HERE
    
        forvalues j = 1999/2010{
            local eststo
            if inlist(`j', 2007, 2010) {
                local eststo eststo:
            }
            forvalues i = 1/14 {
                local a : word `i' of $variables
                
                `eststo' reghdfe dlog_`a'_`j' var1 var2 if ${spec`spec'}, vce(robust)
                local coefficient_exp = _b[var1]
                local coefficient_imp = _b[var2]
                local std_error_exp = _se[var1]
                local std_error_imp = _se[var2]
    
                
                post results CODE HERE
                }
            }
        postclose results
    }
    should do it.

    Comment


    • #3
      Clyde Schechter thank you! Would it be possible to name the models that are stored?

      As in

      Code:
      eststo model`i'_`j'_`spec': reghdfe ...
      I don't know if the name would go in the local eststo eststo part, or on the regression itself

      Comment


      • #4
        To make this work, the code creating local macro eststo has to be moved inside the -forvalues i = 1/14- loop so that the correct value of `i' will be referenced in the -eststo- prefix. So:
        Code:
        clear
        forvalues spec = 1/3 {
            
            clear
            postutil clear
            use "data", replace
        
            postfile results ///
                CODE HERE
        
            forvalues j = 1999/2010{
                forvalues i = 1/14 {
                    local a : word `i' of $variables
                    local eststo
                    if inlist(`j', 2007, 2010) {
                        local eststo eststo model`i'_`j'_`spec':
                    }
                    `eststo' reghdfe dlog_`a'_`j' var1 var2 if ${spec`spec'}, vce(robust)
                    local coefficient_exp = _b[var1]
                    local coefficient_imp = _b[var2]
                    local std_error_exp = _se[var1]
                    local std_error_imp = _se[var2]
        
                    
                    post results CODE HERE
                    }
                }
            postclose results
        }

        Comment

        Working...
        X