Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: generate Spell Counter or Duration Variable


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: generate Spell Counter or Duration Variable
Date   Mon, 10 Dec 2012 10:42:04 +0000

Thanks to Rebecca for the mention. There is an independent tutorial at

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

When I started writing that, there was an idea in my mind that it
would end up mentioning -tsspell-. But it started out as a tutorial on
the principles, with lots of simple examples, and it got long enough,
so it ended that way too. I doubt I'll ever write a sequel on
-tsspell- itself, as the help file is quite detailed, and there are
plenty of other things to write up first.

Nick

On Fri, Dec 7, 2012 at 8:42 PM, Rebecca Pope <rebecca.a.pope@gmail.com> wrote:
> David,
> I'm still confused. You say you give the "final" data set below. But
> what you are wanting is the duration in a particular state, right?
> When I look at what you pasted in, duration looks to me like a count
> of # of observations in that set of "d"s, not an actual duration (i.e.
> the difference between the time the firm entered the state & the time
> they left). Maybe I'm misunderstanding what you're wanting...
>
> Are we supposed to use "Duration" as you already have it to create
> some other duration?
>
> Or, is this the variable you really want & you don't know how to get
> it? Here's how:
> ** begin **
> bys id (date2): gen duration1 = cond(d==1 & d!=d[_n-1],1,cond(d==1 &
> d==d[_n-1],sum(d),0))
> ** end **
>
> Counting all of the 1s per firm, which gives you the number of months
> in the "1" state, but not days, since your dates are (I think) the
> last business day of the month:
> ** begin **
> tempvar one duration
> egen `one' = tag(id)
> bys id: egen `duration' = total(d)
> gen duration = `duration' if `one'
> ** end **
>
> or if you don't mind destroying your data (temporarily) just
> -collapse (sum) d, by(id)-
>
> If you want to know the # of days that the firm spends in each spell
> (i.e. if the gap between first 3 ones and the next is somehow
> significant), then check out -tsspell-. It is my go-to command for
> finding state durations & was written by Nick Cox. It is available
> from SSC (-ssc install tsspell-). It will create a variable (by ID)
> that indicates each distinct "spell" and when it ends. From there it
> is easy to calculate start & end of each spell from your existing id,
> date2, & the new spell identifier. You'll need to do some preparatory
> data manipulation, but it is all pretty basic stuff.
>
> Hope this helps,
>
> Rebecca
>
>
> On Fri, Dec 7, 2012 at 1:30 PM, David Ashcraft <ashcraftd@rocketmail.com> wrote:
>> I am sorry for the confusion: -d- is the dummy variable reflecting change of state from negative to positive (0,1). The final data set would be similar to one appended below.
>>
>> David
>>
>>
>> ----- Original Message -----
>> From: Nick Cox <njcoxstata@gmail.com>
>> To: statalist@hsphsun2.harvard.edu
>> Cc:
>> Sent: Friday, December 7, 2012 8:18:22 PM
>> Subject: Re: st: generate Spell Counter or Duration Variable
>>
>> Somebody else may understand this, but I don't think this is complete.
>> What's -d-, for example?
>>
>> Nick
>>
>> On Fri, Dec 7, 2012 at 4:52 PM, David Ashcraft <ashcraftd@rocketmail.com> wrote:
>>> Hi Statalist,
>>>
>>> I wanted to generate a duration variable to determine, how long a firm remained in a specific state. the dataset is panel where T>N. The change in state of the firm is explained by a dummy variable. The duration variable as shown below capture duration of each spell. I thought of using -xi i.d- but it generate the same variable as -d-.
>>>
>>> id    date2  d    Duration
>>> 1     16587  1     1
>>> 1     16617  1     2
>>> 1     16646  1     3
>>> 1     16679  0     0
>>> 1     16709  1     1
>>> 1     16740  0     0
>>> 1     16770  1     1
>>> 1     16800  0     0
>>> 1     16832  1     1
>>> 1     16860  0     0
>>> 1     16891  1     1
>>>
>>
>> *
>> *   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/
>>
>>
>> *
>> *   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/
>
> *
> *   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/

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


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