Learn R Programming

smooth (version 1.4.4)

ssarima: State-Space ARIMA

Description

Function constructs State-Space ARIMA, estimating AR, MA terms and initial states.

Usage

ssarima(data, ar.orders=c(0), i.orders=c(1), ma.orders=c(1), lags=c(1), constant=FALSE, AR=NULL, MA=NULL, initial=c("backcasting","optimal"), cfType=c("MSE","MAE","HAM","MLSTFE","MSTFE","MSEh"), h=10, holdout=FALSE, intervals=FALSE, level=0.95, intervalsType=c("parametric","semiparametric","nonparametric"), intermittent=c("none","auto","fixed","croston","tsb"), bounds=c("admissible","none"), silent=c("none","all","graph","legend","output"), xreg=NULL, initialX=NULL, updateX=FALSE, persistenceX=NULL, transitionX=NULL, ...)

Arguments

data
Data that needs to be forecasted.
ar.orders
Vector of autoregressive orders, corresponding to seasonal lags. For example ar.orders=c(1,1) with lags=c(1,12) will lead to model with AR(1), SAR(1).
i.orders
Vector of differences, corresponding to seasonal lags. For example i.orders=c(0,1) with lags=c(1,12) will lead to model with I(0), SI(1).
ma.orders
Vector of moving average orders, corresponding to seasonal lags. For example i.orders=c(1,2) with lags=c(1,12) will lead to model with MA(1), SMA(2).
lags
Defines lags for the corresponding orders (see examples above). The length of lags must correspond to the length of either ar.orders or i.orders or ma.orders. There is no restrictions on the length of lags vector. It is recommended to order lags ascending.
constant
If TRUE, constant term is included in the model.
AR
Vector or matrix of AR parameters. The order of parameters should be lag-wise. This means that first all the AR parameters of the firs lag should be passed, then for the second etc. AR of another ssarima can be passed here.
MA
Vector or matrix of MA parameters. The order of parameters should be lag-wise. This means that first all the MA parameters of the firs lag should be passed, then for the second etc. MA of another ssarima can be passed here.
initial
Can be either character or a vector of initial states. If it is character, then it can be "optimal", meaning that the initial states are optimised, or "backcasting", meaning that the initials are produced using backcasting procedure (advised for data with high frequency). If character, then initial.season will be estimated in the way defined by initial.
cfType
Type of Cost Function used in optimization. cfType can be: MSE (Mean Squared Error), MAE (Mean Absolute Error), HAM (Half Absolute Moment), MLSTFE - Mean Log Squared Trace Forecast Error, MSTFE - Mean Squared Trace Forecast Error and MSEh - optimisation using only h-steps ahead error. If cfType!="MSE", then likelihood and model selection is done based on equivalent MSE. Model selection in this cases becomes not optimal.

There are also available analytical approximations for multistep functions: aMSEh, aMSTFE and aMLSTFE. These can be useful in cases of small samples.

