Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: RE: RE: Can you get sign of value only?


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: RE: Can you get sign of value only?
Date   Fri, 24 Mar 2006 18:19:42 -0000

Sorry to quibble, but 

(y / abs(y)) is _not_ a good substitute for sign(y) 

as 0 / abs(0) will get evaluated as missing. You
know that it should be zero, but Stata's working 
on more general rules and doesn't have the mathematical 
horse sense that you possess. 

So even if in your case 0 doesn't occur, 
in other instances you will get bitten by using this. 

A cuter definition that works well in the case of 
positive, zero and negative numbers is that 

sign(y) = (y > 0) - (y < 0) 

I suspect this goes back to Kenneth E. Iverson, 
principal architect of APL and J, circa 1960. 

Nick 
n.j.cox@durham.ac.uk 

Danielle H Ferry
 
> Thanks to all who responded. On the way home, thought of this:
> 
> replace x = x * (y / abs(y))
> 
> I see that there are multiple ways of doing it. Despite the 
> fact that  
> I stupidly  asked a question which it turns out is answered in the  
> "functions" help (never occurred to me it would be a function, and  
> quick Google search turned up nothing... sorry... long day), I  
> learned something from everyone's responses.
 
> On Mar 23, 2006, at 6:27 PM, Nick Cox wrote:
> 
> > Note that the difference between this and using -sign(y)-
> > is that -sign(0)- is 0.
> >
> > A quite different point is that there is another way
> > to write what Jennifer is suggesting, namely
> >
> > gen z = cond(y < 0, -x, x)

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