Announcement

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

  • Time Lag

    Hello, I have a question regarding my code. I would like to include a time lag to account for the fact that changes in var2 have a delayed impact on var1. Therefore, I want to regress the observations of var2 on var1 for the following year for each ID. I attempted to do this with the following code, but afterwards, var2 takes on the same values for each year.

    sort id year
    list year id var1 var2 if id==1
    replace var1 = var2[_n-1] if id==id[_n-1]
    list year id var1 var2 if id==1

    However, if I do it the other way around and reset var2 by one year, it works.

    sort id year
    list year id var1 var2 if id==1
    replace var1 = var2[_n+1] if id==id[_n+1]
    list year id var1 var2 if id==1

    Does anyone know where the error in my code is? Thank you very much!

  • #2
    Use time-series operators when dealing with panel data, as they are more convenient and safer, reducing the risk of errors. These require you to xtset your data.

    Code:
    help tsvarlist
    In addition, you do not need to generate leads or lags beforehand, just specify them directly in the estimation command.

    Code:
    webuse grunfeld, clear
    xtset company year
    xtreg invest L.mvalue
    If you want to create lags within panels as in your manual approach, see

    Code:
    help bysort
    The risk here is that you have missing observations (an unbalanced panel) and a simple code as below will generate wrong results:

    Code:
    webuse grunfeld, clear
    bys company (year): gen Lmvalue=mvalue[_n-1]
    xtreg invest Lmvalue
    More elaborate code would look like:

    Code:
    webuse grunfeld, clear
    bys company (year): gen Lmvalue=mvalue[_n-1] if year== year[_n-1]+1
    xtreg invest Lmvalue

    Comment


    • #3
      Hi Andrew, many thanks for this advice! Now my Code is working

      Comment

      Working...
      X