Announcement

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

  • Lagged Variables: Difference between L.Variable and Variable[_n-1]

    Hi everyone,

    I try to understand the difference between the codes mentioned in the header. On my dataset I tried following codes and got two different results


    Code:
    gen lcumreturn = L.cumreturn
    &

    Code:
    gen lcumreturn_L1 = cumreturn[_n-1]
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float return double cumreturn float(lcumreturn_L1 lcumreturn)
    1.0079681 1.0079680681228638         .         .
            1 1.0079680681228638 1.0079681 1.0079681
    1.0039526 1.0119521853011548 1.0079681 1.0079681
      .984252   .996015938013521 1.0119522 1.0119522
        1.016  1.011952225317481   .996016   .996016
    1.0177165 1.0298805042250194 1.0119523 1.0119523
    1.0019342 1.0019341707229614 1.0298805 1.0298805
            . 1.0019341707229614 1.0019342 1.0019342
            .                  . 1.0019342         .
     .9560819  .9560818672180176         .         .
     1.048836 1.0027730744142218  .9560819  .9560819
    1.0131539 1.0159634618241578  1.002773  1.002773
     .9927707 1.0086187891207565 1.0159634 1.0159634
     .9667113  .9750431634166093 1.0086188 1.0086188
    1.0151422  .9898074643236853  .9750432  .9750432
     .9949268  .9949268102645874  .9898075  .9898075
     .9702436  .9653213434524304  .9949268  .9949268
    1.0298847  .9941696783503577  .9653214  .9653214
    1.0142422 1.0083288141464128  .9941697  .9941697
    1.1347151 1.1441659112738582 1.0083288 1.0083288
    1.0047648  1.149617627634143 1.1441659 1.1441659
     .9118093 1.0482320047894862 1.1496177 1.1496177
     .9703121 1.0171121547990258  1.048232  1.048232
    1.0166008 1.0339970783111274 1.0171121 1.0171121
    1.0062244 1.0404330833607063  1.033997  1.033997
    1.0306382 1.0723100989061582  1.040433  1.040433
    end
    Thanks

  • #2
    value[_n-1] refers to the preceding observation in the current sort order. l.value means the value of the first lag, i.e. one time period before as set by tsset or xtset.

    value[_n-1] and l.value will be exactly the same if the data is sorted on the time (or panel/time) variable, and there are no time gaps in the data.

    However, let's presume that you have yearly data with values for 2018 and 2020, but with 2019 missing, and you have tsset or xtset the data yearly. Then, value[_n-1] for year 2020 will be the value of 2018, but it will be missing for d.value.
    Last edited by Wouter Wakker; 18 Nov 2020, 06:24.

    Comment


    • #3
      There are two key differences. Consider this example:

      Code:
      clear 
      input id time whatever 
      1 1  5
      1 3  6
      1 5  7
      2 1  8 
      2 2  9 
      2 3  10 
      end 
      
      tsset id time 
      
      gen w1 = L1.whatever 
      gen w2 = whatever[_n-1]
      
      bysort id (time): gen w3 = whatever[_n-1]
      
      list, sepby(id)
      
           +-------------------------------------+
           | id   time   whatever   w1   w2   w3 |
           |-------------------------------------|
        1. |  1      1          5    .    .    . |
        2. |  1      3          6    .    5    5 |
        3. |  1      5          7    .    6    6 |
           |-------------------------------------|
        4. |  2      1          8    .    7    . |
        5. |  2      2          9    8    8    8 |
        6. |  2      3         10    9    9    9 |
           +-------------------------------------+
      First, time series operators require a prior specification of time variable which then is taken literally. That bites if any gaps are present. In the example above the previous values for identifier 1 and times 3 and 5 would be those at times 2 and 4 which are not in the dataset, so missings are returned for w1. Also, with time series or panel or longitudinal data alike the previous value before the first will always be missing.

      Second, subscripts return values in some previous observation (or possibly some later observation). which isn't guaranteed even to be a value in the same panel unless you spell that out with by:. Indeed, if observations are jumbled with respect to panel or time order a previous or later observation could contain something completely irrelevant.

      Positively put, the point about time series operators (which historically followed the use of subscripts in Stata) is to ensure that users can do the right thing and automatically respect panel structure when it exists and also gaps in time series.

      Comment


      • #4
        To add to Nick's reply, I don't know how you executed the following command:
        Code:
        gen lcumreturn = L.cumreturn
        without getting an error message since you don't seem to have set a time variable.
        If you do set a time variable, there is no difference:
        Code:
        . * Example generated by -dataex-. To install: ssc install dataex
        . clear
        . input float return double cumreturn float(lcumreturn_L1 lcumreturn)
                return   cumreturn  lcumret~1  lcumret~n
          1. 1.0079681 1.0079680681228638         .         .
          2.         1 1.0079680681228638 1.0079681 1.0079681
          3. 1.0039526 1.0119521853011548 1.0079681 1.0079681
          4.   .984252   .996015938013521 1.0119522 1.0119522
          5.     1.016  1.011952225317481   .996016   .996016
          6. 1.0177165 1.0298805042250194 1.0119523 1.0119523
          7. 1.0019342 1.0019341707229614 1.0298805 1.0298805
          8.         . 1.0019341707229614 1.0019342 1.0019342
          9.         .                  . 1.0019342         .
         10.  .9560819  .9560818672180176         .         .
         11.  1.048836 1.0027730744142218  .9560819  .9560819
         12. 1.0131539 1.0159634618241578  1.002773  1.002773
         13.  .9927707 1.0086187891207565 1.0159634 1.0159634
         14.  .9667113  .9750431634166093 1.0086188 1.0086188
         15. 1.0151422  .9898074643236853  .9750432  .9750432
         16.  .9949268  .9949268102645874  .9898075  .9898075
         17.  .9702436  .9653213434524304  .9949268  .9949268
         18. 1.0298847  .9941696783503577  .9653214  .9653214
         19. 1.0142422 1.0083288141464128  .9941697  .9941697
         20. 1.1347151 1.1441659112738582 1.0083288 1.0083288
         21. 1.0047648  1.149617627634143 1.1441659 1.1441659
         22.  .9118093 1.0482320047894862 1.1496177 1.1496177
         23.  .9703121 1.0171121547990258  1.048232  1.048232
         24. 1.0166008 1.0339970783111274 1.0171121 1.0171121
         25. 1.0062244 1.0404330833607063  1.033997  1.033997
         26. 1.0306382 1.0723100989061582  1.040433  1.040433
         27. end
        
        . drop lcumreturn_L1 lcumreturn
        . gen t = _n
        . tsset t
                time variable:  t, 1 to 26
                        delta:  1 unit
        
        . gen lcumreturn = L.cumreturn
        (2 missing values generated)
        
        . gen lcumreturn_L1 = cumreturn[_n-1]
        (2 missing values generated)
        
        . list
        
             +-------------------------------------------------+
             |   return   cumreturn    t   lcumr~n    lcumr~1  |
             |-------------------------------------------------|
          1. | 1.007968   1.0079681    1          .          . |
          2. |        1   1.0079681    2   1.007968   1.007968 |
          3. | 1.003953   1.0119522    3   1.007968   1.007968 |
          4. |  .984252   .99601594    4   1.011952   1.011952 |
          5. |    1.016   1.0119522    5    .996016    .996016 |
             |-------------------------------------------------|
          6. | 1.017717   1.0298805    6   1.011952   1.011952 |
          7. | 1.001934   1.0019342    7   1.029881   1.029881 |
          8. |        .   1.0019342    8   1.001934   1.001934 |
          9. |        .           .    9   1.001934   1.001934 |
         10. | .9560819   .95608187   10          .          . |
             |-------------------------------------------------|
         11. | 1.048836   1.0027731   11   .9560819   .9560819 |
         12. | 1.013154   1.0159635   12   1.002773   1.002773 |
         13. | .9927707   1.0086188   13   1.015963   1.015963 |
         14. | .9667113   .97504316   14   1.008619   1.008619 |
         15. | 1.015142   .98980746   15   .9750432   .9750432 |
             |-------------------------------------------------|
         16. | .9949268   .99492681   16   .9898075   .9898075 |
         17. | .9702436   .96532134   17   .9949268   .9949268 |
         18. | 1.029885   .99416968   18   .9653214   .9653214 |
         19. | 1.014242   1.0083288   19   .9941697   .9941697 |
         20. | 1.134715   1.1441659   20   1.008329   1.008329 |
             |-------------------------------------------------|
         21. | 1.004765   1.1496176   21   1.144166   1.144166 |
         22. | .9118093    1.048232   22   1.149618   1.149618 |
         23. | .9703121   1.0171122   23   1.048232   1.048232 |
         24. | 1.016601   1.0339971   24   1.017112   1.017112 |
         25. | 1.006224   1.0404331   25   1.033997   1.033997 |
             |-------------------------------------------------|
         26. | 1.030638   1.0723101   26   1.040433   1.040433 |
             +-------------------------------------------------+
        
        . log close

        Comment

        Working...
        X