Bayesian Analysis of Multivariate Threshold Autoregressive Models with Missing Data
The R package BMTAR implements parameter estimation using a
Bayesian approach for MTAR models (as specific cases, model: AR, VAR and
TAR) with missing data using Markov Chain Monte Carlo methods. This
package performs the simulation of MTAR process (mtarsim
).
Estimation of matrix parameters and the threshold values conditional on
the autoregressive orders and number of regimes (mtarns
).
Identification of the autoregressive orders using Bayesian variable
selection, together with coefficients and covariance matrices and the
threshold values conditional on the number of regimes
(mtarstr
). Identification of the number of regimes using
Metropolised Carlin and Chib or via NAIC criteria
(mtarnumreg
), to calculate NAIC of any estimated model
(mtarNAIC
). Estimate missing values together with matrix
parameters conditional to threshold values, autoregressive orders and
numbers of regimes (mtarmissing
). The diagnostic of the
residuals in any estimated model can be done
(diagnostic_mtar
). The package manage several class objects
for autoplot and print, functions like
(tsregime
),(mtaregime
) and
(mtarinipars
) make its construction. Finally,
(auto_mtar
) its an automatic function that performs all
above. ## MTAR model Let
and
be stochastic processes such that
and
is a univariate process.
follows a MTAR model with threshold variable
if:
where is the number of regimes, are the thresholds, which define the regimes. are called output covariates and threshold processes respectively.
Additionally, the innovation process follows a multivariate independent Gaussian zero-mean process with covariance identity matrix it is mutually independent of .
You can install the development version from Github.
("devtools")
install.packagesdevtools::install_github("adrincont/BMTAR")
As mention in the first paragraph lets introduce the objects class and usage in the different functions.
tsregime
return an object class ‘tsregime’ which is how
the package manage data.mtaregime
return an object of class ‘regime’ use for
simulation purposes and as standard presentation of the final
estimations.mtarsim
return an object of class ‘mtarsim’ use in
autoplot methods. Its practical to conditionate some functions for
different known parameters.mtarinipars
return an object of class ‘regime_inipars’
that itself contains an object of class ‘tsregime’, it is the main
object that save known parameters and parameters of the prior
distributions for each parameter in a MTAR model. This object needs to
be provided in every estimation function.mtarns
and mtarstr
return an object of
class ‘regime_model’ use in print and autoplot methods, its an standard
presentation for estimations done in this functions. It is the object to
introduce in mtarNAIC
.mtarmissing
return an object of class ‘regime_missing’
for print and autoplot methods.mtarnumreg
return an object of class
‘regime_number’(BMTAR)
library(ggplot2)
library
(datasim_miss)
data
= tsregime(datasim_miss$Yt,datasim_miss$Zt,datasim_miss$Xt)
data (data,1)
autoplot.tsregime(data,2)
autoplot.tsregime(data,3)
autoplot.tsregime
# Fill in the missing data with the component average
= t(datasim_miss$Yt)
Y_temp = apply(Y_temp,1,mean,na.rm = T)
meanY [apply(Y_temp,2,is.na)] = meanY
Y_temp= t(Y_temp)
Y_temp = datasim_miss$Xt
X_temp = mean(X_temp,na.rm = T)
meanX [apply(X_temp,2,is.na)] = meanX
X_temp= datasim_miss$Zt
Z_temp = mean(Z_temp,na.rm = T)
meanZ [apply(Z_temp,2,is.na)] = meanZ
Z_temp
# Estimate the number of regimens with the completed series
= tsregime(Y_temp,Z_temp,X_temp)
data_temp = mtarinipars(tsregime_obj = data_temp,list_model = list(l0_max = 3),method = 'KUO')
initial = mtarnumreg(ini_obj = initial,iterprev = 500,niter_m = 500,burn_m = 500, list_m = TRUE,ordersprev = list(maxpj = 2,maxqj = 2,maxdj = 2),parallel = TRUE)
estim_nr (estim_nr)
print
# Estimate the structural and non-structural parameters
# for the series once we know the number of regimes and some idea of its orders
= mtarinipars(tsregime_obj = data_temp,method = 'KUO',
initial = list(pars = list(l = estim_nr$final_m),
list_model = list(pj = c(2,2))))
orders = mtarstr(ini_obj = initial,niter = 500,chain = TRUE)
estruc (estruc,1)
autoplot.regime_model(estruc,2)
autoplot.regime_model(estruc,3)
autoplot.regime_model(estruc,4)
autoplot.regime_model(estruc,5)
autoplot.regime_model(estruc)
diagnostic_mtar
# With the known structural parameters we estimate the missing data
= list(pars = list(l = estim_nr$final_m,r = estruc$estimates$r[,2],orders = estruc$orders))
list_model = mtarinipars(tsregime_obj = datasim_miss,list_model = list_model)
initial = mtarmissing(ini_obj = initial,chain = TRUE, niter = 500,burn = 500)
missingest (missingest)
print(missingest,1)
autoplot.regime_missing= missingest$tsregim
data_c # ============================================================================================#
# Once the missing data has been estimated, we make the estimates again for all the structural
# and non-structural parameters.
# ============================================================================================#
= mtarinipars(tsregime_obj = data_c,list_model = list(l0_max = 3),method = 'KUO')
initial = mtarnumreg(ini_obj = initial,iterprev = 500,niter_m = 500,burn_m = 500, list_m = TRUE,ordersprev = list(maxpj = 2,maxqj = 2,maxdj = 2))
estim_nr (estim_nr)
print
= mtarinipars(tsregime_obj = data_c,method = 'KUO',
initial = list(pars = list(l = estim_nr$final_m),orders = list(pj = c(2,2))))
list_model = mtarstr(ini_obj = initial,niter = 500,chain = TRUE)
estruc (estruc,1)
autoplot.regime_model(estruc,2)
autoplot.regime_model(estruc,3)
autoplot.regime_model(estruc,4)
autoplot.regime_model(estruc,5)
autoplot.regime_model(estruc) diagnostic_mtar
MTAR is a general model were it is possible to specificate other kind of models we are familiar with, like
spec/Model | AR | VAR | TAR |
---|---|---|---|
k | 1 | >= 1 | 1 |
Regimes | 1 | 1 | > 1 |
Threshold process | x | x | ✓ |
This can be useful when you have missing data in one of this types of models and use BMTAR package for its estimation based on a bayesian approach.
If in the MTAR model specification with k = 1, l = 1 and d = 0 we have:
(mtar)
library(ggplot2)
library(forecast)
library# AR = MTAR k = 1, l = 1, Zt = NO
= mtaregime(orders = list(p = 2),Phi = list(phi1 = 0.4,phi2 = 0.3),Sigma = 2)
R1 = mtarsim(100,list(R1))
data = arima.sim(list(ar = c(0.4,0.3),sd = 2),100)
ardata ggpubr::ggarrange(
(tsregime(ardata)) + ggplot2::labs(title = 'base package'),
autoplot(data$Sim) + ggplot2::labs(title = 'mtar package'),ncol = 2)
autoplot= arima(ts(data$Sim$Yt),c(2,0,0))
arima1 = list(l = 1,orders = list(pj = 2))
parameters = mtarinipars(tsregime_obj = data$Sim,list_model = list(pars = parameters))
initial = mtarns(ini_obj = initial,niter = 1000,chain = TRUE)
estim1 (estim1)
print.regime_modelggpubr::ggarrange(
(estim1,5) + theme(legend.position = 'none') +
autoplot(title = 'mtar package'),
labs(data = NULL,aes(x = 1:100,y = data$Sim$Yt)) +
ggplot(col = 'black') + geom_line(data = NULL,
geom_line(x = 1:100,y = fitted(arima1)),col = "blue") + theme_bw() +
aes(title = 'forecast package'),ncol = 2)
labs(estim1) diagnostic_mtar
If in the MTAR model specification with l = 1 and d = 0 we have:
(mtar)
library(ggplot2)
library# VAR = MTAR k > 1, l = 1, Zt = NO
(vars)
library(BVAR)
library(tsDyn)
library= mtaregime(orders = list(p = 1,q = 0,d = 0),
R1 = list(phi1 = matrix(c(0.3,0.2,0.1,0.4),2,2)),
Phi = matrix(c(1,0.5,0.5,1),2,2))
Sigma = mtarsim(100,list(R1))
data = tsDyn::VAR.sim(B = matrix(c(0.3,0.2,0.1,0.4),2,2),n = 100,lag = 1,include = c('none'),varcov = matrix(c(1,0.5,0.5,1),2,2))
data2 ggpubr::ggarrange(
(data$Sim) + labs(title = 'mtar package'),
autoplotforecast::autoplot(ts(data2),facets = TRUE) + theme_bw() +
(title = 'tsDyn package'),ncol = 2
labs)
= tsDyn::lineVar(data$Sim$Yt,lag = 1,include = 'none',model = 'VAR')
var0 = vars::VAR(y = data$Sim$Yt,p = 1)
var1 = BVAR::bvar(data = data$Sim$Yt,lags = 1)
var2 = list(l = 1,orders = list(pj = 1))
parameters = mtarinipars(tsregime_obj = data$Sim,list_model = list(pars = parameters))
initial = mtarns(ini_obj = initial,niter = 1000,chain = TRUE)
estim1
estim1$regime
var0
var1$varresult(var2$beta[,,1],2,mean)
apply(var2$beta[,,2],2,mean)
apply(var2$sigma[,,1],2,mean)
apply(var2$sigma[,,2],2,mean)
apply(estim1)
print.regime_modelggpubr::ggarrange(
(estim1,5) + theme(legend.position = 'none') +
autoplot(title = 'mtar package'),
labsforecast::autoplot(ts(data$Sim$Yt),facets = TRUE) + theme_bw() +
(title = 'tsDyn package') + forecast::autolayer(ts(var0$fitted.values)) +
labs(title = 'tsDyn package') + theme(legend.position = 'none'),ncol = 2)
labs(estim1) diagnostic_mtar
If in the MTAR model specification with k = 1 we have:
# Example 1, TAR model with 2 regimes
= arima.sim(n = 500,list(ar = c(0.5)))
Z = 2;r = 0;K = c(2,1)
l = matrix(c(1,-0.5,0.5,-0.7,-0.3,NA), nrow = l)
theta = c(1, 1.5)
H = simu.tar.norm(Z,l,r,K,theta,H)
X = tsregim(Yt = X,Zt = Z,r = r)
Yt = mtaregim(orders = list(p = 2),cs = 1,Phi = list(phi1 = -0.5,phi2 = 0.5),
R1 = 1)
Sigma = mtaregim(orders = list(p = 1),cs = -0.7,Phi = list(phi1 = -0.3),
R2 = sqrt(1.5))
Sigma = mtarsim(500,list(R1,R2),r,Zt = Z)
YtSim ggpubr::ggarrange(
(Yt) + ggplot2::labs(title = 'TAR package'),
autoplot(YtSim$Sim) + ggplot2::labs(title = 'mtar package'),ncol = 2)
autoplot# number of regimes
= reg.thr.norm(Z,X)
res
res$L.est
res$L.prob
res$R.est
res$R.CI= mtarinipars(Yt,list_model = list(l0_min = 2,l0_max = 3),method = 'KUO')
initial = mtarnumreg(initial)
resmtar # structural parameters
= ARorder.norm(Z,X,l,r)
res2
res2$K.est
res2$K.prob= mtarinipars(Yt,list_model = list(pars = list(l = 2),
initial = list(pj = c(2,2),dj = c(1,1))),method = 'KUO')
orders = mtarstr(initial)
res2mtar
res2mtar$orders# non-structural parameters
= Param.norm(Z,X,l,r,K) #gibbs
res3 = LS.norm(Z,X,l,r,c(0,0)) #least square
res4 = mtarinipars(Yt,list(pars = list(l = 2,orders = list(pj = c(1,1)))))
initial = mtarns(initial) res3mtar
You will find the theoretical basis of the method in the documents:
This package is free and open source software, licensed under GPL-3.