Announcement

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

  • Skip an iteration within a loop

    Hai all,

    so I am performing bootstrap manually with a for loop. Basically what I am dong is to compute residuals from a first stage regression:
    Code:
        capture quietly xtreg y L.major_recalls_norm average_age_prodbyatc3 avg_prd_sq mean_agefirm_byatc mean_agefirm_squared hhi share_expired share_patented i.Year, fe vce(cluster newid)
        capture predict residuals, e
    and put them in a poisson fixed effects regression:
    Code:
    capture noisily quietly xtpoisson trials y residuals average_age_prodbyatc3 avg_prd_sq mean_agefirm_byatc mean_agefirm_squared hhi share_expired share_patented i.Year, fe vce(robust)
    Now sometimes poisson fixed effects does not converge and an error like the following appears:
    Code:
    cannot compute an improvement -- discontinuous region encountered
    which is my case at iteration 168. The code is:


    Code:
    set seed 1234
    set matsize 11000
    global filedata "DB_atc3.dta"
    global varx "y residuals average_age_prodbyatc3 avg_prd_sq mean_agefirm_byatc mean_agefirm_squared hhi share_expired share_patented"
    global resid "residuals"
    global filename "bootexcel.xlsx"
    global nrepl 300
    
    
    local nx=0
    set more off
    foreach var in $varx {
    local nx=`nx'+1 
    }
    global ncol= `nx' +2 
    
    mat coeff = J($nrepl ,$ncol,.)
    
    set more off
    forval i=1/$nrepl {
    use "$filedata", clear 
    rename tot_count trials
    *bysort idatc3 (Year): gen byte panelsize = _N
    bsample, cluster(idatc3) idcluster(newid)
    ***
        capture xtset newid Year 
        capture quietly xtreg y L.major_recalls_norm average_age_prodbyatc3 avg_prd_sq mean_agefirm_byatc mean_agefirm_squared hhi share_expired share_patented i.Year, fe vce(cluster newid)
        capture predict residuals, e
        capture noisily quietly xtpoisson trials y residuals average_age_prodbyatc3 avg_prd_sq mean_agefirm_byatc mean_agefirm_squared hhi share_expired share_patented i.Year, fe vce(robust)
            
    ***
    local conta=0
    set more off
    foreach var in $varx {
    local conta=`conta'+1 
    mat coeff[`i',`conta']=_b[`var']
    }
    mat coeff[`i',`conta'+1]=_b[$resid]
    
    di as text " Replication # `i'"
    mat coeff[`i',$ncol]=`i' 
    
    }
    What I would like to do is to ignore the error and keep looping. I wished I could do it with capture but it actually does not work. Is there a way to ignore iteration number 168 and carry on looping? Let's say I have 300 repetitions: the goal is to perform 299 repetitions ignoring the 168 (the one raising the error message)

    Thank in advance,

    Federico

  • #2
    see
    Code:
    help continue
    Code:
    forvalues i = 1/10 {
        
        capture assert `i' != 6
        
        if ( _rc ) {
            
            continue
        } 
        
        di "`i'"
    }

    Comment


    • #3
      Bjarte Aagnes many thanks. I will check!

      Comment


      • #4
        Bjarte Aagnes

        EDIT: So sorry. I checked the help of capture which is quire new to me. I modified the code as follows but it does not quite work:

        Code:
        set seed 1234
        forvalues i = 1/170 {
            use "/Users/federiconutarelli/Desktop/DB_atc3.dta", clear
            rename tot_count trials
            
            bsample, cluster(idatc3) idcluster(newid)
            
            capture assert `i' != 167 & `i' != 168
            
            if ( _rc ) {
                
                      continue
            } 
            
            xtset newid Year 
            quietly xtreg y L.major_recalls_norm average_age_prodbyatc3 avg_prd_sq mean_agefirm_byatc mean_agefirm_squared hhi share_expired share_patented i.Year, fe vce(cluster newid)
            predict residuals, e
            quietly xtpoisson trials y residuals average_age_prodbyatc3 avg_prd_sq mean_agefirm_byatc mean_agefirm_squared hhi share_expired share_patented i.Year, fe vce(robust)
            di "`i'"
        }
        The problem I guess is more structural. Indeed I should ignore the error:

        Code:
        cannot compute an improvement -- discontinuous region encountered
        Is there a way to ignore the error message and carry on the iteration ignoring it?

        Thanks again!
        Last edited by Federico Nutarelli; 09 Mar 2020, 14:27.

        Comment


        • #5

          capture the command of interest, or possible a block of commands
          Code:
          capture xtpoisson 
          
          if ( _rc ) {
              
              continue 
          }

          Comment


          • #6
            OH I see. Thanks a lot again!

            Comment


            • #7
              Hi Bjarte Aagnes , I faced the same problem, could you please help me. My code is like that:

              Code:
              levelsof id, local(levels) // id is company id
              foreach i of local levels{
              
              
              nlsur (l_gm_Y=ln({A})+({s}/(1-{s}))*(ln(({alph}*gm_K^(({s}-1)/{s}))+((1-{alph})*((exp({g}))*gm_L))^(({s}-1)/{s})))) (l_KY=ln({alph})+(({s}-1)/{s})*(ln(gm_Y/gm_K)-ln({A}))) (l_LY=ln(1-{alph})+(({s}-1)/{s})*(ln(gm_Y/gm_L)-ln({A})-{g})), initial(aa), if id==`i'
              
              
              replace coeff_A= _b[/A] if id==`i'
              replace coeff_s= _b[/s] if id==`i'
              replace coeff_alph= _b[/alph] if id==`i'
              replace coeff_g= _b[/g] if id==`i'
              
              }
              My panel data include about 280 firms. I am trying to fit my equation for each firm and to save results. However, Stata does not solve the minimization problem for some firms such as firm 6. I have received error code r(430) and my loop does not continue for the rest of firms. I want that my loop codes ignore this firm and continue for other firms even if there is no solution for some firms. How can I do that?

              Comment


              • #8
                Code:
                help capture

                Comment


                • #9
                  Another way through which you can ignore the firms for which there is non-convergence when saving results in a loop is to add

                  Code:
                    if e(converged) == 1 {
                    
                   local conta=`conta'+1  mat coeff[`i',`conta']=_b[`var']  
                  }
                  


                  after the code for your regression.

                  Comment

                  Working...
                  X