varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in...

15
varstan: An R package for Bayesian analysis of structured time series models with Stan Izhar Asael Alonzo Matamoros Universidad de Nacional AutÃşnoma de Honduras Cristian Andres Cruz Torres Abstract varstan is an R package (R Core Team 2017) for Bayesian analysis of time series mod- els using Stan (Stan, Development. Team 2017). The package offers a dynamic way to choose a model, define priors in a wide range of distributions, check model’s fit, and fore- cast with the m-steps ahead predictive distribution. The users can widely choose between implemented models such as multiplicative seasonal ARIMA, dynamic regression, ran- dom walks, GARCH, dynamic harmonic regressions,VARMA, stochastic Volatility Mod- els, and generalized t-student with unknown degree freedom GARCH models. Every model constructor in varstan defines weakly informative priors, but prior specifications can be changed in a dynamic and flexible way, so the prior distributions reflect the parameter’s initial beliefs. For model selection, the package offers the classical information criteria: AIC, AICc, BIC, DIC, Bayes factor. And more recent criteria such as Widely-applicable information criteria (WAIC ), and the Bayesian leave one out cross-validation (loo). In ad- dition, a Bayesian version for automatic order selection in seasonal ARIMA and dynamic regression models can be used as an initial step for the time series analysis. Keywords : Time series, Bayesian analysis, structured models, Stan, R. 1. Introduction: Structured models such as ARIMA Box and Jenkins (1978), GARCH Engle (1982) and Boller- slev (1986), Random walks and VARMA models are widely used for time series analysis and forecast. Several R packages (R Core Team 2017) such as forecast (Hyndman and Khandakar 2008) and astsa (Stoffer 2019), have been developed for estimating the models with classic inferences methods. Although a Bayesian approach offers several advantages such as incorpo- rating prior knowledge in parameters, estimating the posterior distribution in complex models is a hard task and an analytical solution may not be feasible. A Markov chain Monte Carlo (MCMC) approach using Gibbs sampler limits prior selection to be conjugate to the likelihood or Metropolis-Hasting algorithms struggles with slow convergence in high dimensional mod- els. The No U-Turn Sampler Hoffman and Gelman (2014) algorithm provided by Stan (Stan, Development. Team 2017) offers a fast convergence, prior flexibility and its own programming language for modeling (for a further discussion of samplers and algorithms BÃijrkner (2017)). The package varstan, is an R interface of Stan’s language for time series modeling, offering a wide range of models, priors choice and methods making Bayesian time series analysis feasible. The aim of this article is to give a general overview of the package functionality. First, arXiv:2005.10361v1 [stat.CO] 20 May 2020

Transcript of varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in...

Page 1: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

varstan An R package for Bayesian analysis ofstructured time series models with Stan

Izhar Asael Alonzo MatamorosUniversidad de Nacional AutAtildeşnoma de Honduras

Cristian Andres Cruz Torres

Abstract

varstan is an R package (R Core Team 2017) for Bayesian analysis of time series mod-els using Stan (Stan Development Team 2017) The package offers a dynamic way tochoose a model define priors in a wide range of distributions check modelrsquos fit and fore-cast with the m-steps ahead predictive distribution The users can widely choose betweenimplemented models such as multiplicative seasonal ARIMA dynamic regression ran-dom walks GARCH dynamic harmonic regressionsVARMA stochastic Volatility Mod-els and generalized t-student with unknown degree freedom GARCH models Every modelconstructor in varstan defines weakly informative priors but prior specifications can bechanged in a dynamic and flexible way so the prior distributions reflect the parameterrsquosinitial beliefs For model selection the package offers the classical information criteriaAIC AICc BIC DIC Bayes factor And more recent criteria such as Widely-applicableinformation criteria (WAIC ) and the Bayesian leave one out cross-validation (loo) In ad-dition a Bayesian version for automatic order selection in seasonal ARIMA and dynamicregression models can be used as an initial step for the time series analysis

Keywords Time series Bayesian analysis structured models Stan R

1 Introduction

Structured models such as ARIMA Box and Jenkins (1978) GARCH Engle (1982) and Boller-slev (1986) Random walks and VARMA models are widely used for time series analysis andforecast Several R packages (R Core Team 2017) such as forecast (Hyndman and Khandakar2008) and astsa (Stoffer 2019) have been developed for estimating the models with classicinferences methods Although a Bayesian approach offers several advantages such as incorpo-rating prior knowledge in parameters estimating the posterior distribution in complex modelsis a hard task and an analytical solution may not be feasible A Markov chain Monte Carlo(MCMC) approach using Gibbs sampler limits prior selection to be conjugate to the likelihoodor Metropolis-Hasting algorithms struggles with slow convergence in high dimensional mod-els The No U-Turn Sampler Hoffman and Gelman (2014) algorithm provided by Stan (StanDevelopment Team 2017) offers a fast convergence prior flexibility and its own programminglanguage for modeling (for a further discussion of samplers and algorithms BAtildeijrkner (2017))The package varstan is an R interface of Stanrsquos language for time series modeling offering awide range of models priors choice and methods making Bayesian time series analysis feasible

The aim of this article is to give a general overview of the package functionality First

arX

iv2

005

1036

1v1

[st

atC

O]

20

May

202

0

2 varstan Bayesian time series analysis with Stan in R

general definitions of structured time series models and priors selection Then a discussionof the estimating process is given Also packagersquos functionality is introduced as well as apresentation of the most important functions Finally an analysis of the monthly live birthsin the USA (1948 -1979) is given as an example of the package modeling process

2 Structured Time series modelsA time series model is just a sample of a stochastic process (Y = Ytinfint=1) where everyobservation Yt describes the random variable response in a particular time t Letrsquos say theprocess follows a location-scale model Migon Gamerman and Louzada (2014) with normaldistribution Yt sim N(microt σt) where the mean (microt) and variance (σt) are considered the locationand scale parameters with a time dependency In other words every observation can bewritten as follows

Yt = microt + σtεt εt sim N(0 1) iid

The basic ARIMA model proposed by Box and Jenkins (1978) can be seen as a location-scalemodel where the time dependency structure is for the location parameter

(1minusB)dYi = micro0 +psum

i=0φi(1minusB)dYtminusi minus

qsumi=1

θiεtminusi + σ0εt (1)

where the previous equation is written as

(1minusB)dYt = microt + σtεt

notice that

bull B is the back-shift operator where BdYt = Ytminusd

bull d is the number of differences needed so the process is stationary

bull p is the number of considered lags in the auto-regressive component

bull q is the number of considered lags in the mean average component

bull microt = micro0 +sumpi=0 φi(1minusB)dYtminusi minus

sumqi=1 θiεtminusi clearly has a time dependency

bull σt = σ0 does not have a time dependency because is constant in time

bull εt sim N(0 1) are the independent identically distributed (iid) random errors

Models with more complex structure such as multiplicative seasonal ARIMA models

SARIMA(p d q)x(PDQ)S

where S is the periodicityfrequency D the seasonal differences P auto-regressive and Qmean average seasonal components Tsay (2010) and dynamic regression Kennedy (1992)are location-scale models with additional changes in the basic ARIMA structure Letrsquos sayY = Yt | Yt sim N(microt σ) follows an ARIMA(pdq) model as in (1) then a dynamic regressionis just adding independent terms to the location parameter

(1minusB)dYt = microt + (1minusB)dXtb

Izhar Asael Alonzo Matamoros 3

Xt are the additional variables (no time dependence considered) b are the regression pa-rameters and every variable in Xt has the same differences as in the ARIMA model AGARCH model proposed by Bollerslev (1986) as a generalization of an ARCH (Engle 1982)the location-scale structure is easier to be noticed but is fair to recall that the time depen-dency structure is for the scale parameter

Yt = microt + σtεt

σ2t = σ0 +

ssumi=1

αiε2tminusi +

ksumi=1

βiσ2t

In this model the location parameter is constant in time and σ0 is the arch constant param-eter1 An additional variation of the garch model is the student-t innovations with unknowndegrees of freedom Which implies adding latent parameters to the GARCH structure

Yt = micro+ εt

(v minus 2v

σ2t λt

)12

where the unknown degrees of freedom have an inverse gamma distribution (λt sim IG(v2 v2))and the hyper-parameter v is unknown A further discussion is given by Fonseca CerqueiraMigon and Torres (2019)

21 Prior Distribution

By default varstan declares weakly informative normal priors for every lagged parame-ter2 Other distributions can be chosen and declared to every parameter in a dynamicway before the estimation process starts For simplicity varstan provides parameters()and distribution() functions that prints the defined parameters for a specific model andprints the available prior distributions for a specific parameter respectively

The priors distributions for the constant mean (micro0) and regression coefficients can be chosenbetween normal t student Cauchy gamma uniform and beta For the constant scale pa-rameter (σ0) a gamma inverse gamma (IG) half normal chi square half t-student or halfCauchy distributions

In SARIMA models non stationary or explosive process Shumway and Stoffer (2010) couldcause divergences in Stanrsquos estimation procedure To avoid this the ARMA coefficients arerestricted to a Φ = [minus1 1] domain and the available prior distributions are uniform normaland beta3 4

For VARMA models the covariance matrix Σ0 is factorized in terms of a correlation matrixΩ0 and a diagonal matrix that has the standard deviations on the non zero values through

Σ0 = DΩ0D

1 Wuertz Setz Chalabi Boudt Chausse and Miklovac (2020) denoted σ0 as α0 or ω02Lagged parameters are the ones in the ARMA or GARCH components in (1) the φirsquos are the lagged

parameters of the auto-regressive part3 In SARIMA we define in Φ domain4 If θ sim beta(α β) in [0 1] then θ1 = 2(θ minus 1) sim beta(α β) in [minus1 1]

4 varstan Bayesian time series analysis with Stan in R

where D = diag(σ1 σ2 σd) denotes the diagonal matrix with σi elements that acceptspriors just like scale constant parameter (σ0) Following the Stan recommendations for Ω0a LKJ-correlation prior with parameter ζ gt 0 by Lewandowski Kurowicka and Joe (2009)is proposed For a further discussion of why this option is better than a conjugated inverseWhishart distribution see BAtildeijrkner (2017) and Natarajan and Kass (2000)

The prior distribution for α and β parameters in a GARCH model can be chosen betweennormal uniform or beta distribution this is due to their similarity to an auto-regressivecoefficient constrained in [01] For the MGARCH parameters a normal t-student Cauchygamma uniform or beta distributions can be chosen Finally for the v hyper-parameterin a GARCH model with unknown degree freedom innovations a normal inverse gammaexponential gamma and a non-informative Jeffreyrsquos prior are available Fonseca Ferreiraand Migon (2008)

3 Estimation processJust like brms (BAtildeijrkner 2017) or rstanarm (Goodrich Gabry Ali and Brilleman 2020)packages varstan does not fit the model parameters itself It only provides a Stan (StanDevelopment Team 2017) interface in R (R Core Team 2017) and works exclusively with theextended Hamiltonian Monte Carlo Duane (1987) No U-Turn Sampler algorithm of Hoffmanand Gelman (2014) The main reasons for using this sampler are its fast convergences andits less correlated samples BAtildeijrkner (2017) compares between Hamiltonian Monte Carloand other MCMC methods and Betancourt (2017) provides a conceptual introduction of theHamiltonian Monte Carlo

After fitting the model varstan provides functions to extract the posterior residuals and fittedvalues such as the predictive m-steps ahead and predictive errors distribution For modelselection criteria varstan provides posterior sample draws for the pointwise log-likelihoodAkaike Information Criteria (AIC) corrected AIC (AICc) and Bayesian Information Criteria(BIC) Bierens (2006) For a better performance in the model selection an adaptation of thebayes_factor of the bridgesampling (Gronau Singmann and Wagenmakers 2020) packagethe Bayesian leave one out (loo) and the Watanabe Akaike information criteria (waic) fromthe loo (Vehtari and Gabry 2017) package are provided Vehtari Gelman and Gabry (2016)and Kass and Raftery (1995)

The bayes_factor() approximates the modelrsquos marginal likelihood using the bridgesamplingalgorithm see Gronau Sarafoglou Matzke Ly Boehm Marsman Leslie Forster Wagen-makers and Steingroever (2017) for further detail The waic proposed by Watanabe (2010)is an improvement of the Deviance information criteria (DIC) proposed by Spiegelhalter BestCarlin and Van Der Linde (2002) The loo is asymptotically equivalent to the waic Watan-abe (2010) and is usually preferred over the second one Vehtari et al (2016)

31 Automatic order selection in arima models

Selecting an adequate order in a seasonal ARIMA model might be considered a difficult task

Izhar Asael Alonzo Matamoros 5

In Stan an incorrect order selection might be considered an ill model producing multiple di-vergent transitions Several procedures for automatic order selection have been proposed Tsay(2010) Hannan and Rissanen (1982) and Gomez (1998) A Bayesian version of HyndmanAthanasopoulos Bergmeir Caceres Chhay OrsquoHara-Wild Petropoulos Razbash Wang andYasmeen (2020) algorithm implemented in their forecast (Hyndman and Khandakar 2008)package is proposed This adaptation consists in proposing several models and select thebest one using a simple criteria such as AIC BIC or loglik Finally fit the selected model

In the proposed function the BIC is used as selection criteria for several reasons it can befast computed and it is asymptotically equivalent to the bayes_factor After a model isselected the function fits the model with default weak informative priors Even so a BICis a poor criteria for model selection This methodology usually selects a good initial modelwith a small amount of divergences (usually solved with more iterations) delivering acceptableresults For further reading and discussion see Hyndman and Khandakar (2008)

4 Package structure and modeling procedureSimilar to brms (BAtildeijrkner 2017) and rstanarm (Goodrich et al 2020) varstan is an R inter-face for Stan therefore the rstan (Stan Development Team 2020) package and a C++ compileris required the httpsgithubcomstan-devrstanwiki RStan-Getting-Started vignette hasa detailed explanation of how to install all the prerequisites in every operative system (Win-dows Mac and Linux) We recommend to install R-4000 version or hihger for avoidingcompatibility problems The current development version can be installed from GitHub usingthe next code

Rgt if (requireNamespace(remotes)) installpackages(remotes)

Rgt remotesinstall_github(asael697varstandependencies = TRUE)

The varstan dynamic is different from other packages First the parameters are not fittedafter a model is declared this was considerate so the user could select the parameter priors in adynamic way and call the sampler with a satisfactory defined model Second all fitted modelbecame a varstan S3 class the reason of this is to have available summary plot diagnosticand predict methods for every model regardless of its complexity The procedure for a timeseries analysis with varstan is explained in the next steps

1 Prepare the data varstan package supports numeric matrix and time series classes(ts)

2 Select the model the version() function provides a list of the current models Theirinterface is similar to forecast and Rrsquos stats packagesThese functions return a list withthe necessary data to fit the model in Stan

3 Change the priors varstan package defines by default weak informative priors Func-tions like set_prior() get_prior and print() aloud to change and check the modelspriors

6 varstan Bayesian time series analysis with Stan in R

Other useful functions are parameters() that prints the parameterrsquos names of a spec-ified model and distribution() prints the available prior distributions of a specifiedparameter

4 Fit the model the varstan() function call Stan and fit the defined model Parameterslike number of iterations and chains warm-up and other Stanrsquos control options areavailable The varstan() class contains a stanfit object returned from the rstanpackage that can be used for more complex Bayesian analysis

5 Check the model summary() plot()autoplot() and extract_stan() methods areavailable for model diagnostic and extract the parameterAcircťs posterior chains

The plot and summary methods will only provide general diagnostics and visualizationsfor further analysis use the bayesplot (Gabry and Mahr 2019) package

6 Select the model For multiple models varstan provides loglik() posterior_residuals()posterior_fit() AIC() BIC() WAIC() loo() and bayes_factor functions for modelselection criterias

7 Forecast the posterior_predict() function samples from the modelrsquos n-steps aheadpredictive distribution

5 Case study Analyzing the monthly live birth in US an example

As an example a time series modeling for the monthly live births in the United States 1948-1979 published in astsa (Stoffer 2019) package is provided In Figure 1 the data has aseasonal behavior that repeats annually The series waves in the whole 40 years period (supe-rior part) In addition the partial (pacf) and auto-correlation (acf) functions are far fromzero (middle part) and have the same wave pattern as birth series Indicating non stationaryand a strong periodic behavior After applying a difference to the data the acf and pacfplots still have some non-zero values every 12 lags (inferior part)

Izhar Asael Alonzo Matamoros 7

Figure 1 Monthly live birth USA

For start a seasonal ARIMA model could give a good fit to the data Following Tsay (2010)recommendations for order selection using the auto-correlation functions a p = 1 d = 1q = 1 and for the seasonal part P = 1 D = 1 Q = 1 The model is defined in varstan asfollows

Rgt model1 = Sarima(birthorder = c(111)seasonal = c(111))Rgt model1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

PriorsInterceptmu0 ~ t (loc = 0 scl = 25 df = 6 )

Scale Parametersigma0 ~ half_t (loc = 0 scl = 1 df = 7 )

