generate a time series comprising seasonal, trend and remainder components in R 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
 A: 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")


