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: Generating a Count Variable of Number of Obs in a Time Interval Preceding the Current Obs


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Generating a Count Variable of Number of Obs in a Time Interval Preceding the Current Obs
Date   Tue, 19 Nov 2013 18:09:18 +0000

Let us first look at your code and explain, at least in part, why you
got your results.

(1)

-egen-'s -count()- function just counts how often -- in your example
-- the result of evaluating

timedispatch<= timecreate[_n] & timedispatch +53<= timecreate[_n]

is not missing, and it does that separately for each observation. The
subscript [_n] here does no harm, but it just means "in the current
observation" and can be omitted. So your expression is

timedispatch <= timecreate & timedispatch +53 <= timecreate

and, regardless of the meanings or values of these variables, this is
a logical expression evaluating to true or false, and as such can only
ever be 0 or 1, and as such is guaranteed to be non-missing in every
observation: hence your result.

(2)

gen countcall=o

Unless -o- is variable or scalar that looks like a typo for 0 (zero).

foreach obs of numlist 1/20 {
    sum timecreate if _n==`obs'
    local time1=r(mean)
    gen dum1=1 if timedispatch<= `time1' & timedispatch<= `time1'-53
    egen dum2=total(dum1)
    replace countcall=dum2 if _n==`obs'
    drop dum*
}

This reduces essentially to

forval i = 1/20 {
    count if timedispatch <= timecreate[`i'] & timedispatch <=
timecreate[`i'] - 53
    replace countcall = r(N) in `i'
}

and so counts how many observations  satisfy your double inequality.
But that is of the form

   x <= y & x <= y - 53

yet the first condition is redundant given the second and so we get

forval i = 1/20 {
       count if timedispatch <= timecreate[`i'] - 53
       replace countcall = r(N) in `i'
}

if I understand this correctly. However, you can evidently see that
your code is incorrect, and this may help you to understand why.

In terms of something more positive this sounds like problems discussed in

SJ-7-3  pr0033  . . . . . . . . . . . . . .  Stata tip 51: Events in intervals
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q3/07   SJ 7(3):440--443                                 (no commands)
        tip for counting or summarizing irregularly spaced
        events in intervals

which is accessible at http://www.stata-journal.com/sjpdf.html?articlenum=pr0033

What remains unclear is the exact nature of your time variables. Stata
date-times have units milliseconds, in which case 53 minutes is 53 *
60000 milliseconds. The implication is that you are using something
different, and if so you should explain what it is to get good advice.

Nick
[email protected]


On 19 November 2013 12:27, David Feinswog <[email protected]>

> I am working with Emergency Medical Services (EMS) ambulance call data
> using Stata/IC 12.  The variables of consequence are:
>
> 1. timecreate: the time when the 911 dispatcher first received the 911
> call
> 2. timedispatch: the time when the 911 dispatcher sent an EMS resource
> (i.e. ambulance) to the location
> 3. timeclose: the time when the EMS resource cleared from the call and
> returned to service
> * if it matters, times are formatted %tc
>
> I am trying to capture a measure of EMS system use load.
> Specifically, I need to generate a count of all open calls when a call
> came in.  For simplicity, I am going to keep things simple for now and
> disregard "timeclose".  I will try to capture ALL CALLS DISPATCHED IN
> THE 53 MINUTES (average call duration) PRIOR TO EACH "timecreate" OBS.
>
>
> The simple attempt using egen:
>
> egen countcall= count(timedispatch<= timecreate[_n] & timedispatch
> +53<= timecreate[_n])
>
> This returned _N for each observation.
>
>
> Attempt using foreach loops:
>
> gen countcall=o
>
> *using a limited numlist (1/20 vs 1/_N) to speed things up while I
> figure out the appropriate coding
>
> foreach obs of numlist 1/20 {
> sum timecreate if _n==`obs'
> local time1=r(mean)
> gen dum1=1 if timedispatch<= `time1' & timedispatch<= `time1'-53
> egen dum2=total(dum1)
> replace countcall=dum2 if _n==`obs'
> drop dum*
> }
>
> This returned _n for each obs in the specified numlist 1/20
>
> Please let me know if there is any additional information that could
> help.
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index