heuristicSTR: Automatic STR decomposition with heuristic search of the...

Description Usage Arguments Value Author(s) References See Also Examples

View source: R/heuristicSTR.R

Description

Automatically selects parameters (lambda coefficients) for an STR decomposition of time series data. Heuristic approach can give a better estimate compare to a standard optmisaton methods used in STR.

If a parallel backend is registered for use before STR call, heuristicSTR will use it for n-fold cross validation computations.

Usage

1
2
3
4
heuristicSTR(data, predictors, confidence = NULL, lambdas = NULL,
  pattern = extractPattern(predictors), nFold = 5, reltol = 0.005,
  gapCV = 1, solver = c("Matrix", "cholesky"), trace = FALSE,
  ratioGap = 1e+12, relCV = 0.01)

Arguments

data

Time series or a vector of length L.

predictors

List of predictors.
According to the paradigm of this implementation, the trend, the seasonal components, the flexible predictors and the seasonal predictors are all presented in the same form (as predictors) and must be described in this list.
Every predictor is a list of the following structures:

  • data – vector of length L (length of input data, see above). For trend or for a seasonal component it is a vector of ones. For a flexible or a seasonal predictor it is a vector of the predictor's data.

  • times – vector of length L of times of observations.

  • seasons – vector of length L. It is a vector of ones for a trend or a flexible predictor. It is vector assigning seasons to every observation (for a seasonal component or a seasonal predictor). Seasons can be fractional for observations in between seasons.

  • timeKnots – vector of times (time knots) where knots are positioned (for a seasonal component or a seasonal predictor a few knots have the same time; every knot is represented by time and season). Usually this vector coincides with times vector described above, or timeKnots is a subset of times vector.

  • seasonalStructure – describes seasonal topology (which can have complex structure) and seasonal knots.The seasonal topology is described by a list of segments and seasonal knots, which are positioned inside the segments, on borders of the segments or, when they are on on borders, they can connect two or more segments.
    seasonalStructure is a list of two elements:

    • segments – a list of vectors representing segments. Each vector must contain two ordered real values which represent left and right borders of a segment. Segments should not intersect (inside same predictor).

    • sKnots – a list of real values (vectors of length one) or vectors of lengths two or greater (seasonal knots) defining seasons of the knots (every knot is represented by time and season). All real values must belong (be inside or on border of) segments listed in segments. If a few values represent a single seasonal knot then all these values must be on borders of some segments (or a single segment). In this case they represent a seasonal knot which connects a few segments (or both sides of one segment).

  • lambdas – a vector with three values representing lambda (smoothing) parameters (time-time, season-season, time-season flexibility parameters) for this predictor.

confidence

A vector of percentiles giving the coverage of confidence intervals. It must be greater than 0 and less than 1. If NULL, no confidence intervals are produced.

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 lambdas although with a different meaning. All zero values correspond to lambda (smoothing) parameters which will not be estimated.

nFold

An optional parameter setting the number of folds for cross validation.

reltol

An optional parameter which is passed directly to optim() when optimising the parameters of the model.

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.

trace

When TRUE, tracing is turned on.

ratioGap

Ratio to define hyperparameter bounds for one-dimensional search.

relCV

Minimum improvement required after all predictors tried. It is used to exit heuristic serach of lambda parameters.

Value

A structure containing input and output data. It is an S3 class STR, which is a list with the following components:

Author(s)

Alexander Dokumentov

References

Dokumentov, A., and Hyndman, R.J. (2016) STR: A Seasonal-Trend Decomposition Procedure Based on Regression www.monash.edu/business/econometrics-and-business-statistics/research/publications/ebs/wp13-15.pdf

See Also

STR STRmodel AutoSTR

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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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 <- heuristicSTR(data = Data,
                predictors = Predictors,
                gapCV = 48*7)

plot(elec.fit,
     xTime = as.Date("2000-01-11")+((Times-1)/48-10),
     forecastPanels = NULL)

########################################

TrendSeasonalStructure <- list(segments = list(c(0,1)),
sKnots = list(c(1,0)))
DailySeasonalStructure <- list(segments = list(c(0,48)),
                               sKnots = c(as.list(1:47), list(c(48,0))))
WeeklySeasonalStructure <- list(segments = list(c(0,336)),
                                sKnots = c(as.list(seq(4,332,4)), list(c(336,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))
DailySeasons <- as.vector(electricity[,"DailySeasonality"])
WeeklySeasons <- as.vector(electricity[,"WeeklySeasonality"])
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)
SeasonTimeKnots2 <- seq(from = head(Times, 1), to = tail(Times, 1), length.out = 12)

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))
WSeason <- list(name = "Weekly seas",
                data = SeasonData,
                times = Times,
                seasons = WeeklySeasons,
                timeKnots = SeasonTimeKnots2,
                seasonalStructure = WeeklySeasonalStructure,
                lambdas = c(0.8,0.6,100))
WDSeason <- list(name = "Dayly seas",
                 data = SeasonData,
                 times = Times,
                 seasons = WDSeasons,
                 timeKnots = SeasonTimeKnots,
                 seasonalStructure = WDSeasonalStructure,
                 lambdas = c(0.003,0,240))
TrendTempM <- list(name = "Trend temp Mel",
                   data = TempM,
                   times = Times,
                   seasons = TrendSeasons,
                   timeKnots = TrendTimeKnots,
                   seasonalStructure = TrendSeasonalStructure,
                   lambdas = c(1e7,0,0))
TrendTempM2 <- list(name = "Trend temp Mel^2",
                    data = TempM2,
                    times = Times,
                    seasons = TrendSeasons,
                    timeKnots = TrendTimeKnots,
                    seasonalStructure = TrendSeasonalStructure,
                    lambdas = c(0.01,0,0)) # Starting parameter is too far from the optimal value
Predictors <- list(Trend, WSeason, WDSeason, TrendTempM, TrendTempM2)

elec.fit <- heuristicSTR(data = Data,
                         predictors = Predictors,
                         gapCV = 48*7)

plot(elec.fit,
     xTime = as.Date("2000-01-11")+((Times-1)/48-10),
     forecastPanels = NULL)

plotBeta(elec.fit, predictorN = 4)
plotBeta(elec.fit, predictorN = 5)

stR documentation built on May 2, 2019, 2:34 a.m.