flag_ts | R Documentation |
The function 'flags' independent sections of time series using up to three methods. This is useful for identifying gaps in time series and/or for models of time series in which independent segments of time series need to be treated as such. To flag time series, the function can consider two drivers of independence: (a) a grouping factor (fct
) which defines inherently independent time series in a dataset (e.g. a dataset may comprise time series for different individuals) and (b) gaps in the time series which, when greater than a user-defined threshold (duration_threshold
), separate time series that can be considered effectively independent, even if they are derived from the same level of a grouping factor. Using these two criteria, time series can be flagged using three methods which different modelling approaches may require.
flag_ts(x, fct = NULL, dat = NULL, duration_threshold, flag = 1:3)
x |
A vector of time stamps, either as an integer/numeric object, a |
fct |
(optional) A grouping factor defining independent segments of time series (e.g. individuals). |
dat |
(optional) A dataframe with a column |
duration_threshold |
A numeric input defining the number of units (if |
flag |
A numeric input/vector of |
Inputs should be ordered by ordered by fct
(if applicable) then x
.
A dataframe with the duration between each observation and the next observation and corresponding flags.
Edward Lavender
#### Define some irregularly spaced time series t <- c(seq.POSIXt(as.POSIXct("2016-01-01"), as.POSIXct("2016-01-02"), by = "6 hours"), as.POSIXct("2016-01-02 12:00:00"), seq.POSIXt(as.POSIXct("2016-01-02 18:00:00"), as.POSIXct("2016-01-03"), by = "5 hours") ) #### Example (1) Supply a vector of time stamps to flag independent sections of time series flag_ts( x = t, duration_threshold = 8*60, flag = 1:3) #### Example (2) Supply a dataframe with a time stamp column instead flag_ts(dat = data.frame(x = t), duration_threshold = 8*60, flag = 1:3) #### Example (3) Supply a factor level which separates unique time series fct_levels <- c(rep(1, length(t)), rep(2, length(t))) t2 <- rep(t, 2) flag_ts( x = t2, fct = fct_levels, duration_threshold = 8*60, flag = 1:3) #### Example (4) Supply time series and a factor in a dataframe organised by fct then time stamp flag_ts(dat = data.frame(x = t2, fct = fct_levels), duration_threshold = 8*60, flag = 1:3) #### Example (5) Numeric example without factor x <- c(seq(1, 5, by = 1), seq(100, 105, by = 1)) flag_ts(x = x, fct = NULL, dat = NULL, duration_threshold = 5, flag = 1:3) #### Example (6) Numeric example with factor fct_levels <- c(rep(1, length(x)), rep(2, length(x))) x <- rep(x, 2) flag_ts(x = x, fct = fct_levels, dat = NULL, duration_threshold = 5, flag = 1:3) #### Example (7) Numeric example with factor via dat argument cbind(x, flag_ts(dat = data.frame(x = x, fct = fct_levels), duration_threshold = 5, flag = 1:3) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.