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

st: Efficiency: was RE: Taking averages, etc.


From   Allan Reese <[email protected]>
To   Stata distribution list <[email protected]>
Subject   st: Efficiency: was RE: Taking averages, etc.
Date   Wed, 17 Dec 2003 11:51:06 +0000 (GMT)

On Wed, 17 Dec 2003, Nick Cox wrote:
> As it happens,
>
> . count if mi(mpg)
> . su mpg if r(N) == 0
>
> is not only legal, but produces the correct result.
> What happens is that Stata looks at the condition
>
> 	if r(N) == 0
>
> and says in turn: is this true for observation 1?
> for observation 2? for observation 3? and so
> forth.

By coincidence I wrote to Tech Support just yesterday on this point.
While it is possible to replace one value in a variable (say the 4th)
using:

  replace  x = y if _n == 4

that code tests _n for every element.  It's very wasteful, though you may
not notice in most interactive sessions.  In a program however, the
alternative

  replace  x = y in 4

will be about _N times faster.  As this will typically be inside a loop
dealing will each element of the variable, it may reduce execution time
from O(_N squared) to O(_N).

R. Allan Reese                       Email:     [email protected]
Associate Manager GRI                Direct voice:   +44 1482 466845
Graduate School                      Voice messages: +44 1482 466844
Hull University, Hull HU6 7RX, UK.   Fax:            +44 1482 466436
====================================================================
Be careful in handling the battery of the remote control transmitter
If swallowed consult a physician immediately for emergency treatment
               [Safety instructions: Hitachi CP-X275 data projector]

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index