ar[ 1 ] ~ normal (mu = 0 sd = 05 )ma[ 1 ] ~ normal (mu = 0 sd = 05 )

8 varstan Bayesian time series analysis with Stan in R

Seasonal Parameterssar[ 1 ] ~ normal (mu = 0 sd = 05 )sma[ 1 ] ~ normal (mu = 0 sd = 05 )NULL

The function Sarima generates a Seasonal ARIMA model ready to be fitted in Stan (StanDevelopment Team 2017) As the model is printed all the important information is shownthe model to be fit the total observations of the data the seasonal period the current obser-vations that can be used after differences and a list of priors for all the modelrsquos parametersUsing the information provided by the acf-plot in Figure 1 (middle right) the partial auto-correlations are not that strong and a normal distribution for the auto-regressive coefficient(ar[1]) could explore values close to 1 or -1 causing the prior to be too informative In-stead beta distribution in [minus1 1]5 centered at zero could be a more proper prior With thefunctions set_prior() and get_prior() any change is automatically updated and checked

Rgt model1 = set_prior(model = model1dist = beta(22)par = ar)Rgt get_prior(model = model1par = ar)

ar[ 1 ] ~ beta (form1 = 2 form2 = 2 )

Now that the model and priors are defined what follows is to fit the model using thevarstan() function One chain of 2000 iterations and a warm-up of the first 1000 chainrsquosvalues is simulated

Rgt sfit1 = varstan(model = model1chains = 1iter = 2000warmup = 1000)Rgt sfit1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

mean se 25 975 ess Rhatmu0 00061 00020 00020 00101 3941739 10028sigma0 73612 00043 73528 73697 4001665 10000phi -02336 00014 -02362 -02309 3594099 10007theta 00692 00017 00658 00726 3808853 10019sphi -00351 00015 -00381 -00321 3376811 10033stheta 06188 00017 06153 06222 3427074 10048loglik -12322519 00325 -12323157 -12321882 3198671 10033

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

5 If θ sim beta(α β) in [0 1] then θ1 = 2(θ minus 1) sim beta(α β) in [minus1 1]

Izhar Asael Alonzo Matamoros 9

All fitted models are varstan objects these are S3 classes with the stanfit results provided bythe rstan (Stan Development Team 2020) package and other useful elements that make themodeling process easier After fitting the proposed model a visual diagnostic of parameterscheck residuals and fitted values using the plot methods On figure 2 trace and posteriordensity plots are illustrated for all the model parameters

Rgt plot(sfit1type = parameter)

Figure 2 Trace and density plots for all the fitted parameters

In figure 2 all the chains appeared to be stationary and the posteriors seems to have nomulti-modal distributions Indicating that all chains have mixed and converged One usefulway to assess modelsrsquo fit is by the residuals (et = YtminusYt) The package provides the posteriorsample of every residual but checking all of them is an exhausting task An alternative ischecking the process generated by the residuals posterior estimate A white noise behaviorindicates a good model fit The modelrsquos residuals in figure 3 seems to follow a random noisethe auto-correlation in acf plots quickly falls to zero indicating an acceptable model fit

Rgt p1 = plot(sfit1type = residuals)Rgt p2 = plot(sfit1)

Rgt gridarrange(p2p1ncol = 1)

10 varstan Bayesian time series analysis with Stan in R

Figure 3 Posterior median residual plot

Because of the sinusoidal pattern that birth series (figure 1) presents a dynamic Harmonicregression (A fourier transform with arima structure for errors) could also assess a goodfit Kennedy (1992) To declare this model varstan offers a similar declaration structure offorecast (Hyndman and Khandakar 2008) package A harmonic regression with 4 fourierterms and ARIMA(111) residuals is declared and fitted to the birth data

Rgt model2 = Sarima(birthorder = c(111)xreg = fourier(birthK = 2))Rgt sfit2 = varstan(model = model2chains = 1iter = 2000warmup = 1000)Rgt sfit2

y ~ Sarima(111)reg[4]373 observations and 1 dimensionDifferences 1 seasonal Differences 0Current observations 372

mean se 25 975 ess Rhatmu0 -00712 00068 -00846 -00578 9396338 10032sigma0 108085 00124 107841 108328 9949722 10003phi -02705 00019 -02742 -02668 10044136 10006theta 06242 00015 06212 06272 9216062 09992breg1 -216318 00407 -217116 -215520 9659837 10006breg2 06619 00305 06021 07217 9767075 10002breg3 47937 00207 47531 48344 10791161 10003

Izhar Asael Alonzo Matamoros 11

breg4 -53570 00249 -54059 -53082 10994993 10005loglik -14153887 00697 -14155252 -14152521 9360271 10005

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

In this scenario both models suggest to be a good choice for birth series analysis Even sothe harmonic regression fits more parameters It is an obvious choice for birthrsquos sinusoidalbehavior As an example of model selection criteria the bayes_factor in logarithmic scalethat compares the modelsrsquo marginal likelihoods is computed Values above 6 (in logarithmicscale) provides good evidence for selecting the first model And for birth data the seasonalarima model (model1 ) is a better choice

Rgt bayes_factor(x1 = sf1x2 = sfit2log = TRUE)

Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Estimated log Bayes factor in favor of model1 over model2 19913745

Now a comparison of the selected model (model1 sim Sarima(111)(111)[12]) and the onegiven by the autosarima() function For this purpose a leave of one out cross validationloo() is used and both looic are compared with the loo_compare() function provided bythe loo (Vehtari and Gabry 2017) package

Rgt sfit3 = autosarima(birthchains = 1iter = 4000)Rgt sfit3

y ~ Sarima(012)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Diferences 1Current observations 360

mean se 25 975 ess Rhatmu0 00080 00018 00045 00116 2050372 09997sigma0 73517 00060 73399 73634 1991938 10008

12 varstan Bayesian time series analysis with Stan in R

theta1 03642 00013 03616 03668 1978174 10006theta2 01358 00011 01336 01379 2023769 10004sphi -02465 00016 -02496 -02433 2084503 10005stheta 03040 00017 03006 03073 2167639 09995loglik -12317452 00395 -12318225 -12316679 1789987 10009

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

Different from model1 the selected one does not contemplate an auto-regressive componentand use 2 mean average components insteadNow what proceeds is to estimate the loo forboth models

Rgt loo1 = loo(sfit1)Rgt loo3 = loo(sfit3)

Rgt lc = looloo_compare(loo1loo3)Rgt print(lcsimplify = FALSE)

elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looicmodel2 00 00 -12354 154 71 08 2470 308model1 -08 58 -12362 156 78 09 2472 312

loo_compare() prints first the best model In this example is the one provided by theautosarima() function where its looic is 2 units below model1 This autosarima()function is useful as starting point But the reader is encouraged to test more complexmodels and priors that adjust to the initial beliefs

ConclusionsThe paper gives a general overview of varstan package as a starting point of Bayesian timeseries analysis with structured models and it offers a simple dynamic interface inspired inthe classic functions provided by forecast astsa and var packages The interface functionsand prior flexibility that varstan offers makes Bayesian analysis flexible as classic methodsfor structured linear time series models The packagersquos goal is to provide a wide range ofmodels with a prior selection flexibility In a posterior version non-linear models such aswackier GARCH variants stochastic volatility hidden Markov state-space and uni-variateDynamic linear models will be included Along with several improvements in the packagersquosfunctionality

AcknowledgmentsFirst of all we would like to thank the Stan Development team and the Stan forumrsquos commu-nity for always being patience and helping fixing bugs and doubts in the package development

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 2: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

2 varstan Bayesian time series analysis with Stan in R

general definitions of structured time series models and priors selection Then a discussionof the estimating process is given Also packagersquos functionality is introduced as well as apresentation of the most important functions Finally an analysis of the monthly live birthsin the USA (1948 -1979) is given as an example of the package modeling process

2 Structured Time series modelsA time series model is just a sample of a stochastic process (Y = Ytinfint=1) where everyobservation Yt describes the random variable response in a particular time t Letrsquos say theprocess follows a location-scale model Migon Gamerman and Louzada (2014) with normaldistribution Yt sim N(microt σt) where the mean (microt) and variance (σt) are considered the locationand scale parameters with a time dependency In other words every observation can bewritten as follows

Yt = microt + σtεt εt sim N(0 1) iid

The basic ARIMA model proposed by Box and Jenkins (1978) can be seen as a location-scalemodel where the time dependency structure is for the location parameter

(1minusB)dYi = micro0 +psum

i=0φi(1minusB)dYtminusi minus

qsumi=1

θiεtminusi + σ0εt (1)

where the previous equation is written as

(1minusB)dYt = microt + σtεt

notice that

bull B is the back-shift operator where BdYt = Ytminusd

bull d is the number of differences needed so the process is stationary

bull p is the number of considered lags in the auto-regressive component

bull q is the number of considered lags in the mean average component

bull microt = micro0 +sumpi=0 φi(1minusB)dYtminusi minus

sumqi=1 θiεtminusi clearly has a time dependency

bull σt = σ0 does not have a time dependency because is constant in time

bull εt sim N(0 1) are the independent identically distributed (iid) random errors

Models with more complex structure such as multiplicative seasonal ARIMA models

SARIMA(p d q)x(PDQ)S

where S is the periodicityfrequency D the seasonal differences P auto-regressive and Qmean average seasonal components Tsay (2010) and dynamic regression Kennedy (1992)are location-scale models with additional changes in the basic ARIMA structure Letrsquos sayY = Yt | Yt sim N(microt σ) follows an ARIMA(pdq) model as in (1) then a dynamic regressionis just adding independent terms to the location parameter

(1minusB)dYt = microt + (1minusB)dXtb

Izhar Asael Alonzo Matamoros 3

Xt are the additional variables (no time dependence considered) b are the regression pa-rameters and every variable in Xt has the same differences as in the ARIMA model AGARCH model proposed by Bollerslev (1986) as a generalization of an ARCH (Engle 1982)the location-scale structure is easier to be noticed but is fair to recall that the time depen-dency structure is for the scale parameter

Yt = microt + σtεt

σ2t = σ0 +

ssumi=1

αiε2tminusi +

ksumi=1

βiσ2t

In this model the location parameter is constant in time and σ0 is the arch constant param-eter1 An additional variation of the garch model is the student-t innovations with unknowndegrees of freedom Which implies adding latent parameters to the GARCH structure

Yt = micro+ εt

(v minus 2v

σ2t λt

)12

where the unknown degrees of freedom have an inverse gamma distribution (λt sim IG(v2 v2))and the hyper-parameter v is unknown A further discussion is given by Fonseca CerqueiraMigon and Torres (2019)

21 Prior Distribution

By default varstan declares weakly informative normal priors for every lagged parame-ter2 Other distributions can be chosen and declared to every parameter in a dynamicway before the estimation process starts For simplicity varstan provides parameters()and distribution() functions that prints the defined parameters for a specific model andprints the available prior distributions for a specific parameter respectively

The priors distributions for the constant mean (micro0) and regression coefficients can be chosenbetween normal t student Cauchy gamma uniform and beta For the constant scale pa-rameter (σ0) a gamma inverse gamma (IG) half normal chi square half t-student or halfCauchy distributions

In SARIMA models non stationary or explosive process Shumway and Stoffer (2010) couldcause divergences in Stanrsquos estimation procedure To avoid this the ARMA coefficients arerestricted to a Φ = [minus1 1] domain and the available prior distributions are uniform normaland beta3 4

For VARMA models the covariance matrix Σ0 is factorized in terms of a correlation matrixΩ0 and a diagonal matrix that has the standard deviations on the non zero values through

Σ0 = DΩ0D

1 Wuertz Setz Chalabi Boudt Chausse and Miklovac (2020) denoted σ0 as α0 or ω02Lagged parameters are the ones in the ARMA or GARCH components in (1) the φirsquos are the lagged

parameters of the auto-regressive part3 In SARIMA we define in Φ domain4 If θ sim beta(α β) in [0 1] then θ1 = 2(θ minus 1) sim beta(α β) in [minus1 1]

4 varstan Bayesian time series analysis with Stan in R

where D = diag(σ1 σ2 σd) denotes the diagonal matrix with σi elements that acceptspriors just like scale constant parameter (σ0) Following the Stan recommendations for Ω0a LKJ-correlation prior with parameter ζ gt 0 by Lewandowski Kurowicka and Joe (2009)is proposed For a further discussion of why this option is better than a conjugated inverseWhishart distribution see BAtildeijrkner (2017) and Natarajan and Kass (2000)

The prior distribution for α and β parameters in a GARCH model can be chosen betweennormal uniform or beta distribution this is due to their similarity to an auto-regressivecoefficient constrained in [01] For the MGARCH parameters a normal t-student Cauchygamma uniform or beta distributions can be chosen Finally for the v hyper-parameterin a GARCH model with unknown degree freedom innovations a normal inverse gammaexponential gamma and a non-informative Jeffreyrsquos prior are available Fonseca Ferreiraand Migon (2008)

3 Estimation processJust like brms (BAtildeijrkner 2017) or rstanarm (Goodrich Gabry Ali and Brilleman 2020)packages varstan does not fit the model parameters itself It only provides a Stan (StanDevelopment Team 2017) interface in R (R Core Team 2017) and works exclusively with theextended Hamiltonian Monte Carlo Duane (1987) No U-Turn Sampler algorithm of Hoffmanand Gelman (2014) The main reasons for using this sampler are its fast convergences andits less correlated samples BAtildeijrkner (2017) compares between Hamiltonian Monte Carloand other MCMC methods and Betancourt (2017) provides a conceptual introduction of theHamiltonian Monte Carlo

After fitting the model varstan provides functions to extract the posterior residuals and fittedvalues such as the predictive m-steps ahead and predictive errors distribution For modelselection criteria varstan provides posterior sample draws for the pointwise log-likelihoodAkaike Information Criteria (AIC) corrected AIC (AICc) and Bayesian Information Criteria(BIC) Bierens (2006) For a better performance in the model selection an adaptation of thebayes_factor of the bridgesampling (Gronau Singmann and Wagenmakers 2020) packagethe Bayesian leave one out (loo) and the Watanabe Akaike information criteria (waic) fromthe loo (Vehtari and Gabry 2017) package are provided Vehtari Gelman and Gabry (2016)and Kass and Raftery (1995)

The bayes_factor() approximates the modelrsquos marginal likelihood using the bridgesamplingalgorithm see Gronau Sarafoglou Matzke Ly Boehm Marsman Leslie Forster Wagen-makers and Steingroever (2017) for further detail The waic proposed by Watanabe (2010)is an improvement of the Deviance information criteria (DIC) proposed by Spiegelhalter BestCarlin and Van Der Linde (2002) The loo is asymptotically equivalent to the waic Watan-abe (2010) and is usually preferred over the second one Vehtari et al (2016)

31 Automatic order selection in arima models

Selecting an adequate order in a seasonal ARIMA model might be considered a difficult task

Izhar Asael Alonzo Matamoros 5

In Stan an incorrect order selection might be considered an ill model producing multiple di-vergent transitions Several procedures for automatic order selection have been proposed Tsay(2010) Hannan and Rissanen (1982) and Gomez (1998) A Bayesian version of HyndmanAthanasopoulos Bergmeir Caceres Chhay OrsquoHara-Wild Petropoulos Razbash Wang andYasmeen (2020) algorithm implemented in their forecast (Hyndman and Khandakar 2008)package is proposed This adaptation consists in proposing several models and select thebest one using a simple criteria such as AIC BIC or loglik Finally fit the selected model

In the proposed function the BIC is used as selection criteria for several reasons it can befast computed and it is asymptotically equivalent to the bayes_factor After a model isselected the function fits the model with default weak informative priors Even so a BICis a poor criteria for model selection This methodology usually selects a good initial modelwith a small amount of divergences (usually solved with more iterations) delivering acceptableresults For further reading and discussion see Hyndman and Khandakar (2008)

4 Package structure and modeling procedureSimilar to brms (BAtildeijrkner 2017) and rstanarm (Goodrich et al 2020) varstan is an R inter-face for Stan therefore the rstan (Stan Development Team 2020) package and a C++ compileris required the httpsgithubcomstan-devrstanwiki RStan-Getting-Started vignette hasa detailed explanation of how to install all the prerequisites in every operative system (Win-dows Mac and Linux) We recommend to install R-4000 version or hihger for avoidingcompatibility problems The current development version can be installed from GitHub usingthe next code

Rgt if (requireNamespace(remotes)) installpackages(remotes)

Rgt remotesinstall_github(asael697varstandependencies = TRUE)

The varstan dynamic is different from other packages First the parameters are not fittedafter a model is declared this was considerate so the user could select the parameter priors in adynamic way and call the sampler with a satisfactory defined model Second all fitted modelbecame a varstan S3 class the reason of this is to have available summary plot diagnosticand predict methods for every model regardless of its complexity The procedure for a timeseries analysis with varstan is explained in the next steps

1 Prepare the data varstan package supports numeric matrix and time series classes(ts)

2 Select the model the version() function provides a list of the current models Theirinterface is similar to forecast and Rrsquos stats packagesThese functions return a list withthe necessary data to fit the model in Stan

