Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: patterns of panel data (indicators of)


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: patterns of panel data (indicators of)
Date   Mon, 12 Aug 2002 14:14:15 +0100

gcruces@worldbank.org started a thread with 
> 
> I am working with panel data and I found the program 
> xtpattern extremely useful.
> Basically, it creates a string variable for each 
> observation with the patterns
> you would obtain by "xtdes" (describing) the data, sp say:
> 1.1..1
> indicates that a unit of observation (say, a household) is 
> present on time
> periods 1,3 and 6.
> What I need now is to know how many contiguous observations 
> there are, so for
> instance:
> 1.1..1 would be zero
> 111111 would be six
> 111.11  would be (perhaps) three (the max).
> 
> The obvious thing would be to parse through the string 
> generated by xtpattern...
> but I couldn't find an elegant way around this problem. Any 
> ideas on this?
> 

I had forgotten about -xtpattern-, but it's not obvious to me 
that it's a good starting point. I tried using -spell-, but 
couldn't get that to work... Incidentally, -spell- was 
written by Richard Goldstein as well as myself. 

So here's another approach. 

As I understand it, Guillermo wants the length of the longest 
run of consecutive observations. 

Here's a made-up panel data set: 

. l

         panel       time
  1.         1          1
  2.         1          2
  3.         1          3
  4.         1          5
  5.         1          7
  6.         2          1
  7.         2          3
  8.         2          4
  9.         2          7
 10.         2          8
 11.         2         10

. tsset panel time
       panel variable:  panel, 1 to 2
        time variable:  time, 1 to 10, but with gaps

A spell of consecutive observations ends when F.time is missing: 

. bysort panel (time) : gen gapfollows =   F.time 
(7 missing values generated)

. l 

         panel       time  gapfoll~s
  1.         1          1          2
  2.         1          2          3
  3.         1          3          .
  4.         1          5          .
  5.         1          7          .
  6.         2          1          .
  7.         2          3          4
  8.         2          4          .
  9.         2          7          8
 10.         2          8          .
 11.         2         10          .

Let's chop into spells using those markers. The 
white magic here is that gap[0] is also missing. 

. bysort panel : gen spell = sum(gap[_n-1] == .) 

. l 

         panel       time  gapfoll~s      spell
  1.         1          1          2          1
  2.         1          2          3          1
  3.         1          3          .          1
  4.         1          5          .          2
  5.         1          7          .          3
  6.         2          1          .          1
  7.         2          3          4          2
  8.         2          4          .          2
  9.         2          7          8          3
 10.         2          8          .          3
 11.         2         10          .          4

For each spell, we want the number of observations: 

. bysort panel spell : gen freq = _N 

. l 

         panel       time  gapfoll~s      spell       freq
  1.         1          1          2          1          3
  2.         1          2          3          1          3
  3.         1          3          .          1          3
  4.         1          5          .          2          1
  5.         1          7          .          3          1
  6.         2          1          .          1          1
  7.         2          3          4          2          2
  8.         2          4          .          2          2
  9.         2          7          8          3          2
 10.         2          8          .          3          2
 11.         2         10          .          4          1

For each panel, we want the longest run: 

. bysort panel (freq)  : gen longestrun = freq[_N] 

. l 

         panel       time  gapfoll~s      spell       freq  longest~n
  1.         1          7          .          3          1          3
  2.         1          5          .          2          1          3
  3.         1          1          2          1          3          3
  4.         1          3          .          1          3          3
  5.         1          2          3          1          3          3
  6.         2         10          .          4          1          2
  7.         2          1          .          1          1          2
  8.         2          8          .          3          2          2
  9.         2          3          4          2          2          2
 10.         2          4          .          2          2          2
 11.         2          7          8          3          2          2

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

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index