Announcement

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

  • #16
    I may have found the problem. In any case, the following code is wrong:
    Code:
     loc regif "if country != "`c'""
     loc ex_lab "Excluding, "`c'","
    You cannot nest quoted material, "`c'" inside quoted material "if country != ...". When you try to do that, the result, when doing the non-aggregated iterations of your loops comes out to be something like -reghdfe ... if country == CAN"-, which is a syntax error because the " following the country code is unmatched. This has to do with the way quotes are handled in macros, but, more fundamentally, there is no context where you can correctly nest quotes inside quotes. The solution is simple, just remove the outer quotes, while preserving those around `c'. So:
    Code:
     loc regif if country != "`c'"
     loc ex_lab Excluding, "`c'",
    Notice that this problem only arises in the non-aggregated analyses, which may indeed be why only your column 1 analysis survives. So, if there is nothing else wrong, this will likely resolve your problem.

    A couple of thoughts:
    1. In general, it is a bad idea to wrap the definition of a macro in quotes. They are usually not necessary, and, as you can see, they can sometimes mess things up badly. There are circumstances where you have to wrap the definition in quotes, and in that case, to be on the safe side you should always use Stata's compound double quotes, `" ... "', not ordinary quotes, " ... " for that purpose.
    2. If I am correctly understanding your code, these quote problems in the non-aggregated iterations should also have resulted in the -reghdfe- commands not being able to execute, and given the -capture- structure you used, you mus have gotten many "Unexpected regression error: ..." messages. Mentioning that would have been helpful in resolving this sooner. More broadly: -capture- structures are inherently dangerous because they suppress error notification. The structure you used that checks the value of c(rc) and branches to notify you of the unexpected errors mitigates this problem, but only if you pay attention to those error messages. If your code is correct and your data conforms to all necessary conditions for the code to work with it, you should not get any of those "Unexpected regression error" messages. Their presence signals that something important is wrong, and you should focus on fixing that before worrying that your output doesn't look right. Of course your output won't look right when the code itself is telling you that something is amiss! Error messages are your friends. Ignore them at your peril.

    Comment


    • #17
      Clyde Schechter thanks for your suggestions and help, I appreciate it a lot!
      Maybe I wasn't clear about my issue.. The main issue here is from aggregated side, not on non-aggregated side.
      It's supposed to be 4 regressions for aggregated version (columns 1-4) + 4*n non-aggregated version (from columns 5 until the end) in total.
      But only the column 4 is survived in aggregated version and the rest of non-aggregated version is all good, in other words, columns 1-3 is gone for aggregated version.
      And I still don;t understand why this issue arises and don;t really have any idea where to fix them..

      Comment


      • #18
        OK, I've found some other errors in the code, including one which would cause there to be only one output for the aggregated case.
        Code:
        clear
        local folder folderdirectory 
        encode country, gen(cty)  
        loc job var1 var2 var3 var4 
        loc analysislevel 0        // 1 if one-by-one (country); 0 if excluding one country
        
        loc iteration 1
        
        levelsof country, local(countries)
        
        
        foreach var of local job {
            local estimates list
            
            foreach c in "" `countries' {
        
            * Excel file replace/append option
            if `iteration' == 1 local replace_op replace 
            if `iteration' > 1  local replace_op append
        
            * Regression versions: Aggregate + 0/1
            if `analysislevel' {     // 1; one-by-one
                loc regif "if country =="`c'""
                loc ex_lab 
                loc cn `c'
            }
            else {                    // 0; excluding
                loc regif "if country != "`c'""
                loc ex_lab "Excluding, "`c'","
                loc cn `c'
            }
            
            if missing("`c'")  {        // Aggregate
                loc regif 
                loc ex_lab 
                loc cn Aggregate
            }
            
            noi di _n " =================       Country: `cn'         =================== "
        
        
                
                * Basic setup for regression
                loc model_name "5-year `var'"
                loc FElbl13     i.cty#i.year i.cty#i.ind_a38
                loc FElbl24     i.cty#i.ind_a38#i.year
                loc FE13        "C-I C-Y"
                loc FE24         "C-I-Y"
                loc cl_CI        i.cty#i.ind_a38
        
                * Regression (Columns 1 to 4)
                forval col = 1/4 {
                    
                    * Setting RHS for columns 1&2 // 3&4
                    if inlist(`col', 1, 2)    ///
                       local xvars ib3.p_LogLP_VA LogL_av     // Columns 1&2
                    else                     ///
                       local xvars F1LogLP_VA_av ib3.p_LogLP_VA `var'_1L LogL_av // Columns 3&4
                
                    * Setting FE/labels for columns 1&3 // 2&4
                    if inlist(`col', 1, 3) {
                       local fe `FElbl13'   
                       local fe_table `FE13'
                    }
                    else { 
                       local fe `FElbl24'  // THIS COMMAND ORIGINAL HAD A TYPO: local fe `FElbl124'
                       local fe_table `FE24'
                    }
                     
                    * REGRESSION
                    reghdfe `var'_5L `xvars' `regif' [aw=weightvar], a(`fe') vce(cluster `cl_CI')
                    if c(rc) == 0 {
                        estimates store `var'_`cn'_`col'
                        
                        levelsof country if e(sample), clean local(countrylist)
                        local n_countries = `r(r)'
                        levelsof ind_a38 if e(sample)
                        local n_inds = `r(r)'
                        
                        local estimates_list `estimates_list' `var'_`cn'_`col' // PROVIDES FOR AGGREGATE IN NAME AND DISTINGUISHES 4 COLUMNS
        
                        * Export to excel file --MOVED TO FOLLOW LOOPS
                   }
                    else if !inlist(c(rc), 2000, 2001) {
                        display as error `"Unexpected regression error: var = `var', country = `cn', col = `col'"'
                    }
                
                }
            
            } // end of country loop
            
            * Export to excel file THIS COMMAND MUST BE MODIFIED TO EXPORT ALL OF THE ESTIMATES
            * IN LOCAL MACRO estimates_list
            //  INSERT MODIFIED outreg2 COMMAND HERE ; IT MUST REFER TO estimates_list   
            local ++iteration
           
            
        } // end of job loop
        There are two key changes, which appear in several places. First, the name of the estimates put in the list is built from `cn', not `c', so that the aggregate iterations have explicit identification as such. Probably more important, _`col' has been added to the name of the estimates so that the four different columns have distinct names and do not clobber each other. And most important of all, the various estimates are now stored after the regression, so they are not going to get lost.

        Unrelated to the symptoms you were having with the output, there was previously a typo in the code that resulted in there being no fixed effects specified in the -a()- option of -reghdfe- for columns 2 and 4.

        I hope this will finally work out for you. I think it comes down to finding a way to get -outreg2- to deal with the list of estimates.

        Comment


        • #19
          Clyde Schechter unfortunately still columns 1-3 are missing from the output even after the modification from #18..
          maybe it's really on outreg2 part (that you suggested to modify with estimates_list) but I'm afraid I didn't really get the point ;(

          Code:
          cap encode country, gen(cty)
          
          loc job var1 var2 var3 var4
          
          loc analysislevel 0        // 1 if one-by-one (country); 0 if excluding one country
          
          loc iteration 1
          
          levelsof country, local(countries)
          
          foreach var of local job {
              
              local estimates list
              
              foreach c in "" `countries' {
          
                  * Excel file replace/append option
                  if `iteration' == 1 local replace_op replace 
                  if `iteration' > 1  local replace_op append
          
                  * Regression versions: Aggregate + 0/1
                  if `analysislevel' {     // 1; one-by-one
                      loc regif if country =="`c'"
                      loc ex_lab 
                      loc cn `c'
                  }
                  else {                    // 0; excluding
                      loc regif if country != "`c'"
                      loc ex_lab Excluding, "`c'",
                      loc cn `c'
                  }
                  
                  if missing("`c'")  {        // Aggregate
                      loc regif 
                      loc ex_lab 
                      loc cn Aggregate
                  }
                  
                  noi di _n " =================       Country: `cn'         =================== "
                      
                  * Basic setup for regression
                  loc LHS         `var'_5L
                  loc model_name "`var'"
                  loc FElbl13     i.cty#i.year i.cty#i.ind_a38
                  loc FElbl24        i.cty#i.ind_a38#i.year
                  loc FE13        "C-I C-Y"
                  loc FE24         "C-I-Y"
                  loc cl_CI        i.cty#i.ind_a38
          
                  * Regression (Columns 1 to 4)
                  forval col = 1/4 {
                          
                      * Setting RHS for columns 1&2 // 3&4
                      if inlist(`col', 1, 2)        /// Columns 1&2
                         local xvars         ib3.p_LogLP_VA LogL_av      
                      else                         /// Columns 3&4
                         local xvars         F1LogLP_VA_av ib3.p_LogLP_VA `var'_1L LogL_av 
                  
                      * Setting FE/labels for columns 1&3 // 2&4
                      if inlist(`col', 1, 3) {
                         local fe         `FElbl13'   
                         local fe_table     `FE13'
                      }
                      else {                    
                         local fe         `FElbl24'  
                         local fe_table     `FE24'
                      }
                           
                      * REGRESSION
                      capture reghdfe `LHS' `xvars' `regif' [aw=weightvar], a(`fe') vce(cluster `cl_CI')
                          
                      if c(rc) == 0 {
                          
                          estimates store `var'_`cn'_`col'
                          
                          levelsof country if e(sample), clean local(countrylist)
                          local n_countries = `r(r)'
                          levelsof ind_a38 if e(sample)
                          local n_inds = `r(r)'
          
                          local estimates_list `estimates_list' `var'_`cn'_`col'
                          
                          * Export to excel file
                          outreg2 using "${folder}/Baseline/`analysislevel'_`var'.xls", `replace_op' ctitle(`model_name') label ///    
                                  addtext(Fixed effects, `fe_table', Country List, `countrylist', `ex_lab' Countries, `n_countries', Industries, `n_inds')
                      }
                      else if !inlist(c(rc), 2000, 2001) {
                          display as error `"Unexpected regression error: var = `var', country = `cn', col = `col'"'
                      }
                      
                  }  // end of forval col loop
                  
              } // end of country loop
              
              local ++iteration
              
          } // end of job loop

          Comment

          Working...
          X