1
$\begingroup$

I am self-learning about structural time series, and for me the best way to understand topic is to simulate the data myself. I want to simulate a time series of local level model with seasonal components:

$$y_t = \mu_t + \gamma_t + v_t$$ $$ \mu_t = \mu_{t-1} +w_t$$

Where both disturbance terms are normally distributed. My code to simulate the time series is:

require(bsts)
set.seed(1234)
y2 <- c()
mu2 <- c(0)
seasons <- seq(-5,5,length.out =12)/5

for (i in 2:200) {
    w1 <- rnorm(1,0,0.1)
    v <- rnorm(1,0,0.1)
    mu2 <- c(mu2, mu2[i-1] +w1)
    y2 <- c(y2,mu2[i]+seasons[i%%12-1] +v)
}

I intentionally put really strong seasonality and small disturbance variances, so the model could capture it easier:

ss <- AddLocalLevel(list(), y2)
ss <- AddSeasonal(ss, y2, nseasons = 12)

model <- bsts(y2 ,state.specification = ss,niter = 3000)
pr <- predict(model,horizon = 30,burn = 100)
plot(pr)

Unfortunately, the predictions don't seem to capture the seasonality at all:

enter image description here

What did I do wrong? Did I poorly simulate the time series? Or did I mis-specify the model? Or boht?

$\endgroup$

1 Answer 1

0
$\begingroup$

The issue is how you're generating y2:

y2 <- c(y2,mu2[i]+seasons[i%%12 - 1] +v)

should be

y2 <- c(y2,mu2[i]+seasons[i%%12 + 1] +v)

Figured this out by checking if seasons[i%%12 -1] went from 1:12 cyclically:

purrr::map_dbl(0:23, ~ .x %% 12 - 1)

which shows that you you're going from -1:10 cyclically, which will wash out your seasonality.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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