Announcement

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

  • compare the L. and [_n-1]

    Hi,

    I use both L.var and var[_n-1] to gen Lag variable.

    But sometimes I find var[_n-1] is better and L.var cannot get the accurate lag values.

    Does anyone know it?

    Thanks

  • #2
    The following example shows that if you have missing periods in your time series, the lag operator L correctly generates a missing value in the next period, while the [_n-1] subscript uses the value from two periods ago, which is an error.

    So although you have not shown any example of your data, nor any explanation of what you mean by "the accurate lag values" that the lag operator is not giving you, it seems likely that when you are using the [_n-1] subscript you are calculating erroneous results, while the lag operator L is correctly telling you that there is no value because the previous period is missing.

    With that said, to improve future posts please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

    Section 12.1 is particularly pertinent

    12.1 What to say about your commands and your problem

    Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
    ...
    Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.
    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Code:
    . tsset year
            time variable:  year, 2011 to 2015, but with a gap
                    delta:  1 unit
    
    . generate lag_x = L.x
    (2 missing values generated)
    
    . generate x_minus1 = x[_n-1]
    (1 missing value generated)
    
    . list, clean
    
           year   x   lag_x   x_minus1  
      1.   2011   2       .          .  
      2.   2012   4       2          2  
      3.   2014   3       .          4  
      4.   2015   0       3          3

    Comment


    • #3
      Add that x[_n-1] just yields the previous value in the dataset as you have it at present.

      If your data are in the wrong sort order, that could be nonsense.

      If your data are panel data and your command ignores the panel context, the previous value could refer to a different panel!

      Those may sound too obvious to mention, but people often forget them.

      The claim that "L.var cannot get the accurate lag value" is unsubstantiated. It might mean:

      1. Your tsset or xtset settings were wrong.

      2. You want (e.g.) the previous value in the same panel, regardless of spacing and/or the previous non-missing value. It's not the job of L. to deliver those if there are gaps or missings.

      Comment


      • #4
        Thanks a lot for Nick Cox and William Lisowski's kind answers!!

        "1. Your tsset or xtset settings were wrong. "

        yes! I see where I went wrong when I am working with my time data....

        Your comments are very helpful!!!

        Comment

        Working...
        X