50% off | Unlimited Data & AI Learning

Last chance! 50% off unlimited learning

Sale ends in


autostsm (version 1.1)

stsm_estimate: Trend cycle seasonal decomposition using the Kalman filter.

Description

Estimates a structural time series model using the Kalman filter and maximum likelihood. The seasonal and cycle components are assumed to be of a trigonometric form. The function checks three trend specifications to decompose a univariate time series into trend, cycle, and/or seasonal components plus noise. The function automatically detects the frequency and checks for a seasonal and cycle component if the user does not specify the frequency or decomposition model. This can be turned off by setting freq or specifying decomp. State space model for decomposition follows Yt = T_t + C_t + S_t + A*X_t + e_t, e_t ~ N(0, sig_e^2) Y is the data T is the trend component C is the cycle component S is the seasonal component X is the exogenous data with parameter vector B e is the observation error

Usage

stsm_estimate(
  y,
  exo = NULL,
  freq = NULL,
  decomp = NULL,
  trend = NULL,
  unconstrained = FALSE,
  saturating_growth = FALSE,
  multiplicative = NULL,
  par = NULL,
  seasons = NULL,
  harmonics = NULL,
  cycle = NULL,
  det_obs = FALSE,
  det_trend = NULL,
  det_seas = FALSE,
  det_drift = FALSE,
  det_cycle = FALSE,
  sig_level = 0.01,
  optim_methods = c("BFGS", "NM", "CG", "SANN"),
  maxit = 10000,
  verbose = FALSE,
  full_seasonal_spectrum = FALSE,
  seasonal_spectrum = NULL
)

Arguments

y

Univariate time series of data values. May also be a 2 column data frame containing a date column.

exo

Matrix of exogenous variables. Can be used to specify regression effects or other seasonal effects like holidays, etc.

freq

Frequency of the data (1 (yearly), 4 (quarterly), 12 (monthly), 365.25/7 (weekly), 365.25 (daily)), default is NULL and will be automatically detected

decomp

Decomposition model ("tend-cycle-seasonal", "trend-seasonal", "trend-cycle", "trend-noise")

trend

Trend specification ("random-walk", "random-walk-drift", "double-random-walk", "random-walk2"). The default is NULL which will choose the best of all specifications based on the maximum likelihood. "random-walk" is the random walk trend. "random-walk-drift" is the random walk with constant drift trend. "double-random-walk" is the random walk with random walk drift trend. "random-walk2" is a 2nd order random walk trend as in the Hodrick-Prescott filter. If trend is "random-walk", the trend model is T_t = T_t-1 + e_t, e_t ~ N(0, sig_t^2) If trend is "random-walk-drift", the trend model is T_t = T_t-1 + M_t-1 + e_t, e_t ~ N(0, sig_t^2) with M_t = d + phi*M_t-1 + n_t, n_t ~ N(0, sig_m^2) If trend is "double-random-walk", the trend model is T_t = M_t-1 + T_t-1 + e_t, e_t ~ N(0, sig_t^2) with M_t = M_t-1 + n_t, n_t ~ N(0, sig_m^2) If trend is "random-walk2", the trend model is T_t = 2T_t-1 - T_t-2 + e_t, e_t ~ N(0, sig_t^2)

unconstrained

Logical whether to remove inequality constraints on the trend during estimation

saturating_growth

Force the growth rate to converge to 0 in the long term

multiplicative

If data should be logged to create a multiplicative model. If multiplicative = TRUE, then the data is logged and the original model becomes multiplicative (Y_t = T_t * C_t * S_t * BX_t * e_t)

par

Initial parameters, default is NULL

seasons

The seasonal periods: i.e. c(365.25, 7 if yearly and weekly seasonality). Default is NULL and will be estimated via wavelet analysis.

harmonics

Vector for number of cycles per year for all of the seasons: i.e. c(unique(1:floor(12/2)), unique(1:floor(7/2))).

cycle,

The period for the longer-term cycle The default is NULL, which will use 1 harmonic per season (freq/seasons) if seasons is estimated via wavelet analysis for parsimony. Otherwise it will use c(1, 2, 4, 6, 12, 24, 365.25/7, 365.25/3.5, 365.25/2) to capture harmonics at yearly, semi-yearly, quarterly, every other month, monthly, twice per month, weekly, twice per week, and every other day to fully parameterize yearly, quarterly, monthly, and weekly but will keep only harmonics > 0 and <= freq/2. This is more parsimonious than using harmonics at 1:floor(seasons/2).

det_obs

Set the observation equation error variance to 0 (deterministic observation equation) If det_obs = TRUE then the error variance of the observation equation (sig_e) is set to 0

det_trend

Set the trend error variance to 0 (deterministic trend) If det_trend = TRUE then the error variance of the trend equation (sig_t) is set to 0 and is referred to as a smooth trend

det_seas

Set the seasonality error variances to 0 (deterministic seasonality) If det_seas = TRUE then the error variance all seasonality frequency j equations (sig_s) are set to 0 and is referred to as deterministic seasonality

det_drift

Set the drift error variance to 0 (deterministic drift) If det_drift = TRUE then the error variance of the drift equation (sig_m) is set to 0 and is refereed to as a deterministic drift

det_cycle

Set the cycle error variance to 0 (deterministic cycle) If det_cycle = TRUE then the error variance of the cycle equation (sig_c) is set to 0 and is referred to as a deterministic cycle

sig_level

Significance level to determine statistically significant seasonal frequencies

optim_methods

Vector of 1 to 3 optimization methods in order of preference ("NR", "BFGS", "CG", "BHHH", or "SANN")

maxit

Maximum number of iterations for the optimization

verbose

Logical whether to print messages or not

full_seasonal_spectrum

Whether to check for full spectrum of seasonality rather than weekly and yearly suggestion

seasonal_spectrum,

Vector of seasonal periods to check

Value

List of estimation values including a data table with coefficients, convergence code, frequency, decomposition, seasonality, cyclicality, and trend specification as well as the a data table with the original data with dates. Any exogenous data given is also returned.

Examples

Run this code
# NOT RUN {
#GDP Not seasonally adjusted
library(autostsm)
data("NA000334Q", package = "autostsm") #From FRED
NA000334Q = data.table(NA000334Q, keep.rownames = TRUE)
colnames(NA000334Q) = c("date", "y")
NA000334Q[, "date" := as.Date(date)]
NA000334Q[, "y" := as.numeric(y)]
NA000334Q = NA000334Q[date >= "1990-01-01", ]
stsm = stsm_estimate(NA000334Q)
# }

Run the code above in your browser using DataLab