mboeck11 / BGVAR

Toolbox for the estimation of Bayesian Global Vector Autoregressions in R.
27 stars 20 forks source link

Error when increasing the number of draws for a model with an exogenous (global) variable #26

Closed borisblagov closed 1 month ago

borisblagov commented 2 months ago

Hello! First of all thank you very much for all the work on the package. My name is Boris and I am a researcher based in Germany. I have been experimenting working with the package and I stumbled across some unexpected behaviour.

I get an error, when I increase the number of draws for a simulation above 1000 for a model with an exogenous variable (a global variable not attached to any country).

First of all, I have no experience in R, so it might be my misunderstanding and I apologize if that is the case but I have the following situation and would be grateful if you can pinpoint me what I am doing wrong.

library(BGVAR)
data(eerData)

cN<-c("EA","US","RU")
eerData<-eerData[cN]
W.trade0012<-W.trade0012[cN,cN]
W.trade0012<-apply(W.trade0012,2,function(x)x/rowSums(W.trade0012))

eerData2<-eerData
eerData2$OC<-eerData$US[,c("poil"),drop=FALSE] # move oil prices into own slot
eerData2$US<-eerData$US[,c("y","Dp", "rer" , "stir", "ltir","tb")] # exclude it from US model

OC.weights<-list()
OC.weights$weights<-rep(1/3, 3)
names(OC.weights$weights)<-names(eerData2)[1:3] # last one is OC model, hence only until 3
OC.weights$variables<-c(colnames(eerData2$OC)) # first entry, endog. variables, second entry weighted average of y from the other countries to proxy demand
OC.weights$exo<-"poil"
# other entities weights with same name as new oil country
OE.weights <- list(OC=OC.weights)

model.2<-bgvar(Data=eerData2,
               W=W.trade0012,
               draws=1000,
               burnin=1000,
               plag=1,
               prior="MN",
               hyperpara=NULL, 
               SV=TRUE,
               thin=1,
               expert=list(OE.weights=OE.weights,save.shrink.store=TRUE),
               trend=FALSE,
               hold.out=0,
               eigen=1
)

This code works. However, if I increase the number of draws, say to 2000, or the burn in to 2000 it does not work.

For example this does not work on my machine

model.2<-bgvar(Data=eerData2,
               W=W.trade0012,
               draws=1000,
               burnin=2000,
               plag=1,
               prior="MN",
               hyperpara=NULL, 
               SV=TRUE,
               thin=1,
               expert=list(OE.weights=OE.weights,save.shrink.store=TRUE),
               trend=FALSE,
               hold.out=0,
               eigen=1
)

and produces the following error:

BGVAR incurred an error when estimating the models.
See original error message:
Error occured in countrymodel: OC. Please check.
Error in detail: 

Error in if ((post4.prop - post4) > log(runif(1, 0, 1))) { : 
  missing value where TRUE/FALSE needed

Error in bvar$Y : $ operator is invalid for atomic vectors

When I use a model without exogenous setup (i.e. no OC "country"), the default settings of 5000 for draws and burn in seem to work. For example the file that comes with the documentaion works flawlessly, i.e. this code

###############################################################################
### Replication Material for
### BGVAR: Bayesian Global Vector Autoregressions with Shrinkage Priors in R
### Maximilian Boeck, Martin Feldkircher, and Florian Huber
### Journal of Statistical Software
###
### created by Maximilian Boeck 30/04/2022
### created with Sweave
###
###############################################################################

### R code from vignette source 'jss4147.Rnw'

###################################################
### code chunk number 1: preliminaries
###################################################
options(prompt = "R> ", continue = "+  ", width = 76, useFancyQuotes = FALSE)
library("MASS")

###################################################
### code chunk number 3: start
###################################################
library("BGVAR")
data("pesaranData", package = "BGVAR")
set.seed(571)

###################################################
### code chunk number 4: model.1
###################################################
model.1 <- bgvar(Data = pesaranData, W = W.8016)

Also, I don't know if it is related, but the following code which also has an exogenous variable and has been provided on the webpage also doesn't work. The error is also visible on the webpage itself if you scroll down. I can replicate the error on my machine as well.

model.ssvs.4<-bgvar(Data=eerData2,
                    W=W.trade0012,
                    plag=1,
                    draws=100,
                    burnin=100,
                    prior="SSVS",
                    SV=TRUE,
                    expert=list(OE.weights=OE.weights,save.shrink.store=TRUE),
                    trend=TRUE
                    )
## 
## BGVAR incurred an error when estimating the models.
## See original error message:

## Error occured in countrymodel: OC. Please check.

## Error in detail:

## Error in gamma_store[, , count] <- gamma : 
##   number of items to replace is not a multiple of replacement length

## Error in bvar$Y: $ operator is invalid for atomic vectors

In any case, thank you for your time!

mboeck11 commented 2 months ago

I have now looked into these issues. The had a common reason in the prior specification when estimating the models. This issues are now fixed and both the code in the vignette as also the example provided should now run through. It was (partly) a numerical issue, which apparently did not pop up with a low number of draws but popped up eventually with a larger number of draws.

Thanks for pointing this out. The errors are (for now) corrected in the development version of the package and will be soon uploaded to CRAN.