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

st: RE: using max and sdev functions more flexibly


From   "Nick Winter" <[email protected]>
To   <[email protected]>
Subject   st: RE: using max and sdev functions more flexibly
Date   Mon, 4 Nov 2002 10:10:04 -0500

> -----Original Message-----
> From: Pezzini,S (pgr) [mailto:[email protected]] 
> Sent: Monday, November 04, 2002 9:49 AM
> To: [email protected]
> Subject: st: using max and sdev functions more flexibly
> 
> 
> Dear Stata-listers, 
> 
> as my programming skills are very low, I would like to ask if 
> someone could help me get around a problem that I've been 
> trying to solve without success.
> 
> The starting point is a dataset with inflation for years from 
> 1900 to 1995 and some country codes.
> I would like to use the max and sdev function in order to 
> compute the maximum and the standard deviation of inflation 
> over an interval from 1920 (and successive decades) to every 
> year between 1975 and 1995, by country. 
> If I type:
> 
> by ccode: egen maxinf=max(inflation) if year>=1920
> 
> this only allows me to compute max and sdev over 1920 and 
> 1995, i.e. I get a constant over the whole time interval, not 
> staggered by year. I can't get to incorporate the information 
> "compute the standard deviation or the max UP TO EACH YEAR IN 
> THE INTERVAL [1975; 1995]". 
> 
> Maybe my request will be clearer if I tell you that I was 
> able to get what I wanted with another function, sum. 
> By typing:
> by ccode: gen cuminf=sum(abs(inflation)) if year>=1920
> I was able to compute the cumulative inflation from 1920 up 
> to each year in the interval [1975; 1995]. This was possible 
> because "gen y=sum(x)", computes a sum where the j-th obs on 
> y contains the sum of the first through j-th observations on x. 
> 
> Finally, I am using Stata 7.0 SE in Windows environment.
> 
> Maybe this is a straightforward problem for you, so I would 
> be very grateful if you could point me to a solution.
> 
> Thanks a lot, 
> 
> Silvia Pezzini

So you want the "running maximum", it sounds like.  This should do it:

	generate maxinf = inflation
	replace maxinf = max(maxinf,l.maxinf)

If your data are not -tsset-, then the second line should be 

	replace maxinf = max(maxinf,maxinf[_n-1])

This creates the maxinflation variable and sets it equal to the
inflation variable.  Then it takes advantage of the fact that -replace-
works its way through the data, observation by observation.  So for each
observation, if the current inflation is larger than the current
inflation, then maxinf is set to the current inflation; otherwise it is
set to the prior inflation.  So the maximum cascades through the data.
(Note that the maximum function treats all numbers as greater than
missing, so this will skip over any missing inflation values.)

For the running standard deviation, I'm not sure it is so easy.  The
-mvsumm- command (available from SSC) does moving summary statistics,
but it calculates a fixed-size window, not a running window.  You might
have to loop over observations:

	generate sdinf = .
	local N=_N
	forvalues i=1/`N' {
		quietly sum inflation in 1/`i'
		quietly replace sdinf = r(sd) in `i'
	}

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