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

st: RE: RE: using max and sdev functions more flexibly


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

> -----Original Message-----
> From: Nick Winter 
> Sent: Monday, November 04, 2002 10:10 AM
> To: [email protected]
> Subject: st: RE: using max and sdev functions more flexibly
> 
> 
> > -----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)
> 

I didn't take note of your desire to do this -by ccode- in my initial
posting.  If your data are properly -tsset-, then the code above will
work.  (And Nick Cox's suggestion of -egenmore-'s record() function will
also work.

But the standard deviation code I originally listed does not work.  One
approach is to use the alternate form of the standard deviation
calculation (which doesn't have great numerical accuracy, but might
serve your purposes):

	by ccode (year) : gen double s_infl=sum(infl)
	by ccode (year) : gen double s_infl2 = sum(infl^2)
	by ccode (year) : gen n = sum(1)
	gen run_sd = sqrt((n*s_infl2 - s_infl^2)/(n*(n-1)))
	drop s_infl s_infl2 n

-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