[R] Problem with forward prediction using StructTS output

David Crary david.crary at gmail.com
Tue Feb 22 15:52:17 CET 2011


I am having problems with forward prediction using the output of the
Basic Structural Model from StructTS.  The following snippet
illustrates the problem:

t_end <- 139
nahead <- 20
data(AirPassengers)
ap <- log10(AirPassengers)-2
fit <- StructTS(ts(ap[1:t_end], freq=12), type="BSM")
p <- stats:::predict.StructTS(fit, n.ahead=nahead)
plot(1:t_end, ap[1:t_end], xlim=c(0,t_end+nahead), type="l", ylim=c(0,1.2))
points((t_end+1):(t_end+nahead), p$pred[1:nahead], type="l", col=2)

Varying the length of the time series, I find for values of the
variable t_end > 139, the forward prediction looks reasonable. For
values  t_end <= 139 it does not.

Looking through the R source code for StructTS, I find a factor of 100
that I can't account for at about line 99:

vx <- var(x, na.rm=TRUE)/100

If I change this line to:

vx <- var(x, na.rm=TRUE)

I get good forward predictions with the above code, even if the time
series is short.

Is there a problem with the above code snippet, or is this factor of
100 misplaced, or is there something else wrong?

Thanks,

Dave

David Crary
Applied Research Associates, Inc.
Arlington, VA  USA



More information about the R-help mailing list