Statalist


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

Re: st: loops


From   n j cox <[email protected]>
To   [email protected]
Subject   Re: st: loops
Date   Mon, 24 Sep 2007 19:34:44 +0100

I agree with Svend: this is all a matter of precision.
Of the nine numbers 0.1(0.1)0.9, only one (0.5) has an
exact binary equivalent.

Partly because of this, I typically loop over integers:

use ~/desktop/beetle.dta
gen p = r/n
forvalues i = 1/3 {
glm p ldose if beetle==`i', f(b)
forvalues j = 1/9 {
display as text "LD `j'0 = " ///
as res logit(`j'/10) - _b[_cons])/_b[ldose]
}
}

Note in passing that there is a -logit()- function.

Nick
[email protected]

Svend Juul replied to Arun:

--------------------------------------------------------------------------------
I am dealing with huge datasets for which I have to
estimate percentile exposure time within two to three
subsets. I noticed something that happened there and
tried duplicating it with a stata example dataset
[webuse beetle] and I noticed it again. Attached below
is the do file modified for beetle dataset.

/* Stata/IC 10.0 for Macintosh */
/* Do file to assess LD10-90 for beetle.dta */
/* Obtain beetle from webuse beetle */
use ~/desktop/beetle.dta
gen p=r/n
forvalues i=1/3 {
glm p ldose if beetle==`i', f(b)
forvalues j=0.1(0.1)0.9 {
local k=`j'*100
display as text "LD `k' = " as result (log(`j'/(1-`j'))-_b[_cons])/_b [ldose]
}
}

A part of the result for beetle.dta looks like this...
...
LD 10 = 1.7169257
LD 20 = 1.7389921
LD 30 = 1.7536588
LD 40 = 1.7656816
LD 50 = 1.7767148
LD 60 = 1.787748
LD 70 = 1.7997708
LD 80 = 1.8144375
LD 89.99999999999999 = 1.8365039

Why is this last LD 90 showing as 89.99... ... .
Am I doing something odd (bad math) in the do file?
--------------------------------------------------------------

You do things right, but computers (not only Stata)
do have limited precision; see, e.g.,
http://www.stata.com/support/faqs/data/prec.html <http://www.stata.com/support/faqs/data/prec.html> .

Take a look at this:

. local k=1/3

. display `k'
.33333333

. display "`k'"
.3333333333333333

If a macro is expanded to a text string it contains the
result in double precision. If you let -display- display
it, you see fewer digits. This means that you could write

display "LD " `k' " = ..."

rather than

display "LD `k' = ..."

to get:

LD 90 = 1.8365039


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