Announcement

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

  • Lagged variable that covers a range of time?

    Hello:

    I am a new user and I am wondering if there a way in Stata to create a lagged variable that covers a range of time?

    I have a dataset of daily observations of violence between two countries across a disputed border. What I’m interested in looking at is whether there is any relationship between the local patterns of violence and international political events. So, for example, do we see a spike in violence +/ -5 days around a meeting of the UN General assembly or the arrival of an international delegation to the disputed region, etc.?

    I understand how to make “point“ leading and lag variables (-1 day, -2 days, -3 days) but I’m less interested in knowing if we see peaks of violence specifically three days after such a meeting than if we see them at any time in five days before or after.

    I know that this can be coded using “brute force” manual methods, but I am hoping there is some kind of way to do it via the software.

    Thank you in advance!

  • #2
    You didn't show any example data, so I made a demonstration data set to illustrate the one-line solution. Of course, your real data may be nothing like this and it may not work for you.

    Code:
    //  CREATE EXAMPLE DATA SET
    clear*
    
    set obs 50
    
    gen date = _n + td(1jan2021)
    format date %td
    set seed 1234
    gen event = runiform() < 0.05
    
    //  CALCULATE WHETHER ANY EVENT WITHIN 5 DAYS BEFORE OR AFTER
    rangestat (max) event, interval(date -5 5)
    -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer. It is available from SSC.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment

    Working...
    X