Description Usage Arguments Details Methods (by class) References See Also Examples
Calculate a simple moving average (SMA) of a time series by applying a moving average kernel to the sample path.
1 2 3 4 5 |
x |
a numeric time series object. |
... |
further arguments passed to or from methods. |
width |
a positive, finite |
interpolation |
the sample path interpolation method. Either |
align |
either |
interior |
logical. Should time windows lie entirely in the interior of the temporal support of |
Three different time series sample path interpolation schemes are supported for "uts"
objects. Each method implicitly puts different weights on the observation values inside the rolling time window:
last
: Use last-point interpolation for the time series sample path. Equivalently, each observation value is weighted by how long it remained unchanged.
next
: Use next-point interpolation for the time series sample path. Equivalently, each observation value is weighted by how long it remained the next (i.e. upcoming) observation.
linear
: Use linear interpolation of the time series sample path. The behavior is approximately halfway in-between last-point and next-point interpolation.
See the first reference below for precise mathematical definitions.
Depending on the application, one sample path interpolation method will often be preferable.
For example, to calculate the average FED funds target rate over the past three years, it is desirable to weight each observation value by the amount of time it remained unchanged, which is achieved by using method "last"
.
On the other hand, method "linear"
can be used to estimate the rolling average value of a discretely-observed continuous-time stochastic processes (see the second reference below for a precise mathematical statement).
However, these SMAs are usually not ideally suited for analyzing discrete events, such as for calculating the average insurance loss per hurricane over the past twelve months, or for determining the average number of IBM common shares traded on the NYSE per executed order during the past 30 minutes.
These quantities are unweighted averages of the observation values inside a rolling time window, and they can be calculated using rolling_apply
using argument FUN=mean
.
uts
: simple moving average for "uts"
objects with finite, non-NA observation values.
Eckner, A. (2017) Algorithms for Unevenly Spaced Time Series: Moving Averages and Other Rolling Operators.
Eckner, A. (2017) Some Properties of Operators for Unevenly Spaced Time Series.
ema
for exponential moving averages.
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 | sma(ex_uts(), ddays(1))
sma(ex_uts(), ddays(1), interpolation="linear")
sma(ex_uts(), ddays(1), interpolation="next")
sma(ex_uts(), ddays(1))
sma(ex_uts(), ddays(1), align="center")
sma(ex_uts(), ddays(1), align="left")
# Plot a monotonically increasing time series 'x' together with
# a backward-looking and forward-looking SMA.
# Note how the forward-looking SMA is leading the increase in 'x', which
# in turn is leading the increase in the backward-looking SMA.
## Not run:
x <- uts(0:10, Sys.time() + dhours(0:10))
par(mfrow=c(1, 3))
plot(x, ylim=c(0, 10), main="Original time series")
plot(sma(x, dhours(3), align="right"), ylim=c(0, 10), main="Backward-looking SMA")
plot(sma(x, dhours(3), align="left"), ylim=c(0, 10), main="Forward-looking SMA")
## End(Not run)
# Plot three different SMAs of a monotonically increasing time series.
# Note that SMA_last(x)_t <= SMA_linear(x)_t <= SMA_next(x)_t for all observation times t
## Not run:
x <- uts(0:8, Sys.time() + dhours(0:8))
par(mfrow=c(1, 3))
plot(sma(x, dhours(10), interpolation="last"), ylim=c(0, 4), main="Last-point interpolation")
plot(sma(x, dhours(10), interpolation="linear"), ylim=c(0, 4), main="Linear interpolation")
plot(sma(x, dhours(10), interpolation="next"), ylim=c(0, 4), main="Next-point interpolation")
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.