APPENDIX A – Step by step use of Winbugs for Bayesian inference of the nonlinear structural equation model
STEP 1: Open Winbugs by clicking on
STEP 2: Click on File and open the program called “nonlinear SEM” (available from http://www.biostat.umn.edu/~melanie/NONLINEARSEM/index.html)
STEP 3: Click on Model à Specification and then the Specification Tool will pop-up
STEP 4: Highlight the word “model” in the “nonlinear SEM” program and then click on the “check model” button in the specification tool. If there are any syntactical errors in your model, Winbugs will produce an error box. If the model syntax is fine, then in the lower left hand corner of the window it will say “model is syntactically correct”. Notice now that the “load data” and “compile” buttons are also now active.
STEP 5: Highlight the variable names “z[,1]-z[,9]” representing the nine columns of observed data and click on “load data”. Note that after the last line of the data in the file there is an additional row added with the word “END” written (without quotes). If there is a problem with reading the data Winbugs will produce an error box. If the data reads correctly, it will say “data loaded” in the lower left hand corner.
STEP 6: Click on the “Compile” button. If there is a logical error compiling the model with the data, Winbugs will produce an error box. If the model compiles correctly, it will say “model compiled” in the lower left hand corner. Notice that now the “load inits” and “gen inits” are activated.
STEP 7: The MCMC method is used to draw samples from the posterior distribution of the parameters of interest. Winbugs can draw these samples from one long Markov chain, or from multiple Markov chains. Each chain requires its own set of starting values for the parameters. Hence we can “load inits” for each chain. Here we will only draw from one chain, but if more than one is desired, the step below is repeated for each chain. Highlight the word “list” that contains the starting values for the parameters, then click on “load inits”.
STEP 8: Notice that in the lower left hand corner it says “initial values loaded but this or another chain contain uninitialized variables”. It says this because all of the latent variables in the model (within the Bayesian framework) are also considered parameters and thus each individual’s latent variables need starting values too. Rather than enter 500*3 starting values for all the latent variables, we use the “gen inits” button which allows Winbugs to specify the starting values. Click on the “gen inits” button and notice that now it says “initial values generated, model initialized” in the lower left hand corner.
STEP 9: Click on InferenceàSamples and then the “Sample Monitor Tool” will pop up. It is necessary to tell Winbugs the name of every parameter (“node”) that posterior samples should be outputted for. Type the name of a parameter in the “node” box and when the name matches a variable in the model, the “set” button will become activated. Click “set”, then type in the next parameter into the “node” box and click “set” until all parameters are listed. A list of all the nodes that have been entered can be seen by clicking on the down arrow next to “node”.
STEP 10: Click on ModelàUpdate and the “Update Tool” will pop-up. In the “updates” box, type in how many total MCMC draws Winbugs should take for each chain. Here we request 14,000, since the first 4000 will be disregarded as ‘burn-in’ samples thus leaving 10,000 samples to use for inference. The “refresh” box specifies how often Winbugs will report which iteration it is on at any given time during the computation. Click on the “update” button and the computation will start. The lower left hand corner will say “model is updating” while it is running and the number in the “iteration” box will change as the updates continue. The model is finished running, when the lower left hand reports that all the updates are completed and the time it took, see “14000 updates took 442 s” (i.e. 7 minutes and 22 seconds).
STEP 11: When the MCMC has finished running, the posterior samples for each parameter can be investigated. Using the “Sample Monitor Tool” choose a node (parameter) of interest and click the “stats” button and the “density” button. Below are the results for the gam[1]-gam[6] parameters.
The mean and sd under “node statistics” are the empirical posterior mean and standard deviation (based on 10,000 draws after a 4,000 iteration burn-in period) from the MCMC simulated posterior distribution for each parameter. These values are used as the estimate and standard error (respectively) of the associated parameters. Moreover the val2.5pc and val97.5pc correspond to the 2.5 and 97.5 percentiles which can be used as the 95% credible interval. The “posterior density” plots are histograms of the same 10,000 draws.
STEP 12: Diagnostics for assessing convergence of the MCMC method can be done by examining the “history” plot of the samples at each iteration and looking for random scatter.