robjhyndman / forecast

Forecasting Functions for Time Series and Linear Models
http://pkg.robjhyndman.com/forecast
1.13k stars 342 forks source link

Add model argument to bats and tbats #23

Closed robjhyndman closed 8 years ago

robjhyndman commented 11 years ago

To compute forecasts and fitted values without re-estimating the model.

jamiefolson commented 10 years ago

From my understanding of the model and the code it looks like this requires updating y, x, fitted.values and errors in the "bats" object.

The time series, y, can be directly assigned, but the rest require re-estimating the states. It looks to me like this happens in forecast:::fitSpecificBATS:

        w <- .Call("makeBATSWMatrix", smallPhi_s = small.phi,
            sPeriods_s = seasonal.periods, arCoefs_s = ar.coefs,
            maCoefs_s = ma.coefs, PACKAGE = "forecast")
        g <- .Call("makeBATSGMatrix", as.numeric(alpha), beta.v,
            gamma.v, seasonal.periods, as.integer(p), as.integer(q),
            PACKAGE = "forecast")
        F <- makeFMatrix(alpha = alpha, beta = beta.v, small.phi <- small.phi,
            seasonal.periods = seasonal.periods, gamma.bold.matrix = g$gamma.bold.matrix,
            ar.coefs = ar.coefs, ma.coefs = ma.coefs)
        fitted.values.and.errors <- calcModel(y, x.nought, F,
            g$g, w)
        fitted.values.and.errors$x #these are the states and errors

Although things are somewhat more complicated than that if lambda is not NULL, but that seems to be the only temporal information returned in the model. I'd be happy to put together a patch if my understanding is accurate.

jamiefolson commented 10 years ago

I neglected to note that the actual likelihood needs to be updated, too, but that looks easy.

robjhyndman commented 10 years ago

Thanks for the offer. I'm always happy to receive patches if it saves me some work!

This patch needs to apply to tbats as well as bats but the logic is essentially identical.

There are two options here.

  1. Do not re-estimate anything, just update the object with a new y, new x, new fitted values and new residuals. Note that the new x would be conditional on the initial states previously estimated, and the new y.
  2. Re-estimate initial states, but leave all other parameters as they are. Then update the object as in 1.

I don't mind if we implement only 1 as a first pass at this, and leave 2 until later.

It might be simplest to produce a new function that is similar to fitSpecificBATS but without the optimization. Similarly for fitSpecificTBATS.

As a check, the new functions should give identical models and forecasts if the model is applied to the same data on which it was estimated, and identical models but different residuals, fitted values and forecasts if the model is applied to different data.

robjhyndman commented 8 years ago

See https://github.com/robjhyndman/forecast/commit/38b6be314069ca96626b19693212569efaa2440a