sleek: Velleman PF (1980)- 4253H, twice smoothing

Description Usage Arguments Details Value Author(s) References Examples

View source: R/sleek.R

Description

In Time Series Resistant Smooth offers 4253H, twice smoothing method.

Usage

1
sleek(y)

Arguments

y

a vector or time series

Details

4253H, twice consisting of a running median of 4, then 2, then 5, then 3 followed by Hanning. Endpoints are always handled using smoothers of shorter, even span or odd span. Hanning is a running weighted average, the weights being 1/4, 1/2 and 1/4. The result of this smoothing is then reroughed by computing residuals, applying the same smoother to them and adding the result to the smooth of the first pass. The endpoint rule modifies the values first and last values of series.

Value

vector or time series of smoothed values of the same length as x

Author(s)

Muntashir-Al-Arefin sheen4783@yahoo.com based on R.

References

Velleman, P. F. 1980. Definition and comparison of robust nonlinear data smoothing algorithms. Journal of the American Statistical Association 75: 609-61.

Velleman, P. F., and D. C. Hoaglin. 1981. Applications, Basics, and Computing of Exploratory Data Analysis. Boston: Duxbury.

Tukey, J. W. 1977. Exploratory Data Analysis. Reading, MA: Addison-Wesley.

Velleman, P. F. 1977. Robust nonlinear data smoothers: Definitions and recommendations. Proceedings of the National Academy of Sciences 74: 434-436.

Examples

 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
54
55
56
57
58
59
60
61
62
63
64
65
##Example of sleek function using time series data tsdata
#(GDP per capita Bangladesh, currency gold in ounce) in sleekts package:
library(sleekts)
data(tsData)
tsData
sleek(tsData)
# To see original data plot and smoothed data plot
par(mfrow = c(2, 1))
plot(tsData); plot(sleek(tsData));

## The function is currently defined as
function (y) 
{
    h <- function(y) {
        N <- length(y)
        z <- NULL
        z[1] <- y[1]
        z[2] <- median(c(y[1], y[2]))
        z[3] <- median(c(y[2], y[3]))
        for (i in 4:(N)) {
            z[i] <- median(c(y[i - 3], y[i - 2], y[i - 1], y[i]))
        }
        z[N - 1] <- median(c(y[N - 2], y[N - 1]))
        z[N] <- median(c(y[N - 1], y[N]))
        z[N + 1] <- y[N]
        z1 <- NULL
        for (i in 1:N) {
            z1[i] <- (z[i] + z[i + 1])/2
        }
        z2 <- NULL
        z2[1] <- z1[1]
        z2[2] <- median(c(z1[1], z1[2], z1[3]))
        for (i in 3:(N - 2)) {
            z2[i] <- median(c(z1[i - 2], z1[i - 1], z1[i], z1[i + 
                1], z1[i + 2]))
        }
        z2[N - 1] <- median(c(z1[N - 2], z1[N - 1], z1[N]))
        z2[N] <- z1[N]
        z3 <- NULL
        z3[1] <- z2[1]
        for (i in 2:(N - 1)) {
            z3[i] <- median(c(z2[i - 1], z2[i], z2[i + 1]))
        }
        z3[N] <- z2[N]
        z4 <- NULL
        z4[1] <- z3[1]
        for (i in 2:(N - 1)) {
            z4[i] <- (z3[i - 1] + z3[i] + z3[i + 1])/4
        }
        z4[N] <- z3[N]
        z4[1] <- median(c(z4[1], z4[2], (3 * z4[2] - 2 * z4[3])))
        z4[N] <- median(c(z4[N], z4[N - 1], (3 * z4[N - 2] - 
            2 * z4[N - 1])))
        return(z4)
    }
    sm <- h(y)
    rf <- (y - sm)
    sm.rf <- h(rf)
    smooth <- (sm.rf + sm)
    if (is.ts(y) == 1) {
        date <- start(y)
        smooth <- ts(smooth, start = date)
    }
    return(smooth)
  }

Example output

