STR {stR} | R Documentation |
Automatic STR decomposition
Description
Automatically selects parameters for an STR decomposition of time series data.
If a parallel backend is registered for use before STR
call,
STR
will use it for n-fold cross validation computations.
Usage
STR(
data,
predictors,
confidence = NULL,
robust = FALSE,
lambdas = NULL,
pattern = extractPattern(predictors),
nFold = 5,
reltol = 0.005,
gapCV = 1,
solver = c("Matrix", "cholesky"),
nMCIter = 100,
control = list(nnzlmax = 1e+06, nsubmax = 3e+05, tmpmax = 50000),
trace = FALSE,
iterControl = list(maxiter = 20, tol = 1e-06)
)
Arguments
data |
Time series or a vector of length L. |
predictors |
List of predictors.
|
confidence |
A vector of percentiles giving the coverage of confidence intervals.
It must be greater than 0 and less than 1.
If |
robust |
When |
lambdas |
An optional parameter. A structure which replaces lambda parameters provided with predictors. It is used as either a starting point for the optimisation of parameters or as the exact model parameters. |
pattern |
An optional parameter which has the same structure as |
nFold |
An optional parameter setting the number of folds for cross validation. |
reltol |
An optional parameter which is passed directly to |
gapCV |
An optional parameter defining the length of the sequence of skipped values in the cross validation procedure. |
solver |
A vector with two string values. The only supported combinations are: c("Matrix", "cholesky") (default), and c("Matrix", "qr"). The parameter is used to specify a particular library and method to solve the minimisation problem during STR decompositon. |
nMCIter |
Number of Monte Carlo iterations used to estimate confidence intervals for Robust STR decomposition. |
control |
Passed directly to |
trace |
When |
iterControl |
Control parameters for some experimental features. This should not be used by an ordinary user. |
Value
A structure containing input and output data.
It is an S3 class STR
, which is a list with the following components:
-
output – contains decomposed data. It is a list of three components:
-
predictors – a list of components where each component corresponds to the input predictor. Every such component is a list containing the following:
-
data – fit/forecast for the corresponding predictor (trend, seasonal component, flexible or seasonal predictor).
-
beta – beta coefficients of the fit of the coresponding predictor.
-
lower – optional (if requested) matrix of lower bounds of confidence intervals.
-
upper – optional (if requested) matrix of upper bounds of confidence intervals.
-
-
random – a list with one component data, which contains residuals of the model fit.
-
forecast – a list with two components:
-
data – fit/forecast for the model.
-
beta – beta coefficients of the fit.
-
lower – optional (if requested) matrix of lower bounds of confidence intervals.
-
upper – optional (if requested) matrix of upper bounds of confidence intervals.
-
-
-
input – input parameters and lambdas used for final calculations.
-
data – input data.
-
predictors - input predictors.
-
lambdas – smoothing parameters used for final calculations (same as input lambdas for STR method).
-
-
cvMSE – optional cross validated (leave one out) Mean Squared Error.
-
optim.CV.MSE or optim.CV.MAE – best cross validated Mean Squared Error or Mean Absolute Error (n-fold) achieved during minimisation procedure.
-
nFold – the input
nFold
parameter. -
gapCV – the input
gapCV
parameter. -
method – contains strings
"STR"
or"RSTR"
depending on used method.
Author(s)
Alexander Dokumentov
References
Dokumentov, A., and Hyndman, R.J. (2022) STR: Seasonal-Trend decomposition using Regression, INFORMS Journal on Data Science, 1(1), 50-62. https://robjhyndman.com/publications/str/
See Also
Examples
TrendSeasonalStructure <- list(
segments = list(c(0, 1)),
sKnots = list(c(1, 0))
)
WDSeasonalStructure <- list(
segments = list(c(0, 48), c(100, 148)),
sKnots = c(as.list(c(1:47, 101:147)), list(c(0, 48, 100, 148)))
)
TrendSeasons <- rep(1, nrow(electricity))
WDSeasons <- as.vector(electricity[, "WorkingDaySeasonality"])
Data <- as.vector(electricity[, "Consumption"])
Times <- as.vector(electricity[, "Time"])
TempM <- as.vector(electricity[, "Temperature"])
TempM2 <- TempM^2
TrendTimeKnots <- seq(from = head(Times, 1), to = tail(Times, 1), length.out = 116)
SeasonTimeKnots <- seq(from = head(Times, 1), to = tail(Times, 1), length.out = 24)
TrendData <- rep(1, length(Times))
SeasonData <- rep(1, length(Times))
Trend <- list(
name = "Trend",
data = TrendData,
times = Times,
seasons = TrendSeasons,
timeKnots = TrendTimeKnots,
seasonalStructure = TrendSeasonalStructure,
lambdas = c(1500, 0, 0)
)
WDSeason <- list(
name = "Dayly seas",
data = SeasonData,
times = Times,
seasons = WDSeasons,
timeKnots = SeasonTimeKnots,
seasonalStructure = WDSeasonalStructure,
lambdas = c(0.003, 0, 240)
)
StaticTempM <- list(
name = "Temp Mel",
data = TempM,
times = Times,
seasons = NULL,
timeKnots = NULL,
seasonalStructure = NULL,
lambdas = c(0, 0, 0)
)
StaticTempM2 <- list(
name = "Temp Mel^2",
data = TempM2,
times = Times,
seasons = NULL,
timeKnots = NULL,
seasonalStructure = NULL,
lambdas = c(0, 0, 0)
)
Predictors <- list(Trend, WDSeason, StaticTempM, StaticTempM2)
elec.fit <- STR(
data = Data,
predictors = Predictors,
gapCV = 48 * 7
)
plot(elec.fit,
xTime = as.Date("2000-01-11") + ((Times - 1) / 48 - 10),
forecastPanels = NULL
)
#########################################################
n <- 70
trendSeasonalStructure <- list(segments = list(c(0, 1)), sKnots = list(c(1, 0)))
ns <- 5
seasonalStructure <- list(
segments = list(c(0, ns)),
sKnots = c(as.list(1:(ns - 1)), list(c(ns, 0)))
)
seasons <- (0:(n - 1)) %% ns + 1
trendSeasons <- rep(1, length(seasons))
times <- seq_along(seasons)
data <- seasons + times / 4
set.seed(1234567890)
data <- data + rnorm(length(data), 0, 0.2)
data[20] <- data[20] + 3
data[50] <- data[50] - 5
plot(times, data, type = "l")
timeKnots <- times
trendData <- rep(1, n)
seasonData <- rep(1, n)
trend <- list(
data = trendData, times = times, seasons = trendSeasons,
timeKnots = timeKnots, seasonalStructure = trendSeasonalStructure, lambdas = c(1, 0, 0)
)
season <- list(
data = seasonData, times = times, seasons = seasons,
timeKnots = timeKnots, seasonalStructure = seasonalStructure, lambdas = c(1, 0, 1)
)
predictors <- list(trend, season)
rstr <- STR(data, predictors,
reltol = 0.0000001, gapCV = 10,
confidence = 0.95, nMCIter = 400, robust = TRUE
)
plot(rstr)