h
the forecasting horizon.
holdout
if TRUE, the holdout of the size h is taken from the end of the data.
intervals
If TRUE, the prediction interval is constructed.
level
Confidence level. Defines width of prediction interval.
intervalsType
Type of intervals to construct. First letter can be used instead of the whole word. This can be:

  • parametric use state-space structure of ETS. For multiplicative models they are approximated using the same function as for additive. As a result they are a bit wider than should be but are still efficient. In case of mixed models this is done using simulations, which may take longer time than for the pure additive and pure multiplicative models.

  • semiparametric are based on covariance matrix of 1 to h steps ahead errors and assumption of normal distribution.
  • nonparametric intervals use values from a quantile regression on error matrix (see Taylor and Bunn, 1999). The model used in this process is e[j] = a j^b, where j=1,..,h.
  • intermittent
    Defines type of intermittent model used. Can be: 1. none, meaning that the data should be considered as non-intermittent; 2. fixed, taking into account constant Bernoulli distribution of demand occurancies; 3. croston, based on Croston, 1972 method with SBA correction; 4. tsb, based on Teunter et al., 2011 method. 5. auto - automatic selection of intermittency type based on information criteria. The first letter can be used instead.
    bounds
    What type of bounds to use for smoothing parameters ("admissible" or "none"). The first letter can be used instead of the whole word.
    silent
    If silent="none", then nothing is silent, everything is printed out and drawn. silent="all" means that nothing is produced or drawn (except for warnings). In case of silent="graph", no graph is produced. If silent="legend", then legend of the graph is skipped. And finally silent="output" means that nothing is printed out in the console, but the graph is produced. silent also accepts TRUE and FALSE. In this case silent=TRUE is equivalent to silent="all", while silent=FALSE is equivalent to silent="none". The parameter also accepts first letter of words ("n", "a", "g", "l", "o").
    xreg
    Vector (either numeric or time series) or matrix (or data.frame) of exogenous variables that should be included in the model. If matrix included than columns should contain variables and rows - observations. Note that xreg should have number of observations equal either to in-sample or to the whole series. If the number of observations in xreg is equal to in-sample, then values for the holdout sample are produced using Naive.
    initialX
    Vector of initial parameters for exogenous variables. Ignored if xreg is NULL.
    updateX
    If TRUE, transition matrix for exogenous variables is estimated, introducing non-linear interractions between parameters. Prerequisite - non-NULL xreg.
    persistenceX
    Persistence vector $g_X$, containing smoothing parameters for exogenous variables. If NULL, then estimated. Prerequisite - non-NULL xreg.
    transitionX
    Transition matrix $F_x$ for exogenous variables. Can be provided as a vector. Matrix will be formed using the default matrix(transition,nc,nc), where nc is number of components in state vector. If NULL, then estimated. Prerequisite - non-NULL xreg.
    ...
    Other non-documented parameters. For example parameter model can accept a previously estimated SSARIMA model and use all its parameters. FI=TRUE will make the function produce Fisher Information matrix, which then can be used to calculated variances of parameters of the model.

    Value

    Object of class "smooth" is returned. It contains the list of the following values:
    • model - the name of the estimated model.
    • timeElapsed - time elapsed for the construction of the model.
    • states - the matrix of the fuzzy components of ssarima, where rows correspond to time and cols to states.
    • transition - matrix F.
    • persistence - the persistence vector. This is the place, where smoothing parameters live.
    • AR - the matrix of coefficients of AR terms.
    • I - the matrix of coefficients of I terms.
    • MA - the matrix of coefficients of MA terms.
    • constant - the value of the constant term.
    • initialType - Typetof initial values used.
    • initial - the initial values of the state vector (extracted from states).
    • nParam - number of estimated parameters.
    • fitted - the fitted values of ETS.
    • forecast - the point forecast of ETS.
    • lower - the lower bound of prediction interval. When intervals=FALSE then NA is returned.
    • upper - the higher bound of prediction interval. When intervals=FALSE then NA is returned.
    • residuals - the residuals of the estimated model.
    • errors - The matrix of 1 to h steps ahead errors.
    • s2 - standard deviation of the residuals (taking degrees of freedom into account).
    • intervalsType - type of intervals asked by user.
    • level - confidence level for intervals.
    • actuals - the original data.
    • holdout - the holdout part of the original data.
    • iprob - the fitted and forecasted values of the probability of demand occurrence.
    • intermittent - type of intermittent model fitted to the data.
    • xreg - the provided vector or matrix of exogenous variables.
    • updateX - boolean, defining, if the states of exogenous variables were estimated as well.
    • initialX - initial values for parameters of exogenous variables.
    • persistenceX - persistence vector g for exogenous variables.
    • transitionX - transition matrix F for exogenous variables.
    • ICs - values of information criteria of the model. Includes AIC, AICc and BIC.
    • cf - Cost function value.
    • cfType - Type of cost function used in the estimation.
    • FI - Fisher Information. Equal to NULL if FI=FALSE or when FI is not provided at all.
    • accuracy - the vector or accuracy measures for the holdout sample. Includes MPE, MAPE, SMAPE, MASE, MAE/mean, RelMAE and Bias coefficient (based on complex numbers). Available only when holdout=TRUE.

    Details

    The function constructs ARIMA in the Single Source of Error State-space form (first proposed in Snyder, 1985):

    $y_[t] = o_[t] (w' v_[t-l] + x_t a_[t-1] + \epsilon_[t])$

    $v_[t] = F v_[t-1] + g \epsilon_[t]$

    $a_[t] = F_[X] a_[t-1] + g_[X] \epsilon_[t] / x_[t]$

    Where $o_[t]$ is Bernoulli distributed random variable (in case of normal data equals to 1 for all observations), $v_[t]$ is a state vector (defined using ar.orders and i.orders), $x_t$ vector of exogenous parameters.

    Due to the flexibility of the model, multiple seasonalities can be used. For example, something crazy like this can be constructed: SARIMA(1,1,1)(0,1,1)[24](2,0,1)[24*7](0,0,1)[24*30], but the estimation may take a lot of time...

    References

    1. Snyder, R. D., 1985. Recursive Estimation of Dynamic Linear Models. Journal of the Royal Statistical Society, Series B (Methodological) 47 (2), 272-276.
    2. Hyndman, R.J., Koehler, A.B., Ord, J.K., and Snyder, R.D. (2008) Forecasting with exponential smoothing: the state space approach, Springer-Verlag. http://www.exponentialsmoothing.net.

    See Also

    ets, es, ces, sim.es, ges

    Examples

    Run this code
    # ARIMA(1,1,1)
    test <- ssarima(rnorm(118,100,3),ar.orders=c(1),lags=c(1),h=18,holdout=TRUE,intervals=TRUE)
    
    # The same model applied to a different data
    ssarima(rnorm(118,100,3),model=test,h=18,holdout=TRUE)
    
    # SARIMA(1,0,0)(1,0,0)[4]
    ## Not run: ssarima(rnorm(118,100,3),ar.orders=c(2,1),lags=c(1,4),h=18,holdout=TRUE)
    
    # SARIMA(1,1,1)(0,0,1)[4]
    ## Not run: ssarima(rnorm(118,100,3),ar.orders=c(1),i.orders=c(1),ma.orders=c(1,1),
    #         lags=c(1,4),h=18,holdout=TRUE)
    # ssarima(rnorm(118,100,3),ar.orders=c(1),i.orders=c(1),ma.orders=c(1,1),
    #         lags=c(1,4),h=18,holdout=TRUE,initial="o")## End(Not run)
    
    # SARIMA of a perculiar order on AirPassengers data
    ## Not run: ssarima(AirPassengers,ar.orders=c(1,0,3),i.orders=c(1,0,1),ma.orders=c(0,1,2),lags=c(1,6,12),
    #         h=10,holdout=TRUE)## End(Not run)
    
    # ARIMA(1,1,1) with Mean Squared Trace Forecast Error
    ## Not run: ssarima(rnorm(118,100,3),ar.orders=1,lags=1,h=18,holdout=TRUE,cfType="MSTFE")
    # ssarima(rnorm(118,100,3),ar.orders=1,lags=1,h=18,holdout=TRUE,cfType="aMSTFE")## End(Not run)
    
    # SARIMA(0,1,1) with exogenous variables
    ssarima(rnorm(118,100,3),ar.orders=c(1),h=18,holdout=TRUE,xreg=c(1:118))
    
    # SARIMA(0,1,1) with exogenous variables with crazy estimation of xreg
    test <- ssarima(rnorm(118,100,3),ar.orders=c(1),h=18,holdout=TRUE,xreg=c(1:118),updateX=TRUE)
    
    summary(test)
    forecast(test)
    plot(forecast(test))
    

    Run the code above in your browser using DataLab