Announcement

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

  • ivregress looped on individuals of panel data

    Hi all,

    I have a panel data and I want to run an instrumental variable regression on each individual. The code I am using is the following:

    Code:
    ivregress 2sls y (x = z) if person_id==7, vce(hac bartlett 12)
    But I get the following error:

    Code:
    sample may not include multiple panels
    Shouldn't the "if person_id==7" solve the issue?
    How can I solve this?

    Alternatively, do you know a code that can do the trick, i.e. run an IV regression on each individual of a panel, with HAC s.e.?

    Thank you!

  • #2
    It works with vce(robust) but not with vce(hac bartlett 12)

    Comment


    • #3

      vce(hac bartlett 12) is for use with time series and not panel data. Even if you use the -if- qualifier, you have already xtset your data and declared it a panel to Stata. That is why you get the error. One solution is to use -preserve- and -restore- repeatedly.

      Code:
        
      preserve
      keep if person_id==7
      ivregress 2sls y (x = z), vce(hac bartlett 12)
      restore  
      preserve
      keep if person_id==8
      ivregress 2sls y (x = z), vce(hac bartlett 12)
      restore
      Alternatively, if your panel is balanced, you can tsset your data using the observation number and use the -if- qualifier

      Code:
      xtset, clear
      sort id year
      gen t= _n
      tsset t
      
      ivregress 2sls y (x = z) if person_id==7, vce(hac bartlett 12)
      Last edited by Andrew Musau; 22 Apr 2017, 10:41.

      Comment


      • #4
        Hey Andrew,

        Nice work arounds!

        I instead used ivreg2 with if and HAC s.e. - it should give the same results, right?

        Comment


        • #5
          Absolutely the same. Remember HAC= Heteroskedasticity and Autocorrelation Consistent, so remember to include robust option in -ivreg2- (otherwise you have AC covariance estimation).

          Code:
          
          
          . clear
          
          . webuse grunfeld
          
          . keep if company==1
          (180 observations deleted)
          
          . tsset year
                  time variable:  year, 1935 to 1954
                          delta:  1 year
          
          . ivregress 2sls mvalue invest kstock, vce(hac nw 5)
          
          Instrumental variables (2SLS) regression          Number of obs   =         20
                                                            Wald chi2(2)    =     164.64
                                                            Prob > chi2     =     0.0000
                                                            R-squared       =     0.6396
                                                            Root MSE        =     529.14
          
          ------------------------------------------------------------------------------
                       |                 HAC
                mvalue |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                invest |   4.664183   .4846287     9.62   0.000     3.714329    5.614038
                kstock |  -1.456692   .2845878    -5.12   0.000    -2.014474   -.8989102
                 _cons |   2442.498   180.3164    13.55   0.000     2089.085    2795.912
          ------------------------------------------------------------------------------
          (no endogenous regressors)
          HAC VCE:       Bartlett kernel with 5 lags
          
          . ivreg2 mvalue invest kstock, bw(6) kernel(bartlett) robust
          
          OLS estimation
          --------------
          
          Estimates efficient for homoskedasticity only
          Statistics robust to heteroskedasticity and autocorrelation
            kernel=Bartlett; bandwidth=6
            time variable (t):  year
          
                                                                Number of obs =       20
                                                                F(  2,    17) =    69.97
                                                                Prob > F      =   0.0000
          Total (centered) SS     =  15537575.83                Centered R2   =   0.6396
          Total (uncentered) SS   =  391181828.9                Uncentered R2 =   0.9857
          Residual SS             =  5599712.818                Root MSE      =    529.1
          
          ------------------------------------------------------------------------------
                       |               Robust
                mvalue |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                invest |   4.664183   .4846287     9.62   0.000     3.714329    5.614038
                kstock |  -1.456692   .2845878    -5.12   0.000    -2.014474   -.8989102
                 _cons |   2442.498   180.3164    13.55   0.000     2089.085    2795.912
          ------------------------------------------------------------------------------
          Included instruments: invest kstock
          ------------------------------------------------------------------------------

          Comment


          • #6
            Thank you!!

            (yes I type both bw and robust)

            Comment

            Working...
            X