[R] StructTS with 2 seasons
Birgit Erni
Birgit.Erni at uct.ac.za
Tue Nov 30 09:40:00 CET 2010
Dear All,
I am trying to fit a structural time series model using the StructTS
function (package stats) with only 2 seasons (summer and winter). More
than 2 seasons work fine but with 2 seasons I get this error:
> fit <- StructTS(y.ts, type="BSM")
Error in T[cbind(ind + 1L, ind)] <- 1 : subscript out of bounds
I have looked at Prof. Ripley's 2002 RNews article but cannot see why
theoretically it should not be possible to have only 2 seasons. I have
also looked at the StructTS code, and the problem is with the transition
matrix in makeBSM, but I am not sure whether I should overwrite this or
whether my thinking is wrong.
I attach code that simulates data for the basic structural model (with
time-varying trend and season), the top line s determines the number of
seasons in a year (3 and 4 work fine, 2 gives the above error), and at
the end tries to fit the model. I am using R version 2.11.1, Windows XP.
# ----- Basic structural model -----
s <- 2 # frequency or period
mu0 <- 6
beta0 <- 1
it0 <- 0
it.states <- sample(1:6,s)
mu <- numeric() # mean
beta <- numeric() # slope
it <- numeric() # seasonal state
mu[1] <- mu0 + beta0 + rnorm(1,0,2)
beta[1] <- beta0 + rnorm(1,0,0.5)
it[1] <- - sum(it.states[2:s]) + rnorm(1,0,1)
it.states <- c(it[1],it.states[2:s])
for (i in 2:30)
{ mu[i] <- mu[i-1] + beta[i-1] + rnorm(1,0,2)
beta[i] <- beta[i-1] + rnorm(1,0,2)
it[i] <- - sum(it.states[1:(s-1)]) + rnorm(1,0,1)
it.states <- c(it[i],it.states[1:(s-1)])
}
y <- numeric()
for (i in 1:30)
{ y[i] <- mu[i] + it[i] + rnorm(1,0,3) }
par(mfrow=c(2,2))
plot(y,type="b")
plot(mu)
plot(beta)
plot(it)
y.ts <- ts(y,start=c(1977,1),frequency=s)
y.ts
fit <- StructTS(y.ts, type="BSM")
fit
# ------------------------------------------------------
Thank you.
Birgit Erni
Department of Statistical Sciences
University of Cape Town, South Africa
###
UNIVERSITY OF CAPE TOWN
This e-mail is subject to the UCT ICT policies and e-mai...{{dropped:13}}
More information about the R-help
mailing list