3 Change the priors varstan package defines by default weak informative priors Func-tions like set_prior() get_prior and print() aloud to change and check the modelspriors

6 varstan Bayesian time series analysis with Stan in R

Other useful functions are parameters() that prints the parameterrsquos names of a spec-ified model and distribution() prints the available prior distributions of a specifiedparameter

4 Fit the model the varstan() function call Stan and fit the defined model Parameterslike number of iterations and chains warm-up and other Stanrsquos control options areavailable The varstan() class contains a stanfit object returned from the rstanpackage that can be used for more complex Bayesian analysis

5 Check the model summary() plot()autoplot() and extract_stan() methods areavailable for model diagnostic and extract the parameterAcircťs posterior chains

The plot and summary methods will only provide general diagnostics and visualizationsfor further analysis use the bayesplot (Gabry and Mahr 2019) package

6 Select the model For multiple models varstan provides loglik() posterior_residuals()posterior_fit() AIC() BIC() WAIC() loo() and bayes_factor functions for modelselection criterias

7 Forecast the posterior_predict() function samples from the modelrsquos n-steps aheadpredictive distribution

5 Case study Analyzing the monthly live birth in US an example

As an example a time series modeling for the monthly live births in the United States 1948-1979 published in astsa (Stoffer 2019) package is provided In Figure 1 the data has aseasonal behavior that repeats annually The series waves in the whole 40 years period (supe-rior part) In addition the partial (pacf) and auto-correlation (acf) functions are far fromzero (middle part) and have the same wave pattern as birth series Indicating non stationaryand a strong periodic behavior After applying a difference to the data the acf and pacfplots still have some non-zero values every 12 lags (inferior part)

Izhar Asael Alonzo Matamoros 7

Figure 1 Monthly live birth USA

For start a seasonal ARIMA model could give a good fit to the data Following Tsay (2010)recommendations for order selection using the auto-correlation functions a p = 1 d = 1q = 1 and for the seasonal part P = 1 D = 1 Q = 1 The model is defined in varstan asfollows

Rgt model1 = Sarima(birthorder = c(111)seasonal = c(111))Rgt model1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

PriorsInterceptmu0 ~ t (loc = 0 scl = 25 df = 6 )

Scale Parametersigma0 ~ half_t (loc = 0 scl = 1 df = 7 )

ar[ 1 ] ~ normal (mu = 0 sd = 05 )ma[ 1 ] ~ normal (mu = 0 sd = 05 )

8 varstan Bayesian time series analysis with Stan in R

Seasonal Parameterssar[ 1 ] ~ normal (mu = 0 sd = 05 )sma[ 1 ] ~ normal (mu = 0 sd = 05 )NULL

The function Sarima generates a Seasonal ARIMA model ready to be fitted in Stan (StanDevelopment Team 2017) As the model is printed all the important information is shownthe model to be fit the total observations of the data the seasonal period the current obser-vations that can be used after differences and a list of priors for all the modelrsquos parametersUsing the information provided by the acf-plot in Figure 1 (middle right) the partial auto-correlations are not that strong and a normal distribution for the auto-regressive coefficient(ar[1]) could explore values close to 1 or -1 causing the prior to be too informative In-stead beta distribution in [minus1 1]5 centered at zero could be a more proper prior With thefunctions set_prior() and get_prior() any change is automatically updated and checked

Rgt model1 = set_prior(model = model1dist = beta(22)par = ar)Rgt get_prior(model = model1par = ar)

ar[ 1 ] ~ beta (form1 = 2 form2 = 2 )

Now that the model and priors are defined what follows is to fit the model using thevarstan() function One chain of 2000 iterations and a warm-up of the first 1000 chainrsquosvalues is simulated

Rgt sfit1 = varstan(model = model1chains = 1iter = 2000warmup = 1000)Rgt sfit1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

mean se 25 975 ess Rhatmu0 00061 00020 00020 00101 3941739 10028sigma0 73612 00043 73528 73697 4001665 10000phi -02336 00014 -02362 -02309 3594099 10007theta 00692 00017 00658 00726 3808853 10019sphi -00351 00015 -00381 -00321 3376811 10033stheta 06188 00017 06153 06222 3427074 10048loglik -12322519 00325 -12323157 -12321882 3198671 10033

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

5 If θ sim beta(α β) in [0 1] then θ1 = 2(θ minus 1) sim beta(α β) in [minus1 1]

Izhar Asael Alonzo Matamoros 9

All fitted models are varstan objects these are S3 classes with the stanfit results provided bythe rstan (Stan Development Team 2020) package and other useful elements that make themodeling process easier After fitting the proposed model a visual diagnostic of parameterscheck residuals and fitted values using the plot methods On figure 2 trace and posteriordensity plots are illustrated for all the model parameters

Rgt plot(sfit1type = parameter)

Figure 2 Trace and density plots for all the fitted parameters

In figure 2 all the chains appeared to be stationary and the posteriors seems to have nomulti-modal distributions Indicating that all chains have mixed and converged One usefulway to assess modelsrsquo fit is by the residuals (et = YtminusYt) The package provides the posteriorsample of every residual but checking all of them is an exhausting task An alternative ischecking the process generated by the residuals posterior estimate A white noise behaviorindicates a good model fit The modelrsquos residuals in figure 3 seems to follow a random noisethe auto-correlation in acf plots quickly falls to zero indicating an acceptable model fit

Rgt p1 = plot(sfit1type = residuals)Rgt p2 = plot(sfit1)

Rgt gridarrange(p2p1ncol = 1)

10 varstan Bayesian time series analysis with Stan in R

Figure 3 Posterior median residual plot

Because of the sinusoidal pattern that birth series (figure 1) presents a dynamic Harmonicregression (A fourier transform with arima structure for errors) could also assess a goodfit Kennedy (1992) To declare this model varstan offers a similar declaration structure offorecast (Hyndman and Khandakar 2008) package A harmonic regression with 4 fourierterms and ARIMA(111) residuals is declared and fitted to the birth data

Rgt model2 = Sarima(birthorder = c(111)xreg = fourier(birthK = 2))Rgt sfit2 = varstan(model = model2chains = 1iter = 2000warmup = 1000)Rgt sfit2

y ~ Sarima(111)reg[4]373 observations and 1 dimensionDifferences 1 seasonal Differences 0Current observations 372

mean se 25 975 ess Rhatmu0 -00712 00068 -00846 -00578 9396338 10032sigma0 108085 00124 107841 108328 9949722 10003phi -02705 00019 -02742 -02668 10044136 10006theta 06242 00015 06212 06272 9216062 09992breg1 -216318 00407 -217116 -215520 9659837 10006breg2 06619 00305 06021 07217 9767075 10002breg3 47937 00207 47531 48344 10791161 10003

Izhar Asael Alonzo Matamoros 11

breg4 -53570 00249 -54059 -53082 10994993 10005loglik -14153887 00697 -14155252 -14152521 9360271 10005

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

In this scenario both models suggest to be a good choice for birth series analysis Even sothe harmonic regression fits more parameters It is an obvious choice for birthrsquos sinusoidalbehavior As an example of model selection criteria the bayes_factor in logarithmic scalethat compares the modelsrsquo marginal likelihoods is computed Values above 6 (in logarithmicscale) provides good evidence for selecting the first model And for birth data the seasonalarima model (model1 ) is a better choice

Rgt bayes_factor(x1 = sf1x2 = sfit2log = TRUE)

Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Estimated log Bayes factor in favor of model1 over model2 19913745

Now a comparison of the selected model (model1 sim Sarima(111)(111)[12]) and the onegiven by the autosarima() function For this purpose a leave of one out cross validationloo() is used and both looic are compared with the loo_compare() function provided bythe loo (Vehtari and Gabry 2017) package

Rgt sfit3 = autosarima(birthchains = 1iter = 4000)Rgt sfit3

y ~ Sarima(012)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Diferences 1Current observations 360

mean se 25 975 ess Rhatmu0 00080 00018 00045 00116 2050372 09997sigma0 73517 00060 73399 73634 1991938 10008

12 varstan Bayesian time series analysis with Stan in R

theta1 03642 00013 03616 03668 1978174 10006theta2 01358 00011 01336 01379 2023769 10004sphi -02465 00016 -02496 -02433 2084503 10005stheta 03040 00017 03006 03073 2167639 09995loglik -12317452 00395 -12318225 -12316679 1789987 10009

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

Different from model1 the selected one does not contemplate an auto-regressive componentand use 2 mean average components insteadNow what proceeds is to estimate the loo forboth models

Rgt loo1 = loo(sfit1)Rgt loo3 = loo(sfit3)

Rgt lc = looloo_compare(loo1loo3)Rgt print(lcsimplify = FALSE)

elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looicmodel2 00 00 -12354 154 71 08 2470 308model1 -08 58 -12362 156 78 09 2472 312

loo_compare() prints first the best model In this example is the one provided by theautosarima() function where its looic is 2 units below model1 This autosarima()function is useful as starting point But the reader is encouraged to test more complexmodels and priors that adjust to the initial beliefs

ConclusionsThe paper gives a general overview of varstan package as a starting point of Bayesian timeseries analysis with structured models and it offers a simple dynamic interface inspired inthe classic functions provided by forecast astsa and var packages The interface functionsand prior flexibility that varstan offers makes Bayesian analysis flexible as classic methodsfor structured linear time series models The packagersquos goal is to provide a wide range ofmodels with a prior selection flexibility In a posterior version non-linear models such aswackier GARCH variants stochastic volatility hidden Markov state-space and uni-variateDynamic linear models will be included Along with several improvements in the packagersquosfunctionality

AcknowledgmentsFirst of all we would like to thank the Stan Development team and the Stan forumrsquos commu-nity for always being patience and helping fixing bugs and doubts in the package development

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 3: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

Izhar Asael Alonzo Matamoros 3

Xt are the additional variables (no time dependence considered) b are the regression pa-rameters and every variable in Xt has the same differences as in the ARIMA model AGARCH model proposed by Bollerslev (1986) as a generalization of an ARCH (Engle 1982)the location-scale structure is easier to be noticed but is fair to recall that the time depen-dency structure is for the scale parameter

Yt = microt + σtεt

σ2t = σ0 +

ssumi=1

αiε2tminusi +

ksumi=1

βiσ2t

In this model the location parameter is constant in time and σ0 is the arch constant param-eter1 An additional variation of the garch model is the student-t innovations with unknowndegrees of freedom Which implies adding latent parameters to the GARCH structure

Yt = micro+ εt

(v minus 2v

σ2t λt

)12

where the unknown degrees of freedom have an inverse gamma distribution (λt sim IG(v2 v2))and the hyper-parameter v is unknown A further discussion is given by Fonseca CerqueiraMigon and Torres (2019)

21 Prior Distribution

By default varstan declares weakly informative normal priors for every lagged parame-ter2 Other distributions can be chosen and declared to every parameter in a dynamicway before the estimation process starts For simplicity varstan provides parameters()and distribution() functions that prints the defined parameters for a specific model andprints the available prior distributions for a specific parameter respectively

The priors distributions for the constant mean (micro0) and regression coefficients can be chosenbetween normal t student Cauchy gamma uniform and beta For the constant scale pa-rameter (σ0) a gamma inverse gamma (IG) half normal chi square half t-student or halfCauchy distributions

In SARIMA models non stationary or explosive process Shumway and Stoffer (2010) couldcause divergences in Stanrsquos estimation procedure To avoid this the ARMA coefficients arerestricted to a Φ = [minus1 1] domain and the available prior distributions are uniform normaland beta3 4

For VARMA models the covariance matrix Σ0 is factorized in terms of a correlation matrixΩ0 and a diagonal matrix that has the standard deviations on the non zero values through

Σ0 = DΩ0D

1 Wuertz Setz Chalabi Boudt Chausse and Miklovac (2020) denoted σ0 as α0 or ω02Lagged parameters are the ones in the ARMA or GARCH components in (1) the φirsquos are the lagged

parameters of the auto-regressive part3 In SARIMA we define in Φ domain4 If θ sim beta(α β) in [0 1] then θ1 = 2(θ minus 1) sim beta(α β) in [minus1 1]

4 varstan Bayesian time series analysis with Stan in R

where D = diag(σ1 σ2 σd) denotes the diagonal matrix with σi elements that acceptspriors just like scale constant parameter (σ0) Following the Stan recommendations for Ω0a LKJ-correlation prior with parameter ζ gt 0 by Lewandowski Kurowicka and Joe (2009)is proposed For a further discussion of why this option is better than a conjugated inverseWhishart distribution see BAtildeijrkner (2017) and Natarajan and Kass (2000)

The prior distribution for α and β parameters in a GARCH model can be chosen betweennormal uniform or beta distribution this is due to their similarity to an auto-regressivecoefficient constrained in [01] For the MGARCH parameters a normal t-student Cauchygamma uniform or beta distributions can be chosen Finally for the v hyper-parameterin a GARCH model with unknown degree freedom innovations a normal inverse gammaexponential gamma and a non-informative Jeffreyrsquos prior are available Fonseca Ferreiraand Migon (2008)

3 Estimation processJust like brms (BAtildeijrkner 2017) or rstanarm (Goodrich Gabry Ali and Brilleman 2020)packages varstan does not fit the model parameters itself It only provides a Stan (StanDevelopment Team 2017) interface in R (R Core Team 2017) and works exclusively with theextended Hamiltonian Monte Carlo Duane (1987) No U-Turn Sampler algorithm of Hoffmanand Gelman (2014) The main reasons for using this sampler are its fast convergences andits less correlated samples BAtildeijrkner (2017) compares between Hamiltonian Monte Carloand other MCMC methods and Betancourt (2017) provides a conceptual introduction of theHamiltonian Monte Carlo

After fitting the model varstan provides functions to extract the posterior residuals and fittedvalues such as the predictive m-steps ahead and predictive errors distribution For modelselection criteria varstan provides posterior sample draws for the pointwise log-likelihoodAkaike Information Criteria (AIC) corrected AIC (AICc) and Bayesian Information Criteria(BIC) Bierens (2006) For a better performance in the model selection an adaptation of thebayes_factor of the bridgesampling (Gronau Singmann and Wagenmakers 2020) packagethe Bayesian leave one out (loo) and the Watanabe Akaike information criteria (waic) fromthe loo (Vehtari and Gabry 2017) package are provided Vehtari Gelman and Gabry (2016)and Kass and Raftery (1995)

The bayes_factor() approximates the modelrsquos marginal likelihood using the bridgesamplingalgorithm see Gronau Sarafoglou Matzke Ly Boehm Marsman Leslie Forster Wagen-makers and Steingroever (2017) for further detail The waic proposed by Watanabe (2010)is an improvement of the Deviance information criteria (DIC) proposed by Spiegelhalter BestCarlin and Van Der Linde (2002) The loo is asymptotically equivalent to the waic Watan-abe (2010) and is usually preferred over the second one Vehtari et al (2016)

31 Automatic order selection in arima models

Selecting an adequate order in a seasonal ARIMA model might be considered a difficult task

Izhar Asael Alonzo Matamoros 5

In Stan an incorrect order selection might be considered an ill model producing multiple di-vergent transitions Several procedures for automatic order selection have been proposed Tsay(2010) Hannan and Rissanen (1982) and Gomez (1998) A Bayesian version of HyndmanAthanasopoulos Bergmeir Caceres Chhay OrsquoHara-Wild Petropoulos Razbash Wang andYasmeen (2020) algorithm implemented in their forecast (Hyndman and Khandakar 2008)package is proposed This adaptation consists in proposing several models and select thebest one using a simple criteria such as AIC BIC or loglik Finally fit the selected model

In the proposed function the BIC is used as selection criteria for several reasons it can befast computed and it is asymptotically equivalent to the bayes_factor After a model isselected the function fits the model with default weak informative priors Even so a BICis a poor criteria for model selection This methodology usually selects a good initial modelwith a small amount of divergences (usually solved with more iterations) delivering acceptableresults For further reading and discussion see Hyndman and Khandakar (2008)

4 Package structure and modeling procedureSimilar to brms (BAtildeijrkner 2017) and rstanarm (Goodrich et al 2020) varstan is an R inter-face for Stan therefore the rstan (Stan Development Team 2020) package and a C++ compileris required the httpsgithubcomstan-devrstanwiki RStan-Getting-Started vignette hasa detailed explanation of how to install all the prerequisites in every operative system (Win-dows Mac and Linux) We recommend to install R-4000 version or hihger for avoidingcompatibility problems The current development version can be installed from GitHub usingthe next code

Rgt if (requireNamespace(remotes)) installpackages(remotes)

Rgt remotesinstall_github(asael697varstandependencies = TRUE)

The varstan dynamic is different from other packages First the parameters are not fittedafter a model is declared this was considerate so the user could select the parameter priors in adynamic way and call the sampler with a satisfactory defined model Second all fitted modelbecame a varstan S3 class the reason of this is to have available summary plot diagnosticand predict methods for every model regardless of its complexity The procedure for a timeseries analysis with varstan is explained in the next steps

1 Prepare the data varstan package supports numeric matrix and time series classes(ts)

2 Select the model the version() function provides a list of the current models Theirinterface is similar to forecast and Rrsquos stats packagesThese functions return a list withthe necessary data to fit the model in Stan

