Description Usage Arguments See Also Examples
Apply an iterative rolling mean
1 |
x |
numeric; data vector |
w |
integer; width of the rolling window |
partial |
logical; should partial results at the beginning/end be calculated? |
sharp |
logical; experimental option. Should window width be reduced
by one |
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | # Increasing the number of iterations progressively reduces the ringing
set.seed(1)
l <- 50
r <- rnorm(l) + rpois(l, (1:l/10))
w <- 6
plot(r, type="p", cex=0.5, pch=16)
points(rolliter(r, w, 1, partial=TRUE), type="o", col="orange", cex=0.5, pch=16)
points(rolliter(r, w, 2, partial=TRUE), type="o", col="hotpink", cex=0.5, pch=16)
points(rolliter(r, w, 3, partial=TRUE), type="o", col="purple", cex=0.5, pch=16)
points(rolliter(r, w, 4, partial=TRUE), type="o", col="blue", cex=0.5, pch=16)
# When w is even and i is odd the smoothed vector is shifted half a position
# to the left relative to the input vector
y <- c(rep(0, 18), 1, rep(0, 18))
col <- rainbow(20, start=0.2)
plot(y, ylim=c(0, 0.25))
for (i in 1:length(col)) {
lines(rolliter(y, 4, i, partial=TRUE), col=col[i])
}
# Smoothing a binomial series with high missingness
set.seed(1)
z <- rbinom(100, 1, prob=seq(0.8, 0.2, length.out=100))
z[sample(as.logical(0:1), length(z), replace=TRUE, prob=c(0.6, 0.4))] <- NA
plot(z)
lines(rolliter(z, 6, 1, partial=TRUE), col="grey80")
lines(rolliter(z, 6, 2, partial=TRUE), col="grey60")
lines(rolliter(z, 6, 4, partial=TRUE), col="grey30")
lines(rolliter(z, 6, 6, partial=TRUE), col="black")
# Using the experimental sharp=TRUE appears to give a sharper transition while
# still keeping high frequency ringing at a low level
par(mfcol=c(4, 2), mar=c(2, 2, 1, 1), oma=c(0, 0, 0.5, 0))
set.seed(1)
r <- tanh(rnorm(2e4, 0, 0.4))
w <- 11
it <- c(2, 3, 6, 9)
s <- expand.grid(it=it, sharp=c(FALSE, TRUE))
for (i in 1:nrow(s)) {
ri <- rolliter(r, w, s$it[i], partial=TRUE, sharp=s$sharp[i])
spectrum(na.omit(ri), xlim=c(0, 0.5), ylim=c(1e-20, 1), xaxs="i",
main=ifelse(i %% 4 == 1, paste0("sharp=", s$sharp[i]), ""))
grid(col="#00000066")
text(0.005, 1e-20, paste0("w = ", w, "\n", "i = ", s$it[i]), adj=c(0, 0))
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.