Statalist


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

Re: st: A loop for S.D calculation in paneldata set


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: A loop for S.D calculation in paneldata set
Date   Tue, 22 Apr 2008 10:16:56 +0100 (BST)

--- Asgar Khademvatani <[email protected]> wrote:
> I am using stata 8.2 platform in Windows. I have loaded a panel data
> set in a long-format  for 4 sectors each sketched from 1958 to 2000. 
> Thus, I have 172 observations for each variable. My target is
> calculating S.D or mean cross-4 sectors for each year and graph over 
> time. For more clarity, for instance, I would like to 
> calculate S.D. of 4-sectors for year 1958. In doing so, I need to
> have a loop to use observations numbers: 1, 45, 87, and 131 in
> calculation of S.D for year 1958, and this continues for calculation
> of S.D cross-sectors for other years, and so on and so forth.

There is no need for a loop, you can use -by-. Say your year variable
is called year and you want the mean and standard deviation of the
variable called var:

bys year : egen mean = mean(var)
bys year : egen sd = se(var)

twoway line mean year , sort
twoway line sd year , sort


Notice that each year occurs four times in your graph, one for each
sector, but that you won't see it because all sectors have the same
cross-sector mean and standard deviation. However, they are in your
graph and make the graph larger in terms of memory than it needs to be.
With four sectors that is probably not going to be such a big deal, in
larger panels you can use the following trick which can save a lot of
memory for larger panels.

bys year : egen mean = mean(var)
bys year : egen sd = se(var)

gen byte mis = missing(mean, sd)
bys year mis : gen byte mark = _n == 1 if mis == 0

twoway line mean year if mark == 1, sort
twoway line sd year if mark == 1, sort

Hope this helps,
Maarten



-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      __________________________________________________________
Sent from Yahoo! Mail.
A Smarter Email http://uk.docs.yahoo.com/nowyoucan.html
*
*   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