3 Change the priors varstan package defines by default weak informative priors Func-tions like set_prior() get_prior and print() aloud to change and check the modelspriors

6 varstan Bayesian time series analysis with Stan in R

Other useful functions are parameters() that prints the parameterrsquos names of a spec-ified model and distribution() prints the available prior distributions of a specifiedparameter

4 Fit the model the varstan() function call Stan and fit the defined model Parameterslike number of iterations and chains warm-up and other Stanrsquos control options areavailable The varstan() class contains a stanfit object returned from the rstanpackage that can be used for more complex Bayesian analysis

5 Check the model summary() plot()autoplot() and extract_stan() methods areavailable for model diagnostic and extract the parameterAcircťs posterior chains

The plot and summary methods will only provide general diagnostics and visualizationsfor further analysis use the bayesplot (Gabry and Mahr 2019) package

6 Select the model For multiple models varstan provides loglik() posterior_residuals()posterior_fit() AIC() BIC() WAIC() loo() and bayes_factor functions for modelselection criterias

7 Forecast the posterior_predict() function samples from the modelrsquos n-steps aheadpredictive distribution

5 Case study Analyzing the monthly live birth in US an example

As an example a time series modeling for the monthly live births in the United States 1948-1979 published in astsa (Stoffer 2019) package is provided In Figure 1 the data has aseasonal behavior that repeats annually The series waves in the whole 40 years period (supe-rior part) In addition the partial (pacf) and auto-correlation (acf) functions are far fromzero (middle part) and have the same wave pattern as birth series Indicating non stationaryand a strong periodic behavior After applying a difference to the data the acf and pacfplots still have some non-zero values every 12 lags (inferior part)

Izhar Asael Alonzo Matamoros 7

Figure 1 Monthly live birth USA

For start a seasonal ARIMA model could give a good fit to the data Following Tsay (2010)recommendations for order selection using the auto-correlation functions a p = 1 d = 1q = 1 and for the seasonal part P = 1 D = 1 Q = 1 The model is defined in varstan asfollows

Rgt model1 = Sarima(birthorder = c(111)seasonal = c(111))Rgt model1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

PriorsInterceptmu0 ~ t (loc = 0 scl = 25 df = 6 )

Scale Parametersigma0 ~ half_t (loc = 0 scl = 1 df = 7 )

ar[ 1 ] ~ normal (mu = 0 sd = 05 )ma[ 1 ] ~ normal (mu = 0 sd = 05 )

8 varstan Bayesian time series analysis with Stan in R

Seasonal Parameterssar[ 1 ] ~ normal (mu = 0 sd = 05 )sma[ 1 ] ~ normal (mu = 0 sd = 05 )NULL

The function Sarima generates a Seasonal ARIMA model ready to be fitted in Stan (StanDevelopment Team 2017) As the model is printed all the important information is shownthe model to be fit the total observations of the data the seasonal period the current obser-vations that can be used after differences and a list of priors for all the modelrsquos parametersUsing the information provided by the acf-plot in Figure 1 (middle right) the partial auto-correlations are not that strong and a normal distribution for the auto-regressive coefficient(ar[1]) could explore values close to 1 or -1 causing the prior to be too informative In-stead beta distribution in [minus1 1]5 centered at zero could be a more proper prior With thefunctions set_prior() and get_prior() any change is automatically updated and checked

Rgt model1 = set_prior(model = model1dist = beta(22)par = ar)Rgt get_prior(model = model1par = ar)

ar[ 1 ] ~ beta (form1 = 2 form2 = 2 )

Now that the model and priors are defined what follows is to fit the model using thevarstan() function One chain of 2000 iterations and a warm-up of the first 1000 chainrsquosvalues is simulated

Rgt sfit1 = varstan(model = model1chains = 1iter = 2000warmup = 1000)Rgt sfit1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

mean se 25 975 ess Rhatmu0 00061 00020 00020 00101 3941739 10028sigma0 73612 00043 73528 73697 4001665 10000phi -02336 00014 -02362 -02309 3594099 10007theta 00692 00017 00658 00726 3808853 10019sphi -00351 00015 -00381 -00321 3376811 10033stheta 06188 00017 06153 06222 3427074 10048loglik -12322519 00325 -12323157 -12321882 3198671 10033

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

5 If θ sim beta(α β) in [0 1] then θ1 = 2(θ minus 1) sim beta(α β) in [minus1 1]

Izhar Asael Alonzo Matamoros 9

All fitted models are varstan objects these are S3 classes with the stanfit results provided bythe rstan (Stan Development Team 2020) package and other useful elements that make themodeling process easier After fitting the proposed model a visual diagnostic of parameterscheck residuals and fitted values using the plot methods On figure 2 trace and posteriordensity plots are illustrated for all the model parameters

Rgt plot(sfit1type = parameter)

Figure 2 Trace and density plots for all the fitted parameters

In figure 2 all the chains appeared to be stationary and the posteriors seems to have nomulti-modal distributions Indicating that all chains have mixed and converged One usefulway to assess modelsrsquo fit is by the residuals (et = YtminusYt) The package provides the posteriorsample of every residual but checking all of them is an exhausting task An alternative ischecking the process generated by the residuals posterior estimate A white noise behaviorindicates a good model fit The modelrsquos residuals in figure 3 seems to follow a random noisethe auto-correlation in acf plots quickly falls to zero indicating an acceptable model fit

Rgt p1 = plot(sfit1type = residuals)Rgt p2 = plot(sfit1)

Rgt gridarrange(p2p1ncol = 1)

10 varstan Bayesian time series analysis with Stan in R

Figure 3 Posterior median residual plot

Because of the sinusoidal pattern that birth series (figure 1) presents a dynamic Harmonicregression (A fourier transform with arima structure for errors) could also assess a goodfit Kennedy (1992) To declare this model varstan offers a similar declaration structure offorecast (Hyndman and Khandakar 2008) package A harmonic regression with 4 fourierterms and ARIMA(111) residuals is declared and fitted to the birth data

Rgt model2 = Sarima(birthorder = c(111)xreg = fourier(birthK = 2))Rgt sfit2 = varstan(model = model2chains = 1iter = 2000warmup = 1000)Rgt sfit2

y ~ Sarima(111)reg[4]373 observations and 1 dimensionDifferences 1 seasonal Differences 0Current observations 372

mean se 25 975 ess Rhatmu0 -00712 00068 -00846 -00578 9396338 10032sigma0 108085 00124 107841 108328 9949722 10003phi -02705 00019 -02742 -02668 10044136 10006theta 06242 00015 06212 06272 9216062 09992breg1 -216318 00407 -217116 -215520 9659837 10006breg2 06619 00305 06021 07217 9767075 10002breg3 47937 00207 47531 48344 10791161 10003

Izhar Asael Alonzo Matamoros 11

breg4 -53570 00249 -54059 -53082 10994993 10005loglik -14153887 00697 -14155252 -14152521 9360271 10005

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

In this scenario both models suggest to be a good choice for birth series analysis Even sothe harmonic regression fits more parameters It is an obvious choice for birthrsquos sinusoidalbehavior As an example of model selection criteria the bayes_factor in logarithmic scalethat compares the modelsrsquo marginal likelihoods is computed Values above 6 (in logarithmicscale) provides good evidence for selecting the first model And for birth data the seasonalarima model (model1 ) is a better choice

Rgt bayes_factor(x1 = sf1x2 = sfit2log = TRUE)

Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Estimated log Bayes factor in favor of model1 over model2 19913745

Now a comparison of the selected model (model1 sim Sarima(111)(111)[12]) and the onegiven by the autosarima() function For this purpose a leave of one out cross validationloo() is used and both looic are compared with the loo_compare() function provided bythe loo (Vehtari and Gabry 2017) package

Rgt sfit3 = autosarima(birthchains = 1iter = 4000)Rgt sfit3

y ~ Sarima(012)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Diferences 1Current observations 360

mean se 25 975 ess Rhatmu0 00080 00018 00045 00116 2050372 09997sigma0 73517 00060 73399 73634 1991938 10008

12 varstan Bayesian time series analysis with Stan in R

theta1 03642 00013 03616 03668 1978174 10006theta2 01358 00011 01336 01379 2023769 10004sphi -02465 00016 -02496 -02433 2084503 10005stheta 03040 00017 03006 03073 2167639 09995loglik -12317452 00395 -12318225 -12316679 1789987 10009

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

Different from model1 the selected one does not contemplate an auto-regressive componentand use 2 mean average components insteadNow what proceeds is to estimate the loo forboth models

Rgt loo1 = loo(sfit1)Rgt loo3 = loo(sfit3)

Rgt lc = looloo_compare(loo1loo3)Rgt print(lcsimplify = FALSE)

elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looicmodel2 00 00 -12354 154 71 08 2470 308model1 -08 58 -12362 156 78 09 2472 312

loo_compare() prints first the best model In this example is the one provided by theautosarima() function where its looic is 2 units below model1 This autosarima()function is useful as starting point But the reader is encouraged to test more complexmodels and priors that adjust to the initial beliefs

ConclusionsThe paper gives a general overview of varstan package as a starting point of Bayesian timeseries analysis with structured models and it offers a simple dynamic interface inspired inthe classic functions provided by forecast astsa and var packages The interface functionsand prior flexibility that varstan offers makes Bayesian analysis flexible as classic methodsfor structured linear time series models The packagersquos goal is to provide a wide range ofmodels with a prior selection flexibility In a posterior version non-linear models such aswackier GARCH variants stochastic volatility hidden Markov state-space and uni-variateDynamic linear models will be included Along with several improvements in the packagersquosfunctionality

AcknowledgmentsFirst of all we would like to thank the Stan Development team and the Stan forumrsquos commu-nity for always being patience and helping fixing bugs and doubts in the package development

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 4: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

4 varstan Bayesian time series analysis with Stan in R

where D = diag(σ1 σ2 σd) denotes the diagonal matrix with σi elements that acceptspriors just like scale constant parameter (σ0) Following the Stan recommendations for Ω0a LKJ-correlation prior with parameter ζ gt 0 by Lewandowski Kurowicka and Joe (2009)is proposed For a further discussion of why this option is better than a conjugated inverseWhishart distribution see BAtildeijrkner (2017) and Natarajan and Kass (2000)

The prior distribution for α and β parameters in a GARCH model can be chosen betweennormal uniform or beta distribution this is due to their similarity to an auto-regressivecoefficient constrained in [01] For the MGARCH parameters a normal t-student Cauchygamma uniform or beta distributions can be chosen Finally for the v hyper-parameterin a GARCH model with unknown degree freedom innovations a normal inverse gammaexponential gamma and a non-informative Jeffreyrsquos prior are available Fonseca Ferreiraand Migon (2008)

3 Estimation processJust like brms (BAtildeijrkner 2017) or rstanarm (Goodrich Gabry Ali and Brilleman 2020)packages varstan does not fit the model parameters itself It only provides a Stan (StanDevelopment Team 2017) interface in R (R Core Team 2017) and works exclusively with theextended Hamiltonian Monte Carlo Duane (1987) No U-Turn Sampler algorithm of Hoffmanand Gelman (2014) The main reasons for using this sampler are its fast convergences andits less correlated samples BAtildeijrkner (2017) compares between Hamiltonian Monte Carloand other MCMC methods and Betancourt (2017) provides a conceptual introduction of theHamiltonian Monte Carlo

After fitting the model varstan provides functions to extract the posterior residuals and fittedvalues such as the predictive m-steps ahead and predictive errors distribution For modelselection criteria varstan provides posterior sample draws for the pointwise log-likelihoodAkaike Information Criteria (AIC) corrected AIC (AICc) and Bayesian Information Criteria(BIC) Bierens (2006) For a better performance in the model selection an adaptation of thebayes_factor of the bridgesampling (Gronau Singmann and Wagenmakers 2020) packagethe Bayesian leave one out (loo) and the Watanabe Akaike information criteria (waic) fromthe loo (Vehtari and Gabry 2017) package are provided Vehtari Gelman and Gabry (2016)and Kass and Raftery (1995)

The bayes_factor() approximates the modelrsquos marginal likelihood using the bridgesamplingalgorithm see Gronau Sarafoglou Matzke Ly Boehm Marsman Leslie Forster Wagen-makers and Steingroever (2017) for further detail The waic proposed by Watanabe (2010)is an improvement of the Deviance information criteria (DIC) proposed by Spiegelhalter BestCarlin and Van Der Linde (2002) The loo is asymptotically equivalent to the waic Watan-abe (2010) and is usually preferred over the second one Vehtari et al (2016)

31 Automatic order selection in arima models

Selecting an adequate order in a seasonal ARIMA model might be considered a difficult task

Izhar Asael Alonzo Matamoros 5

In Stan an incorrect order selection might be considered an ill model producing multiple di-vergent transitions Several procedures for automatic order selection have been proposed Tsay(2010) Hannan and Rissanen (1982) and Gomez (1998) A Bayesian version of HyndmanAthanasopoulos Bergmeir Caceres Chhay OrsquoHara-Wild Petropoulos Razbash Wang andYasmeen (2020) algorithm implemented in their forecast (Hyndman and Khandakar 2008)package is proposed This adaptation consists in proposing several models and select thebest one using a simple criteria such as AIC BIC or loglik Finally fit the selected model

In the proposed function the BIC is used as selection criteria for several reasons it can befast computed and it is asymptotically equivalent to the bayes_factor After a model isselected the function fits the model with default weak informative priors Even so a BICis a poor criteria for model selection This methodology usually selects a good initial modelwith a small amount of divergences (usually solved with more iterations) delivering acceptableresults For further reading and discussion see Hyndman and Khandakar (2008)

4 Package structure and modeling procedureSimilar to brms (BAtildeijrkner 2017) and rstanarm (Goodrich et al 2020) varstan is an R inter-face for Stan therefore the rstan (Stan Development Team 2020) package and a C++ compileris required the httpsgithubcomstan-devrstanwiki RStan-Getting-Started vignette hasa detailed explanation of how to install all the prerequisites in every operative system (Win-dows Mac and Linux) We recommend to install R-4000 version or hihger for avoidingcompatibility problems The current development version can be installed from GitHub usingthe next code

Rgt if (requireNamespace(remotes)) installpackages(remotes)

Rgt remotesinstall_github(asael697varstandependencies = TRUE)

The varstan dynamic is different from other packages First the parameters are not fittedafter a model is declared this was considerate so the user could select the parameter priors in adynamic way and call the sampler with a satisfactory defined model Second all fitted modelbecame a varstan S3 class the reason of this is to have available summary plot diagnosticand predict methods for every model regardless of its complexity The procedure for a timeseries analysis with varstan is explained in the next steps

1 Prepare the data varstan package supports numeric matrix and time series classes(ts)

2 Select the model the version() function provides a list of the current models Theirinterface is similar to forecast and Rrsquos stats packagesThese functions return a list withthe necessary data to fit the model in Stan

3 Change the priors varstan package defines by default weak informative priors Func-tions like set_prior() get_prior and print() aloud to change and check the modelspriors

6 varstan Bayesian time series analysis with Stan in R

Other useful functions are parameters() that prints the parameterrsquos names of a spec-ified model and distribution() prints the available prior distributions of a specifiedparameter

4 Fit the model the varstan() function call Stan and fit the defined model Parameterslike number of iterations and chains warm-up and other Stanrsquos control options areavailable The varstan() class contains a stanfit object returned from the rstanpackage that can be used for more complex Bayesian analysis

5 Check the model summary() plot()autoplot() and extract_stan() methods areavailable for model diagnostic and extract the parameterAcircťs posterior chains

The plot and summary methods will only provide general diagnostics and visualizationsfor further analysis use the bayesplot (Gabry and Mahr 2019) package

6 Select the model For multiple models varstan provides loglik() posterior_residuals()posterior_fit() AIC() BIC() WAIC() loo() and bayes_factor functions for modelselection criterias

7 Forecast the posterior_predict() function samples from the modelrsquos n-steps aheadpredictive distribution

5 Case study Analyzing the monthly live birth in US an example

As an example a time series modeling for the monthly live births in the United States 1948-1979 published in astsa (Stoffer 2019) package is provided In Figure 1 the data has aseasonal behavior that repeats annually The series waves in the whole 40 years period (supe-rior part) In addition the partial (pacf) and auto-correlation (acf) functions are far fromzero (middle part) and have the same wave pattern as birth series Indicating non stationaryand a strong periodic behavior After applying a difference to the data the acf and pacfplots still have some non-zero values every 12 lags (inferior part)

Izhar Asael Alonzo Matamoros 7

Figure 1 Monthly live birth USA

For start a seasonal ARIMA model could give a good fit to the data Following Tsay (2010)recommendations for order selection using the auto-correlation functions a p = 1 d = 1q = 1 and for the seasonal part P = 1 D = 1 Q = 1 The model is defined in varstan asfollows

Rgt model1 = Sarima(birthorder = c(111)seasonal = c(111))Rgt model1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

PriorsInterceptmu0 ~ t (loc = 0 scl = 25 df = 6 )

