knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/" ) options(width=100) library(uts)
An Unevenly-spaced Time Series (uts) is a sequence of observation time and value pairs (tn, Xn) with strictly increasing observation times. As opposed to equally spaced time series, the spacing of observation times may not be constant.
As of early 2018, to the best of my knowledge, there is no R
time series package that allows to write 100% of the application logic in terms of this definition. Either directly or indirectly, existing implementations fall back on equally spaced data for some of their functionality. For example, the window width of a rolling time series operator, such as a moving average, is usually specified in terms of the number of observations (e.g. 7 observations) instead of a temporal duration (e.g. 6 hours).
Even when a time series is equally spaced, it is often preferable to define operations using a temporal duration (e.g. a moving average over the past year) instead of number of observations (e.g. a moving average over the last 12 observations values). Should the frequency of the time series change, then in the former case the code would not require any changes, while it would in the later case. Moreover, in this way an identical analysis can be carried out on multiple time series of different frequencies, without having to keep track of the individual observation frequencies.
I therefore decided to design a new time series package, partially based on my research on this topic.
R
objects (not just numbers).This package is not yet available on CRAN, but can be installled from GitHub, either using the R
package devtools:
devtools::install_github("andreas50/uts", build_vignettes=TRUE)
or using the R
package remotes:
remotes::install_github("andreas50/uts")
# Get sample unevenly-spaced time series with six observations x <- ex_uts() x
# Plot the time series par(mai=c(0.5, 0.5, 0.2, 0.2)) plot(x, type="o", cex.axis=0.8)
# Get first and last observation value(!) first(x); last(x) # Get first and last observation time(!) start(x); end(x) # Get time series length, first in terms of number of observations, second in terms of temporal length length(x); length_t(x) # Insert new observation x[as.POSIXct("2007-11-10 10:00:00")] <- 45 # Sample the time series at a specific time point, using one of several supported interpolation methods sample_values(x, as.POSIXct("2007-11-10"), interpolation="linear") # Shift observation times by 3 hours lag_t(x, dhours(3)) # Get maximum observation value and the corresponding time max(x); which.max(x) # Time series arithmetic x*2 + 5 # Convert time series to data.frame as.data.frame(x) # Get "tail" of time series, first in terms of number of observations, second in terms of temporal length tail(x, 3) tail_t(x, ddays(1))
uts
relies on the POSIXct class available in base R
.zoo
that facilitates inter-class operability.zoo
that provides many (stock) trading indicators and interface to tslib
(a time series library in C++).irts
class for irregular time series, but with little functionality.Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.