Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Is it possible to update a BSTS model?

$
0
0

I am working with time-series data which updates daily. Therefore, I thought that the Bayesian framework would fit perfectly because theoretically, it is possible to update the model as new data comes in. Thus, I proceed to model my data with the bsts package in R, which give me quite promising results. Now, I have two questions:

1 - How can I save my current model and then update it with the new data?

Alternatively,

2 - Is it possible to extract the coefficient distribution so that I can plug-in as a prior of a new model based on new data?

Here is a minimal example:

library(bsts)
library(magrittr)

# Use data included in the bsts package for reproducible example

data(iclaims)
plot(initial.claims)

# Let's assume I only have the data until 2011

t0 <- window(initial.claims, end = "2011-01-01")
t0

# Model data
ss <- AddLocalLinearTrend(list(), t0$iclaimsNSA)
ss <- AddSeasonal(ss, t0$iclaimsNSA, nseasons = 52)
model1 <- bsts(iclaimsNSA ~ .,
               state.specification = ss,
               data = t0,
               niter = 1000)

predict(model1, horizon = 30) %>%
  plot(.)

# What I would like to do next

saveRDS(model1, file = "model_t0")

# Next month/year

load("model_t0")

NEW_DATA <- window(initial.claims, start = "2011-01-01")

# Option 1, get the priors manually
priors <- get_priors(model_t0) # imaginary function of what I want to do
updated_model <- bsts(iclaimsNSA ~ .,
                     state.specification = ss,
                     data = NEW_DATA,
                     niter = 1000,
                     prior = priors)

# Option 2, update the model directly
updated_model <- bsts(iclaimsNSA ~ .,
                     state.specification = ss,
                     data = NEW_DATA,
                     niter = 1000, 
                     old_model = model_t0)

I have seen this kind of approach with the brms package, but unfortunately, that package cannot deal with time-series related data.

Kind regards


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>