Scale Parametersigma0 ~ half_t (loc = 0 scl = 1 df = 7 )

ar[ 1 ] ~ normal (mu = 0 sd = 05 )ma[ 1 ] ~ normal (mu = 0 sd = 05 )

8 varstan Bayesian time series analysis with Stan in R

Seasonal Parameterssar[ 1 ] ~ normal (mu = 0 sd = 05 )sma[ 1 ] ~ normal (mu = 0 sd = 05 )NULL

The function Sarima generates a Seasonal ARIMA model ready to be fitted in Stan (StanDevelopment Team 2017) As the model is printed all the important information is shownthe model to be fit the total observations of the data the seasonal period the current obser-vations that can be used after differences and a list of priors for all the modelrsquos parametersUsing the information provided by the acf-plot in Figure 1 (middle right) the partial auto-correlations are not that strong and a normal distribution for the auto-regressive coefficient(ar[1]) could explore values close to 1 or -1 causing the prior to be too informative In-stead beta distribution in [minus1 1]5 centered at zero could be a more proper prior With thefunctions set_prior() and get_prior() any change is automatically updated and checked

Rgt model1 = set_prior(model = model1dist = beta(22)par = ar)Rgt get_prior(model = model1par = ar)

ar[ 1 ] ~ beta (form1 = 2 form2 = 2 )

Now that the model and priors are defined what follows is to fit the model using thevarstan() function One chain of 2000 iterations and a warm-up of the first 1000 chainrsquosvalues is simulated

Rgt sfit1 = varstan(model = model1chains = 1iter = 2000warmup = 1000)Rgt sfit1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

mean se 25 975 ess Rhatmu0 00061 00020 00020 00101 3941739 10028sigma0 73612 00043 73528 73697 4001665 10000phi -02336 00014 -02362 -02309 3594099 10007theta 00692 00017 00658 00726 3808853 10019sphi -00351 00015 -00381 -00321 3376811 10033stheta 06188 00017 06153 06222 3427074 10048loglik -12322519 00325 -12323157 -12321882 3198671 10033

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

5 If θ sim beta(α β) in [0 1] then θ1 = 2(θ minus 1) sim beta(α β) in [minus1 1]

Izhar Asael Alonzo Matamoros 9

All fitted models are varstan objects these are S3 classes with the stanfit results provided bythe rstan (Stan Development Team 2020) package and other useful elements that make themodeling process easier After fitting the proposed model a visual diagnostic of parameterscheck residuals and fitted values using the plot methods On figure 2 trace and posteriordensity plots are illustrated for all the model parameters

Rgt plot(sfit1type = parameter)

Figure 2 Trace and density plots for all the fitted parameters

In figure 2 all the chains appeared to be stationary and the posteriors seems to have nomulti-modal distributions Indicating that all chains have mixed and converged One usefulway to assess modelsrsquo fit is by the residuals (et = YtminusYt) The package provides the posteriorsample of every residual but checking all of them is an exhausting task An alternative ischecking the process generated by the residuals posterior estimate A white noise behaviorindicates a good model fit The modelrsquos residuals in figure 3 seems to follow a random noisethe auto-correlation in acf plots quickly falls to zero indicating an acceptable model fit

Rgt p1 = plot(sfit1type = residuals)Rgt p2 = plot(sfit1)

Rgt gridarrange(p2p1ncol = 1)

10 varstan Bayesian time series analysis with Stan in R

Figure 3 Posterior median residual plot

Because of the sinusoidal pattern that birth series (figure 1) presents a dynamic Harmonicregression (A fourier transform with arima structure for errors) could also assess a goodfit Kennedy (1992) To declare this model varstan offers a similar declaration structure offorecast (Hyndman and Khandakar 2008) package A harmonic regression with 4 fourierterms and ARIMA(111) residuals is declared and fitted to the birth data

Rgt model2 = Sarima(birthorder = c(111)xreg = fourier(birthK = 2))Rgt sfit2 = varstan(model = model2chains = 1iter = 2000warmup = 1000)Rgt sfit2

y ~ Sarima(111)reg[4]373 observations and 1 dimensionDifferences 1 seasonal Differences 0Current observations 372

mean se 25 975 ess Rhatmu0 -00712 00068 -00846 -00578 9396338 10032sigma0 108085 00124 107841 108328 9949722 10003phi -02705 00019 -02742 -02668 10044136 10006theta 06242 00015 06212 06272 9216062 09992breg1 -216318 00407 -217116 -215520 9659837 10006breg2 06619 00305 06021 07217 9767075 10002breg3 47937 00207 47531 48344 10791161 10003

Izhar Asael Alonzo Matamoros 11

breg4 -53570 00249 -54059 -53082 10994993 10005loglik -14153887 00697 -14155252 -14152521 9360271 10005

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

In this scenario both models suggest to be a good choice for birth series analysis Even sothe harmonic regression fits more parameters It is an obvious choice for birthrsquos sinusoidalbehavior As an example of model selection criteria the bayes_factor in logarithmic scalethat compares the modelsrsquo marginal likelihoods is computed Values above 6 (in logarithmicscale) provides good evidence for selecting the first model And for birth data the seasonalarima model (model1 ) is a better choice

Rgt bayes_factor(x1 = sf1x2 = sfit2log = TRUE)

Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Estimated log Bayes factor in favor of model1 over model2 19913745

Now a comparison of the selected model (model1 sim Sarima(111)(111)[12]) and the onegiven by the autosarima() function For this purpose a leave of one out cross validationloo() is used and both looic are compared with the loo_compare() function provided bythe loo (Vehtari and Gabry 2017) package

Rgt sfit3 = autosarima(birthchains = 1iter = 4000)Rgt sfit3

y ~ Sarima(012)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Diferences 1Current observations 360

mean se 25 975 ess Rhatmu0 00080 00018 00045 00116 2050372 09997sigma0 73517 00060 73399 73634 1991938 10008

12 varstan Bayesian time series analysis with Stan in R

theta1 03642 00013 03616 03668 1978174 10006theta2 01358 00011 01336 01379 2023769 10004sphi -02465 00016 -02496 -02433 2084503 10005stheta 03040 00017 03006 03073 2167639 09995loglik -12317452 00395 -12318225 -12316679 1789987 10009

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

Different from model1 the selected one does not contemplate an auto-regressive componentand use 2 mean average components insteadNow what proceeds is to estimate the loo forboth models

Rgt loo1 = loo(sfit1)Rgt loo3 = loo(sfit3)

Rgt lc = looloo_compare(loo1loo3)Rgt print(lcsimplify = FALSE)

elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looicmodel2 00 00 -12354 154 71 08 2470 308model1 -08 58 -12362 156 78 09 2472 312

loo_compare() prints first the best model In this example is the one provided by theautosarima() function where its looic is 2 units below model1 This autosarima()function is useful as starting point But the reader is encouraged to test more complexmodels and priors that adjust to the initial beliefs

ConclusionsThe paper gives a general overview of varstan package as a starting point of Bayesian timeseries analysis with structured models and it offers a simple dynamic interface inspired inthe classic functions provided by forecast astsa and var packages The interface functionsand prior flexibility that varstan offers makes Bayesian analysis flexible as classic methodsfor structured linear time series models The packagersquos goal is to provide a wide range ofmodels with a prior selection flexibility In a posterior version non-linear models such aswackier GARCH variants stochastic volatility hidden Markov state-space and uni-variateDynamic linear models will be included Along with several improvements in the packagersquosfunctionality

AcknowledgmentsFirst of all we would like to thank the Stan Development team and the Stan forumrsquos commu-nity for always being patience and helping fixing bugs and doubts in the package development

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 5: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

Izhar Asael Alonzo Matamoros 5

In Stan an incorrect order selection might be considered an ill model producing multiple di-vergent transitions Several procedures for automatic order selection have been proposed Tsay(2010) Hannan and Rissanen (1982) and Gomez (1998) A Bayesian version of HyndmanAthanasopoulos Bergmeir Caceres Chhay OrsquoHara-Wild Petropoulos Razbash Wang andYasmeen (2020) algorithm implemented in their forecast (Hyndman and Khandakar 2008)package is proposed This adaptation consists in proposing several models and select thebest one using a simple criteria such as AIC BIC or loglik Finally fit the selected model

In the proposed function the BIC is used as selection criteria for several reasons it can befast computed and it is asymptotically equivalent to the bayes_factor After a model isselected the function fits the model with default weak informative priors Even so a BICis a poor criteria for model selection This methodology usually selects a good initial modelwith a small amount of divergences (usually solved with more iterations) delivering acceptableresults For further reading and discussion see Hyndman and Khandakar (2008)

4 Package structure and modeling procedureSimilar to brms (BAtildeijrkner 2017) and rstanarm (Goodrich et al 2020) varstan is an R inter-face for Stan therefore the rstan (Stan Development Team 2020) package and a C++ compileris required the httpsgithubcomstan-devrstanwiki RStan-Getting-Started vignette hasa detailed explanation of how to install all the prerequisites in every operative system (Win-dows Mac and Linux) We recommend to install R-4000 version or hihger for avoidingcompatibility problems The current development version can be installed from GitHub usingthe next code

Rgt if (requireNamespace(remotes)) installpackages(remotes)

Rgt remotesinstall_github(asael697varstandependencies = TRUE)

The varstan dynamic is different from other packages First the parameters are not fittedafter a model is declared this was considerate so the user could select the parameter priors in adynamic way and call the sampler with a satisfactory defined model Second all fitted modelbecame a varstan S3 class the reason of this is to have available summary plot diagnosticand predict methods for every model regardless of its complexity The procedure for a timeseries analysis with varstan is explained in the next steps

1 Prepare the data varstan package supports numeric matrix and time series classes(ts)

2 Select the model the version() function provides a list of the current models Theirinterface is similar to forecast and Rrsquos stats packagesThese functions return a list withthe necessary data to fit the model in Stan

3 Change the priors varstan package defines by default weak informative priors Func-tions like set_prior() get_prior and print() aloud to change and check the modelspriors

6 varstan Bayesian time series analysis with Stan in R

Other useful functions are parameters() that prints the parameterrsquos names of a spec-ified model and distribution() prints the available prior distributions of a specifiedparameter

4 Fit the model the varstan() function call Stan and fit the defined model Parameterslike number of iterations and chains warm-up and other Stanrsquos control options areavailable The varstan() class contains a stanfit object returned from the rstanpackage that can be used for more complex Bayesian analysis

5 Check the model summary() plot()autoplot() and extract_stan() methods areavailable for model diagnostic and extract the parameterAcircťs posterior chains

The plot and summary methods will only provide general diagnostics and visualizationsfor further analysis use the bayesplot (Gabry and Mahr 2019) package

6 Select the model For multiple models varstan provides loglik() posterior_residuals()posterior_fit() AIC() BIC() WAIC() loo() and bayes_factor functions for modelselection criterias

7 Forecast the posterior_predict() function samples from the modelrsquos n-steps aheadpredictive distribution

5 Case study Analyzing the monthly live birth in US an example

As an example a time series modeling for the monthly live births in the United States 1948-1979 published in astsa (Stoffer 2019) package is provided In Figure 1 the data has aseasonal behavior that repeats annually The series waves in the whole 40 years period (supe-rior part) In addition the partial (pacf) and auto-correlation (acf) functions are far fromzero (middle part) and have the same wave pattern as birth series Indicating non stationaryand a strong periodic behavior After applying a difference to the data the acf and pacfplots still have some non-zero values every 12 lags (inferior part)

Izhar Asael Alonzo Matamoros 7

Figure 1 Monthly live birth USA

For start a seasonal ARIMA model could give a good fit to the data Following Tsay (2010)recommendations for order selection using the auto-correlation functions a p = 1 d = 1q = 1 and for the seasonal part P = 1 D = 1 Q = 1 The model is defined in varstan asfollows

Rgt model1 = Sarima(birthorder = c(111)seasonal = c(111))Rgt model1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

PriorsInterceptmu0 ~ t (loc = 0 scl = 25 df = 6 )

Scale Parametersigma0 ~ half_t (loc = 0 scl = 1 df = 7 )

ar[ 1 ] ~ normal (mu = 0 sd = 05 )ma[ 1 ] ~ normal (mu = 0 sd = 05 )

8 varstan Bayesian time series analysis with Stan in R

Seasonal Parameterssar[ 1 ] ~ normal (mu = 0 sd = 05 )sma[ 1 ] ~ normal (mu = 0 sd = 05 )NULL

The function Sarima generates a Seasonal ARIMA model ready to be fitted in Stan (StanDevelopment Team 2017) As the model is printed all the important information is shownthe model to be fit the total observations of the data the seasonal period the current obser-vations that can be used after differences and a list of priors for all the modelrsquos parametersUsing the information provided by the acf-plot in Figure 1 (middle right) the partial auto-correlations are not that strong and a normal distribution for the auto-regressive coefficient(ar[1]) could explore values close to 1 or -1 causing the prior to be too informative In-stead beta distribution in [minus1 1]5 centered at zero could be a more proper prior With thefunctions set_prior() and get_prior() any change is automatically updated and checked

Rgt model1 = set_prior(model = model1dist = beta(22)par = ar)Rgt get_prior(model = model1par = ar)

ar[ 1 ] ~ beta (form1 = 2 form2 = 2 )

Now that the model and priors are defined what follows is to fit the model using thevarstan() function One chain of 2000 iterations and a warm-up of the first 1000 chainrsquosvalues is simulated

Rgt sfit1 = varstan(model = model1chains = 1iter = 2000warmup = 1000)Rgt sfit1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

mean se 25 975 ess Rhatmu0 00061 00020 00020 00101 3941739 10028sigma0 73612 00043 73528 73697 4001665 10000phi -02336 00014 -02362 -02309 3594099 10007theta 00692 00017 00658 00726 3808853 10019sphi -00351 00015 -00381 -00321 3376811 10033stheta 06188 00017 06153 06222 3427074 10048loglik -12322519 00325 -12323157 -12321882 3198671 10033

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

5 If θ sim beta(α β) in [0 1] then θ1 = 2(θ minus 1) sim beta(α β) in [minus1 1]

Izhar Asael Alonzo Matamoros 9

All fitted models are varstan objects these are S3 classes with the stanfit results provided bythe rstan (Stan Development Team 2020) package and other useful elements that make themodeling process easier After fitting the proposed model a visual diagnostic of parameterscheck residuals and fitted values using the plot methods On figure 2 trace and posteriordensity plots are illustrated for all the model parameters

Rgt plot(sfit1type = parameter)

Figure 2 Trace and density plots for all the fitted parameters

In figure 2 all the chains appeared to be stationary and the posteriors seems to have nomulti-modal distributions Indicating that all chains have mixed and converged One usefulway to assess modelsrsquo fit is by the residuals (et = YtminusYt) The package provides the posteriorsample of every residual but checking all of them is an exhausting task An alternative ischecking the process generated by the residuals posterior estimate A white noise behaviorindicates a good model fit The modelrsquos residuals in figure 3 seems to follow a random noisethe auto-correlation in acf plots quickly falls to zero indicating an acceptable model fit

Rgt p1 = plot(sfit1type = residuals)Rgt p2 = plot(sfit1)

Rgt gridarrange(p2p1ncol = 1)

10 varstan Bayesian time series analysis with Stan in R

Figure 3 Posterior median residual plot

Because of the sinusoidal pattern that birth series (figure 1) presents a dynamic Harmonicregression (A fourier transform with arima structure for errors) could also assess a goodfit Kennedy (1992) To declare this model varstan offers a similar declaration structure offorecast (Hyndman and Khandakar 2008) package A harmonic regression with 4 fourierterms and ARIMA(111) residuals is declared and fitted to the birth data

Rgt model2 = Sarima(birthorder = c(111)xreg = fourier(birthK = 2))Rgt sfit2 = varstan(model = model2chains = 1iter = 2000warmup = 1000)Rgt sfit2

y ~ Sarima(111)reg[4]373 observations and 1 dimensionDifferences 1 seasonal Differences 0Current observations 372

mean se 25 975 ess Rhatmu0 -00712 00068 -00846 -00578 9396338 10032sigma0 108085 00124 107841 108328 9949722 10003phi -02705 00019 -02742 -02668 10044136 10006theta 06242 00015 06212 06272 9216062 09992breg1 -216318 00407 -217116 -215520 9659837 10006breg2 06619 00305 06021 07217 9767075 10002breg3 47937 00207 47531 48344 10791161 10003

Izhar Asael Alonzo Matamoros 11

breg4 -53570 00249 -54059 -53082 10994993 10005loglik -14153887 00697 -14155252 -14152521 9360271 10005

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

In this scenario both models suggest to be a good choice for birth series analysis Even sothe harmonic regression fits more parameters It is an obvious choice for birthrsquos sinusoidalbehavior As an example of model selection criteria the bayes_factor in logarithmic scalethat compares the modelsrsquo marginal likelihoods is computed Values above 6 (in logarithmicscale) provides good evidence for selecting the first model And for birth data the seasonalarima model (model1 ) is a better choice

Rgt bayes_factor(x1 = sf1x2 = sfit2log = TRUE)

Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Estimated log Bayes factor in favor of model1 over model2 19913745

