Announcement

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

  • How to this program

    Hi all,

    I am trying to write a program for my own analysis to save time because I have to repeat similar analyses numerous times. Since I am pretty new to Stata programming, I have tried a simple one as the followings:
    Code:
    cap program drop example
    program  example
        args y x1 x2 x3
        di "`y' `x1' `x2' `x3'"
        
        qui reg `y' `x1' `x2' `x3', vce(robust)
            sum `y' if e(sample)
            sca m_y = r(mean)
            sca b = _b[`x1']
            sca se = _se[`x1']
            
            sca di m_y b se
    end
    
        sysuse auto, clear
        example price headroom mpg rep78
    That program works, but I now want to make it a bit more complicated. Suppose that I run two first stage, obtain first stage predicted values and then plug these predicted values into the second stage. Let y the outcome, z1 and z2 are instrumental variables, y1 and y2 are endogenous variables and x1 and x2 are exogenous variables. I want to write a program for the following regressions and store their associated estimates.
    Code:
        reg y1 z1 x1, robust
            predict yhat1
            
        reg y2 z2 x2, robust
            predict yhat2
        
        reg y yhat1 yhat2 x1 x2, robust
            sum y if e(sample)
            sca m_y = r(mean)
            sca b = _b[yhat1]
            sca se = _se[yhat2]
    Any help is much appreciated. Thank you.
    Last edited by Duong Le; 05 Jul 2022, 10:18.

  • #2
    I am sorry but any advice?

    Comment


    • #3
      It's not really clear from your post what you are asking or what the problem is. Could you be more specific?

      Comment


      • #4
        Use rclass and it should be able to do what you need:

        Code:
        sysuse auto, clear
        
        capture program drop example
        program define example, rclass
            args y1 z1 x1 y2 z2 x2 y
            
            reg `y1' `z1' `x1', robust
            capture drop yhat1
            predict yhat1
            
            reg `y2' `z2' `x2', robust
            capture drop yhat2
            predict yhat2
            
            reg `y' yhat1 yhat2 `x1' `x2', robust
            return scalar b = _b[yhat1]
            return scalar se = _se[yhat2]
            
            sum `y' if e(sample)
            return scalar m_y = r(mean)
        end
        
        example price mpg rep78 headroom trunk length turn
        return list

        Comment


        • #5
          Other than Ken's helpful code, I'd like to mention a theoretical issue. It seems that the OP plans to regress y on y1, y2, x1, and x2, where y1 and y2 are endogenous variables instrumented by z1 and z2. Then the first stage regressions should be

          Code:
          reg y1 z1 z2 x1 x2
          reg y2 z1 z2 x1 x2
          Another issue would be standard error adjustment. But I'll stop here, as Daniel has pointed out in #2, it's not quite clear what you were asking.

          Comment


          • #6
            Originally posted by Fei Wang View Post
            Other than Ken's helpful code, I'd like to mention a theoretical issue. It seems that the OP plans to regress y on y1, y2, x1, and x2, where y1 and y2 are endogenous variables instrumented by z1 and z2. Then the first stage regressions should be

            Code:
            reg y1 z1 z2 x1 x2
            reg y2 z1 z2 x1 x2
            Another issue would be standard error adjustment. But I'll stop here, as Daniel has pointed out in #2, it's not quite clear what you were asking.
            Thank you Ken for the code. It works well.

            Hi Fei, thank you for pointing out the theoretical issue. The code provided by Ken is what I wanted. I understand your code in #5, and a corresponding command in Stata could be: ivregress 2sls y (y1 y2 = z1 z2) x1 x2. In doing so, one would no need to adjust standard errors by "hands" because ivregress automatically adjusts standard errors for us. However, in my case, I need to run two separate first stages and then plug their predicted values into the second stage. Could you please guide me how to adjust standard error in my case?

            Thank you.

            Comment


            • #7
              Originally posted by Duong Le View Post

              Thank you Ken for the code. It works well.

              Hi Fei, thank you for pointing out the theoretical issue. The code provided by Ken is what I wanted. I understand your code in #5, and a corresponding command in Stata could be: ivregress 2sls y (y1 y2 = z1 z2) x1 x2. In doing so, one would no need to adjust standard errors by "hands" because ivregress automatically adjusts standard errors for us. However, in my case, I need to run two separate first stages and then plug their predicted values into the second stage. Could you please guide me how to adjust standard error in my case?

              Thank you.
              For your reference:
              https://www.statalist.org/forums/for...tandard-errors
              https://www.statalist.org/forums/for...-bootstrapping

              Comment


              • #8
                Thank Fei for the helpful references. I appreciate your help.

                Following instructions in the weblink you sent, I was able to write a programming for bootstrap, but I got the following error
                "insufficient observations to compute bootstrap standard errors no results will be saved"
                Interestingly, the above error disappears (that means the program I wrote works well) if I use a smaller sample size, say using dataex command to randomly draw 500 observations. However, the error occurs when I use the whole sample. I have tried to use option -nodrop- but it does not work. Do you have any advice for this issue?

                Note: I did not provide an example data here because the program works well with the smaller sample as I mentioned above.

                The program I wrote is as follows:
                Code:
                capture program drop example
                program define example, rclass    
                    reg y1 z1 x1, robust
                    capture drop yhat1
                    predict yhat1
                    
                    reg y2 z2 x2, robust
                    capture drop yhat2
                    predict yhat2
                    
                    reg y yhat1 yhat2 x1 x2, robust
                    return scalar y1 = _b[yhat1]
                    return scalar y2 = _b[yhat2]
                    
                end
                
                bootstrap y1coef = (r(y1)) y2coef = (r(y2)), seed(123) reps(50) nodrop: example

                Comment


                • #9
                  Duong, I didn't see problems in your code. There might be issues in your whole sample and the subsample you drew may happen to avoid the issues. You may try running the bootstrap code with the first 500 obs, then the first 600, and so on, to see when the error message pops up.

                  Comment

                  Working...
                  X