Description Usage Arguments Details See Also Examples
ema
returns a vector whose values are the
exponentially smoothed values of the input vector
1 | ema(x, a = 2, burnin = 1)
|
x |
numeric; input vector |
a |
numeric; smoothing factor |
burnin |
integer or "auto"; set the initial estimate (S[1]) to the average of the first N values of X S[1] = mean(X[1:N]) Default is 1, if "auto" is selected it will be set to ceiling(a). |
With X[t] being the input at time t,
S[t] being the output at time t, and
α = 1/a
– the default function can be defined like this:
S[t] = {X[1] for t = 1; α * X[t] + (1 - α) * S[t-1] for t > 1}
The function is also sometimes referred to as an exponentially weighted moving average (EWMA).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | x <- c(rep(0, 4), 1, rep(0, 59))
a <- 1:10*5
col <- rainbow(length(a), start=0.15)
plot(x, type="p", pch=16, cex=0.4, ylim=c(0, 0.21), xaxs="i",
main="Exponential Moving Average", xlab="Time", ylab="Magnitude")
for (i in seq_along(a)) {
lines(ema(x, a[i]), col=col[i])
}
mtext("Single impulse of magnitude 1 occuring at time = 5", line=0.2)
L <- lapply(seq_along(a), function(x) bquote(alpha == 1 / .(a[x])))
legend("topright", legend=as.expression(L),
bty="n", col=col, lwd=1.2, cex=0.8, y.intersp=1.2)
# Works well with missing data
# Burnin can be useful to quickly stabalize the filter
set.seed(4)
re <- rexp(50) * sqrt(50:1)
re[sample(1:50, 5)] <- NA
plot(re)
lines(ema(re, 9, burnin=1), col="red")
lines(ema(re, 9, burnin=5), col="blue")
lines(ema(re, 9, burnin="auto"), col="green")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.