Announcement

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

  • Error when trying to solve the "generated regressor problem"

    Dear all,

    I'm trying to correct the standard errors for the generated regressor problem for a series of local projections. I looked at similar posts and apply the same procedure, however when I run the following code I get an "end of do-file" --Break-- r(1), any idea on how I can solve this issue?

    Code:
    forvalues i = 1/16 {
    
    capture program drop one_rep
    program define one_rep
    
    ** First stage
    ivreg2 f`i'g instrument1 instrument2 L(1/4).instrument1 L(1/4).instrument2, robust bw(auto)
    capture drop shock`i'
    predict double shock`i'
    
    ** Second stage 
    ivreg2 f`i'var`dep' shock`i' L(1/4).shock`i', robust bw(auto)
    
    exit
    end }
    Thanks!

  • #2
    You seem to be defining a program within the loop, but there is no reason for a program, and you never run it as a command, unless you have failed to show us something. So with that said, the following code would seem to accomplish what your code attempted.
    Code:
    forvalues i = 1/16 {
    
    ** First stage
    ivreg2 f`i'g instrument1 instrument2 L(1/4).instrument1 L(1/4).instrument2, robust bw(auto)
    capture drop shock`i'
    predict double shock`i'
    
    ** Second stage
    ivreg2 f`i'var`dep' shock`i' L(1/4).shock`i', robust bw(auto)
    
    }
    But I'm thinking perhaps you are referring back to your previous topic where you were asking for advice on bootstrapping, but in your code you neglected to include the bootstrap command. In that case, you will need the program definition. To understand source of the error message you received, you need to know that if you want
    • a Stata program, whose definition will be terminated by end
    to run within
    • a Stata block surrounded by braces {}, such as a loop
    you need to
    • define your program outside the brace-enclosed block, passing needed local variables to it as arguments
    • call program within the brace-enclosed block
    Even though your "end" is meant to terminate the program definition, at the time Stata is parsing the loop, the fact that the "end" belongs to the program definition and not to the loop is not recognized, and causes Stata to react incorrectly.

    So I'd imagine that perhaps what you need is something like the following.
    Code:
    capture program drop one_rep
    program define one_rep
        args i
        
        ** First stage
        ivreg2 f`i'g instrument1 instrument2 L(1/4).instrument1 L(1/4).instrument2, robust bw(auto)
        capture drop shock`i'
        predict double shock`i'
    
        ** Second stage
        ivreg2 f`i'var`dep' shock`i' L(1/4).shock`i', robust bw(auto)
    
        exit
    end
    
    forvalues i = 1/16 {
        bootstrap, reps(50): one_rep `i'
    }
    Last edited by William Lisowski; 01 Mar 2022, 11:05.

    Comment


    • #3
      Thanks a lot William, you are indeed right. I forgot to include the bootstrap. Anyway, I already figured it out how to bootstrap both stages (your response is highly appreciated though). This is my method, that should do what I need:

      Code:
      capture program drop one_rep
      program define one_rep
      forvalues i = 1/16{
      
      ** First-stage
      ivreg2 f`i'g instrument1 instrument2 L(1/4).instrument1 L(1/4).instrument2, robust bw(auto);
          capture drop shock`i';
          predict double shock`i';
      
      ** Second-stage 
      ivreg2 f`i'var`dep' shock`i' L(1/4).shock`i', robust bw(auto)
      
      }
      
      exit
      end
      
      bootstrap, reps(200): one_rep
      Last edited by Alessandro Franconi; 01 Mar 2022, 11:44.

      Comment

      Working...
      X