Add simple bayesian structure time series model into pick best forecast component.
The mathematical models behind the approach are:
\[Time Series = Trend + Seasonality + Regression + Noise\] \[Y_t = U_t + S_t + \beta^T X_t+ \epsilon_t\]
\[U_t=U_{t-1}+\delta_{t-1}+\omega_t\] \[\delta_t=\delta_{t-1}+v_t\] \[S_t=\sum_{s=1}^{s-1} S_{t-s}+\gamma_t\] Where \(\epsilon_t\), \(\omega_t\), \(v_t\) and \(\gamma_t\) are independent components of Gaussian random noise. \(U_t\) is the current level of the trend, the current slop of the trend is \(\delta_t\). The seasonal component \(S_t\) can be thought of as a set of \(s\) of seasons. \(X_t\) is the external regressors and \(\beta^T\) is the regression coefficients.
If the univariate time series data set is used for forecasting, the Bayesian structural model can be simplified as follow, since the series doesn’t have any regressors.
\[Time Series = Trend + Seasonality + Noise\] \[Y_t = U_t + S_t + \epsilon_t\]
\[U_t=U_{t-1}+\delta_{t-1}+\omega_t\] \[\delta_t=\delta_{t-1}+v_t\] \[S_t=\sum_{s=1}^{s-1} S_{t-s}+\gamma_t\]
rabsts(data, season.num, season.dur, pred.horizon)
data - Input data. season.num - The number of season to be modeled.
i.e., for a time series with quarterly seasonality, season.num=4. season.dur - The number of time periods in each season.
i.e., when season.num=4,
if it is monthly data, then season.dur=3;
if it is weekly data, then season.dur=13. pred.horizon - The number of periods you wish to predict.
Note: seasonal component (season.num and season.dur) can be determined by external analysis.A data frame containing the following as columns:
result$mean - The posterior mean of the prediction; result$median - The posterior median of the prediction. bsts## BEGIN: function ##
rabsts <- function(data, season.num, season.dur, pred.horizon) {
##get trend or/and seasonal state
if(season.num==0){
ss <- bsts::AddLocalLinearTrend(list(), data)}
else{
ss <- bsts::AddLocalLinearTrend(list(), data)
ss <- bsts::AddSeasonal(ss, data, nseasons = season.num, season.duration = season.dur)}
##build Bayesial model
bsts.model <- bsts::bsts(data, state.specification = ss, niter = 666, ping=0, seed=1000)
##predict
result<-bsts::predict.bsts(bsts.model, horizon = pred.horizon, burn = SuggestBurn(0.1, bsts.model), quantiles = c(.025, .975))
pred<-data.frame(result$mean,result$median)
return(pred)
}
## END: function ##
First, we need to load the required R packages.
suppressMessages(library(bsts))
Now, we need some input data. The unit test data sets for rapbf package is hired for the prototype. It looks like:
Segment the data by seriesId:
#Input data
dat <- subset(inputData, seriesId=="1")
dat <- droplevels(dat)
#Holdout data
holdout.dat<-subset(holdoutData, seriesId=="1")
holdout.dat <- droplevels(holdout.dat)
Apply the Bayesial time series model for forecast:
x<-rabsts(dat$value,season.num=12,season.dur=1,pred.horizon=3) #assume that there are 12 seasons and each season is one month.
data.frame(x,holdout.dat$value)
y<-rabsts(dat$value,season.num=0,pred.horizon=3) #If no season, set season.num=0.
data.frame(y,holdout.dat$value)