FMenchetti / CausalMBSTS

16 stars 0 forks source link

Error in rInvWishart؛ 'scal' matrix is not positive-definite #47

Closed Mohammad-Mahdavi98 closed 2 years ago

Mohammad-Mahdavi98 commented 2 years ago

Hello When we put the data in the (as.mbsts) function, the following error is observed, what is the reason for this error? How to fix it?

Error in rInvWishart(1, nu, s[[paste("degree.", i, sep = "")]]) : 'scal' matrix is not positive-definite

Kind regards

FMenchetti commented 2 years ago

Hello! The error is either coming from s0.r or s0.eps, i.e., the scale matrices of the Inverse-Wishart priors. They must be both positive definite and, by default, they are set to the variance-covariance matrix of the data (multiplied by a scaling factor). It could be that your var-covar matrix isn't positive definite...Have you checked the determinant and the eigenvalues of the var-covar matrix implied by your data? You might have multicollinearity issues

Mohammad-Mahdavi98 commented 2 years ago

Thank you very much for your explanation But this problem also occurs when I use a diagonal matrix for s0.r and s0.eps s0.eps =0.01*diag(ncol(data)),s0.r =0.01*diag(ncol(data))

Error in rInvWishart(1, nu, s[[paste("degree.", i, sep = "")]]) : 'scal' matrix is not positive-definite

FMenchetti commented 2 years ago

Can you post a small reproducible example with your actual data? I suspect the error is coming from the function stateVarianceDef but I need to understand what exactly is causing it

Mohammad-Mahdavi98 commented 2 years ago

Of course

data<-read.csv("https://raw.githubusercontent.com/Mohammad-Mahdavi98/data/main/logdata.csv") model <- as.mbsts(data, components = c("trend"), niter = 1000, burn = 700,s0.eps =0.01*diag(ncol(data)),s0.r =0.01*diag(ncol(data)))

FMenchetti commented 2 years ago

The problem was caused by a -Inf obs in your data, you can see it by doing cbind(min = apply(data, 2, min), mean = apply(data, 2, mean), max = apply(data, 2, max), sd = apply(data, 2, sd)). I replaced it with a random observation ind <- which(data[, "data__shakhes.Close"] == "-Inf") ; data[ind, "data__shakhes.Close"] <- 9 and everything works as expected. However, do not take my solution as optimal, it would be best to investigate what is causing that observation and whether it is better to remove it or impute it and how.

Mohammad-Mahdavi98 commented 2 years ago

I appreciate you so much

FMenchetti commented 2 years ago

happy to help!