I want to generate a time series comprising three components: a seasonal component, a trend component and a remainder component. Moreover, I want to be able to chnage the level of trend, seasonality and randomness. How can I do that using R? could you please help me

  • 1
    $\begingroup$ can you please clarify if you want to generate a new time series with seasonal+trend+remainder or do you want to identify the same from a given time series. They are two different questions. $\endgroup$
    – forecaster
    Nov 29 '14 at 18:10
  • $\begingroup$ Thank you for your comment, Actually I need both. Firstly, I want to generate a new time series composing seasonal+trend+remainder. In the next step of my work, I want to identify these componenets for a given series( real data) $\endgroup$
    – Roji
    Nov 30 '14 at 1:18
  • $\begingroup$ The R package stsm is pretty much related to both purposes that you mention. The directory stsm/inst/sims in the sources of the package contains some scripts to carry out some simulations that where used to test the package. You may also be interested in this document. $\endgroup$
    – javlacalle
    Nov 30 '14 at 10:13

One possibility is to generate the data upon the state-space representation of the basic structural time series model described in Harvey (1989).

Harvey, A. C. (1989) Forecasting, Structural Time Series Models and the Kalman Filter. Cambridge University Press.

The basic structural model is defined as follows:

\begin{eqnarray*} \begin{array}{rll} \hbox{observed series:} & y_t = \mu_t + \gamma_t + \epsilon_t , & \epsilon_t \sim \hbox{NID}(0,\, \sigma^2_\epsilon) ; \\ \hbox{latent level:} & \mu_t = \mu_{t-1} + \beta_{t-1} + \xi_t , & \xi_t \sim \hbox{NID}(0,\, \sigma^2_\xi) ; \\ \hbox{latent drift:} & \beta_t = \beta_{t-1} + \zeta_t , & \zeta_t \sim \hbox{NID}(0,\, \sigma^2_\zeta) ; \\ \hbox{latent seasonal:} & \gamma_t = \sum_{j=1}^{s-1} -\gamma_{t-j} + \omega_t , & \omega_t \sim \hbox{NID}(0,\, \sigma^2_\omega) , \\ \end{array} \end{eqnarray*}

for $t=s,\dots,n$; $s$ is the periodicity of the data (e.g. $s=4$ for quarterly data).

The model provides a flexible framework to generate the kind of the data you are interested in. Setting $\sigma^2_\omega=0$ yields a model with deterministic seasonality. Setting also $\gamma_1=\dots=\gamma_{s-1}=0$ and $\sigma^2_\zeta=0$ removes the seasonal component and gives the local level model (random walk plus noise model with drift $\beta_0$). If $\sigma^2_\zeta > 0$ the local trend model is obtained, where the drift follows a random walk.

The function datagen.stsm in package stsm generates data from this model. For example, the data employed in some of the simulation exercises used to test package are generated as follows:

# generate a quarterly series from a local level plus seasonal model
pars <- c(var1 = 300, var2 = 10, var3 = 100)
m <- stsm.model(model = "llm+seas", y = ts(seq(120), frequency = 4), 
  pars = pars, nopars = NULL,)
ss <- char2numeric(m)
y <- datagen.stsm(n = 120, model = list(Z = ss$Z, T = ss$T, H = ss$H, Q = ss$Q), 
  n0 = 20, freq = 4, old.version = TRUE)$data
plot(y, main = "data generated from the local-level plus seasonal component")

data simulated from llm+seasonal model

  • $\begingroup$ So in the structural model, we need to change σ2ϵ to have different randomness values, βt and σ2ζ to change the trend level, σ2ω and γ1=⋯=γs−1 to change the seasonality level , am I right? $\endgroup$
    – Roji
    Nov 30 '14 at 1:35
  • $\begingroup$ In the R code, var1 , var2 , var3 correpond to which variances? $\endgroup$
    – Roji
    Nov 30 '14 at 1:36
  • $\begingroup$ Currently, the parameters that you can choose are the variances of each component, i.e, the sigma-squared parameters. The parameters var1, var2, var3 and var3 are related to the variances of each component in the same order as the equations described in the answer. That is, var1 is related to $\sigma^2_\epsilon$, var2 to $\sigma^2_\xi$, var3 to $\sigma^2_\zeta$ and var4 to $\sigma^2_\omega$. Similarly, in the local-level plus seasonal model (no drift component), var1, var2 and var3 are respectively related to $\sigma^2_\epsilon$, $\sigma^2_\xi$ and $\sigma^2_\omega$. $\endgroup$
    – javlacalle
    Nov 30 '14 at 10:06
  • $\begingroup$ Is it possible to generate only positive values using this package? $\endgroup$
    – Roji
    Dec 23 '14 at 21:58
  • $\begingroup$ I don't know what your purpose is. Could you simply shift the values of the series, for example adding the absolute value of the smallest generated value? $\endgroup$
    – javlacalle
    Dec 23 '14 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.