Announcement

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

  • Including lagged values in regression

    Hi. I want to include lagged values of my dependent variable (the value of "confirmed_1" from the week before) in the regressions below. Since I have repeated time values in my panel, xtset followed by l. doesn't work over here. For other reasons, I also can't collapse this to one observation per week. I tried "bysort state weeknum : gen lag1 = confirmed_1[_n-1]", but that did not work.

    How do I create a variable that reflects the values of variable "confirmed_1" for the previous week? Alternately, is there any other way in which I can include lagged values of confirmed_1. Any help will be much appreciated

    Code:
    areg confirmed_1 post treated_post tested i.weeknum if eventBlock == 1, absorb(state)  
    areg confirmed_1 post treated_post tested i.weeknum if eventBlock == 2, absorb(state)  
    
    *******SAMPLE DATA
    input str50 state byte weeknum float(confirmed_1 post treated_post tested eventBlock)
    "Andaman and Nicobar Islands"  3 .00025188917 0 0         0  8
    "Andaman and Nicobar Islands"  3 .00025188917 0 0         0 11
    "Andaman and Nicobar Islands"  3 .00025188917 0 0         0 12
    "Andaman and Nicobar Islands"  3 .00025188917 0 0         0 10
    "Andaman and Nicobar Islands"  3 .00025188917 0 0         0  6
    "Andaman and Nicobar Islands"  3 .00025188917 0 0         0  5
    "Andaman and Nicobar Islands"  4  .0024469234 0 0         0  4
    "Andaman and Nicobar Islands"  4  .0024469234 0 0         0  8
    "Andaman and Nicobar Islands"  4  .0024469234 0 0         0  3
    "Andaman and Nicobar Islands"  4  .0024469234 1 0         0 12
    "Andaman and Nicobar Islands"  4  .0024469234 0 0         0  2
    "Andaman and Nicobar Islands"  5   .002626844 0 0         0  8
    "Andaman and Nicobar Islands"  5   .002626844 0 0         0  3
    "Andaman and Nicobar Islands"  5   .002626844 1 0         0 11
    "Andaman and Nicobar Islands"  5   .002626844 0 0         0  1
    "Andaman and Nicobar Islands"  5   .002626844 0 0         0  5
    "Andaman and Nicobar Islands"  5   .002626844 0 0         0  4
    "Andaman and Nicobar Islands"  6   .002806765 0 0 .05048579  2
    "Andaman and Nicobar Islands"  6   .002806765 0 0 .05048579  3
    "Andaman and Nicobar Islands"  6   .002806765 0 0 .05048579 10
    "Andaman and Nicobar Islands"  6   .002806765 1 0 .05048579 11
    "Andaman and Nicobar Islands"  7   .004713926 0 0 .09640159  6
    "Andaman and Nicobar Islands"  7   .004713926 0 0 .09640159  1
    "Andaman and Nicobar Islands"  7   .004713926 1 0 .09640159 11
    "Andaman and Nicobar Islands"  7   .004713926 0 0 .09640159  3
    "Andaman and Nicobar Islands"  7   .004713926 1 0 .09640159  2
    "Andaman and Nicobar Islands"  7   .004713926 0 0 .09640159  9
    "Andaman and Nicobar Islands"  8   .008312343 0 0 .23756747  3
    "Andaman and Nicobar Islands"  8   .008312343 0 0 .23756747  7
    "Andaman and Nicobar Islands"  8   .008312343 1 0 .23756747  2
    "Andaman and Nicobar Islands"  9   .008312343 1 0         0 11
    "Andaman and Nicobar Islands"  9   .008312343 0 0         0  5
    "Andaman and Nicobar Islands"  9   .008312343 0 0         0  7
    "Andaman and Nicobar Islands"  9   .008312343 1 0         0  2
    "Andaman and Nicobar Islands"  9   .008312343 0 0         0  4
    "Andaman and Nicobar Islands" 10   .008312343 0 0         0  3
    "Andaman and Nicobar Islands" 10   .008312343 0 0         0  7
    "Andaman and Nicobar Islands" 10   .008312343 0 0         0  4
    "Andaman and Nicobar Islands" 10   .008312343 1 0         0 10
    "Assam"  4 .000023328375 0 0 .0004007482  9
    "Assam"  4 .000023328375 0 0 .0004007482  3
    "Assam"  4 .000023328375 0 0 .0004007482  5
    "Assam"  4 .000023328375 0 0 .0004007482  6
    "Assam"  4 .000023328375 0 0 .0004007482 10
    "Assam"  5    .000079983 1 0 .0011926632 11
    "Assam"  5    .000079983 0 0 .0011926632  5
    "Assam"  5    .000079983 0 0 .0011926632 10
    "Assam"  5    .000079983 0 0 .0011926632  4
    "Assam"  5    .000079983 0 0 .0011926632  9
    Assam"  6  .00009456323 1 0  .008996838 11
    "Assam"  6  .00009456323 0 0  .008996838  8
    "Assam"  6  .00009456323 0 0  .008996838  5
    "Assam"  6  .00009456323 0 0  .008996838 13
    "Assam"  7   .0001028948 0 0  .011068898  6
    "Assam"  7   .0001028948 0 0  .011068898  7
    "Assam"  7   .0001028948 0 0  .011068898  8
    "Assam"  7   .0001028948 0 0  .011068898  1
    "Assam"  7   .0001028948 0 0  .011068898 10

  • #2
    How do I create a variable that reflects the values of variable "confirmed_1" for the previous week?
    You can't. It's a mathematical impossibility. Take a look at observation #7: Adaman and Nicobar Islands for week 4. There are 6 different possibilities for "the" value of confirmed_1 for the previous week--one in each of the 6 observations covering that state in week 3. You need some rule for picking which one is the appropriate one.

    Now, you didn't explain the other variables in your data, so I'm just going to go out on a limb here and make a guess. I'm going to guess that what you really want to do is consider the value of confirmed_1 for the previous week in the same eventBlock. In your example data, at least, for any combination of state and eventBlock in a given week there is, at most, one observation for that state in the previous week for that same eventBlock. So the lag is defined. To implement this in code, you would do this:
    Code:
    egen panel = group(state eventBlock)
    xtset panel weeknum
    and then you could refer to the lagged value of confirmed_1 in your regression as L1.confirmed_1.

    If I have guessed wrong about eventBlock, perhaps there is some other variable, or group of variables, that would serve the same purpose: to indicate which of the many observations for a given state and week is "the" previous observation. Unless there is some such variable or group of variables, what you seek is literally impossible, as "the" lag is undefined.

    Comment


    • #3
      Clyde Schechter Thank you for your response. I'm a little confused - where do you see "6 different possibilities for "the" value of confirmed_1 for the previous week"? While, yes, there are six rows, the value for confirmed_1 in all six is the same. So if any of them are picked it is perfectly fine.

      I apologize for not explaining the other variables. However, your guess worked perfectly and the code generates just the lag I need. Thanks a lot for that!

      Comment


      • #4
        where do you see "6 different possibilities for "the" value of confirmed_1 for the previous week"? While, yes, there are six rows, the value for confirmed_1 in all six is the same.
        Oh! I didn't notice that they were all the same! If I had, I actually would have done things a bit differently. But since what you have is working, we'll leave things as they are.

        Comment

        Working...
        X