Announcement

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

  • Loop with 2SLS (Xtivreg2)

    Hi everyone,

    I have panel data with a list of 60 endogeneous variables that I would like to instrument with 60 corresponding instruments (I want to run 60 separate regressions). Please find below a simplified example of what I would like to do:
    xtivreg2 depvar control1 control2 (endogeneous1= instrument_endogeneous1 variablex), fe endog( endogeneous1)

    xtivreg2 depvar control1 control2 (endogeneous2= instrument_endogeneous2 variablex ), fe endog( endogeneous2)

    xtivreg2 depvar control1 control2 (endogeneous3= instrument_endogeneous3 variablex ), fe endog( endogeneous3)

    and so on for the remaining ones.

    I tried creating loops for endogenous variables and instruments. Please find below a simplification:
    local ivar1 endogeneous1
    local ivar2 endogeneous2

    local dvar1 instrument_endogeneous1
    local dvar2 instrument_endogeneous2


    forv i=1/60 {
    forv d=1/60 {
    xtivreg2 depvar control1 control2 (`ivar`i''=`dvar`d'' variablex), fe endog(`ivar`i'') first savefirst
    }
    }


    The problem here is that it runs regressions for each endogenous variable instrumented once by each instrument in the list. This is not what I intend to do. I want to instrument each variable with its corresponding instrument (not the whole list of instruments).

    I would be grateful if you could advise on this. I am sorry in advance if this has been asked before and I couldn't locate the answer.

    Many thanks in advance.

    Jala Youssef

  • #2
    The index is the same, so no need to define it separately for each set of locals. xtivreg2 is from SSC (FAQ Advice #12).

    Code:
    forv i=1/60 {
        xtivreg2 depvar control1 control2 (`ivar`i''=`dvar`i'' variablex), fe endog(`ivar`i'') first savefirst
    }

    Comment


    • #3
      Dear Andrew Musau

      Many thanks for the prompt and very useful reply, much appreciated. The code you suggested works perfectly.

      Jala

      Comment


      • #4
        Dear Andrew Musau

        Sorry for the too many questions. Relative to the same code discussed above, I would be grateful if you could advise if there is a way to report the F-statistic of the first-stage results. I tried using outreg2 but I am not sure if it would allow me to report it.

        I read this relevant thread but it is more about the first stage results in general:
        https://www.statalist.org/forums/for...eg2-by-outreg2

        And this one raising the same question (though unanswered unfortunately):
        https://www.statalist.org/forums/for...after-xtivreg2

        Many thanks in advance for your help.

        Jala

        Comment


        • #5
          outreg2 is from SSC (FAQ Advice #12). As I show here if using estout from SSC, you need to estimate the first stage regression to retrieve the statistic. Here is how you would do it using outreg2.

          Code:
          use http://fmwww.bc.edu/ec-p/data/wooldridge/mroz.dta, clear
          ivreg2 lwage exper expersq (educ=age kidslt6 kidsge6), first savefirst
          qui reg `e(instd)' `e(insts)' if e(sample)
          local F= e(F)
          est restore _ivreg2_educ
          outreg2 using "ivreg2-12.rtf", replace noobs
          ivreg2 lwage exper expersq (educ=age kidslt6 kidsge6)
          outreg2 using "ivreg2-12.rtf", append addstat(First stage F, `F')
          Res.:

          Code:
          . ivregress 2sls lwage exper expersq (educ=age kidslt6 kidsge6), first
          
          First-stage regressions
          -----------------------
          
                                                          Number of obs     =        428
                                                          F(   5,    422)   =       3.04
                                                          Prob > F          =     0.0105
                                                          R-squared         =     0.0347
                                                          Adj R-squared     =     0.0233
                                                          Root MSE          =     2.2586
          
          ------------------------------------------------------------------------------
                  educ |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                 exper |   .0501414   .0452026     1.11   0.268    -.0387088    .1389916
               expersq |  -.0017657   .0013568    -1.30   0.194    -.0044326    .0009012
                   age |   -.012015   .0178791    -0.67   0.502    -.0471582    .0231281
               kidslt6 |   .7254425   .2979333     2.43   0.015     .1398244    1.311061
               kidsge6 |  -.2219447    .093675    -2.37   0.018    -.4060725   -.0378169
                 _cons |   13.12194   .8598407    15.26   0.000     11.43184    14.81204
          ------------------------------------------------------------------------------
          
          
          Instrumental variables (2SLS) regression          Number of obs   =        428
                                                            Wald chi2(3)    =      22.69
                                                            Prob > chi2     =     0.0000
                                                            R-squared       =     0.1556
                                                            Root MSE        =     .66378
          
          ------------------------------------------------------------------------------
                 lwage |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                  educ |   .0964002   .0814278     1.18   0.236    -.0631952    .2559957
                 exper |    .042193   .0138831     3.04   0.002     .0149827    .0694033
               expersq |  -.0008323   .0004204    -1.98   0.048    -.0016563   -8.33e-06
                 _cons |  -.3848718   1.011551    -0.38   0.704    -2.367476    1.597732
          ------------------------------------------------------------------------------
          Instrumented:  educ
          Instruments:   exper expersq age kidslt6 kidsge6
          Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	32.7 KB
ID:	1662518

          Last edited by Andrew Musau; 01 May 2022, 13:41.

          Comment

          Working...
          X