Time Series:
Start = 1960 
End = 2014 
Frequency = 1 
 [1] 2.44675 2.68223 2.75241 2.81204 2.76469 2.94027 3.10538 3.40418 2.98683
[10] 3.18363 3.77241 3.17106 1.57120 1.18845 1.10656 1.66834 1.09288 0.85893
[19] 0.88129 0.63751 0.35732 0.50767 0.55336 0.45295 0.59388 0.72244 0.59393
[28] 0.53575 0.57463 0.67228 0.73147 0.77735 0.81997 0.80225 0.74896 0.82413
[37] 0.85657 1.02235 1.17610 1.26209 1.27542 1.28676 1.12123 1.02659 0.97875
[46] 0.94752 0.70770 0.67177 0.61658 0.61469 0.54232 0.46583 0.44942 0.58761
[55] 0.93967
Time Series:
Start = 1960 
End = 2014 
Frequency = 1 
 [1] 2.4465618 2.4465618 2.5503057 2.6013297 2.6394575 2.7154617 2.8105709
 [8] 2.9004378 2.9677845 3.0047335 3.0249102 2.8525458 2.3272367 1.6783478
[15] 1.1724499 1.0035142 0.9384391 0.8737028 0.7864507 0.6716695 0.5583764
[22] 0.4835817 0.4616612 0.4799438 0.5118391 0.5385712 0.5540416 0.5571025
[29] 0.5625246 0.5907559 0.6425497 0.7028435 0.7430234 0.7589036 0.7667170
[36] 0.7737615 0.8040697 0.8831069 1.0058354 1.1234293 1.1893706 1.1957267
[43] 1.1390939 1.0471146 0.9506631 0.8687943 0.7774840 0.6745351 0.5921348
[50] 0.5459881 0.5281912 0.5196567 0.5403418 0.6623377 0.6623377
function (y) 
{
    h <- function(y) {
        N <- length(y)
        z <- NULL
        z[1] <- y[1]
        z[2] <- median(c(y[1], y[2]))
        z[3] <- median(c(y[2], y[3]))
        for (i in 4:(N)) {
            z[i] <- median(c(y[i - 3], y[i - 2], y[i - 1], y[i]))
        }
        z[N - 1] <- median(c(y[N - 2], y[N - 1]))
        z[N] <- median(c(y[N - 1], y[N]))
        z[N + 1] <- y[N]
        z1 <- NULL
        for (i in 1:N) {
            z1[i] <- (z[i] + z[i + 1])/2
        }
        z2 <- NULL
        z2[1] <- z1[1]
        z2[2] <- median(c(z1[1], z1[2], z1[3]))
        for (i in 3:(N - 2)) {
            z2[i] <- median(c(z1[i - 2], z1[i - 1], z1[i], z1[i + 
                1], z1[i + 2]))
        }
        z2[N - 1] <- median(c(z1[N - 2], z1[N - 1], z1[N]))
        z2[N] <- z1[N]
        z3 <- NULL
        z3[1] <- z2[1]
        for (i in 2:(N - 1)) {
            z3[i] <- median(c(z2[i - 1], z2[i], z2[i + 1]))
        }
        z3[N] <- z2[N]
        z4 <- NULL
        z4[1] <- z3[1]
        for (i in 2:(N - 1)) {
            z4[i] <- (z3[i - 1] + z3[i] + z3[i + 1])/4
        }
        z4[N] <- z3[N]
        z4[1] <- median(c(z4[1], z4[2], (3 * z4[2] - 2 * z4[3])))
        z4[N] <- median(c(z4[N], z4[N - 1], (3 * z4[N - 2] - 
            2 * z4[N - 1])))
        return(z4)
    }
    sm <- h(y)
    rf <- (y - sm)
    sm.rf <- h(rf)
    smooth <- (sm.rf + sm)
    if (is.ts(y) == 1) {
        date <- start(y)
        smooth <- ts(smooth, start = date)
    }
    return(smooth)
}

sleekts documentation built on May 2, 2019, 12:19 p.m.

Related to sleek in sleekts...