Statalist


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

st: weighted sum


From   Maarten buis <[email protected]>
To   stata list <[email protected]>
Subject   st: weighted sum
Date   Sun, 27 Jan 2008 13:28:37 +0000 (GMT)

Rick wrote the following mail. The answer to his question is yes, it
can 
be done in Stata and below is an example of how to do that. Notice that
due to nature of the procedure he proposes the oldest observation is
always missing and the second oldest observation is always equal to the
oldest observation (a weighted mean of one number is that one number) 

Hope this helps,
Maarten

PS. I strongly recommend sending these kinds of questions to the
statalist instead of me personally. On statalist there are thousands of
people who might choose to answer you. If you send it to me personaly
there is only one. As I am now feeling the stress of finishing my
dissertation there is a very real chance that I won't answer.

*------------------- begin example ---------------------
drop _all
input str9 strdate str3 city rainfall
15Apr2007 SEA 2.3
14Apr2007 SEA 1.1
20Mar2007 SEA 1.5
11Feb2007 SEA 2.0
08Feb2007 NYC 3.0
02Feb2007 NYC 2.6
28Jan2007 NYC 1.4
21Jan2007 NYC 2.2
04Apr2007 CHI 2.1
21Mar2007 CHI 1.7
05Mar2007 CHI 2.4
end
gen date = date(strdate, "DMY")
format date %td

bys city: gen long N = _N
sum N, meanonly
local N = r(max)

gen long datesince = .
gen double w = .
gen double sumwXrain = .
gen double sumw = .
gen double wmean = .

forvalues i = 2/`N' {
	quietly {
		bys city (date): replace datesince = date[`i']- date
		replace w = exp(-datesince/43) 
		bys city (date) : replace w = 0 if _n >= `i'
		bys city (date) : replace sumwXrain = sum(w*rain)
		bys city (date) : replace sumw = sum(w)
		bys city (date) : replace wmean = ///
                                  sumwXrain/sumw if _n == `i'
	}
}
list
*------------------------ end example --------------------------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

--- rick levey [[email protected]] wrote:
> What I am trying to do is as follows:
> 
> I have weather data that tracks rainfall by date/city/rainfall.  For
> each day of rainfall, I want to calculate a recency-weighted average
> of the previous rainfall days in that city.
>
> More recent rainfall will count more heavily than rainfall long ago. 
> The half-life will be approximately 30 days.  So it is weighted by
> e^-t where t is dayssince over 43.  (the current days rainfall is not
> included in the calculation)
>
> The data looks like:
> 
> DATE,CITY,RAINFALL
> 15Apr2007,SEA,2.3
> 14Apr2007,SEA,1.1
> 20Mar2007,SEA,1.5
> 11Feb2007,SEA,2.0
> 08Feb2007,NYC,3.0
> 02Feb2007,NYC,2.6
> 28Jan2007,NYC,1.4
> 21Jan2007,NYC,2.2
> 04Apr2007,CHI,2.1
> 21Mar2007,CHI,1.7
> 05Mar2007,CHI,2.4
>
> Here is an example calculation for the variable to be generated,
PRWA,  >for the date 15apr2007 for the city SEA.
> 
> PREVDATES,CITY,RAINFALL,DAYSSINCE,WEIGHT,WEIGHT*RAINFALL
> 14apr2007,SEA,1.1,1,0.9770,1.0747
> 20mar2007,SEA,1.5,26,0.5463,0.8194
> 11feb007,SEA,2.0,63,0.2311,0.4621
> 
> SUM OF WEIGHT*RAINFALL = (0.9770*1.1) + (0.5463*1.5) + (0.2311*2.0) =
> 2.3562
>
> SUM OF WEIGHTS = e^-(1/43) + e^-(26/43) + e^-(63/43) = 0.9770 +
0.5463
> + 0.2311 = 1.7543
>
> GENERATED VARIABLE PRWA = 2.3562/1.7543 = 1.3431 (15apr2007/SEA)
>
> Can this be accomplished in Stata or do I need to do it elsewhere and
> import it in?



-----------------------------------------
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 inbox http://uk.mail.yahoo.com
*
*   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