cweights: Compute a centered vector of weights

Description Usage Arguments Value Examples

View source: R/cweights.R

Description

Compute a centered vector of weights

Usage

1
cweights(k, e = 1)

Arguments

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.

Value

A vector of weights.

Examples

 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

jiho/castr documentation built on April 5, 2020, 2:12 p.m.