Now a comparison of the selected model (model1 sim Sarima(111)(111)[12]) and the onegiven by the autosarima() function For this purpose a leave of one out cross validationloo() is used and both looic are compared with the loo_compare() function provided bythe loo (Vehtari and Gabry 2017) package

Rgt sfit3 = autosarima(birthchains = 1iter = 4000)Rgt sfit3

y ~ Sarima(012)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Diferences 1Current observations 360

mean se 25 975 ess Rhatmu0 00080 00018 00045 00116 2050372 09997sigma0 73517 00060 73399 73634 1991938 10008

12 varstan Bayesian time series analysis with Stan in R

theta1 03642 00013 03616 03668 1978174 10006theta2 01358 00011 01336 01379 2023769 10004sphi -02465 00016 -02496 -02433 2084503 10005stheta 03040 00017 03006 03073 2167639 09995loglik -12317452 00395 -12318225 -12316679 1789987 10009

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

Different from model1 the selected one does not contemplate an auto-regressive componentand use 2 mean average components insteadNow what proceeds is to estimate the loo forboth models

Rgt loo1 = loo(sfit1)Rgt loo3 = loo(sfit3)

Rgt lc = looloo_compare(loo1loo3)Rgt print(lcsimplify = FALSE)

elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looicmodel2 00 00 -12354 154 71 08 2470 308model1 -08 58 -12362 156 78 09 2472 312

loo_compare() prints first the best model In this example is the one provided by theautosarima() function where its looic is 2 units below model1 This autosarima()function is useful as starting point But the reader is encouraged to test more complexmodels and priors that adjust to the initial beliefs

ConclusionsThe paper gives a general overview of varstan package as a starting point of Bayesian timeseries analysis with structured models and it offers a simple dynamic interface inspired inthe classic functions provided by forecast astsa and var packages The interface functionsand prior flexibility that varstan offers makes Bayesian analysis flexible as classic methodsfor structured linear time series models The packagersquos goal is to provide a wide range ofmodels with a prior selection flexibility In a posterior version non-linear models such aswackier GARCH variants stochastic volatility hidden Markov state-space and uni-variateDynamic linear models will be included Along with several improvements in the packagersquosfunctionality

AcknowledgmentsFirst of all we would like to thank the Stan Development team and the Stan forumrsquos commu-nity for always being patience and helping fixing bugs and doubts in the package development

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 6: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

6 varstan Bayesian time series analysis with Stan in R

Other useful functions are parameters() that prints the parameterrsquos names of a spec-ified model and distribution() prints the available prior distributions of a specifiedparameter

4 Fit the model the varstan() function call Stan and fit the defined model Parameterslike number of iterations and chains warm-up and other Stanrsquos control options areavailable The varstan() class contains a stanfit object returned from the rstanpackage that can be used for more complex Bayesian analysis

5 Check the model summary() plot()autoplot() and extract_stan() methods areavailable for model diagnostic and extract the parameterAcircťs posterior chains

The plot and summary methods will only provide general diagnostics and visualizationsfor further analysis use the bayesplot (Gabry and Mahr 2019) package

6 Select the model For multiple models varstan provides loglik() posterior_residuals()posterior_fit() AIC() BIC() WAIC() loo() and bayes_factor functions for modelselection criterias

7 Forecast the posterior_predict() function samples from the modelrsquos n-steps aheadpredictive distribution

5 Case study Analyzing the monthly live birth in US an example

As an example a time series modeling for the monthly live births in the United States 1948-1979 published in astsa (Stoffer 2019) package is provided In Figure 1 the data has aseasonal behavior that repeats annually The series waves in the whole 40 years period (supe-rior part) In addition the partial (pacf) and auto-correlation (acf) functions are far fromzero (middle part) and have the same wave pattern as birth series Indicating non stationaryand a strong periodic behavior After applying a difference to the data the acf and pacfplots still have some non-zero values every 12 lags (inferior part)

Izhar Asael Alonzo Matamoros 7

Figure 1 Monthly live birth USA

For start a seasonal ARIMA model could give a good fit to the data Following Tsay (2010)recommendations for order selection using the auto-correlation functions a p = 1 d = 1q = 1 and for the seasonal part P = 1 D = 1 Q = 1 The model is defined in varstan asfollows

Rgt model1 = Sarima(birthorder = c(111)seasonal = c(111))Rgt model1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

PriorsInterceptmu0 ~ t (loc = 0 scl = 25 df = 6 )

Scale Parametersigma0 ~ half_t (loc = 0 scl = 1 df = 7 )

ar[ 1 ] ~ normal (mu = 0 sd = 05 )ma[ 1 ] ~ normal (mu = 0 sd = 05 )

8 varstan Bayesian time series analysis with Stan in R

Seasonal Parameterssar[ 1 ] ~ normal (mu = 0 sd = 05 )sma[ 1 ] ~ normal (mu = 0 sd = 05 )NULL

The function Sarima generates a Seasonal ARIMA model ready to be fitted in Stan (StanDevelopment Team 2017) As the model is printed all the important information is shownthe model to be fit the total observations of the data the seasonal period the current obser-vations that can be used after differences and a list of priors for all the modelrsquos parametersUsing the information provided by the acf-plot in Figure 1 (middle right) the partial auto-correlations are not that strong and a normal distribution for the auto-regressive coefficient(ar[1]) could explore values close to 1 or -1 causing the prior to be too informative In-stead beta distribution in [minus1 1]5 centered at zero could be a more proper prior With thefunctions set_prior() and get_prior() any change is automatically updated and checked

Rgt model1 = set_prior(model = model1dist = beta(22)par = ar)Rgt get_prior(model = model1par = ar)

ar[ 1 ] ~ beta (form1 = 2 form2 = 2 )

Now that the model and priors are defined what follows is to fit the model using thevarstan() function One chain of 2000 iterations and a warm-up of the first 1000 chainrsquosvalues is simulated

Rgt sfit1 = varstan(model = model1chains = 1iter = 2000warmup = 1000)Rgt sfit1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

mean se 25 975 ess Rhatmu0 00061 00020 00020 00101 3941739 10028sigma0 73612 00043 73528 73697 4001665 10000phi -02336 00014 -02362 -02309 3594099 10007theta 00692 00017 00658 00726 3808853 10019sphi -00351 00015 -00381 -00321 3376811 10033stheta 06188 00017 06153 06222 3427074 10048loglik -12322519 00325 -12323157 -12321882 3198671 10033

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

5 If θ sim beta(α β) in [0 1] then θ1 = 2(θ minus 1) sim beta(α β) in [minus1 1]

Izhar Asael Alonzo Matamoros 9

All fitted models are varstan objects these are S3 classes with the stanfit results provided bythe rstan (Stan Development Team 2020) package and other useful elements that make themodeling process easier After fitting the proposed model a visual diagnostic of parameterscheck residuals and fitted values using the plot methods On figure 2 trace and posteriordensity plots are illustrated for all the model parameters

Rgt plot(sfit1type = parameter)

Figure 2 Trace and density plots for all the fitted parameters

In figure 2 all the chains appeared to be stationary and the posteriors seems to have nomulti-modal distributions Indicating that all chains have mixed and converged One usefulway to assess modelsrsquo fit is by the residuals (et = YtminusYt) The package provides the posteriorsample of every residual but checking all of them is an exhausting task An alternative ischecking the process generated by the residuals posterior estimate A white noise behaviorindicates a good model fit The modelrsquos residuals in figure 3 seems to follow a random noisethe auto-correlation in acf plots quickly falls to zero indicating an acceptable model fit

Rgt p1 = plot(sfit1type = residuals)Rgt p2 = plot(sfit1)

Rgt gridarrange(p2p1ncol = 1)

10 varstan Bayesian time series analysis with Stan in R

Figure 3 Posterior median residual plot

Because of the sinusoidal pattern that birth series (figure 1) presents a dynamic Harmonicregression (A fourier transform with arima structure for errors) could also assess a goodfit Kennedy (1992) To declare this model varstan offers a similar declaration structure offorecast (Hyndman and Khandakar 2008) package A harmonic regression with 4 fourierterms and ARIMA(111) residuals is declared and fitted to the birth data

Rgt model2 = Sarima(birthorder = c(111)xreg = fourier(birthK = 2))Rgt sfit2 = varstan(model = model2chains = 1iter = 2000warmup = 1000)Rgt sfit2

y ~ Sarima(111)reg[4]373 observations and 1 dimensionDifferences 1 seasonal Differences 0Current observations 372

mean se 25 975 ess Rhatmu0 -00712 00068 -00846 -00578 9396338 10032sigma0 108085 00124 107841 108328 9949722 10003phi -02705 00019 -02742 -02668 10044136 10006theta 06242 00015 06212 06272 9216062 09992breg1 -216318 00407 -217116 -215520 9659837 10006breg2 06619 00305 06021 07217 9767075 10002breg3 47937 00207 47531 48344 10791161 10003

Izhar Asael Alonzo Matamoros 11

breg4 -53570 00249 -54059 -53082 10994993 10005loglik -14153887 00697 -14155252 -14152521 9360271 10005

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

In this scenario both models suggest to be a good choice for birth series analysis Even sothe harmonic regression fits more parameters It is an obvious choice for birthrsquos sinusoidalbehavior As an example of model selection criteria the bayes_factor in logarithmic scalethat compares the modelsrsquo marginal likelihoods is computed Values above 6 (in logarithmicscale) provides good evidence for selecting the first model And for birth data the seasonalarima model (model1 ) is a better choice

Rgt bayes_factor(x1 = sf1x2 = sfit2log = TRUE)

Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Estimated log Bayes factor in favor of model1 over model2 19913745

Now a comparison of the selected model (model1 sim Sarima(111)(111)[12]) and the onegiven by the autosarima() function For this purpose a leave of one out cross validationloo() is used and both looic are compared with the loo_compare() function provided bythe loo (Vehtari and Gabry 2017) package

Rgt sfit3 = autosarima(birthchains = 1iter = 4000)Rgt sfit3

y ~ Sarima(012)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Diferences 1Current observations 360

mean se 25 975 ess Rhatmu0 00080 00018 00045 00116 2050372 09997sigma0 73517 00060 73399 73634 1991938 10008

12 varstan Bayesian time series analysis with Stan in R

theta1 03642 00013 03616 03668 1978174 10006theta2 01358 00011 01336 01379 2023769 10004sphi -02465 00016 -02496 -02433 2084503 10005stheta 03040 00017 03006 03073 2167639 09995loglik -12317452 00395 -12318225 -12316679 1789987 10009

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

Different from model1 the selected one does not contemplate an auto-regressive componentand use 2 mean average components insteadNow what proceeds is to estimate the loo forboth models

Rgt loo1 = loo(sfit1)Rgt loo3 = loo(sfit3)

Rgt lc = looloo_compare(loo1loo3)Rgt print(lcsimplify = FALSE)

elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looicmodel2 00 00 -12354 154 71 08 2470 308model1 -08 58 -12362 156 78 09 2472 312

loo_compare() prints first the best model In this example is the one provided by theautosarima() function where its looic is 2 units below model1 This autosarima()function is useful as starting point But the reader is encouraged to test more complexmodels and priors that adjust to the initial beliefs

ConclusionsThe paper gives a general overview of varstan package as a starting point of Bayesian timeseries analysis with structured models and it offers a simple dynamic interface inspired inthe classic functions provided by forecast astsa and var packages The interface functionsand prior flexibility that varstan offers makes Bayesian analysis flexible as classic methodsfor structured linear time series models The packagersquos goal is to provide a wide range ofmodels with a prior selection flexibility In a posterior version non-linear models such aswackier GARCH variants stochastic volatility hidden Markov state-space and uni-variateDynamic linear models will be included Along with several improvements in the packagersquosfunctionality

AcknowledgmentsFirst of all we would like to thank the Stan Development team and the Stan forumrsquos commu-nity for always being patience and helping fixing bugs and doubts in the package development

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 7: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

Izhar Asael Alonzo Matamoros 7

Figure 1 Monthly live birth USA

For start a seasonal ARIMA model could give a good fit to the data Following Tsay (2010)recommendations for order selection using the auto-correlation functions a p = 1 d = 1q = 1 and for the seasonal part P = 1 D = 1 Q = 1 The model is defined in varstan asfollows

Rgt model1 = Sarima(birthorder = c(111)seasonal = c(111))Rgt model1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

PriorsInterceptmu0 ~ t (loc = 0 scl = 25 df = 6 )

Scale Parametersigma0 ~ half_t (loc = 0 scl = 1 df = 7 )

ar[ 1 ] ~ normal (mu = 0 sd = 05 )ma[ 1 ] ~ normal (mu = 0 sd = 05 )

8 varstan Bayesian time series analysis with Stan in R

Seasonal Parameterssar[ 1 ] ~ normal (mu = 0 sd = 05 )sma[ 1 ] ~ normal (mu = 0 sd = 05 )NULL

The function Sarima generates a Seasonal ARIMA model ready to be fitted in Stan (StanDevelopment Team 2017) As the model is printed all the important information is shownthe model to be fit the total observations of the data the seasonal period the current obser-vations that can be used after differences and a list of priors for all the modelrsquos parametersUsing the information provided by the acf-plot in Figure 1 (middle right) the partial auto-correlations are not that strong and a normal distribution for the auto-regressive coefficient(ar[1]) could explore values close to 1 or -1 causing the prior to be too informative In-stead beta distribution in [minus1 1]5 centered at zero could be a more proper prior With thefunctions set_prior() and get_prior() any change is automatically updated and checked

Rgt model1 = set_prior(model = model1dist = beta(22)par = ar)Rgt get_prior(model = model1par = ar)

ar[ 1 ] ~ beta (form1 = 2 form2 = 2 )

Now that the model and priors are defined what follows is to fit the model using thevarstan() function One chain of 2000 iterations and a warm-up of the first 1000 chainrsquosvalues is simulated

Rgt sfit1 = varstan(model = model1chains = 1iter = 2000warmup = 1000)Rgt sfit1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

mean se 25 975 ess Rhatmu0 00061 00020 00020 00101 3941739 10028sigma0 73612 00043 73528 73697 4001665 10000phi -02336 00014 -02362 -02309 3594099 10007theta 00692 00017 00658 00726 3808853 10019sphi -00351 00015 -00381 -00321 3376811 10033stheta 06188 00017 06153 06222 3427074 10048loglik -12322519 00325 -12323157 -12321882 3198671 10033

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

5 If θ sim beta(α β) in [0 1] then θ1 = 2(θ minus 1) sim beta(α β) in [minus1 1]

Izhar Asael Alonzo Matamoros 9

All fitted models are varstan objects these are S3 classes with the stanfit results provided bythe rstan (Stan Development Team 2020) package and other useful elements that make themodeling process easier After fitting the proposed model a visual diagnostic of parameterscheck residuals and fitted values using the plot methods On figure 2 trace and posteriordensity plots are illustrated for all the model parameters

Rgt plot(sfit1type = parameter)

Figure 2 Trace and density plots for all the fitted parameters

In figure 2 all the chains appeared to be stationary and the posteriors seems to have nomulti-modal distributions Indicating that all chains have mixed and converged One usefulway to assess modelsrsquo fit is by the residuals (et = YtminusYt) The package provides the posteriorsample of every residual but checking all of them is an exhausting task An alternative ischecking the process generated by the residuals posterior estimate A white noise behaviorindicates a good model fit The modelrsquos residuals in figure 3 seems to follow a random noisethe auto-correlation in acf plots quickly falls to zero indicating an acceptable model fit

Rgt p1 = plot(sfit1type = residuals)Rgt p2 = plot(sfit1)

Rgt gridarrange(p2p1ncol = 1)

10 varstan Bayesian time series analysis with Stan in R

Figure 3 Posterior median residual plot

Because of the sinusoidal pattern that birth series (figure 1) presents a dynamic Harmonicregression (A fourier transform with arima structure for errors) could also assess a goodfit Kennedy (1992) To declare this model varstan offers a similar declaration structure offorecast (Hyndman and Khandakar 2008) package A harmonic regression with 4 fourierterms and ARIMA(111) residuals is declared and fitted to the birth data

Rgt model2 = Sarima(birthorder = c(111)xreg = fourier(birthK = 2))Rgt sfit2 = varstan(model = model2chains = 1iter = 2000warmup = 1000)Rgt sfit2

y ~ Sarima(111)reg[4]373 observations and 1 dimensionDifferences 1 seasonal Differences 0Current observations 372

mean se 25 975 ess Rhatmu0 -00712 00068 -00846 -00578 9396338 10032sigma0 108085 00124 107841 108328 9949722 10003phi -02705 00019 -02742 -02668 10044136 10006theta 06242 00015 06212 06272 9216062 09992breg1 -216318 00407 -217116 -215520 9659837 10006breg2 06619 00305 06021 07217 9767075 10002breg3 47937 00207 47531 48344 10791161 10003

Izhar Asael Alonzo Matamoros 11

breg4 -53570 00249 -54059 -53082 10994993 10005loglik -14153887 00697 -14155252 -14152521 9360271 10005

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

