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

st: Re: patterns of panel data


From   Christopher F Baum <baum@bc.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Re: patterns of panel data
Date   Sat, 10 Aug 2002 10:14:03 -0500

--On Saturday, August 10, 2002 2:33 -0400 Guillermo wrote:

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?

Dick Campbell responded that if we changed the . to 0 and treated the result as a binary number, we could get at the issue. Not quite sure this will do that (especially in a language like Stata's that is a bit short on bitwise operators--you really need a bitwise 'shift' here), since the issue is the number of consecutive 1's. But Nick Cox (Harry Potter's sidekick) has also provided a solution to that problem, known as
-spell-.

BUT whilst embedding spell in a hacked version of xtpattern, I noticed one leetle flaw in xtpattern. If a year (time period) is missing completely from your panel--which is sometimes the case--it is not picked up in the pattern. Quite reasonably, since xtpattern can only interpret what is ever present. But for Guillermo's purposes I think we better deal with that issue.

You may within Stata

. copy http://fmwww.bc.edu/ec-p/baum/xtpattern2.ado .

and you must also

. ssc install spell

If you don't have this installed. Then it will do this job, as

xtpattern2, gen(p2) con(consec)

where the 'con' argument will be a (new) integer variable containing the greatest number of consecutive observations, including any missing from all panels.

E.g. from a hacked version of the grunfeld data, xtpattern yields:

firm year p
1. 1 1935 111111111111.111.11
2. 1 1936 111111111111.111.11
3. 1 1937 111111111111.111.11
4. 1 1938 111111111111.111.11
5. 1 1939 111111111111.111.11
6. 1 1940 111111111111.111.11
7. 1 1941 111111111111.111.11
8. 1 1942 111111111111.111.11
9. 1 1943 111111111111.111.11
10. 1 1944 111111111111.111.11
11. 1 1945 111111111111.111.11
12. 1 1946 111111111111.111.11
13. 1 1948 111111111111.111.11
14. 1 1949 111111111111.111.11
15. 1 1951 111111111111.111.11
16. 1 1953 111111111111.111.11
17. 1 1954 111111111111.111.11
18. 2 1935 11111111111.1..11.1
...
81. 5 1949 1111111111111111111
82. 5 1951 1111111111111111111
83. 5 1952 1111111111111111111
84. 5 1953 1111111111111111111
85. 5 1954 1111111111111111111


Note that 1950, which I have dropped from all years, is not flagged.
After running xtpattern2,

firm year p2 consec
1. 1 1935 111111111111.11.1.11 12
2. 1 1936 111111111111.11.1.11 12
3. 1 1937 111111111111.11.1.11 12
4. 1 1938 111111111111.11.1.11 12
5. 1 1939 111111111111.11.1.11 12
6. 1 1940 111111111111.11.1.11 12
7. 1 1941 111111111111.11.1.11 12
8. 1 1942 111111111111.11.1.11 12
9. 1 1943 111111111111.11.1.11 12
10. 1 1944 111111111111.11.1.11 12
11. 1 1945 111111111111.11.1.11 12
12. 1 1946 111111111111.11.1.11 12
13. 1 1948 111111111111.11.1.11 12
14. 1 1949 111111111111.11.1.11 12
15. 1 1951 111111111111.11.1.11 12
16. 1 1953 111111111111.11.1.11 12
17. 1 1954 111111111111.11.1.11 12
18. 2 1935 11111111111.1...11.1 11
19. 2 1936 11111111111.1...11.1 11
...
81. 5 1949 111111111111111.1111 15
82. 5 1951 111111111111111.1111 15
83. 5 1952 111111111111111.1111 15
84. 5 1953 111111111111111.1111 15
85. 5 1954 111111111111111.1111 15

which properly notes the missing 1950 obs (and thus the length of p2 does not agree with that of p from xtpattern).

Kit




*
* 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