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

st: RE: observation about float()


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: observation about float()
Date   Wed, 25 Sep 2002 19:58:36 +0100

VISINTAINER PAUL
>
> I have an interesting problem (or not) about float().
>
> I have a small program to categorize a continuous variable
> into quartiles,
> and then assigns obs within each quartile the median of the
> quartile range.
>
> The commands are:
>
> xtile varx=var, nq(4)
> sort varx
>   forvalues num=1/4 {
>       sum var if varx==`num', de
>       replace varx=r(p50) if varx==`num'
> }
>
> The median for the first quartile (Q1) was 1.43.  When I
> tried to list the
> observations in the first quartile, there were no observations.
>
> 	.list if varx==float(1.43)
> 	.(no observations)
>
> In looking at the data editor, the assigned values for Q1
> were 1.4300001
> (varx is stored as a float).  Using -recast- and changing
> the storage to a
> double didn't seem to work.
>
> If I enter by hand the value "1.43" using the data editor
> as the median for
> the first observation and then type
>
> 	.list if varx==float(1.43)
> 	.(output for one observation)
>
> The hand-entered value is listed as 1.4299999, while all others are
> 1.4300001
>
> It seems that I can get float() to work on all observations with the
> following code:
>
> 	.gen x=varx*100
> 	.gen vary=x/100
>
> 	.list if vary==float(1.43)
> 	.(output for all observations)
>
> In this case, the value of "varx" is 1.4300001, whereas after the
> conversion, the value of "vary" is 1.4299999.
>
> It appears that float() is sensitive to the direction of
> the error.  My
> question is whether this is expected behavior of float()
> and is there a
> better way for dealing with these types of values

There will be more subtle advice, but my suggestion is that the
best way to solve this problem is to avoid it altogether.
In effect, you are overwriting a variable which would
be useful later.

Instead of

> xtile varx=var, nq(4)
> sort varx
>   forvalues num=1/4 {
>       sum var if varx==`num', de
>       replace varx=r(p50) if varx==`num'
> }

go

xtile varx=var, nq(4)
generate median = .
forvalues num=1/4 {
       sum var if varx==`num', de
       replace median = r(p50) if varx==`num'
}

or

xtile varx = var, nq(4)
egen median = median(var), by(varx)

and then later

list if varx==1

and so on.

That way, you never need spend any time worrying
about how Stata is treating the binary-decimal
conversion.

Nick
[email protected]

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