Yes, you can Google the key words to find the BUGS ( including WinBUGS and OpenBUGS) codes for your model but usually they will not work or you just can not understand what they are writing. You should firstly understand basic codes skill about BUGS:

1. Process for Building Bayesian Model
You could refer to the following picture.
Process for Building Bayesian Model 
see the example in the book,Applied Bayesian Statistics, pp.147


2. BUGS code structure
  "Data analysis using regression and multilevel/hierarchical models" (section 16.8, p.366)  gives excellent explanations about the BUGS code structure.  

3. How to perform the codes
"Bayesian Modeling Using WinBUGS" (chapter 3 and 4) gives clear pictures to teach you how to run your codes. 

4. Run BUGS through R
"Simple linear regression using R2OpenBUGS"
It is not easy to import your data in BUGS if your dataset is too big. Try to use R as a tool to manage data and deal with the MCMC results. This author offers a simple example (linear model) to teach you how to use these. If you want to totally include your BUGS code in R console, you should use sink and cat function , see this blog for a very vivid example. Other useful tricks for running BUGS within R could also found in book, Introduction to WinBUGS for Ecologists, (Appendix 1 - A List of WinBUGS Tricks).

5. Other special BUGS functions
Useful functions could be found here. More examples about using these should refer to The BUGS Book.


6. Bayes' Theory
I have gave a reading list for understanding this part in my another post. You should at least fully understand the first chapter of Bayesian Analysis for the Social Sciences.

Now you understand the basics, and you could try to search the codes about your interested models. The best and workable sources come from the books and journal articles.  Most authors below are very generous to provide the codes in their website. I will not provide these codes here but will suggest the references for you.

1. Efficiency estimation
(1)Stochastic frontier model
"Bayesian stochastic frontier analysis using WinBUGS"
The author (Professor Jim Griffin) provides the WinBUGS code in his website.
I depict their data structure as follows and you should also read their paper on page 165.


2. Panel data analysis
"Introduction to applied Bayesian statistics and estimation for social scientists" (ch.9)
Relevant theory of Bayesian panel data model could refer to Bayesian Econometrics (ch.7), Panel Data Modeling and Inference: A Bayesian PrimerIntroduction to Modern Bayesian Econometrics (ch.7)
For specific panel data analysis demonstration:


3. Multilevel/hierarchical models
"Data analysis using regression and multilevel/hierarchical models" especially chapter 17, p.305
"Applied Bayesian Hierarchical Methods"
"Scope economies in Australian distance education"

4. Econometrics
"An introduction to modern Bayesian Econometrics"

5. Rasch (IRT) Model
"Bayesian Estimation for the Rasch Model using WinBUGS"

6. Random Coefficient Dynamic Factor Models
"Bayesian Estimation of Random Coefficient Dynamic Factor Models"

7. Categorical models
Bayesian Models for Categorical Data, codes

8. Latent Growth Model
latent basis growth curve model with varying residual variances
latent basis growth curve model

9. Prediction
Linear regression: Bayesian Ideas and Data Analysis p. 241
Other models
"The BUGS Book"
"Bayesian modeling using WinBUGS"





This article will continuously be updated and I welcome anyone of you to post your experience here.




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.