# [R] ewma help

From: Leeds, Mark (IED) <Mark.Leeds_at_morganstanley.com>
Date: Wed 03 Jan 2007 - 19:20:17 GMT

I wrote my own ewma function to deal with the somewhat odd way that filter handles missing values.
The function I wrote works as long as the NA isn't first but when it is first I still get a zero in the output.
I'm not expert enough to look at filter and undeerstand what it is doing.

# 1) THE FIRST CASE DOESN'T WORK
# I WOULD PREFER A 1 IN THE SECOND ELEMENT OF THE OUTPUT
# BECAUSE THAT IS THE INITIAL NON NA VALUE OF THE SERIES
# I DON"T KNOW WHY A ZERO GETS THERE AND THAT
# MAKES THE REST OF THE SERIES WRONG BECAUSE IT'S
#A RECURSIVE RELATIONSHIP. BASICALLY
#I PREFER THE OUTPUT TO BE THE SAME AS THE SECOND SET OF OUTPUT :
# 1 NA
# 2 1.00
# 3 1.50
# 4 2.25
# WITH THE NA JUST IN A DIFFERENT PLACE
x<-zoo(matrix(c(NA,1,2,3),nc=1))
ewma(x,lambda=0.5)

1 NA
2 0 (
3 1
4 2

#=======================================================================

# 2) THIS CASE DOES WORK
x<-zoo(matrix(c(1,NA,2,3),nc=1))
ewma(x,lambda=0.5)

1 1.00
2 NA
3 1.50
4 2.25

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

# work with 'non-zoo' data for speed and then recombine     .raw <- coredata(x)
good.ind <- !is.na(.raw) # determine good values

method='recursive', init=coredata(init))     zoo(.raw, index(x)) # create zoo object for return }

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

R-help@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. Received on Thu Jan 04 13:51:04 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 04 Jan 2007 - 03:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.