Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Accumulated mean and standard deviation


From   Matthew White <[email protected]>
To   [email protected]
Subject   Re: st: Accumulated mean and standard deviation
Date   Sun, 8 Jan 2012 11:35:48 -0500

Hi Pedro,

For the mean, you can use -sum()-, which calculates the running sum:

generate mean = sum(x) / _n

The SD is harder. Here's one attempt. It was faster for me than using
-egen sd()- in a loop:

generate sd   = .
generate touse = 0
forvalues i = 1/`=_N' {
	quietly replace touse = 1 in `i'
	quietly summarize x if touse
	quietly replace sd = r(sd) in `i'
}

Best,
Matt

On Sun, Jan 8, 2012 at 10:50 AM, Pedro Nakashima
<[email protected]> wrote:
> Hi everybody
>
> I need to replicate the following data:
>
> clear*
>
> input x mean sd
> 2 2 .
> 4 3 1.4142
> 1 2.33 1.5275
> 3 2.55 1.2909
> end
>
> The variables mean and sd are like a mix of sum() and egen functions.
>
> For the variable "mean", in each observation j, its entry is the mean
> of the values of the variable "x" from observation 1 up to observation
> j.
> For example:
> 2 is the mean of 2
> 3 is the mean of 4 and 2
> 2.33 is the mean of 1, 4 and 2
> 2.55 is the mean of 3, 1, 4 and 2
>
> The same is true for the variable sd, but here we consider the
> standard deviation.
>
> Does anyone know how to solve this problem?
>
> Many thanks,
> Pedro Nakashima.
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/



-- 
Matthew White
Data Coordinator
Innovations for Poverty Action
101 Whitney Avenue, New Haven, CT 06510 USA
+1 434-305-9861
www.poverty-action.org

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index