Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Identifying visits with 1 time period of an event
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Identifying visits with 1 time period of an event
Date
Tue, 18 Feb 2014 20:20:28 +0000
The -followup- variable is easy enough.
Also, although you have not commented, I don't think you were explicit
that there would be at most one procedure per panel. Without wanting
to peek behind the screen here, so to speak, let's think up code that
is general enough to cope with that if it happens.
A framework in which the -followup- variable is natural is thinking of
spells as being initiated by -procedure == 1-. Then we apply -tsspell-
(SSC). For that data must be -tsset- and for that in turn it's best to
lie to Stata and tell it that the time variable is just a sequence
number created for the purpose.
The dataset here is modified from your example to include a panel with
events of -procedure == 1-.
. clear
. input id time procedure
id time procedure
1. 1 1 0
2. 1 2 1
3. 1 3 0
4. 1 4 0
5. 1 5 0
6. 2 3 1
7. 2 4 0
8. 2 5 0
9. 2 7 0
10. 2 8 0
11. 2 10 0
12. 3 1 0
13. 3 2 0
14. 3 3 0
15. 3 4 0
16. 4 2 0
17. 4 3 1
18. 4 6 0
19. 4 7 0
20. 4 12 1
21. 4 13 0
22. end
. bysort id (time) : gen pseudot = _n
. tsset id pseudot
panel variable: id (unbalanced)
time variable: pseudot, 1 to 6
delta: 1 unit
. tsspell , f(procedure == 1)
. gen followup = cond(_seq, _seq - 1, 0)
. egen time1 = min(time / procedure), by(id _spell)
(6 missing values generated)
. egen time2 = min(time / (followup == 1)), by(id _spell)
(6 missing values generated)
. gen followup1 = (followup == 1) & (time2 - time1) <= 1
. l id time procedure followup*, sepby(id)
+--------------------------------------------+
| id time proced~e followup follow~1 |
|--------------------------------------------|
1. | 1 1 0 0 0 |
2. | 1 2 1 0 0 |
3. | 1 3 0 1 1 |
4. | 1 4 0 2 0 |
5. | 1 5 0 3 0 |
|--------------------------------------------|
6. | 2 3 1 0 0 |
7. | 2 4 0 1 1 |
8. | 2 5 0 2 0 |
9. | 2 7 0 3 0 |
10. | 2 8 0 4 0 |
11. | 2 10 0 5 0 |
|--------------------------------------------|
12. | 3 1 0 0 0 |
13. | 3 2 0 0 0 |
14. | 3 3 0 0 0 |
15. | 3 4 0 0 0 |
|--------------------------------------------|
16. | 4 2 0 0 0 |
17. | 4 3 1 0 0 |
18. | 4 6 0 1 0 |
19. | 4 7 0 2 0 |
20. | 4 12 1 0 0 |
21. | 4 13 0 1 1 |
+--------------------------------------------+
There is a detailed discussion of spells in
SJ-7-2 dm0029 . . . . . . . . . . . . . . Speaking Stata: Identifying spells
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
Q2/07 SJ 7(2):249--265 (no commands)
shows how to handle spells with complete control over
spell specification
http://www.stata-journal.com/sjpdf.html?articlenum=dm0029
When I started writing that, there was an intention to mention
-tsspell-, but the article grew long and the deadline drew near, and
it did not happen. So, as it were, -tsspell- is one practical
implementation and that 2007 article is the theory, such as it is.
I'd assert that several, although naturally not all, spell
manipulations are made easier by that machinery.
Nick
[email protected]
On 18 February 2014 18:39, paul o'brien <[email protected]> wrote:
> Thanks Nick,
>
> I think I have not been clear enough. I have the first 3 variable, and
> want to create the two on the right, follow-up and follow-up within 1
> time period.
>
> The key thing is how to mark visits that follow the procedure. So I
> need to create a variable followup with the value 1 for the first
> observation/visit after the procedure. With that I can use your
> suggestion to identify the follow-up visits within 1 time period after
> the procedure.
>
> Thanks,
>
> Paul
>
> On 17 February 2014 23:33, Nick Cox <[email protected]> wrote:
>> The first time when something happened is one and the same as the
>> minimum time. For panel data, -egen-'s -min()- function serves fine to
>> identify such times separately by panels.
>>
>> Here "something" means either -procedure == 1- or -followup == 1- so
>> specifying either condition can be done by instructing Stata to ignore
>> anything else. -min()- takes expressions, so that can be made explicit
>>
>> egen time1 = min( cond(procedure == 1, time, .) ) , by(id)
>> egen time2 = min( cond(followup == 1, time, .) ), by(id)
>>
>> This is equivalent and according to taste cunning or cryptic:
>>
>> egen time1 = min(time/procedure), by(id)
>> egen time2 = min(time/(followup == 1)), by(id)
>>
>> Now we are on the home straight and contemplating reward:
>>
>> gen lag = time2 - time1
>>
>> gen within1 = (time2 - time1) <= 1
>>
>> For a self-indulgent exploration of similar trickery, see
>>
>> http://www.stata-journal.com/article.html?article=dm0055
>>
>> More concise statements are likely to be possible, but may defy
>> simultaneous maintenance of sanity and balancing of parentheses.
>>
>> Nick
>> [email protected]
>>
>> On 17 February 2014 20:23, Paul O'Brien <[email protected]> wrote:
>>
>>> I have longitudinal data on visits to a clinic during which a procedure sometimes occurs.
>>>
>>> I want to mark follow-up visits if the procedure occurred and mark the visits within 1 time period of the procedure, as in the last two variables here.
>>>
>>> clear
>>> input ///
>>> id time procedure followup followup1
>>> 1 1 0 0 0
>>> 1 2 1 0 0
>>> 1 3 0 1 1
>>> 1 4 0 2 0
>>> 1 5 0 3 0
>>> 2 3 1 0 0
>>> 2 4 0 1 1
>>> 2 5 0 2 0
>>> 2 7 0 3 0
>>> 2 8 0 4 0
>>> 2 10 0 5 0
>>> 3 1 0 0 0
>>> 3 2 0 0 0
>>> 3 3 0 0 0
>>> 3 4 0 0 0
>>> 4 2 0 0 0
>>> 4 3 1 0 0
>>> 4 6 0 1 0
>>> 4 7 0 2 0
>>> end
>>>
>>> I have struggled with _n and _N but cannot get the follow to start after the event.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/