[R] behavior of ewma function

Leeds, Mark (IED) Mark.Leeds at morganstanley.com
Mon Dec 11 21:26:07 CET 2006


I just had an idea but I'm not sure how to do it. Create a non NA series
( which I can do ) and smooth
that series ( whh I can do ). Then, after that smoothed series is
created, put the NAs back into the smoothed series
in their appropriate places ( which I don't know how to do ).
 
If anyone does know how to do that, the help would be appreciated.



-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Leeds, Mark (IED)
Sent: Monday, December 11, 2006 3:15 PM
To: r-help at stat.math.ethz.ch
Subject: [R] behavior of ewma function

I have the ewma function as shown below. I think I copied it from an
oldSplus help page on filter and then modified it with a lot of help
from Achim.

ewma<-function(x,lambda = 1, init = x[1]) {

rval<-filter(lambda*coredata(x),filter=(1-lambda),method="recursive",ini
t=init)
rval<-zoo(coredata(rval),index(x))
rval
}

It sort of works but , if there are NA values in the input series such
as

x<-c(NA,2,3,4,5,NA,6,7,8,9,10,11,12,13,14,15)

y<-ewma(x,.01)

y

  1         2         3         4         5         6         7
8         9        10        11        12        13        14        15
16 

0.0000000 0.0200000 0.0498000 0.0893020 0.1384090  NA 0.0000000
0.0700000 0.1493000 0.2378070 0.3354289 0.4420746 0.5576539 0.6820774
0.8152566 0.9571040

So, ewma starts off with a zero if there is an NA and this is misleading
in terms of the exponentially weighted moving average numbers calculated
going forward. Maybe this has something to do Woth the fact that I set
init equal x[1] ? The NA in the 6th spot is fine but is there any way to


1) return an NA value when the input into the series is an NA ?

2) start the exponentially weighted moving average at the next non NA
value ?

So, essentually, for the example above, I would prefer the output to be

NA, 2, 2*.01 + .99*3 = 2.99, 2.99*.01 + .99*4 = 3.9899, 3.9899*.01+
0.99*5 = 4.989, 4.989 ( because of the NA ),
4.989*.01 + 0.99* 6 = 5.989 etc.

This might be too complex to get but atleast is it possible to get NA
where the inputs were NA  and not use zeros for smoothing going forward
? Thanks.








 





P.S : The code in ewma may look a little different than one is used to
but that is because it is sometimes run with The inputs being zoo
objects so coredate and index are needed.
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to
buy/se...{{dropped}}

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}




More information about the R-help mailing list