Description Usage Arguments Value Examples
Compute a centered vector of weights
1 | cweights(k, e = 1)
|
k |
order of the window; the window size is 2k+1. |
e |
exponent for the increase of weights towards the center. By default the increase in linear (e=1); e>1 makes for sharper focus on the center; e<1 spreads the focus to the rest of the window. |
A vector of weights.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # Plot the distribution of weights
plot(cweights(4), type="b")
plot(cweights(4, 2), type="b")
plot(cweights(4, 0.1), type="b")
# Compare a simple moving average with a weighted moving average
set.seed(1)
x <- sin(1:100/10) + rnorm(100, sd=0.5)
plot(x)
xm <- slide(x, k=2, mean, na.rm=TRUE)
xwm <- slide(x, k=2, weighted.mean, w=cweights(k=2), na.rm=TRUE)
lines(xm, col="blue")
lines(xwm, col="red")
# The weighted version is a bit smoother
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.