Announcement

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

  • Forvalue loop in self-design bootstrap

    I am a beginning learner of Mata, and I came across a feedback of "break" when running the code below:
    Code:
    forvalues i = 1/20 {
    
    clear all
    set obs 100
      
    program define myboot, rclass
        preserve
        bsample
        gen new7`i'id=id
        xtset new7`i'id qdate
        xtreg F`i'.y $bplinshock $bplinxlist, fe robust
        scalar bylinh`i'=_b[$bplinshock]
        gen new8`i'id=id
        xtset new8`i'id qdate
        xtreg F`i'.g $bplinshock $bplinxlist, fe robust
        scalar bglinh`i' = _b[$bplinshock]
        scalar sumliny = bylinh`i' + sumliny
        scalar sumling = bglinh`i' + sumling
        return scalar multlinh`i'=sumliny/sumling
        restore
    end
    simulate multlinh`i'=r(multlinh`i'), reps(100): myboot
    sum
    bstat, stat(multlinh`i'_obs) n(100)
    estat bootstrap, all
      }
    stata 16 SE

  • #2
    This isn't really a Mata question: no Mata is used in your code, and this post would have seen a larger, more appropriate audience in the General Forum rather than the Mata Forum.

    With that said, you cannot define your program within the loop. You will have to move it outside the loop and pass `i' in as an argument. Perhaps something like the following untested code.
    Code:
    capture program drop myboot
    program define myboot, rclass
        args i
        preserve
        bsample
        gen new7`i'id=id
        xtset new7`i'id qdate
        xtreg F`i'.y $bplinshock $bplinxlist, fe robust
        scalar bylinh`i'=\_b[$bplinshock]
        gen new8`i'id=id
        xtset new8`i'id qdate
        xtreg F`i'.g $bplinshock $bplinxlist, fe robust
        scalar bglinh`i' = \_b[$bplinshock]
        scalar sumliny = bylinh`i' + sumliny
        scalar sumling = bglinh`i' + sumling
        return scalar multlinh`i'=sumliny/sumling
        restore
    end
    
    forvalues i = 1/20 {
    
    clear all
    set obs 100
      
    simulate multlinh`i'=r(multlinh`i'), reps(100): myboot `i'
    sum
    bstat, stat(multlinh`i'\_obs) n(100)
    estat bootstrap, all
      }

    Comment


    • #3
      Really appreciate your help!

      Comment


      • #4
        Hello! I am bothering you again. When I run the improved code in stata 16SE, I receive the following error message:
        Code:
        capture program drop myboot
        program define myboot, rclass
            args i
            preserve
            bsample
            gen new7`i'id=id
            xtset new7`i'id qdate
            xtreg F`i'.y $bplinshock $bplinxlist, fe robust
            scalar bylinh`i'=\_b[$bplinshock]
            gen new8`i'id=id
            xtset new8`i'id qdate
            xtreg F`i'.g $bplinshock $bplinxlist, fe robust
            scalar bglinh`i' = \_b[$bplinshock]
            scalar sumliny = bylinh`i' + sumliny
            scalar sumling = bglinh`i' + sumling
            return scalar multlinh`i'=sumliny/sumling
            restore
        end
        
        forvalues i = 1/20 {
        
        clear all
        set obs 100
          
        simulate multlinh`i'=r(multlinh`i'), reps(100): myboot `i'
        sum
        bstat, stat(multlinh`i'\_obs) n(100)
        estat bootstrap, all
          }
        Code:
        myboot command not found
        r(111);

        Comment


        • #5
          The
          Code:
          clear all
          command drops all programs. Move it before the program definition.

          Comment


          • #6
            Thank you again for your valuable help!

            Comment

            Working...
            X