Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: RE: RE: Using While Loops to Sample Decimal Values and Stata Programming Support?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: RE: Using While Loops to Sample Decimal Values and Stata Programming Support?
Date   Wed, 14 Jan 2009 13:13:07 -0000

Your problems arise because you cannot hold all the decimals 0(0.1)1
exactly in a binary computer. I alluded previously to this precision
problem, which is very well documented (-search precision-). 

I don't know anything in detail about the user-written programs you are
using. 

In terms of your loop, and omitting other kinds of statement, you could
have something like 

forval B = 0/10 {

	
	local b = `B'/ 10 
	


}

And use either `B' or `b' as appropriate. 

Note: -forval- takes care of initialising and incrementing its index.
You do _not_ initialise it or increment it yourself. This would be
clearer to you if you had read the article recommended earlier in this
thread by Martin Weiss! 

Note also, given precision problems, that testing 

	if `b' == 0.1 

is much more likely to fail than 

	if `B' == 1 

Nick 
[email protected] 

John Ayers

Thanks Nick and Martin. I found that forvalues egen dmaxis=
fill(0(0.1)1) did not generate the correct values, after counting to
about .6 it shifted to wild values.

I am using a add on, clarify, to simulate the quantities of interest
for 0 .1 .2 ... .9 1. Unfortunately "setx" will only accept built in
commands, e.g. percent, or macros. As a result where should I place
the division by 10? Moreover, should the final line of code be
altered? See comments in code below

estsimp regress drinking accult asuport2 ac_sup amodel2 ac_mod ///
edu work married age, genname (dmrate)
generate ploo = .
generate pmdd = .
generate phii = .
gen dmaxis = (_n - 1) / 100 in 1/101
setx ac_sup $simac_sup  ac_mod $simac_mod  ///
(asuport2 accult edu work married age) mean
local b = 0
forval `b' = 0/10 {
***WHERE SHOULD THE DIVISOR BY 10 GO?
   setx amodel2 `b'
   simqi, genev(pii)
   _pctile pii, p(5,50,95)
   replace ploo = r(r1) if dmaxis==`b'
   replace pmdd = r(r2) if dmaxis==`b'
   replace phii = r(r3) if dmaxis==`b'
   drop pii
   local b = `b' + 1
***IS THE ABOVE LINE CORRECT FOR MY TASK?

   }
sort dmaxis


On Tue, Jan 13, 2009 at 1:00 PM, Nick Cox <[email protected]> wrote:

> I'd tweak that advice.
>
> I'd use
>
> forval i = 0/10 {
>
>
> }
>
> And divide by 10 within the loop. That way you are less likely to get
> bitten by small binary/decimal precision issues.
>
> generate x = (_n -1 in 1/101
> gen dmaxis = x / 100
>
> could be just that
>
> gen dmaxis = (_n - 1) / 100 in 1/101
>
> (Note the extra parenthesis.)
>
> Nick
> [email protected]
>
> Martin Weiss
>
> Re your -while- loop: Use -forvalues b=0(0.1)1- if the values you want
> are
> regularly spaced. Otherwise, -foreach- will happily take -any_list-
that
> you
> supply to it. -egen, fill()- may be helpful for your first task :-)
>
> John Ayers
>
> Please see my question embedded in the code (in all caps as a
> comment).
>
> estsimp regress drinking accult asuport2 ac_sup amodel2 ac_mod ///
> edu work married age, genname (dmrate)
> generate ploo = .
> generate pmdd = .
> generate phii = .
> generate x = (_n -1 in 1/101
> gen dmaxis = x / 100
> ******IS THERE A BETTER METHOD TO DO THE ABOVE 2 LINES IN A SINGLE
> LINE OF CODE?, I.E. GEN A VARIABLE .01 .02 ... .99 1.00?
> setx ac_sup $simac_sup  ac_mod $simac_mod  ///
> (asuport2 accult edu work married age) mean
> local b = 0
> while `b' <= 1 {
> *****HOW DO I GET THIS TO SAMPLE 0.0 .1 .2 .3 ... .9 1.0 OR SOME OTHER
> DECIMAL VALUE, INSTEAD OF ONLY 0 AND 1?*****
>   setx amodel2 `b'
>   simqi, genev(pii)
>   _pctile pii, p(5,50,95)
>   replace ploo = r(r1) if dmaxis==`b'
>   replace pmdd = r(r2) if dmaxis==`b'
>   replace phii = r(r3) if dmaxis==`b'
>   drop pii
>   local b = `b' + 1
> }
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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