Bookmark and Share

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: Keep observations in panel data


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: Keep observations in panel data
Date   Tue, 18 Jan 2011 21:15:24 +0000

I'm missing in this (and my previous posts) the extra condition that the year before a subsidy must not be a time of subsidy. 

Here is my code recast for that: 

(-tsspell- solution)

tsset id year
tsspell, fcond(subsidy == 0 & subsidy[_n+1] == 1 & subsidy[_n+2] == 0 & subsidy[_n+3] == 0)
drop if _spell == 0 | _seq >= 5

(history wrapped up in string)

gen history = ""
bysort id (year) : replace history = history[_n-1] + string(subsidy)
by id : replace history = history[_N]
by id : gen start = strpos(history, "0100") == _n
by id : keep if start == 1 | start[_n-1] == 1 | start[_n-2] == 1 | start[_n-3] == 1 

(one-liner) 

local s "subsidy"
bysort id (year) : keep if
(`s' == 0 & `s'[_n+1] == 1 & `s'[_n+2] == 0 & `s'[_n+3] == 0) 
|
(`s'[_n-1] == 0 & `s' == 1 & `s'[_n+1] == 0 & `s'[_n+2] == 0) 
| 
(`s'[_n-2] == 0 & `s'[_n-1] == 1 & `s' == 0 & `s'[_n+1] == 0) 
| 
(`s'[_n-3] == 0 & `s'[_n-2] == 1 & `s'[_n-1] == 0 & `s' == 0)    

Nick Cox

Here is another way to do it using -tsspell- from SSC:

. tsset id year
. tsspell, fcond(subsidy & !subsidy[_n+1] & !subsidy[_n+2])

. l

     +--------------------------------------------+
     | id   year   subsidy   _spell   _seq   _end |
     |--------------------------------------------|
  1. |  1   2000         0        0      0      0 |
  2. |  1   2001         0        0      0      0 |
  3. |  1   2002         1        1      1      0 |
  4. |  1   2003         0        1      2      0 |
  5. |  1   2004         0        1      3      0 |
     |--------------------------------------------|
  6. |  1   2005         1        1      4      1 |
  7. |  2   2000         0        0      0      0 |
  8. |  2   2001         0        0      0      0 |
  9. |  2   2002         1        0      0      0 |
 10. |  2   2003         1        0      0      0 |
     |--------------------------------------------|
 11. |  2   2004         0        0      0      0 |
 12. |  2   2005         1        0      0      0 |
 13. |  3   2000         1        0      0      0 |
 14. |  3   2001         1        0      0      0 |
 15. |  3   2002         1        0      0      0 |
     +--------------------------------------------+

. drop if _spell == 0 | _seq >= 4

. l

     +--------------------------------------------+
     | id   year   subsidy   _spell   _seq   _end |
     |--------------------------------------------|
  1. |  1   2002         1        1      1      0 |
  2. |  1   2003         0        1      2      0 |
  3. |  1   2004         0        1      3      0 |
     +--------------------------------------------+

Nick Cox

Here is another way to tackle this that hinges on being able to pack
the history into a string variable:

 gen history = ""
 bysort id (year) : replace history = history[_n-1] + string(subsidy)
 by id : replace history = history[_N]
 by id : gen start = strpos(history, "100") == _n
 by id : keep if start == 1 | start[_n-1] == 1 | start[_n-2] == 1


On Tue, Jan 18, 2011 at 3:29 PM, Nick Cox <[email protected]> wrote:

> bysort id (year) : keep if
> (subsidy == 1 & subsidy[_n+1] == 0 & subsidy[_n+2] == 0) |
> (subsidy == 0 & subsidy[_n-1] == 1 & subsidy[_n+1] == 0) |
> (subsidy == 0 & subsidy[_n-2] == 1 & subsidy[_n-1] == 0)
>


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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index