Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Analysing longitudinal data |

Date |
Tue, 26 Feb 2013 16:21:50 +0000 |

A question likely to be asked is why not gen Wanted = inrange(value, 1, .) bysort ID (Wanted Date) : gen TimeSinceLast = Date - Date[_n-1] if Wanted The answer is that this is _almost_ what is desired. But the sort order is then like 1 0.6 02/03/2010 1 0.6 24/05/2010 1 0.6 16/08/2010 1 0.6 10/11/2010 1 0.9 09/02/2011 1 0.7 01/05/2011 1 0.6 03/05/2011 1 0.6 09/05/2011 1 1.5 28/04/2011 1 1.3 30/04/2011 and the code then implies a spurious comparison between the first wanted date 28/04/2011 and the date in the previous observation. Set aside the empirical fact that the times here would imply a meaningless negative difference, as presumably that is not guaranteed. It is really easier the other way round! Note also that essentially the same question was asked last week, modulo whether an event is marked by an indicator variable. See http://www.stata.com/statalist/archive/2013-02/msg00731.html and the ensuing thread with suggestions by David Kantor and myself. A two-line solution seems the shortest on offer, but not necessarily the easiest to follow. Nick On Tue, Feb 26, 2013 at 3:39 PM, Nick Cox <njcoxstata@gmail.com> wrote: > I don't think any programming is needed here at all, just a few > commands issued in a do-file or interactively. > > Your first two questions are of the form > > {count|average} in moving time interval > > and both are addressed in moderate detail within > > SJ-7-3 pr0033 . . . . . . . . . . . . . . Stata tip 51: Events in intervals > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox > Q3/07 SJ 7(3):440--443 (no commands) > tip for counting or summarizing irregularly spaced > events in intervals > > http://www.stata-journal.com/sjpdf.html?articlenum=pr0033 > > The last has been discussed in various places, but rather than > references here is some sample code. > > gen NotWanted = (value < 1) | missing(value) > bysort ID (NotWanted Date) : gen TimeSinceLast = Date - Date[_n-1] if > !NotWanted > > The logic looks a little contrived, but the idea is to get a sort > order that looks like this, so that the time since the > last previous event is just the current date minus the previous date. > For individual, there will be only one such date, > 30/04/2011, for which the answer will be 2 days. The answer for the > first will be missing. > > 1 1.5 28/04/2011 > 1 1.3 30/04/2011 > 1 0.6 02/03/2010 > 1 0.6 24/05/2010 > 1 0.6 16/08/2010 > 1 0.6 10/11/2010 > 1 0.9 09/02/2011 > 1 0.7 01/05/2011 > 1 0.6 03/05/2011 > 1 0.6 09/05/2011 > > Once you have values in a variable, -summarize- gives you the mean. > > Nick > > On Tue, Feb 26, 2013 at 3:18 PM, K Jensen <k.x.jensen@gmail.com> wrote: > >> I have a dataset that looks like the sample I have pasted in at the >> end of this message, with repeated measures over time on the same >> individuals. >> >> I would like to answer questions like: >> When a value was over 1, how many were measured again within the next >> week? How many of those were also over 1? >> What was the average interval between a value being over 1 and the >> next value for that individual? >> >> How would I go about programming this in Stata? >> >> Thankyou >> >> Karin >> >> ID Value Date >> 1 0.6 02/03/2010 >> 1 0.6 24/05/2010 >> 1 0.6 16/08/2010 >> 1 0.6 10/11/2010 >> 1 0.9 09/02/2011 >> 1 0.7 01/05/2011 >> 1 1.5 28/04/2011 >> 1 1.3 30/04/2011 >> 1 0.6 03/05/2011 >> 1 0.6 09/05/2011 >> 2 0.6 14/04/2009 >> 2 0.7 23/07/2009 >> 2 0.6 16/11/2009 >> 2 0.6 29/12/2009 >> 2 0.6 30/03/2010 >> 2 0.9 01/07/2010 >> 2 1.2 27/09/2010 >> 2 0.7 12/10/2010 >> 2 0.7 26/10/2010 >> 2 0.8 25/03/2011 >> 3 0.3 21/01/2009 >> 3 0.3 05/11/2009 >> 3 0.1 01/03/2010 >> 3 0.2 24/05/2010 >> 3 0.2 18/08/2010 >> 4 0.9 16/02/2010 >> 4 1.0 01/03/2010 >> 4 0.9 12/03/2010 >> 4 1.1 25/03/2010 >> 4 0.5 01/06/2010 >> 4 0.4 31/08/2010 >> 4 0.6 30/11/2010 >> 4 0.6 19/04/2011 * * 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/

**References**:**st: Analysing longitudinal data***From:*K Jensen <k.x.jensen@gmail.com>

**Re: st: Analysing longitudinal data***From:*Nick Cox <njcoxstata@gmail.com>

- Prev by Date:
**Fwd: st: reliability with -icc- and -estat icc-** - Next by Date:
**Re: st: Mhbounds v.s. rbounds in stata** - Previous by thread:
**Re: st: Analysing longitudinal data** - Next by thread:
**st: moptimize uncommon error** - Index(es):