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: spell variable according to a criteria


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: spell variable according to a criteria
Date   Wed, 4 Apr 2012 19:19:18 +0100

I can't see a difference here in terms of what Stata will do between
your first and second postings.

In terms of your original problem, I see no need to -sort- each time
around the loop as no command changes the sort order. In fact I see no
need for a loop here at all.

The problem appears to be this:

Within panels, if -event- is 1 and -stretch- is # then -event- == 1 is
the start of a spell with at most an extra # observations afterwards.

Let's think about an -event- and suppose that -stretch- is 7. Then can
we think of that of starting a count-down: 7, 6, 5, 4, 3, 2, 1, 0.
When we get to 0, the spell is finished. (NASA used to say
"Blast-off".)  But presumably a new -event- can reset the count-down.

Here is sample code setting up a fake dataset.

set obs 100
gen panel_id = cond(_n < 51, 1, 2)
bysort panel_id : gen time = _n
gen event = 1 if  runiform() < 0.2
gen stretch = ceil(18 * runiform()) if event == 1

Now the algorithm is just this:

bysort panel_id (time) : gen inspell = stretch if _n == 1
by panel_id : replace inspell = max(stretch, inspell[_n-1]-1)  if _n > 1

Now your spells are defined by -inspell- being >= 0 and not missing.

Nick

On Wed, Apr 4, 2012 at 5:06 PM, Abhimanyu Arora
<[email protected]> wrote:

> I think I managed.
>
> So in the line
> bys panel_identifier:replace event=1 if  stretch[_n-(`i'+1)]>`i' &
> stretch[_n-(`i'+1)]!=. & event==.
>
> I changed to
>
> bys panel_identifier:replace event=1 if  stretch[_n-`=`i'+1']>`i' &
> stretch[_n-`=`i'+1']!=. & event==.
>
> in order to evaluate the local macro at every loop.

On Wed, Apr 4, 2012 at 5:40 PM, Abhimanyu Arora

>> I have a (monthly) time indicator and a variable 'stretch'  that
>> identifies the spell of an event. I need to create a dummy in case the
>> event took place or not.
>> Currently event=1 for the start of the event (an missing for the
>> rest). If stretch==1, event should equal 1 for the next time period.
>> If stretch =2 event=1 for 2 more time periods (besides the starting
>> time).
>>
>> I know that stretch ranges from 1 to 18 (but may not have the values
>> in between).
>>
>> When I try without using the following loop (putting in the numbers
>> manually that is), it seems OK, But with the following loop things
>> aren't quite right.
>>
>> local i=0
>> while `i'< 18 {
>> bys panel_identifier:replace event=1 if  stretch[_n-(`i'+1)]>`i' &
>> stretch[_n-(`i'+1)]!=. & event==.
>> sort panel_id time
>> local ++i
>> }
>>
>> Where does the mistake lie?

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