# st: RE: Identifying coherent periods of events with irregular reoccurrence from a time sequence

 From "Nick Cox" To Subject st: RE: Identifying coherent periods of events with irregular reoccurrence from a time sequence Date Wed, 25 Feb 2009 14:49:35 -0000

```Gap calculation is easy enough, especially once you have -tsset- data
(in this case with a pseudo-time variable which is just number in
sequence for each patient).

In meteorology and climatology periods of similar conditions are often
known as spells, as may be familiar from media reports, and that
terminology likes behind

1. -tsspell- on SSC

and

2.

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

-- which, surprising though it may seem, are quite disjoint. That is, I
started out with a vague intention to write an article about -tsspell-,
but explaining the principles seemed much more important and in the end
I did not get to it.

A quite different approach is possible using -group1d- on SSC. Your data
get chopped up like this:

. group1d day, max(6)

Partitions of 12 data up to 6 groups

1 group:  sum of squares  3.2e+05
Group Size    First            Last           Mean      SD
1      12    1        0      12      461   241.50  163.74

2 groups: sum of squares 32480.23
Group Size    First            Last           Mean      SD
2       7    6      293      12      461   372.71   58.77
1       5    1        0       5      114    57.80   40.75

3 groups: sum of squares 12761.55
Group Size    First            Last           Mean      SD
3       3   10      407      12      461   434.00   22.05
2       4    6      293       9      363   326.75   27.39
1       5    1        0       5      114    57.80   40.75

4 groups: sum of squares 6395.92
Group Size    First            Last           Mean      SD
4       3   10      407      12      461   434.00   22.05
3       4    6      293       9      363   326.75   27.39
2       2    4       89       5      114   101.50   12.50
1       3    1        0       3       57    28.67   23.27

5 groups: sum of squares 3743.67
Group Size    First            Last           Mean      SD
5       3   10      407      12      461   434.00   22.05
4       2    8      342       9      363   352.50   10.50
3       2    6      293       7      309   301.00    8.00
2       2    4       89       5      114   101.50   12.50
1       3    1        0       3       57    28.67   23.27

6 groups: sum of squares 2511.00
Group Size    First            Last           Mean      SD
6       3   10      407      12      461   434.00   22.05
5       2    8      342       9      363   352.50   10.50
4       2    6      293       7      309   301.00    8.00
3       2    4       89       5      114   101.50   12.50
2       2    2       29       3       57    43.00   14.00
1       1    1        0       1        0     0.00    0.00

Groups     Sums of squares
1       321729.00
2        32480.23
3        12761.55
4         6395.92
5         3743.67
6         2511.00

The help gives a detailed explanation.

You need to spell out, pun intended, your definitions of coherent and
incoherent for those like me who are unfamiliar with drug studies.

Nick
n.j.cox@durham.ac.uk

Jakob Petersen

I have a problem identifying compliant vs. non-compliant periods in
patients' prescription history. I assume that this type of problems is
known from other areas with temporal sequences (climate, transactional,
reoffending, etc.).

How would it be possible to flag and number a) coherent periods of
prescriptions; b) gaps; c) beginning dates; and d) end dates to each
period?

Example (with two clearly separated periods):

order	day
1	0
2	29
3	57
4	89
5	114
6	293
7	309
8	342
9	363
10	407
11	434
12	461

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```