In this scenario both models suggest to be a good choice for birth series analysis Even sothe harmonic regression fits more parameters It is an obvious choice for birthrsquos sinusoidalbehavior As an example of model selection criteria the bayes_factor in logarithmic scalethat compares the modelsrsquo marginal likelihoods is computed Values above 6 (in logarithmicscale) provides good evidence for selecting the first model And for birth data the seasonalarima model (model1 ) is a better choice

Rgt bayes_factor(x1 = sf1x2 = sfit2log = TRUE)

Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Estimated log Bayes factor in favor of model1 over model2 19913745

Now a comparison of the selected model (model1 sim Sarima(111)(111)[12]) and the onegiven by the autosarima() function For this purpose a leave of one out cross validationloo() is used and both looic are compared with the loo_compare() function provided bythe loo (Vehtari and Gabry 2017) package

Rgt sfit3 = autosarima(birthchains = 1iter = 4000)Rgt sfit3

y ~ Sarima(012)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Diferences 1Current observations 360

mean se 25 975 ess Rhatmu0 00080 00018 00045 00116 2050372 09997sigma0 73517 00060 73399 73634 1991938 10008

12 varstan Bayesian time series analysis with Stan in R

theta1 03642 00013 03616 03668 1978174 10006theta2 01358 00011 01336 01379 2023769 10004sphi -02465 00016 -02496 -02433 2084503 10005stheta 03040 00017 03006 03073 2167639 09995loglik -12317452 00395 -12318225 -12316679 1789987 10009

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

Different from model1 the selected one does not contemplate an auto-regressive componentand use 2 mean average components insteadNow what proceeds is to estimate the loo forboth models

Rgt loo1 = loo(sfit1)Rgt loo3 = loo(sfit3)

Rgt lc = looloo_compare(loo1loo3)Rgt print(lcsimplify = FALSE)

elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looicmodel2 00 00 -12354 154 71 08 2470 308model1 -08 58 -12362 156 78 09 2472 312

loo_compare() prints first the best model In this example is the one provided by theautosarima() function where its looic is 2 units below model1 This autosarima()function is useful as starting point But the reader is encouraged to test more complexmodels and priors that adjust to the initial beliefs

ConclusionsThe paper gives a general overview of varstan package as a starting point of Bayesian timeseries analysis with structured models and it offers a simple dynamic interface inspired inthe classic functions provided by forecast astsa and var packages The interface functionsand prior flexibility that varstan offers makes Bayesian analysis flexible as classic methodsfor structured linear time series models The packagersquos goal is to provide a wide range ofmodels with a prior selection flexibility In a posterior version non-linear models such aswackier GARCH variants stochastic volatility hidden Markov state-space and uni-variateDynamic linear models will be included Along with several improvements in the packagersquosfunctionality

AcknowledgmentsFirst of all we would like to thank the Stan Development team and the Stan forumrsquos commu-nity for always being patience and helping fixing bugs and doubts in the package development

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 8: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

8 varstan Bayesian time series analysis with Stan in R

Seasonal Parameterssar[ 1 ] ~ normal (mu = 0 sd = 05 )sma[ 1 ] ~ normal (mu = 0 sd = 05 )NULL

The function Sarima generates a Seasonal ARIMA model ready to be fitted in Stan (StanDevelopment Team 2017) As the model is printed all the important information is shownthe model to be fit the total observations of the data the seasonal period the current obser-vations that can be used after differences and a list of priors for all the modelrsquos parametersUsing the information provided by the acf-plot in Figure 1 (middle right) the partial auto-correlations are not that strong and a normal distribution for the auto-regressive coefficient(ar[1]) could explore values close to 1 or -1 causing the prior to be too informative In-stead beta distribution in [minus1 1]5 centered at zero could be a more proper prior With thefunctions set_prior() and get_prior() any change is automatically updated and checked

Rgt model1 = set_prior(model = model1dist = beta(22)par = ar)Rgt get_prior(model = model1par = ar)

ar[ 1 ] ~ beta (form1 = 2 form2 = 2 )

Now that the model and priors are defined what follows is to fit the model using thevarstan() function One chain of 2000 iterations and a warm-up of the first 1000 chainrsquosvalues is simulated

Rgt sfit1 = varstan(model = model1chains = 1iter = 2000warmup = 1000)Rgt sfit1

y ~ Sarima(111)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Differences 1Current observations 360

mean se 25 975 ess Rhatmu0 00061 00020 00020 00101 3941739 10028sigma0 73612 00043 73528 73697 4001665 10000phi -02336 00014 -02362 -02309 3594099 10007theta 00692 00017 00658 00726 3808853 10019sphi -00351 00015 -00381 -00321 3376811 10033stheta 06188 00017 06153 06222 3427074 10048loglik -12322519 00325 -12323157 -12321882 3198671 10033

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

5 If θ sim beta(α β) in [0 1] then θ1 = 2(θ minus 1) sim beta(α β) in [minus1 1]

Izhar Asael Alonzo Matamoros 9

All fitted models are varstan objects these are S3 classes with the stanfit results provided bythe rstan (Stan Development Team 2020) package and other useful elements that make themodeling process easier After fitting the proposed model a visual diagnostic of parameterscheck residuals and fitted values using the plot methods On figure 2 trace and posteriordensity plots are illustrated for all the model parameters

Rgt plot(sfit1type = parameter)

Figure 2 Trace and density plots for all the fitted parameters

In figure 2 all the chains appeared to be stationary and the posteriors seems to have nomulti-modal distributions Indicating that all chains have mixed and converged One usefulway to assess modelsrsquo fit is by the residuals (et = YtminusYt) The package provides the posteriorsample of every residual but checking all of them is an exhausting task An alternative ischecking the process generated by the residuals posterior estimate A white noise behaviorindicates a good model fit The modelrsquos residuals in figure 3 seems to follow a random noisethe auto-correlation in acf plots quickly falls to zero indicating an acceptable model fit

Rgt p1 = plot(sfit1type = residuals)Rgt p2 = plot(sfit1)

Rgt gridarrange(p2p1ncol = 1)

10 varstan Bayesian time series analysis with Stan in R

Figure 3 Posterior median residual plot

Because of the sinusoidal pattern that birth series (figure 1) presents a dynamic Harmonicregression (A fourier transform with arima structure for errors) could also assess a goodfit Kennedy (1992) To declare this model varstan offers a similar declaration structure offorecast (Hyndman and Khandakar 2008) package A harmonic regression with 4 fourierterms and ARIMA(111) residuals is declared and fitted to the birth data

Rgt model2 = Sarima(birthorder = c(111)xreg = fourier(birthK = 2))Rgt sfit2 = varstan(model = model2chains = 1iter = 2000warmup = 1000)Rgt sfit2

y ~ Sarima(111)reg[4]373 observations and 1 dimensionDifferences 1 seasonal Differences 0Current observations 372

mean se 25 975 ess Rhatmu0 -00712 00068 -00846 -00578 9396338 10032sigma0 108085 00124 107841 108328 9949722 10003phi -02705 00019 -02742 -02668 10044136 10006theta 06242 00015 06212 06272 9216062 09992breg1 -216318 00407 -217116 -215520 9659837 10006breg2 06619 00305 06021 07217 9767075 10002breg3 47937 00207 47531 48344 10791161 10003

Izhar Asael Alonzo Matamoros 11

breg4 -53570 00249 -54059 -53082 10994993 10005loglik -14153887 00697 -14155252 -14152521 9360271 10005

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

In this scenario both models suggest to be a good choice for birth series analysis Even sothe harmonic regression fits more parameters It is an obvious choice for birthrsquos sinusoidalbehavior As an example of model selection criteria the bayes_factor in logarithmic scalethat compares the modelsrsquo marginal likelihoods is computed Values above 6 (in logarithmicscale) provides good evidence for selecting the first model And for birth data the seasonalarima model (model1 ) is a better choice

Rgt bayes_factor(x1 = sf1x2 = sfit2log = TRUE)

Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Estimated log Bayes factor in favor of model1 over model2 19913745

Now a comparison of the selected model (model1 sim Sarima(111)(111)[12]) and the onegiven by the autosarima() function For this purpose a leave of one out cross validationloo() is used and both looic are compared with the loo_compare() function provided bythe loo (Vehtari and Gabry 2017) package

Rgt sfit3 = autosarima(birthchains = 1iter = 4000)Rgt sfit3

y ~ Sarima(012)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Diferences 1Current observations 360

mean se 25 975 ess Rhatmu0 00080 00018 00045 00116 2050372 09997sigma0 73517 00060 73399 73634 1991938 10008

12 varstan Bayesian time series analysis with Stan in R

theta1 03642 00013 03616 03668 1978174 10006theta2 01358 00011 01336 01379 2023769 10004sphi -02465 00016 -02496 -02433 2084503 10005stheta 03040 00017 03006 03073 2167639 09995loglik -12317452 00395 -12318225 -12316679 1789987 10009

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

Different from model1 the selected one does not contemplate an auto-regressive componentand use 2 mean average components insteadNow what proceeds is to estimate the loo forboth models

Rgt loo1 = loo(sfit1)Rgt loo3 = loo(sfit3)

Rgt lc = looloo_compare(loo1loo3)Rgt print(lcsimplify = FALSE)

elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looicmodel2 00 00 -12354 154 71 08 2470 308model1 -08 58 -12362 156 78 09 2472 312

loo_compare() prints first the best model In this example is the one provided by theautosarima() function where its looic is 2 units below model1 This autosarima()function is useful as starting point But the reader is encouraged to test more complexmodels and priors that adjust to the initial beliefs

ConclusionsThe paper gives a general overview of varstan package as a starting point of Bayesian timeseries analysis with structured models and it offers a simple dynamic interface inspired inthe classic functions provided by forecast astsa and var packages The interface functionsand prior flexibility that varstan offers makes Bayesian analysis flexible as classic methodsfor structured linear time series models The packagersquos goal is to provide a wide range ofmodels with a prior selection flexibility In a posterior version non-linear models such aswackier GARCH variants stochastic volatility hidden Markov state-space and uni-variateDynamic linear models will be included Along with several improvements in the packagersquosfunctionality

AcknowledgmentsFirst of all we would like to thank the Stan Development team and the Stan forumrsquos commu-nity for always being patience and helping fixing bugs and doubts in the package development

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 9: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

Izhar Asael Alonzo Matamoros 9

All fitted models are varstan objects these are S3 classes with the stanfit results provided bythe rstan (Stan Development Team 2020) package and other useful elements that make themodeling process easier After fitting the proposed model a visual diagnostic of parameterscheck residuals and fitted values using the plot methods On figure 2 trace and posteriordensity plots are illustrated for all the model parameters

Rgt plot(sfit1type = parameter)

Figure 2 Trace and density plots for all the fitted parameters

In figure 2 all the chains appeared to be stationary and the posteriors seems to have nomulti-modal distributions Indicating that all chains have mixed and converged One usefulway to assess modelsrsquo fit is by the residuals (et = YtminusYt) The package provides the posteriorsample of every residual but checking all of them is an exhausting task An alternative ischecking the process generated by the residuals posterior estimate A white noise behaviorindicates a good model fit The modelrsquos residuals in figure 3 seems to follow a random noisethe auto-correlation in acf plots quickly falls to zero indicating an acceptable model fit

Rgt p1 = plot(sfit1type = residuals)Rgt p2 = plot(sfit1)

Rgt gridarrange(p2p1ncol = 1)

10 varstan Bayesian time series analysis with Stan in R

Figure 3 Posterior median residual plot

Because of the sinusoidal pattern that birth series (figure 1) presents a dynamic Harmonicregression (A fourier transform with arima structure for errors) could also assess a goodfit Kennedy (1992) To declare this model varstan offers a similar declaration structure offorecast (Hyndman and Khandakar 2008) package A harmonic regression with 4 fourierterms and ARIMA(111) residuals is declared and fitted to the birth data

Rgt model2 = Sarima(birthorder = c(111)xreg = fourier(birthK = 2))Rgt sfit2 = varstan(model = model2chains = 1iter = 2000warmup = 1000)Rgt sfit2

y ~ Sarima(111)reg[4]373 observations and 1 dimensionDifferences 1 seasonal Differences 0Current observations 372

mean se 25 975 ess Rhatmu0 -00712 00068 -00846 -00578 9396338 10032sigma0 108085 00124 107841 108328 9949722 10003phi -02705 00019 -02742 -02668 10044136 10006theta 06242 00015 06212 06272 9216062 09992breg1 -216318 00407 -217116 -215520 9659837 10006breg2 06619 00305 06021 07217 9767075 10002breg3 47937 00207 47531 48344 10791161 10003

Izhar Asael Alonzo Matamoros 11

breg4 -53570 00249 -54059 -53082 10994993 10005loglik -14153887 00697 -14155252 -14152521 9360271 10005

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

In this scenario both models suggest to be a good choice for birth series analysis Even sothe harmonic regression fits more parameters It is an obvious choice for birthrsquos sinusoidalbehavior As an example of model selection criteria the bayes_factor in logarithmic scalethat compares the modelsrsquo marginal likelihoods is computed Values above 6 (in logarithmicscale) provides good evidence for selecting the first model And for birth data the seasonalarima model (model1 ) is a better choice

Rgt bayes_factor(x1 = sf1x2 = sfit2log = TRUE)

Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Estimated log Bayes factor in favor of model1 over model2 19913745

Now a comparison of the selected model (model1 sim Sarima(111)(111)[12]) and the onegiven by the autosarima() function For this purpose a leave of one out cross validationloo() is used and both looic are compared with the loo_compare() function provided bythe loo (Vehtari and Gabry 2017) package

Rgt sfit3 = autosarima(birthchains = 1iter = 4000)Rgt sfit3

y ~ Sarima(012)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Diferences 1Current observations 360

mean se 25 975 ess Rhatmu0 00080 00018 00045 00116 2050372 09997sigma0 73517 00060 73399 73634 1991938 10008

12 varstan Bayesian time series analysis with Stan in R

theta1 03642 00013 03616 03668 1978174 10006theta2 01358 00011 01336 01379 2023769 10004sphi -02465 00016 -02496 -02433 2084503 10005stheta 03040 00017 03006 03073 2167639 09995loglik -12317452 00395 -12318225 -12316679 1789987 10009

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

Different from model1 the selected one does not contemplate an auto-regressive componentand use 2 mean average components insteadNow what proceeds is to estimate the loo forboth models

Rgt loo1 = loo(sfit1)Rgt loo3 = loo(sfit3)

Rgt lc = looloo_compare(loo1loo3)Rgt print(lcsimplify = FALSE)

elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looicmodel2 00 00 -12354 154 71 08 2470 308model1 -08 58 -12362 156 78 09 2472 312

loo_compare() prints first the best model In this example is the one provided by theautosarima() function where its looic is 2 units below model1 This autosarima()function is useful as starting point But the reader is encouraged to test more complexmodels and priors that adjust to the initial beliefs

ConclusionsThe paper gives a general overview of varstan package as a starting point of Bayesian timeseries analysis with structured models and it offers a simple dynamic interface inspired inthe classic functions provided by forecast astsa and var packages The interface functionsand prior flexibility that varstan offers makes Bayesian analysis flexible as classic methodsfor structured linear time series models The packagersquos goal is to provide a wide range ofmodels with a prior selection flexibility In a posterior version non-linear models such aswackier GARCH variants stochastic volatility hidden Markov state-space and uni-variateDynamic linear models will be included Along with several improvements in the packagersquosfunctionality

AcknowledgmentsFirst of all we would like to thank the Stan Development team and the Stan forumrsquos commu-nity for always being patience and helping fixing bugs and doubts in the package development

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 10: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

10 varstan Bayesian time series analysis with Stan in R

Figure 3 Posterior median residual plot

Because of the sinusoidal pattern that birth series (figure 1) presents a dynamic Harmonicregression (A fourier transform with arima structure for errors) could also assess a goodfit Kennedy (1992) To declare this model varstan offers a similar declaration structure offorecast (Hyndman and Khandakar 2008) package A harmonic regression with 4 fourierterms and ARIMA(111) residuals is declared and fitted to the birth data

Rgt model2 = Sarima(birthorder = c(111)xreg = fourier(birthK = 2))Rgt sfit2 = varstan(model = model2chains = 1iter = 2000warmup = 1000)Rgt sfit2

y ~ Sarima(111)reg[4]373 observations and 1 dimensionDifferences 1 seasonal Differences 0Current observations 372

mean se 25 975 ess Rhatmu0 -00712 00068 -00846 -00578 9396338 10032sigma0 108085 00124 107841 108328 9949722 10003phi -02705 00019 -02742 -02668 10044136 10006theta 06242 00015 06212 06272 9216062 09992breg1 -216318 00407 -217116 -215520 9659837 10006breg2 06619 00305 06021 07217 9767075 10002breg3 47937 00207 47531 48344 10791161 10003

Izhar Asael Alonzo Matamoros 11

breg4 -53570 00249 -54059 -53082 10994993 10005loglik -14153887 00697 -14155252 -14152521 9360271 10005

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

