I am working with a panel dataset containing spells of events grouped within individuals. I want to identify specific observations (spells) within each panel that meet a condition which implies relating the value of these observations in a specific variable with the value of previous or posterior observations in that same variable. Basically, what I want to tell Stata is something like "identify the first observation with the value X=1 after a previous observation that has the value X=2". The key is how to tell Stata to identify that previous observation. Important: the observations within the panel that should be related are not necessarily consecutive nor are always in the same position within the spell, so the problem cannot be simply solved (to my understanding) by resorting to the [n] and [n+1] variables.
I hope that the next example clarifies my doubt. The panel variable that groups observations is a person ID. Within each person, I have chronologically ordered spells of events. I am working with intimate partner violence (IPV) criminals/aggressors and the events are the different crimes, periods of prison, periods under supervision, etc. that they had during a certain time window. For each aggressor there is an IPV selection crime. That is, the crime that was used to select this aggressor into the sample of analysis. During the whole observation period, this aggressor may have committed more IPV crimes after the selection crime. I want to identify the first IPV crime that occurs after the selection crime. How can this be done easily or efficiently in Stata? I remember that the next IPV crime may not be the event immediately after the selection crime and that there can be other spells of events between them (prison periods, etc.). Below I show a sample dataset to illustrate what I mean:
In this example, the selection crime of aggressor 1 (aggressor_id=1) happens on 1 January 2004 and the "other crime IPV" happens on 25 January 2004. For this aggressor, the two events that I need to consider are consecutive. However, for aggressor 2 the selection crime occurs on 31 July 2015 and the first other crime IPV occurs on 7 March 2016. There are other event spells in between. My question/doubt is, then, how can I tell Stata to flag the first IPV crime after the selection crime when for different aggressors they are not necessarily consecutive and when the rank order of the selection and the other IPV crime also varies across aggressors?
Thank you very much for your attention.
I hope that the next example clarifies my doubt. The panel variable that groups observations is a person ID. Within each person, I have chronologically ordered spells of events. I am working with intimate partner violence (IPV) criminals/aggressors and the events are the different crimes, periods of prison, periods under supervision, etc. that they had during a certain time window. For each aggressor there is an IPV selection crime. That is, the crime that was used to select this aggressor into the sample of analysis. During the whole observation period, this aggressor may have committed more IPV crimes after the selection crime. I want to identify the first IPV crime that occurs after the selection crime. How can this be done easily or efficiently in Stata? I remember that the next IPV crime may not be the event immediately after the selection crime and that there can be other spells of events between them (prison periods, etc.). Below I show a sample dataset to illustrate what I mean:
Code:
clear input float(aggressor_id event_date) str20 event_type 1 16071 "selection crime" 1 16095 "other crime IPV" 1 16121 "Prison entry" 1 16450 "Prison exit" 1 16055 "End observation period" 2 19573 "Other crime no IPV" 2 19590 "Prison entry" 2 20100 "Prison exit" 2 20300 "Selection crime" 2 20350 "Supervision period entry" 2 20450 "Supervision period exit" 2 20520 "Other crime IPV" end format event_date %td list aggressor_id event_date event_type
In this example, the selection crime of aggressor 1 (aggressor_id=1) happens on 1 January 2004 and the "other crime IPV" happens on 25 January 2004. For this aggressor, the two events that I need to consider are consecutive. However, for aggressor 2 the selection crime occurs on 31 July 2015 and the first other crime IPV occurs on 7 March 2016. There are other event spells in between. My question/doubt is, then, how can I tell Stata to flag the first IPV crime after the selection crime when for different aggressors they are not necessarily consecutive and when the rank order of the selection and the other IPV crime also varies across aggressors?
Thank you very much for your attention.
Comment