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

Re: st: RE: A query about min in stata

From   Gawrich Stefan <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   Re: st: RE: A query about min in stata
Date   Mon, 25 Aug 2008 11:46:05 +0200

Hi Statalisters

this is a rather simple question and I would at first favor Philippes
solution because it's easy to understand.

gen min  = min(r, r[_n-1], r[_n-2], r[_n-3], r[_n-4], r[_n-5], r[_n-6],
r[_n-7], r[_n-8], r[_n-9])
(with or without "if _n >= 9")

Same thing using (pseudo-) time series with the Lag function "L": 
gen id = _n
tsset id
gen minimum  = min(r, L1.r, L2.r, L3.r, L4.r, L5.r, L6.r, L7.r, L8.r, L9.r)

Time series analysis in Stata is very powerful but not necessary in this
case because 
like in Philippes solution one can adress the values over the system
variable _n.
So I'll stick to the _n-way further:

Nick Coxs solution saves a lot of typing for the price of being inefficient.

gen min = r 
qui forval i = 1/9 { 
	replace min = min(min, r[_n-`i']) 

But with small or medium sized datasets you'll probably won't notice any
delay. I have some sympathy for that 
because its easy to adjust the lag interval.

Nick Winters solution uses the loop to write the list of cells in a local:

forval i=1/9 {
  local list "`list', r[_n-`i']"
gen min=min(r `list')

So the user is freed from the tedious part and the syntax is efficient. 

There is only one thing to add:
The first item of the minimum list (here r or r[_n]) is not specified in the
loop but in  the "gen"-command.
If the lagging interval should start elsewhere this has to be altered
Nick Winter did this for a good reason. Created in this way the local starts
with a comma, so it 
would produce an error if nothing would stand beforehand in the gen command.

I used the extended function "subinstr" to overcome this. Now starting from
0 instead of 1 all items 
are written to the local. Like in Nick Coxs example the lag intervall
is specified in the forval line only (or in special cases with "foreach ..
numlist ..").

forval i = 0/9 {
local list "`list',  r[_n-`i']"
local list : subinstr local list "," "" // removes the first comma
gen min=min(`list')

Best wishes


*   For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index