Title: | Multivariate Bayesian Structural Time Series |
---|---|
Description: | Tools for data analysis with multivariate Bayesian structural time series (MBSTS) models. Specifically, the package provides facilities for implementing general structural time series models, flexibly adding on different time series components (trend, season, cycle, and regression), simulating them, fitting them to multivariate correlated time series data, conducting feature selection on the regression component. |
Authors: | Jinwen Qiu <[email protected]>, Ning Ning <[email protected]> |
Maintainer: | Ning Ning <[email protected]> |
License: | LGPL-2.1 |
Version: | 3.0 |
Built: | 2024-11-20 02:49:18 UTC |
Source: | https://github.com/cran/mbsts |
Tools for data analysis with multivariate Bayesian structural time series (MBSTS) models. Specifically, the package provides facilities for implementing general structural time series models, flexibly adding on different time series components (trend, season, cycle, and regression), simulating them, fitting them to multivariate correlated time series data, conducting feature selection on the regression component.
mbsts is described in Ning and Qiu (2021).
mbsts is provided under the LGPL-2.1 License.
Jinwen Qiu, Ning Ning
Qiu, Jammalamadaka and Ning (2018), Multivariate Bayesian Structural Time Series Model, Journal of Machine Learning Research 19.68: 1-33.
Jammalamadaka, Qiu and Ning (2019), Predicting a Stock Portfolio with the Multivariate Bayesian Structural Time Series Model: Do News or Emotions Matter?, International Journal of Artificial Intelligence, Vol. 17, Number 2.
Ning and Qiu (2021), The mbsts package: Multivariate Bayesian Structural Time Series Models in R.
The MBSTS model uses MCMC to sample from the posterior distribution of a MBSTS model. The model is given by
where ,
,
,
, and
denote the trend component, the seasonal component, the cycle component, the regression component, and the error term, respectively. Note that, without a regression component, the MBSTS model is an ordinary state space time series model. The predictors and response variables in the MBSTS model are designed to be contemporaneous. Lags and differences can be generated by manipulating the predictor matrix. The "spike-and-slab" prior is used for the regression component of models, which enables feature selection among a large number of features.
mbsts_function( Y, Xtrain, STmodel, ki, pii, b = NULL, v0, kapp = 0.01, R2 = 0.8, v = 0.01, ss = 0.01, mc = 500, burn = 50 ) ## S4 method for signature 'array' mbsts_function( Y, Xtrain, STmodel, ki, pii, b = NULL, v0, kapp = 0.01, R2 = 0.8, v = 0.01, ss = 0.01, mc = 500, burn = 50 )
mbsts_function( Y, Xtrain, STmodel, ki, pii, b = NULL, v0, kapp = 0.01, R2 = 0.8, v = 0.01, ss = 0.01, mc = 500, burn = 50 ) ## S4 method for signature 'array' mbsts_function( Y, Xtrain, STmodel, ki, pii, b = NULL, v0, kapp = 0.01, R2 = 0.8, v = 0.01, ss = 0.01, mc = 500, burn = 50 )
Y |
A ( |
Xtrain |
A ( |
STmodel |
A state space model of SSmodel class returned by tsc.setting. |
ki |
A vector of integer values denoting the acumulated number of predictors for target series. For example, if there are three target series where the first has |
pii |
A vector describing the prior inclusion probability of each candidate predictor. |
b |
NULL or a vector describing the prior means of regression coefficients. The default value is NULL. |
v0 |
A numerical value describing the prior degree of freedom of the inverse Wishart distribution for |
kapp |
A scalar value describing the number of observations worth of weight on the prior mean vector. The default value is |
R2 |
A numerical value taking value in |
v |
A numerical value describing the prior degree of freedom of the inverse Wishart distribution for ( |
ss |
A numerical value describing the prior scale matrix of the inverse Wishart distribution for ( |
mc |
A positive integer giving the desired number of MCMC draws. The default value is |
burn |
A positive integer giving the number of initial MCMC draws to be discarded. The default value is |
An object of mbsts class
Jinwen Qiu [email protected] Ning Ning [email protected]
Qiu, Jammalamadaka and Ning (2018), Multivariate Bayesian Structural Time Series Model, Journal of Machine Learning Research 19.68: 1-33.
Ning and Qiu (2021), The mbsts package: Multivariate Bayesian Structural Time Series Models in R.
Jammalamadaka, Qiu and Ning (2019), Predicting a Stock Portfolio with the Multivariate Bayesian Structural Time Series Model: Do News or Emotions Matter?, International Journal of Artificial Intelligence, Vol. 17, Number 2.
This class constructor build an object of MBSTS class, encoding a state space model together with a uni- or multi-variate time series, which is central to all the package's functionality. One implements the MBSTS model by specifying some or all of its basic components.
Xtrain
A ()-dimensional matrix containing all candidate predictor series for each target series.
is the number of all candidate predictors for all target series. The first
variables are the set of candidate predictors for the first target series, and the next
variables are the set of candidate predictors for the second target series, etc. Note that, one variable can appear in the X.star several times, since different target series can contain the same candidate predictors.
Ind
A ((mc-burn))-dimensional matrix containing MCMC draws of the indicator variable. If X.star is null, it will not be returned.
beta.hat
A ((mc-burn))-dimensional matrix containing MCMC draws of regression coefficients. If X.star is null, it will not be returned.
B.hat
A ((mc-burn))-dimensional array generated by combining beta.hat for all target series. If X.star is null, it will not be returned.
ob.sig2
A ((mc-burn))-dimensional array containing MCMC draws of variance-covariance matrix for residuals.
States
A ((mc-burn))-dimensional array containing MCMC draws of all time series components, where
is the number of all time series components. If the STmodel is null, it will not be returned.
st.sig2
A ((mc-burn))-dimensional matrix containing MCMC draws of variances for time series components. If the STmodel is null, it will not be returned.
ki
A vector of integer values denoting the acumulated number of predictors for target series. For example, if there are three target series where the first has predictors, the second has
predictors, and the third has
predictors, then the vector is c(
).
ntrain
A numerical value for number of observations.
mtrain
A numerical value for number of response variables.
Jinwen Qiu [email protected] Ning Ning [email protected]
Ning and Qiu (2021), The mbsts package: Multivariate Bayesian Structural Time Series Models in R.
Generate draws from the posterior predictive distribution of a mbsts object. Samples from the posterior predictive distribution of the MBSTS model.
mbsts.forecast(object, STmodel, newdata, steps = 1)
mbsts.forecast(object, STmodel, newdata, steps = 1)
object |
An object of the mbsts class created by a call to the mbsts_function function. |
STmodel |
An object of the SSModel class created by a call to the tsc.setting function. |
newdata |
A vector or matrix containing the predictor variables to use in making the prediction. This is only required if the mbsts model has a regression component. |
steps |
An integer value describing the number of time steps ahead to be forecasted. If it is greater than the number of new observations in the newdata, zero values will fill in missing new observations. |
An object of predicted values which is a list containing the following:
pred.dist |
An array of draws from the posterior predictive distribution. The first dimension in the array represents time, the second dimension denotes each target series, and the third dimension indicates each MCMC draw. |
pred.mean |
A matrix giving the posterior mean of the prediction for each target series. |
pred.sd |
A matrix giving the posterior standard deviation of the prediction for each target series. |
pred.se |
A matrix giving the posterior standard error of the prediction for each target series, calculated by pred.sd divided by the square root of the numer of MCMC iterations. |
Jinwen Qiu [email protected] Ning Ning [email protected]
Qiu, Jammalamadaka and Ning (2018), Multivariate Bayesian Structural Time Series Model, Journal of Machine Learning Research 19.68: 1-33.
Ning and Qiu (2021), The mbsts package: Multivariate Bayesian Structural Time Series Models in R.
Jammalamadaka, Qiu and Ning (2019), Predicting a Stock Portfolio with the Multivariate Bayesian Structural Time Series Model: Do News or Emotions Matter?, International Journal of Artificial Intelligence, Vol. 17, Number 2.
Generate feature selection and parameter estimation results of a mbsts object. Provide means and standard deviations of parameter estimation results for selected features.
para.est(object, prob.threshold = 0.2) ## S4 method for signature 'mbsts' para.est(object, prob.threshold = 0.2)
para.est(object, prob.threshold = 0.2) ## S4 method for signature 'mbsts' para.est(object, prob.threshold = 0.2)
object |
An object of the mbsts class created by a call to the mbsts_function function. |
prob.threshold |
A numerical value used as the threshold to only include predictors whose inclusion probabilities are higher than it in the plot. The default is |
A list with the following components
index |
An array of feature selection results. |
para.est.mean |
An array of means of parameter estimation values of selected features. |
para.est.sd |
An array of standard deviations of parameter estimation values of selected features. |
Jinwen Qiu [email protected] Ning Ning [email protected]
Qiu, Jammalamadaka and Ning (2018), Multivariate Bayesian Structural Time Series Model, Journal of Machine Learning Research 19.68: 1-33.
Ning and Qiu (2021), The mbsts package: Multivariate Bayesian Structural Time Series Models in R.
Jammalamadaka, Qiu and Ning (2019), Predicting a Stock Portfolio with the Multivariate Bayesian Structural Time Series Model: Do News or Emotions Matter?, International Journal of Artificial Intelligence, Vol. 17, Number 2.
Plots of the mean of posterior state components of each target series, which is generated by the model training procedure of the MBSTS model.
plot_comp( object, slope, local, season, cyc, time = NULL, title = NULL, component_selection = "All" ) ## S4 method for signature 'mbsts' plot_comp( object, slope, local, season, cyc, time = NULL, title = NULL, component_selection = "All" )
plot_comp( object, slope, local, season, cyc, time = NULL, title = NULL, component_selection = "All" ) ## S4 method for signature 'mbsts' plot_comp( object, slope, local, season, cyc, time = NULL, title = NULL, component_selection = "All" )
object |
An object of the mbsts class created by a call to the mbsts_function function. |
slope |
A logical vector indicating whether there is trend for each target series, such as c(T,T). |
local |
A logical vector indicating whether there is local level for each target series, such as c(T,T). |
season |
A numerical vector indicating the seasonality for each target series, such as c(12,0). |
cyc |
A logical vector indicating whether there is a cycle component for each target series, such as c(F,T). |
time |
Null or a data frame for time index of the time series. The default value is data.frame(seq(1,n)). |
title |
NULL or a character vector whose entries are titles for the plots of target series' posterior state components, such as c("Posterior State Components of y1", "Posterior State Components of y2"). The default is c("y1","y2",...). |
component_selection |
A character variable whose value must be one of "All", "Trend", "Seasonal", "Cycle", and "Regression". Here, "Trend" means the trend component only and "All" means all the components. |
Jinwen Qiu [email protected] Ning Ning [email protected]
Qiu, Jammalamadaka and Ning (2018), Multivariate Bayesian Structural Time Series Model, Journal of Machine Learning Research 19.68: 1-33.
Ning and Qiu (2021), The mbsts package: Multivariate Bayesian Structural Time Series Models in R.
Jammalamadaka, Qiu and Ning (2019), Predicting a Stock Portfolio with the Multivariate Bayesian Structural Time Series Model: Do News or Emotions Matter?, International Journal of Artificial Intelligence, Vol. 17, Number 2.
Plot of the parameter draw for MCMC iterations after burn-in.
plot_cvg( object, index, type = "o", col = "blue", pch = 16, lty = 1, xlab = "Number of iterations", ylab = "Estimation", main = "Predictor", cex.axis = 1.15 ) ## S4 method for signature 'mbsts' plot_cvg( object, index, type = "o", col = "blue", pch = 16, lty = 1, xlab = "Number of iterations", ylab = "Estimation", main = "Predictor", cex.axis = 1.15 )
plot_cvg( object, index, type = "o", col = "blue", pch = 16, lty = 1, xlab = "Number of iterations", ylab = "Estimation", main = "Predictor", cex.axis = 1.15 ) ## S4 method for signature 'mbsts' plot_cvg( object, index, type = "o", col = "blue", pch = 16, lty = 1, xlab = "Number of iterations", ylab = "Estimation", main = "Predictor", cex.axis = 1.15 )
object |
An object of the mbsts class created by a call to the mbsts_function function. |
index |
A numerical value indicating which predictor to analyze. The index can be generated by a call to the para.est function |
type |
NULL or a character vector whose entries are titles for the plots of target series' posterior state components, such as c("Posterior State Components of y1", "Posterior State Components of y2"). The default is c("y1","y2",...). |
col |
The same setting as that of the plot function in the base package. |
pch |
The same setting as that of the plot function in the base package. |
lty |
The same setting as that of the plot function in the base package. |
xlab |
The same setting as that of the plot function in the base package. |
ylab |
The same setting as that of the plot function in the base package. |
main |
The same setting as that of the plot function in the base package. |
cex.axis |
The same setting as that of the plot function in the base package. |
Ning Ning [email protected]
Qiu, Jammalamadaka and Ning (2018), Multivariate Bayesian Structural Time Series Model, Journal of Machine Learning Research 19.68: 1-33.
Ning and Qiu (2021), The mbsts package: Multivariate Bayesian Structural Time Series Models in R.
Jammalamadaka, Qiu and Ning (2019), Predicting a Stock Portfolio with the Multivariate Bayesian Structural Time Series Model: Do News or Emotions Matter?, International Journal of Artificial Intelligence, Vol. 17, Number 2.
Plots of the empirical inclusion probabilities for predictors of each target series, based on a user-defined threshold probability. For example, one predictor is selected times in
MCMC draws (after discard burn-in draws), the empirical inclusion probability for that predictor is
. If the user-defined threshold probability less than or equal to
, then this predictor will show in the plot.
plot_prob(object, title = NULL, prob.threshold = 0.2, varnames = NULL) ## S4 method for signature 'mbsts' plot_prob(object, title = NULL, prob.threshold = 0.2, varnames = NULL)
plot_prob(object, title = NULL, prob.threshold = 0.2, varnames = NULL) ## S4 method for signature 'mbsts' plot_prob(object, title = NULL, prob.threshold = 0.2, varnames = NULL)
object |
An object of the mbsts class created by a call to the mbsts_function function. |
title |
NULL or A character vector whose entries are titles for the inclusion probability plots generated for each target series, such as c("Inclusion Probabilities for y1", "Inclusion Probabilities for y2"). If Null, the output is c("y1","y2",...). |
prob.threshold |
A numerical value used as the threshold to only include predictors whose inclusion probabilities are higher than it in the plot. The default value is |
varnames |
NULL or A character vector whose entries are the variable names for predictors, such as c("x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8"). If Null, the output is c("x11","x12",...,"x21","x22",...). |
Jinwen Qiu [email protected] Ning Ning [email protected]
Qiu, Jammalamadaka and Ning (2018), Multivariate Bayesian Structural Time Series Model, Journal of Machine Learning Research 19.68: 1-33.
Ning and Qiu (2021), The mbsts package: Multivariate Bayesian Structural Time Series Models in R.
Jammalamadaka, Qiu and Ning (2019), Predicting a Stock Portfolio with the Multivariate Bayesian Structural Time Series Model: Do News or Emotions Matter?, International Journal of Artificial Intelligence, Vol. 17, Number 2.
Generate simulated data in the form of structural time series
sim_data( X, beta, cov, k, mu, rho, mean_trend = 1, sd_trend = 0.5, mean_season = 20, sd_season = 0.5, mean_cycle = 20, sd_cycle = 0.5, Dtilde, Season, vrho, lambda ) ## S4 method for signature 'array' sim_data( X, beta, cov, k, mu, rho, mean_trend = 1, sd_trend = 0.5, mean_season = 20, sd_season = 0.5, mean_cycle = 20, sd_cycle = 0.5, Dtilde, Season, vrho, lambda )
sim_data( X, beta, cov, k, mu, rho, mean_trend = 1, sd_trend = 0.5, mean_season = 20, sd_season = 0.5, mean_cycle = 20, sd_cycle = 0.5, Dtilde, Season, vrho, lambda ) ## S4 method for signature 'array' sim_data( X, beta, cov, k, mu, rho, mean_trend = 1, sd_trend = 0.5, mean_season = 20, sd_season = 0.5, mean_cycle = 20, sd_cycle = 0.5, Dtilde, Season, vrho, lambda )
X |
A ( |
beta |
A ( |
cov |
A ( |
k |
A |
mu |
A |
rho |
A |
mean_trend |
A numerical value standing for the mean of the error term of the trend component. The default value is |
sd_trend |
A numerical value standing for the standard deviation of the error term of the trend component. The default value is |
mean_season |
A numerical value standing for the mean of the error term of the seasonal component. The default value is |
sd_season |
A numerical value standing for the standard deviation of the error term of the seasonal component. The default value is |
mean_cycle |
A numerical value standing for the mean of the error term of the cycle component. The default value is |
sd_cycle |
A numerical value standing for the standard deviation of the error term of the cycle component. The default value is |
Dtilde |
A |
Season |
A |
vrho |
A |
lambda |
A |
Jinwen Qiu [email protected] Ning Ning [email protected]
Qiu, Jammalamadaka and Ning (2018), Multivariate Bayesian Structural Time Series Model, Journal of Machine Learning Research 19.68: 1-33.
Ning and Qiu (2021), The mbsts package: Multivariate Bayesian Structural Time Series Models in R.
Jammalamadaka, Qiu and Ning (2019), Predicting a Stock Portfolio with the Multivariate Bayesian Structural Time Series Model: Do News or Emotions Matter?, International Journal of Artificial Intelligence, Vol. 17, Number 2.
###############Setup########### n<-505 #n: sample size m<-2 #m: dimension of target series cov<-matrix(c(1.1,0.7,0.7,0.9), nrow=2, ncol=2) #covariance matrix of target series ###############Regression component########### #coefficients for predictors beta<-t(matrix(c(2,-1.5,0,4,2.5,0,0,2.5,1.5,-1,-2,0,0,-3,3.5,0.5),nrow=2,ncol=8)) set.seed(100) X1<-rnorm(n,5,5^2) X4<-rnorm(n,-2,5) X5<-rnorm(n,-5,5^2) X8<-rnorm(n,0,100) X2<-rpois(n, 10) X6<-rpois(n, 15) X7<-rpois(n, 20) X3<-rpois(n, 5) X<-cbind(X1,X2,X3,X4,X5,X6,X7,X8) ###############Simulated data################ set.seed(100) data=sim_data(X=X, beta=beta, cov, k=c(8,8), mu=c(1,1), rho=c(0.6,0.8), Dtilde=c(-1,3), Season=c(100,0), vrho=c(0,0.99), lambda=c(0,pi/100))
###############Setup########### n<-505 #n: sample size m<-2 #m: dimension of target series cov<-matrix(c(1.1,0.7,0.7,0.9), nrow=2, ncol=2) #covariance matrix of target series ###############Regression component########### #coefficients for predictors beta<-t(matrix(c(2,-1.5,0,4,2.5,0,0,2.5,1.5,-1,-2,0,0,-3,3.5,0.5),nrow=2,ncol=8)) set.seed(100) X1<-rnorm(n,5,5^2) X4<-rnorm(n,-2,5) X5<-rnorm(n,-5,5^2) X8<-rnorm(n,0,100) X2<-rpois(n, 10) X6<-rpois(n, 15) X7<-rpois(n, 20) X3<-rpois(n, 5) X<-cbind(X1,X2,X3,X4,X5,X6,X7,X8) ###############Simulated data################ set.seed(100) data=sim_data(X=X, beta=beta, cov, k=c(8,8), mu=c(1,1), rho=c(0.6,0.8), Dtilde=c(-1,3), Season=c(100,0), vrho=c(0,0.99), lambda=c(0,pi/100))
Specify three time series components for the MBSTS model: the generalized linear trend component, the seasonal component, and the cycle component.
tsc.setting(Ytrain, mu, rho, S, vrho, lambda)
tsc.setting(Ytrain, mu, rho, S, vrho, lambda)
Ytrain |
The multivariate time series to be modeled. |
mu |
A vector of logic values indicating whether to include a local trend for each target series. |
rho |
A vector of numerical values taking values in |
S |
A vector of integer values representing the number of seasons to be modeled for each target series. The value |
vrho |
A vector of numerical values taking values in |
lambda |
A vector of numerical values, whose entries equal to |
An object of the SSModel class.
Jinwen Qiu [email protected] Ning Ning [email protected]
Qiu, Jammalamadaka and Ning (2018), Multivariate Bayesian Structural Time Series Model, Journal of Machine Learning Research 19.68: 1-33.
Ning and Qiu (2021), The mbsts package: Multivariate Bayesian Structural Time Series Models in R.
Jammalamadaka, Qiu and Ning (2019), Predicting a Stock Portfolio with the Multivariate Bayesian Structural Time Series Model: Do News or Emotions Matter?, International Journal of Artificial Intelligence, Vol. 17, Number 2.