Announcement

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

  • Programme in a loop

    Dear All,

    I need to estimate an IV model with Heckman correction with bootstrapped standard errors. Since I need to estimate more models, including augmented set of regressors, I was going to generate a loop. Specifically I have:

    Code:
    local heck1 ""
    local heck2 "$demo $school"
    local heck3 "$demo $school $traits"
    local heck4 "$demo $school $traits $par_edu $par_occ $par_sect"
    local heck5 "$demo $school $traits $par_edu $par_occ $par_sect $geo"
    
    forval i=1/5 {
        cap program drop myboot
        cap drop fitted imr
        program define myboot, eclass
        preserve
        probit credits_dummy $instruments heck`i' log_isee1
        predict fitted, xb
        gen imr = normalden(fitted) / normal(fitted)
        ivreg2 std_gpa (autonuni=$instruments) imr heck`i', cluster(cds) endog(autonuni) first
        matrix b=e(b)
        ereturn post b
        ereturn local cmd="bootstrap"
        restore
        end
        bootstrap _b, seed(12345) reps(500): myboot
        est store heck`i'
    }
    Stata stops at this point:

    Code:
    forval i=1/5 {
      2.         cap program drop myboot
      3.         cap drop fitted imr
      4.         program define myboot, eclass
      5.         preserve
      6.         probit credits_dummy $instruments heck`i' log_isee1
      7.         predict fitted, xb
      8.         gen imr = normalden(fitted) / normal(fitted)
      9.         ivreg2 std_gpa (autonuni=$instruments) imr heck`i', cluster(cds) en
    > dog(autonuni) first
     10.         matrix b=e(b)
     11.         ereturn post b
     12.         ereturn local cmd="bootstrap"
     13.         restore
     14.         end
    --Break--
    r(1);
    Outside the loop I do not have any problem in running the code. I cannot figure out what is going wrong.The reported error message is not informative. For sure I did not press any break. Any suggestion would be greatly appreciated.

    Thanks in advance,

    Dario
    Last edited by Dario Maimone Ansaldo Patti; 28 Mar 2023, 06:06.

  • #2
    It is the word "end" that causes your problem, it ends the loop. In short: you cannot define a program in a loop.

    The solution is to allow an option to your program myboot, so you won't need to redefine it within the loop:

    Code:
    local heck1 ""
    local heck2 "$demo $school"
    local heck3 "$demo $school $traits"
    local heck4 "$demo $school $traits $par_edu $par_occ $par_sect"
    local heck5 "$demo $school $traits $par_edu $par_occ $par_sect $geo"
    
    cap program drop myboot
    program define myboot, eclass
        syntax, i(integer)
        preserve
        probit credits_dummy $instruments heck`i' log_isee1
        predict fitted, xb
        gen imr = normalden(fitted) / normal(fitted)
        ivreg2 std_gpa (autonuni=$instruments) imr heck`i', cluster(cds) endog(autonuni) first
        matrix b=e(b)
        ereturn post b
        ereturn local cmd="bootstrap"
        restore
    end
    
    forval i=1/5 {
        bootstrap _b, seed(12345) reps(500): myboot, i(`i')
        est store heck`i'
    }
    Your extensive use of globals makes me very nervous, but that is a topic that is often enough discussed on this list.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Maarten Buis Thanks for your suggestion. Regarding the usage of macros,, they might be a bit annoying, although I cannot see any other way to simplify the code. Do you suggest to reduce them and possibly writing down the regressors in an extensive way?

      Comment


      • #4
        I have nothing against local macros, I do have something against global macros. They are a source of hard to find bugs.
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Maarten Buis Oh I see. I agree with you at some extent. In fact, I usually need to go back and forth to remember what is inside them. But. on the other hand, they avoid you to write down all the regressors several times. I am estimating about 30 different models and writing down the regressors every time is quite time consuming (I have about 30 regressors). Local are much easier to handle, because you immediately specify them and you know exactly what is going on.

          Comment


          • #6
            Originally posted by Dario Maimone Ansaldo Patti View Post
            But. on the other hand, they [I assume global macros, MLB] avoid you to write down all the regressors several times.
            You should obviously never write down the same thing multiple times. That is a bug waiting to happen (something like I changed those lines on four locations but forgot the fifth...). However, local macros don't require you to do that. In fact, they exist to prevent you from doing so. So I don't understand your point.
            ---------------------------------
            Maarten L. Buis
            University of Konstanz
            Department of history and sociology
            box 40
            78457 Konstanz
            Germany
            http://www.maartenbuis.nl
            ---------------------------------

            Comment


            • #7
              Maarten Buis Indeed, you are correct. I did not express correctly my thoughts, I am afraid. By the way, I tried the suggestion you gave me but apparently when I run....it replicates the first estimation 5 times:

              Code:
              First reported results:
              
              Bootstrap results                                        Number of obs = 4,102
                                                                       Replications  =   500
              
              ------------------------------------------------------------------------------
                           |   Observed   Bootstrap                         Normal-based
                           | coefficient  std. err.      z    P>|z|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                  autonuni |    .390592   .1398537     2.79   0.005     .1164837    .6647002
                       imr |   .0051392   .8083817     0.01   0.995     -1.57926    1.589538
                     _cons |  -.0042143   .0653293    -0.06   0.949    -.1322573    .1238287
              ------------------------------------------------------------------------------
              
              Second reported results:
              
              Bootstrap results                                        Number of obs = 4,102
                                                                       Replications  =   500
              
              ------------------------------------------------------------------------------
                           |   Observed   Bootstrap                         Normal-based
                           | coefficient  std. err.      z    P>|z|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                  autonuni |    .390592   .1398537     2.79   0.005     .1164837    .6647002
                       imr |   .0051392   .8083817     0.01   0.995     -1.57926    1.589538
                     _cons |  -.0042143   .0653293    -0.06   0.949    -.1322573    .1238287
              ------------------------------------------------------------------------------
              Am I missing something?

              Thanks again for your suggestions

              Dario

              Comment


              • #8
                The local macros heck1 till heck5 are not visible in myboot, so we need to pass the contents of those macros to myboot rather than the number.

                In addition, I used temporary variables instead of preserve and restore, and ensured that generated variables with intermediate results are doubles, which won't solve your problem, but is generally a good idea when dealing with intermediate results.

                Code:
                 local heck1 ""
                local heck2 "$demo $school"
                local heck3 "$demo $school $traits"
                local heck4 "$demo $school $traits $par_edu $par_occ $par_sect"
                local heck5 "$demo $school $traits $par_edu $par_occ $par_sect $geo"  
                
                cap program drop myboot
                
                program define myboot, eclass    
                    syntax [if] [in], heck(varlist)    
                    tempvar fitted imr      
                    probit credits_dummy $instruments `heck' log_isee1    
                    predict double `fitted', xb    
                    gen double `imr' = normalden(fitted) / normal(fitted)    
                    ivreg2 std_gpa (autonuni=$instruments) `imr' `heck', cluster(cds) endog(autonuni) first    
                    matrix b=e(b)    
                    ereturn post b    
                    ereturn local cmd="bootstrap"
                end  
                
                forval i=1/5 {    
                    bootstrap _b, seed(12345) reps(500): myboot, heck(`heck`i'')    
                    est store heck`i'
                }
                Last edited by Maarten Buis; 29 Mar 2023, 02:02.
                ---------------------------------
                Maarten L. Buis
                University of Konstanz
                Department of history and sociology
                box 40
                78457 Konstanz
                Germany
                http://www.maartenbuis.nl
                ---------------------------------

                Comment


                • #9
                  Maarten Buis Thanks a lot for your clear and detailed help.

                  Comment

                  Working...
                  X