Announcement

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

  • Rolling window estimation for panel data

    Hello dear all,
    I used the local projection method on yearly data for a panel data (143 countries from 1973-2019), but as a robustness check I am trying to use the rolling window (5-years)and plot it. I tried a lot based on the othere STATA posted and help. but I received several times an error.
    I appreciate receiving your assistance.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year float cur int ifscode double WUI_sum
    1973    .5317895 111           .0897666
    1974   .11001506 111 .28516949999999996
    1975   1.0611881 111 .48200679999999996
    1976    .2049736 111           .3596399
    1977   -.7253248 111 1.0571842999999999
    1978   -.6696295 111 .19876069999999998
    1979 -.005964223 111  .5083416000000001
    1980  .074440725 111 .24863100000000002
    1981    .1499825 111           .3621824
    1982   -.3471212 111           .3713493
    1983  -1.2168833 111           .3962155
    1984   -2.452142 111            .192419
    1985  -2.8683014 111           .0839913
    1986   -3.213665 111 .21242709999999998
    1987  -3.3087516 111           .3327374
    1988   -2.315658 111           .7352632
    1989  -1.7637967 111 .36565369999999997
    1990  -1.3239995 111 .39529499999999995
    1991   .04629653 111           .1536587
    1992   -.7914488 111           .2734836
    1993  -1.2361605 111  .5693062999999999
    1994   -1.669253 111           .2580359
    1995  -1.4864494 111           .2924072
    1996  -1.5449674 111           .1409335
    1997  -1.6406133 111           .0495638
    1998  -2.3727388 111           .4398425
    1999   -2.975982 111           .2346321
    2000   -3.920886 111           .2538247
    2001   -3.724075 111           .9892922
    2002    -4.17334 111            1.07912
    2003   -4.558981 111          1.2123401
    2004   -5.204854 111           .3570366
    2005   -5.745982 111 .49987200000000004
    2006   -5.911019 111 .23886079999999998
    2007   -5.088728 111           1.094794
    2008  -4.7158613 111           .6031295
    2009  -2.6228366 111          1.0202793
    2010   -2.870643 111  .8268708999999999
    2011   -2.918621 111           .7264667
    2012   -2.572368 111          1.5249123
    2013  -2.0153842 111           .8904299
    2014   -2.108055 111           .5881687
    2015   -2.243472 111           1.073512
    2016  -2.1193724 111  .8585571000000001
    2017  -1.8535286 111           .7434678
    2018  -2.1421602 111  .7691144999999999
    2019   -2.085756 111          2.1541754
    1973  -1.2524583 112           .0855359
    1974   -3.613082 112 .38107559999999996
    1975   -1.433419 112           .8089789
    1976  -.59336907 112 1.0808285999999998
    1977   .05513242 112           .3710501
    1978    .6441247 112          1.0868924
    1979  -.17827246 112  .9693126999999999
    1980   1.2146102 112 .46875239999999996
    1981    2.612607 112           .3194212
    1982   1.5504085 112 .36219169999999995
    1983     1.08082 112           .8144914
    1984    .3972896 112                  0
    1985      .67741 112 .06320909999999999
    1986  -.22009194 112           .2062234
    1987  -1.6895826 112                  0
    1988  -3.8812656 112           .5320132
    1989   -4.650699 112           .6230532
    1990   -3.550168 112           1.230429
    1991  -1.6645445 112           .6524292
    1992   -1.966937 112           .3223508
    1993   -1.669506 112           .3112551
    1994   -.8791173 112 .08992339999999999
    1995   -.9980571 112           .1008928
    1996   -.7264912 112 .44899579999999995
    1997  -.08981485 112           .7623446
    1998  -.31861085 112  .6506400999999999
    1999  -2.1135337 112           .5433545
    2000    -1.84713 112           .2850048
    2001  -1.8055797 112           .5955098
    2002  -2.0363908 112 .27691750000000004
    2003   -1.884036 112           .8814714
    2004   -2.362032 112           .6360688
    2005  -2.0342944 112           .6806401
    2006    -3.16443 112           .5955746
    2007   -3.786051 112           .8178505
    2008   -3.987796 112           .7105001
    2009   -3.118611 112           1.818801
    2010   -2.894801 112 1.4819679000000001
    2011  -1.8039025 112 1.3387272000000001
    2012  -3.2902634 112 1.6197735999999998
    2013   -4.782701 112 1.2138284000000001
    2014   -5.138112 112  .9520833000000001
    2015   -5.066957 112          1.9084387
    2016   -5.540469 112          3.9415905
    2017   -3.610205 112          1.8996527
    2018  -4.0648184 112           2.433672
    2019   -2.827337 112          4.7189962
    1973           . 122           .0988142
    1974           . 122           .3575679
    1975           . 122  .9805128999999999
    1976           . 122 .34737660000000004
    1977           . 122                  0
    1978           . 122             .11152
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 8883 observations
    Use the count() option to list more


    my local projection method is :


    Code:
    foreach v in  "cur" {
    
    gen d`v'_0=f0.`v'- l.`v'
    gen d`v'_1=f1.`v'- l.`v'
    gen d`v'_2=f2.`v'- l.`v'
    gen d`v'_3=f3.`v'- l.`v'
    gen d`v'_4=f4.`v'- l.`v'
    gen d`v'_5=f5.`v'- l.`v'
    
    }
    Code:
    foreach v in cur {
    forvalues i=0/4{
    
    /*reghdfe d`v'_`i' l(0/3)WUI_sum   l(1/1)d`v'_0   if inv!=. &  sav!=. &  cur!=. , absorb(i.ifscode i.year) vce( cluster ifscode)*/
    
    xtscc d`v'_`i' l(0/3)l.WUI_sum  l(1/1)d`v'_0    i.year if  cur!=. &  ,fe

    now in terms of the rolling window I did :
    Code:
    bysort ifscode: rolling (window 5): regress cur l(0/3)l.WUI_sum
    
    rolling may not be combined with by
    r(190);
    Also:
    Code:
    bys ifscode: asreg cur l(0/3)lwui, wind(year 5)
    factor-variable and time-series operators not allowed
    r(101);

    Many thanks in advance,

    Last edited by Khati Zolfaghari; 01 Oct 2023, 12:53.

  • #2
    why not just create the lags?

    Comment


    • #3
      @George Ford thank you so much for your reply.
      in the second trying, I created first lag of the WUI (lwui=l.wui), but I get an error.
      Code:
      bys ifscode: asreg cur l(0/3)lwui, wind(year 5) factor-variable and time-series operators not allowed r(101);
      Also, I am trying based on :
      Code:
       xtset ifscode year gen lwui=l.WUI_sum     tempname vector     postfile `vector' time1 time2                ///      beta_lwui  beta_const    ///      se_lwui se_const    ///      using mybeta, replace                    set more off              forvalues i=1973/2019 {     foreach v in "cur""sav""inv"{             local j=`i'+2         display _n(5) in white _col(30) ///          "Time period:" `i' "-" `j'         xtreg `v'  lwui   if year>=`i' & year<=`j'            post `vector'                    ///              (`i') (`j')                ///              (_b[lwui])   (_b[_cons])    ///              (_se[lwui])  (_se[_cons])             local j=`j'+1          }     }         postclose `vector'          set more on          use mybeta,clear
      ***rolling window -5years ---I get an error. also because in my baseline, I used 3 lags of the wui as my shock, but in the rolling window estimation should we add also lags? I appreciate receiving your assistance. Best regards, Khatereh

      Comment


      • #4
        you "by" in asreg as an option.

        and you need to specfy as

        asreg cur wui l1_wui l2_wui l3_wui, wind(year 5) by(ifscode)

        Comment


        • #5
          @George Ford thank you so much for your reply.

          best regards,

          Comment


          • #6
            Hello dear all,

            First of all I appologise for my long message and many thanks for your help.
            Following the assistance of the @George Ford, I try to estimate rolling window.
            But I dealt with some issues:
            1. because in my baseline, I used the fixed-effect regression, the results of the asreg also coincide with panel data and fixed effect estimation? (I tried to find the answer to this question, but unfortunately I could not find)
            2. how can we see the number of observations in our regression for the entire regression after using asreg and 5-years window?
            3. when I add the lag of my dependent variable in my regression as as control like in my baseline, and I use "asreg" I get missing
            Code:
            gen lwui=l.WUI_sum
            gen l2wui=l2.WUI_sum
            gen l3wui=l3.WUI_sum
            gen l4wui=l4.WUI_sum
            
            gen lcur=l.cur
            gen lsav=l.sav
            gen linv=l.inv
            Code:
            asreg cur lwui l2wui l3wui l4wui lcur, wind(year 5) by(ifscode) se
            Code:
                  +-------------------------------------------------------------------------+
                  | _b_lwui   _b_l2wui   _b_l3wui   _b_l4wui   _b_lcur   _b_cons   _se_lwui |
                  |-------------------------------------------------------------------------|
               1. |       .          .          .          .         .         .          . |
               2. |       .          .          .          .         .         .          . |
               3. |       .          .          .          .         .         .          . |
               4. |       .          .          .          .         .         .          . |
               5. |       .          .          .          .         .         .          . |
                  |-------------------------------------------------------------------------|
            4. but by omitting the lag of the dependent variable and l4.wwui, there is no error.
            Code:
            asreg cur lwui l2wui l3wui   , wind(year 5) by(ifscode) se
            like this:
            Code:
                  +-----------------------------------------------------------------------------------------------+
                  | _Nobs         _R2       _adjR2      _b_lwui     _b_l2wui     _b_l3wui      _b_cons   _se_lwui |
                  |-----------------------------------------------------------------------------------------------|
               1. |     .           .            .            .            .            .            .          . |
               2. |     .           .            .            .            .            .            .          . |
               3. |     .           .            .            .            .            .            .          . |
               4. |     .           .            .            .            .            .            .          . |
               5. |     .           .            .            .            .            .            .          . |
                  |-----------------------------------------------------------------------------------------------|
               6. |     .           .            .            .            .            .            .          . |
               7. |     .           .            .            .            .            .            .          . |
               8. |     5    .2763454   -1.8946184   -.82222777   -.34614993    .16123911    .29586781   1.373922 |
               9. |     5   .50957154   -.96171382   -.81050221    .12824472    .60577326   -.20622847   1.027458 |
              10. |     5   .71472337   -.14110653     -.838677    .06586782    .32234586    .03947263   .5933881 |
                  |-----------------------------------------------------------------------------------------------|
              11. |     5   .87320063    .49280253   -12.364169   -1.7521636    3.1780933    3.2324335   5.950426 |
              12. |     5   .80113028    .20452114   -19.955479   -1.6018926    5.2540082    4.8171801   11.82328 |
              13. |     5   .07244482   -2.7102207    1.4849299   -2.5535596   -4.1333663     .5466427   20.89108 |
              14. |     5   .69831217   -.20675134    8.0283335   -5.4539372     1.513998    -3.135335   5.545403 |
              15. |     5   .71681355   -.13274581    1.4804615    4.4612034   -4.5470751    -2.810084    5.87422 |
                  |-----------------------------------------------------------------------------------------------|
            5. after my estimation, I try to plot rolling window estimation with 95% CI, so follow the https://www.stata.com/manuals13/tsrolling.pdf and plot my estimation for the entire sample, I read the https://fintechprofessor.com/2017/12...ions-in-stata/ and also https://www.statalist.org/forums/for...-rolling-betas
            from @Attaullah Shah, and try to follow the steps.

            Code:
            asreg cur lwui l2wui l3wui   , wind(year 5) by(ifscode) se  
            
            
            
            generate upper = _b_lwui + 1.96*_se_lwui
            generate lower = _b_lwui - 1.96*_se_lwui
            
            twoway (line _b_lwui year) (line upper lower year)
            ( it is the graph without adding the lag of the dependent variable as control)
            But the graph is not for the entire sample.
            asreg_graph.gph

            6. I try also using the rolling window estimation, but it is not as fast like asreg command.
            Code:
            rolling  _b _se,window(5) clear: regress  cur lwui l2wui l3wui l4wui lcur
            however, in terms of the output, when I visualise the output, it is clear that the STATA did a rolling window for 5-years and reported the start and end of the date for each observation, but I am not able to plot it.

            Code:
                 +------------------------------------------------------------------------------------------------+
               1. | ifscode | start |  end |   _b_lwui |  _b_l2wui |  _b_l3wui |  _b_l4wui |   _b_lcur |   _b_cons |
                  |     111 |  1973 | 1977 |         . |         . |         . |         . |         . |         . |
                  |------------------------------------------------------------------------------------------------|
                  |                   _se_lwui                    |                    _se_l2~i                    |
                  |                          .                    |                           .                    |
                  +------------------------------------------------------------------------------------------------+
            
                  +------------------------------------------------------------------------------------------------+
               2. | ifscode | start |  end |   _b_lwui |  _b_l2wui |  _b_l3wui |  _b_l4wui |   _b_lcur |   _b_cons |
                  |     111 |  1974 | 1978 |         0 |         0 |         0 |         0 | -.0598683 | -.7130534 |
                  |------------------------------------------------------------------------------------------------|
                  |                   _se_lwui                    |                    _se_l2~i                    |
                  |                          0                    |                           0                    |
                  +------------------------------------------------------------------------------------------------+
            
                  +------------------------------------------------------------------------------------------------+
               3. | ifscode | start |  end |   _b_lwui |  _b_l2wui |  _b_l3wui |  _b_l4wui |   _b_lcur |   _b_cons |
                  |     111 |  1975 | 1979 |  -.816738 |         0 |         0 |         0 | -.6722643 | -.2937968 |
                  |------------------------------------------------------------------------------------------------|
                  |                   _se_lwui                    |                    _se_l2~i                    |
                  |                          0                    |                           0                    |
                  +------------------------------------------------------------------------------------------------+

            I want to have a plot of rolling 5-window for the entire sample. like this:

            Sorry for my long messages. I appreciate any help and assistance you can provide.


            Best regards,
            Attached Files
            Last edited by Khati Zolfaghari; 06 Oct 2023, 13:11.

            Comment


            • #7
              I need your help

              Comment


              • #8
                Your model is saturated in the first case (5 years, 5 coefficients). Change to year 10 and you'll get estimates. That's why it works when you drop lcur.

                ssc install lgraph

                lgraph _b_lwui year, by(ifscode)



                Comment


                • #9
                  @George Ford many thanks for your reply.
                  I changed the window to 10 years. and follow "lgragh" command.

                  But the here the chart shows for all ifscode(unite).
                  I attached the output. Graphroll.gph
                  I need the unique chart for entire sample.

                  If I generate the mean of the "_b_lwui " is correct or not?
                  Code:
                  egen mean_lwui = mean(_b_lwui), by(year )
                  egen mean_se = mean(_se_lwui), by(year )
                  Code:
                  generate upper = mean_lwui + 1.96*mean_se
                  generate lower = mean_lwui - 1.96*mean_se
                  and then for the chart, I use the two-way command. and because all the mwan_lwui are the same among the countries , I restrict only for one country.
                  like this:
                  Code:
                  twoway (line mean_lwui year if ifscode==111) (line upper lower year if ifscode==111)
                  Graphmean rolling.gph

                  I am not sure if this is correct or not? because the magnitude is large now. ( it is te average of all _b_lwui).

                  I appreciate receiving your guidance.


                  best regards,



                  Comment


                  • #10
                    I'm not sure what you're after, so it's hard to provide more direction.

                    If you "by" the asreg, you'll get over 100 estimates to plot (one for each ipscode), which is not sensible. If you don't "by" the asreg, then you'll get one set of estimates, but they'll be listed for each ipscode. Just restrict the graph sample to the first one to avoid generating a new variable (lgraph collapses before hand, so even that's not necessary, but may speed it up).

                    Using what data you've posted:


                    Code:
                    tset ifscode year
                    gen lwui=l.WUI_sum
                    gen l2wui=l2.WUI_sum
                    gen l3wui=l3.WUI_sum
                    gen l4wui=l4.WUI_sum
                    gen lcur=l.cur
                    
                    capture drop _*
                    asreg cur lwui l2wui l3wui l4wui lcur, wind(year 10) se
                    
                    generate upper = _b_lwui + 1.96*_se_lwui
                    generate lower =_b_lwui - 1.96*_se_lwui
                    lgraph _b_lwui year , also(upper lower) addplot(rarea lower upper year, color(gs10%50))
                    
                    ** twoway (connected _b_lwui year) (rarea lower upper year, color(gs10%50)) if ifscode==111 & _b_lwui!=.  //Alternative
                    Click image for larger version

Name:	lg1.jpg
Views:	1
Size:	29.5 KB
ID:	1729707


                    Comment


                    • #11
                      @George Ford thank you so much for your reply.
                      my issue was to use"by" in my estimation .
                      and, then I compute the mean of the sd for 100 observation.


                      Thanks a lot.

                      Comment

                      Working...
                      X