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
    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. – forecaster Nov 29 '14 at 18:10
  • 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) – Roji Nov 30 '14 at 1:18
  • 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. – javlacalle Nov 30 '14 at 10:13
up vote 9 down vote accepted

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
require(stsm)
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)
set.seed(123)
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

  • 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? – Roji Nov 30 '14 at 1:35
  • In the R code, var1 , var2 , var3 correpond to which variances? – Roji Nov 30 '14 at 1:36
  • 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$. – javlacalle Nov 30 '14 at 10:06
  • Is it possible to generate only positive values using this package? – Roji Dec 23 '14 at 21:58
  • 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? – javlacalle Dec 23 '14 at 22:28

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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