In this scenario both models suggest to be a good choice for birth series analysis Even sothe harmonic regression fits more parameters It is an obvious choice for birthrsquos sinusoidalbehavior As an example of model selection criteria the bayes_factor in logarithmic scalethat compares the modelsrsquo marginal likelihoods is computed Values above 6 (in logarithmicscale) provides good evidence for selecting the first model And for birth data the seasonalarima model (model1 ) is a better choice

Rgt bayes_factor(x1 = sf1x2 = sfit2log = TRUE)

Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Estimated log Bayes factor in favor of model1 over model2 19913745

Now a comparison of the selected model (model1 sim Sarima(111)(111)[12]) and the onegiven by the autosarima() function For this purpose a leave of one out cross validationloo() is used and both looic are compared with the loo_compare() function provided bythe loo (Vehtari and Gabry 2017) package

Rgt sfit3 = autosarima(birthchains = 1iter = 4000)Rgt sfit3

y ~ Sarima(012)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Diferences 1Current observations 360

mean se 25 975 ess Rhatmu0 00080 00018 00045 00116 2050372 09997sigma0 73517 00060 73399 73634 1991938 10008

12 varstan Bayesian time series analysis with Stan in R

theta1 03642 00013 03616 03668 1978174 10006theta2 01358 00011 01336 01379 2023769 10004sphi -02465 00016 -02496 -02433 2084503 10005stheta 03040 00017 03006 03073 2167639 09995loglik -12317452 00395 -12318225 -12316679 1789987 10009

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

Different from model1 the selected one does not contemplate an auto-regressive componentand use 2 mean average components insteadNow what proceeds is to estimate the loo forboth models

Rgt loo1 = loo(sfit1)Rgt loo3 = loo(sfit3)

Rgt lc = looloo_compare(loo1loo3)Rgt print(lcsimplify = FALSE)

elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looicmodel2 00 00 -12354 154 71 08 2470 308model1 -08 58 -12362 156 78 09 2472 312

loo_compare() prints first the best model In this example is the one provided by theautosarima() function where its looic is 2 units below model1 This autosarima()function is useful as starting point But the reader is encouraged to test more complexmodels and priors that adjust to the initial beliefs

ConclusionsThe paper gives a general overview of varstan package as a starting point of Bayesian timeseries analysis with structured models and it offers a simple dynamic interface inspired inthe classic functions provided by forecast astsa and var packages The interface functionsand prior flexibility that varstan offers makes Bayesian analysis flexible as classic methodsfor structured linear time series models The packagersquos goal is to provide a wide range ofmodels with a prior selection flexibility In a posterior version non-linear models such aswackier GARCH variants stochastic volatility hidden Markov state-space and uni-variateDynamic linear models will be included Along with several improvements in the packagersquosfunctionality

AcknowledgmentsFirst of all we would like to thank the Stan Development team and the Stan forumrsquos commu-nity for always being patience and helping fixing bugs and doubts in the package development

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 11: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

Izhar Asael Alonzo Matamoros 11

breg4 -53570 00249 -54059 -53082 10994993 10005loglik -14153887 00697 -14155252 -14152521 9360271 10005

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

In this scenario both models suggest to be a good choice for birth series analysis Even sothe harmonic regression fits more parameters It is an obvious choice for birthrsquos sinusoidalbehavior As an example of model selection criteria the bayes_factor in logarithmic scalethat compares the modelsrsquo marginal likelihoods is computed Values above 6 (in logarithmicscale) provides good evidence for selecting the first model And for birth data the seasonalarima model (model1 ) is a better choice

Rgt bayes_factor(x1 = sf1x2 = sfit2log = TRUE)

Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5Iteration 6Estimated log Bayes factor in favor of model1 over model2 19913745

Now a comparison of the selected model (model1 sim Sarima(111)(111)[12]) and the onegiven by the autosarima() function For this purpose a leave of one out cross validationloo() is used and both looic are compared with the loo_compare() function provided bythe loo (Vehtari and Gabry 2017) package

Rgt sfit3 = autosarima(birthchains = 1iter = 4000)Rgt sfit3

y ~ Sarima(012)(111)[12]373 observations and 1 dimensionDifferences 1 seasonal Diferences 1Current observations 360

mean se 25 975 ess Rhatmu0 00080 00018 00045 00116 2050372 09997sigma0 73517 00060 73399 73634 1991938 10008

12 varstan Bayesian time series analysis with Stan in R

theta1 03642 00013 03616 03668 1978174 10006theta2 01358 00011 01336 01379 2023769 10004sphi -02465 00016 -02496 -02433 2084503 10005stheta 03040 00017 03006 03073 2167639 09995loglik -12317452 00395 -12318225 -12316679 1789987 10009

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

Different from model1 the selected one does not contemplate an auto-regressive componentand use 2 mean average components insteadNow what proceeds is to estimate the loo forboth models

Rgt loo1 = loo(sfit1)Rgt loo3 = loo(sfit3)

Rgt lc = looloo_compare(loo1loo3)Rgt print(lcsimplify = FALSE)

elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looicmodel2 00 00 -12354 154 71 08 2470 308model1 -08 58 -12362 156 78 09 2472 312

loo_compare() prints first the best model In this example is the one provided by theautosarima() function where its looic is 2 units below model1 This autosarima()function is useful as starting point But the reader is encouraged to test more complexmodels and priors that adjust to the initial beliefs

ConclusionsThe paper gives a general overview of varstan package as a starting point of Bayesian timeseries analysis with structured models and it offers a simple dynamic interface inspired inthe classic functions provided by forecast astsa and var packages The interface functionsand prior flexibility that varstan offers makes Bayesian analysis flexible as classic methodsfor structured linear time series models The packagersquos goal is to provide a wide range ofmodels with a prior selection flexibility In a posterior version non-linear models such aswackier GARCH variants stochastic volatility hidden Markov state-space and uni-variateDynamic linear models will be included Along with several improvements in the packagersquosfunctionality

AcknowledgmentsFirst of all we would like to thank the Stan Development team and the Stan forumrsquos commu-nity for always being patience and helping fixing bugs and doubts in the package development

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 12: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

12 varstan Bayesian time series analysis with Stan in R

theta1 03642 00013 03616 03668 1978174 10006theta2 01358 00011 01336 01379 2023769 10004sphi -02465 00016 -02496 -02433 2084503 10005stheta 03040 00017 03006 03073 2167639 09995loglik -12317452 00395 -12318225 -12316679 1789987 10009

Samples were drawn using sampling(NUTS) For each parameter essis the effective sample size and Rhat is the potentialscale reduction factor on split chains (at convergence Rhat = 1)

Different from model1 the selected one does not contemplate an auto-regressive componentand use 2 mean average components insteadNow what proceeds is to estimate the loo forboth models

Rgt loo1 = loo(sfit1)Rgt loo3 = loo(sfit3)

Rgt lc = looloo_compare(loo1loo3)Rgt print(lcsimplify = FALSE)

elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looicmodel2 00 00 -12354 154 71 08 2470 308model1 -08 58 -12362 156 78 09 2472 312

loo_compare() prints first the best model In this example is the one provided by theautosarima() function where its looic is 2 units below model1 This autosarima()function is useful as starting point But the reader is encouraged to test more complexmodels and priors that adjust to the initial beliefs

ConclusionsThe paper gives a general overview of varstan package as a starting point of Bayesian timeseries analysis with structured models and it offers a simple dynamic interface inspired inthe classic functions provided by forecast astsa and var packages The interface functionsand prior flexibility that varstan offers makes Bayesian analysis flexible as classic methodsfor structured linear time series models The packagersquos goal is to provide a wide range ofmodels with a prior selection flexibility In a posterior version non-linear models such aswackier GARCH variants stochastic volatility hidden Markov state-space and uni-variateDynamic linear models will be included Along with several improvements in the packagersquosfunctionality

AcknowledgmentsFirst of all we would like to thank the Stan Development team and the Stan forumrsquos commu-nity for always being patience and helping fixing bugs and doubts in the package development

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 13: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

Izhar Asael Alonzo Matamoros 13

Furthermore Paul BAtildeijrknerrsquos encourage and advice made easier the development processEnrique Rivera Gomez for his help in the article redaction and translation Marco Cruz andAlicia Nieto-Reyes for believe in this work A special thanks to Mireya Matamoros for herhelp in every problem we have encountered so far

References

Betancourt M (2017) ldquoA Conceptual Introduction to Hamiltonian Monte Carlordquo 170102434

Bierens HJ (2006) ldquoInformation Criteria and Model Selectionrdquo Technical Report 3 Penssyl-vania State University Pensylvania

Bollerslev T (1986) ldquoGeneralized autoregressive conditional heteroskedasticityrdquo Journalof Econometrics 31(3) 307 ndash 327 ISSN 0304-4076 doihttpsdoiorg1010160304-4076(86)90063-1 URL httpwwwsciencedirectcomsciencearticlepii0304407686900631

Box GEP Jenkins G (1978) ldquoTime series analysis Forecasting and control San FranciscoHolden-Dayrdquo Biometrika 65(2) 297ndash303 doi101093biomet652297 httpoupprodsislanbiometarticle-pdf65229764905865-2-297pdf URL httpsdoiorg101093biomet652297

BAtildeijrkner PC (2017) ldquobrms An R Package for Bayesian Multilevel Models Using StanrdquoJournal of Statistical Software Articles 80(1) 1ndash28 ISSN 1548-7660 doi1018637jssv080i01 URL httpswwwjstatsoftorgv080i01

Duane S ea (1987) ldquoHybrid Monte Carlordquo Physics Letters B 95(2) 216 ndash 222 ISSN0370-2693 doihttpsdoiorg1010160370-2693(87)91197-X URL httpwwwsciencedirectcomsciencearticlepii037026938791197X

Engle RF (1982) ldquoAutoregressive Conditional Heteroscedasticity with Estimates of the Vari-ance of United Kingdom Inflationrdquo Econometrica 50(4) 987ndash1007 ISSN 0012968214680262 URL httpwwwjstororgstable1912773

Fonseca TCO Cerqueira VS Migon HS Torres CAC (2019) ldquoThe effects of degrees of freedomestimation in the Asymmetric GARCH model with Student-t Innovationsrdquo 191001398

Fonseca TCO Ferreira MAR Migon HS (2008) ldquoObjective Bayesian analysis for the Student-t regression modelrdquo Biometrika 95(2) 325ndash333 ISSN 0006-3444 doi101093biometasn001 httpsacademicoupcombiometarticle-pdf952325622763asn001pdf URL httpsdoiorg101093biometasn001

Gabry J Mahr T (2019) ldquobayesplot Plotting for Bayesian Modelsrdquo R package version 171URL httpsmc-stanorgbayesplot

Gomez V (1998) ldquoAutomatic Model Identification in the Presence of Missing Observationsand Outliersrdquo

Goodrich B Gabry J Ali I Brilleman S (2020) ldquorstanarm Bayesian applied regressionmodeling via Stanrdquo R package version 2193 URL httpsmc-stanorgrstanarm

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 14: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

14 varstan Bayesian time series analysis with Stan in R

Gronau QF Sarafoglou A Matzke D Ly A Boehm U Marsman M Leslie DS Forster JJWagenmakers EJ Steingroever H (2017) ldquoA Tutorial on Bridge Samplingrdquo 170305984

Gronau QF Singmann H Wagenmakers EJ (2020) ldquobridgesampling An R Package forEstimating Normalizing Constantsrdquo Journal of Statistical Software 92(10) 1ndash29 doi1018637jssv092i10

Hannan EJ Rissanen J (1982) ldquoRecursive Estimation of Mixed Autoregressive-Moving Av-erage Orderrdquo Biometrika 69(1) 81ndash94 ISSN 00063444 URL httpwwwjstororgstable2335856

Hoffman MD Gelman A (2014) ldquoThe No-U-Turn Sampler Adaptively Setting Path Lengthsin Hamiltonian Monte Carlordquo Journal of Machine Learning Research 15 1593ndash1623 URLhttpjmlrorgpapersv15hoffman14ahtml

Hyndman R Athanasopoulos G Bergmeir C Caceres G Chhay L OrsquoHara-Wild M Petropou-los F Razbash S Wang E Yasmeen F (2020) forecast Forecasting functions for time seriesand linear models R package version 812 URL httppkgrobjhyndmancomforecast

Hyndman R Khandakar Y (2008) ldquoAutomatic Time Series Forecasting The forecast Packagefor Rrdquo Journal of Statistical Software Articles 27(3) 1ndash22 ISSN 1548-7660 doi1018637jssv027i03 URL httpswwwjstatsoftorgv027i03

Kass RE Raftery AE (1995) ldquoBayes Factorsrdquo Journal of the American Statistical Association90(430) 773ndash795 ISSN 01621459 URL httpwwwjstororgstable2291091

Kennedy P (1992) ldquoForecasting with dynamic regression models Alan Pankratz 1991 (JohnWiley and Sons New York) ISBN 0-471-61528-5 [UK pound]4750rdquo International Journalof Forecasting 8(4) 647ndash648 URL httpsEconPapersrepecorgRePEceeeintforv8y1992i4p647-648

Lewandowski D Kurowicka D Joe H (2009) ldquoGenerating random correlation matrices basedon vines and extended onion methodrdquo Journal of Multivariate Analysis 100(9) 1989ndash 2001 ISSN 0047-259X doihttpsdoiorg101016jjmva200904008 URLhttpwwwsciencedirectcomsciencearticlepiiS0047259X09000876

Migon H Gamerman D Louzada F (2014) Statistical inference An integrated approachChapman and Hall CRC Texts in Statistical Science ISBN 9781439878804

Natarajan R Kass RE (2000) ldquoReference Bayesian Methods for Generalized Lin-ear Mixed Modelsrdquo Journal of the American Statistical Association 95(449) 227ndash237 doi10108001621459200010473916 httpswwwtandfonlinecomdoipdf10108001621459200010473916 URL httpswwwtandfonlinecomdoiabs10108001621459200010473916

R Core Team (2017) R A Language and Environment for Statistical Computing R Founda-tion for Statistical Computing Vienna Austria URL httpswwwR-projectorg

Shumway R Stoffer D (2010) Time Series Analysis and Its Applications With R ExamplesSpringer Texts in Statistics Springer New York ISBN 9781441978646 URL httpsbooksgoogleesbooksid=dbS5IQ8P5gYC

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example
Page 15: varstan: An R packageforBayesiananalysisof ...6 varstan: Bayesian time series analysis with Stan in R Otherusefulfunctionsare parameters() thatprintstheparameter’snamesofaspec- ified

Izhar Asael Alonzo Matamoros 15

Spiegelhalter DJ Best NG Carlin BP Van Der Linde A (2002) ldquoBayesian measuresof model complexity and fitrdquo Journal of the Royal Statistical Society Series B(Statistical Methodology) 64(4) 583ndash639 doi1011111467-986800353 httpsrssonlinelibrarywileycomdoipdf1011111467-986800353 URL httpsrssonlinelibrarywileycomdoiabs1011111467-986800353

Stan Development Team (2017) ldquoStan A C++ Library for Probability and SamplingVersion 2160rdquo URL httpmc-stanorg

Stan Development Team (2020) ldquoRStan the R interface to Stanrdquo R package version 2193URL httpmc-stanorg

Stoffer D (2019) ldquoastsa Applied Statistical Time Series Analysisrdquo R package version 19URL httpsCRANR-projectorgpackage=astsa

Tsay RS (2010) Analysis of Financial Time Series Second edi edition Wiley-InterscienceChicago ISBN 978-0470414354 doi1010020471264105 arXiv10111669v3

Vehtari A Gelman A Gabry J (2016) ldquoPractical Bayesian model evaluation using leave-one-out cross-validation and WAICrdquo Statistics and Computing 27(5) 1413acircĂŞ1432ISSN 1573-1375 doi101007s11222-016-9696-4 URL httpdxdoiorg101007s11222-016-9696-4

Vehtari A Gelman A Gabry J (2017) ldquoloo Efficient Leave-One-Out Cross-Validation andWAIC for Bayesian Models R Packagerdquo URL httpsgithubcomjgabryloo

Watanabe S (2010) ldquoAsymptotic Equivalence of Bayes Cross Validation and Widely Applica-ble Information Criterion in Singular Learning Theoryrdquo Journal of Machine Learning Re-search 11 URL httpwwwjmlrorgpapersvolume11watanabe10awatanabe10apdf

Wuertz D Setz T Chalabi Y Boudt C Chausse P Miklovac M (2020) fGarch Rmetrics -Autoregressive Conditional Heteroskedastic Modelling R package version 3042832 URLhttpsCRANR-projectorgpackage=fGarch

AffiliationIzhar Asael Alonzo MatamorosUniversidad Nacional AutAtildeşnoma de HondurasDepartmento de EstadAtildeŋsticaEscuela de MatemAtildeątica Facultad de CienciasBlvd Suyapa Universidad Nacional AutAtildeşnoma de HondurasTegucigalpa HondurasE-mail asaelalonzogmailcomURL httpsasaelamwixsitecomasael697site

  • Introduction
  • Structured Time series models
    • Prior Distribution
      • Estimation process
        • Automatic order selection in arima models
          • Package structure and modeling procedure
          • Case study Analyzing the monthly live birth in US an example