| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: -generate- and run time contingencies
[Regarding my request for generic ways to
to use while or if constructs with a run-time
value of a variable,
n j cox wrote:
>Mike wants generic ways to do itnot
>work-arounds.
>I am still on the lookout for a second
>example of his class of problems.
Actually, I seem to encounter it fairly
commonly. I've always programmed in SPSS and
other procedural languages in which something
like "while condition, calculate.. " is a
natural construct to use with a variable,
i.e, an item in an array of cases. So, FWIW,
here are some more examples that occur to me:
A classic algorithm (See Numerical Recipes...) for
generating values from Normal(0,1) would look
something like this in Stata, if -while- could
access the run-time value of a variable:
tempvar v1 v2 done
gen `done' = 0
while not `done' {
gen `v1' = 2.0 * uniform() - 1.0
gen `v2' = 2.0 * uniform() - 1.0
gen r = `v1' * `v2'
gen `done' = (r < 1.0 ) & (r > 0.0) /*
}
etc. more stuff
More generally, applications using rejection sampling
to generate a random variable would seem to be require
acces to the run-time value of a variable.
Another example occurs when one wants to do what
I think of a "rippling" through from 1 to N, using
the run-time value of a previous case.
For example, to consecutively groups of cases
based on some kind of shared value of a key,
a natural approach in other languages would
be something that would not fit with Stata:
* More Code that wouldn't work in Stata
sort SomeKey
generate GroupID = 1 if _n ==1
replace GroupID = GroupID[_n-1] ///
if (_n > 1) & !(SomeKey == SomeKey[_n-1])
I know there are ways to accomplish this goal in
Stata, but I'm trying to adapting my existing bag
of algorithmic tricks :-}.
I like the suggested approach in a later posting of doing the following:
gen x = uniform()
gen byte OK = x >= 0.8
capture assert OK == 1
while _rc {
replace x = cond(x < 0.8, uniform(), x)
replace OK = x >= 0.8
capture assert OK == 1
}
However, it does seem odd that one needs to use what looks like a
trick to get make a value of a variable something that -while- can
access at run time. I wouldn't have thought of this.
Regards,
=-=-=-=-=-=-=-=-=-=-=-=-=
Mike Lacy
Fort Collins CO USA
(970) 491-6721 office
*
* 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/