Common error messages in BUGS and their possible solutions

1 comment

BUGS (including WinBUGS and OpenBUGS  is a powerful tool to do Bayesian estimation but it usually would not run without a fight (error messages). Most BUGS books focus on teaching you how to build a Bayesian model and run BUGS in your computer but few books teach you how to debug and find the solution to your problems. In this article, I try to bring common error message showing in BUGS and give possible solutions. This post will be divided into three parts: Check Model, Load Data, and Compile. These parts are the 3-step procedure when running BUGS. If you don't know these, I suggest you read the chapter 3 and 4 in the book "Bayesian Modeling Using WinBUGS". "Data analysis using regression and multilevel/hierarchical models" (section 16.8, p.366) also gives excellent explanations about the BUGS code structure.

1. Check Model

You need basic skill for BUGS language syntax and you could refer to Appendix in the book "The BUGS Book". 
  • "Error in is.finite(x)": 
If you run winbug within R using R2WinBugs or R2OpenBugs. Check the R code for your BUGS data if there is any elements which does not belong to the BUGS code. Another reason could be you put other data format in your BUGS data. Remember, BUGS data only accepts matrix format.
    • "unknown type of distribution"
    This simply means that BUGS cannot recognize the distribution you write. For WinBUGS users, you should check your spelling or update the distribution by following the instruction. For OpenBUGS users, some distribution (for example truncated normal could be used by following the procedure above), should be modified following this instruction.
    • "Unknown type of logical function"
    Remember, in BUGS, ~ means "is distributed as" and < - means "is to be replaced by" Therefore, this error message just warn you probably put < - in front of the distribution code.
    • "invalid or unexpected token scanned"
    These are the common mistakes will show in your BUGS because you did not add another half codes like } and ]. I suggest you use code editor  like RStudio since the BUGS use similar codes with R. The beauty of RStudio is that it will automatically help you add another half codes.

    2. Load Data

    One of the difficulties to use BUGS is that its data format is quite different from we usually use. I find it is hard to import your data especially when your data format is excel. If your data is not large, it will be ok just to copy and paste. If it is not, you better consider to use R (its package "R2WinBUGS and "R2OpenBUGS" to call BUGS to work for you.
    • "expected key word structure error pos 1534"
    Check whether there are matrix type (like data[i,j])of data in the Model code but you use vector type (like data[i]) in the Data (including the initial values) code. It's the same the other way round. You should make sure that model and data have a consistent type. See examples in stackoverflow.

    3. Compile

    "model is syntactically correct" Ya! After lot of works, you finally let BUGS compile the model and data and it starts to run! However, you still could get the error messages and BUGS just does not give any results.
    • logical expression contains too many constants
    This usually happens when you are doing the prediction using linear regression model. Example like this one [code] Tcost <- b0+b1*my1+b2*my2+b3*my3+b4*my4+b5*my5+b6*my6+b7*my7 #Total costs +0.5*b11*my1^2+0.5*b22*my2^2+0.5*b33*my3^2+0.5*b44*my4^2+0.5*b55*my5^2+0.5*b66*my6^2+0.5*b77*my7^2 +b12*my1*my2+b13*my1*my3+b14*my1*my4+b15*my1*my5+b16*my1*my6+b17*my1*my7+b23*my2*my3+b24*my2*my4+b25*my2*my5+b26*my2*my6+b27*my2*my7+b34*my3*my4+b35*my3*my5+b36*my3*my6+b37*my3*my7+b45*my4*my5+b46*my4*my6+b47*my4*my7+b56*my5*my6+b57*my5*my7+b67*my6*my7 +bp1*mw1+bp2*mw2)[/code] You try to predict the total cost using the cost function with WinBugs. When your predictors are not too many like this one, you can plug those value like the example. However, once the predictor is too many, you should use the function inprod( ) to solve this problem like the following code: [code]  Tcost <- inprod B[1:K], M[1,1:K]) #total costs[/code] where B is a vector of coefficients, M is a vector of mean, and P is the number of predictors.
    • Trap 66
    See example and solutiohere.
    • "number of items not equal to size of node error"
    Sometimes, even if this error shows up, BUGS will still give you the final results. You need to check whether a initial value vector is the same size as parameter vector in OpenBUGS.
    • "Sorry something went wrong in procedure Sqrt in module Math"
    Check whether the setup of your sigma for data dependent variable (y) is beyond the range of data y. For example, your data y is from 0~100, and you should set your sigma at least to 1000 (when it is uniform distribution). See examples in Bayesian Data Analysis (ed 2, p.5 93).
    • "Sorry something went wrong in procedure Writer.SetPos in module HostFiles"
    Honestly, I don't know what it mean, but I overcome this by reducing the number of iterations and burnin. However, it does not guarantee you will get the right results. In fact, sometimes you could get unstable estimated parameters especially when you lower down the number of iterations greatly. It's better to recheck your codes. You could see more debug techniques in the book, "Data analysis using regression and multilevel/hierarchical models" (chapter 19, p.415).
    • "Sorry something went wrong in procedure ..."
    This problem is more general and ... means any problems including the above problem. If you want to know more what is wrong in your BUGS, see this blog for further info. After you follow the procedure of above blog, you could see trap window writing the errors below instead of "Sorry something went wrong in procedure..."
    • "expected the collection operator c error pos"
    Check whether you do not give initial values to certain nodes. Make sure each node have their own initial values.
    • "undefined real result  Pre: argument of Sqrt must not be negative"
    Check whether your BUGS code variance for your data (y) is larger than your data (y). You should also check your priors. This problem also often occurs in a Hierarchical model especially when you assign a Gamma prior to the variance of your data. If you just want to use a informative prior and then you should just use a Uniform prior instead. However, if you want to use a Gamma distribution, and then try to increase the number within each group and reduce the number of variables. This article will continuously be updated and I welcome anyone of you to post your experience here.

    1 comment:

    Please leave your comments here :)