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   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: A loop for S.D calculation in paneldata set
Date   Tue, 22 Apr 2008 12:01:16 +0100

I agree with Maarten. No such loop seems to be called for. 

Other possibilities include -collapse- for reduction to a new dataset
and -tabstat- to see summary statistics. 

In any case, if you need to select (e.g.) the year being 1958 using -if-
not -in- is generally a much 
better way to go. 

Nick
[email protected] 

Maarten buis

--- 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


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