2.5.3 Bayesian Estimation using WinBUGS or OPENBUGS in R

As we have seen in the previous section with the two files that generates the BayesianModeling can implement the Bayesian Estimation using WinBUGS or OpenBugs programs. Also Bayesian estimation can be implemented by using R2WinBUGS or R2OpenBUGS (Sturtz, Ligges and Gelman, 2005), p ackages for Running WinBUGS and OpeBUGS from R respectively or BRugs (Thomas, et al, 2006) a collection of R functions that allow users to analyze graphical models using MCMC techniques.

For this will need the original text file with the data in columns:

I01 I02 I03 I04 I05 I06 ... I12 I13 I14

1 1 0 1 1 0 1 ... 0 0 1

1 1 1 1 1 1 1 ... 0 1 1

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

1 1 0 1 0 0 1 ... 0 1 1

i.e. the file called MathData.dat (See section 2.5.1) and the syntax of the model generated in BayesianModeling will have to copy only the syntax of the model. In the example below implement the 2SP model.

This should copy the syntax before “data” and save it in a file, for this example modelirt.txt. Then the file modelirt.txt would remain

Afterwards to implement the Bayesian estimation in R will follow the next steps to use the library R2WinBUGS. Remember to install it previously.

1. In R, download the library R2WinBUGS with the following command:

library(R2WinBUGS)

2. Read the data (the MathData.dat file for this example is placed in the folder F:\MILUS\MathData.dat)

datos <- read.table("F:/MILUS/MathData.dat", header=TRUE, sep="", na.strings="NA", dec=".",strip.white=TRUE)

3. Create a list that contain the data and the information that is under data in the file generated by the BayesianModeling.

using the following command

n=nrow(datos)

k=ncol(datos)

data<-list(y=as.matrix(datos),n=131,k=14)

4. Create a program that will generate initial values.

inits <- function(){ list(a=rep(1,k),b=rep(0,k),delta=rep(0,k),theta=rep(n,0.5))}

5. Finally the command bugs implements the Bayesian estimation. Here will explain in brief the syntax of the command bugs

parameters.to.save = is a vector with the names of the parameters of the model which simulations want to store.

model.file = is the name of the file where the model is saved.

n.chains = is the number of chains that will be generated.

n.iter = is the number of total iterations of each chain.

n.burnin = is the number of iterations that will be discharged as burn-in.

program = is the program that will be used to implement the Bayesian inference

n.burnin = is the number of iterations that will be discharged as burn-in.

Then the following command implements the Bayesian estimation and the simulations are stored in the object out.

out<-

bugs(data,inits,parameters.to.save=c("a","b","delta"),

model.file="F:/MILUS/modelirt.txt", n.chains=1, n.iter=24000,

n.burnin=4000, program="WinBUGS")

6. If type out in the line of commands of R obtain a summary of the simulation

> out

Inference for Bugs model at "F:/MILUS/modelirt.txt", fit using WinBUGS,

1 chains, each with 24000 iterations (first 4000 discarded), n.thin = 20

n.sims = 1000 iterations saved

mean sd 2.5% 25% 50% 75% 97.5%

a[1] 0.5 0.2 0.1 0.3 0.5 0.6 1.0

a[2] 0.3 0.2 0.0 0.1 0.2 0.4 0.6

a[3] 0.5 0.2 0.1 0.3 0.5 0.6 0.9

a[4] 0.9 0.3 0.3 0.6 0.8 1.1 1.6

a[5] 0.5 0.2 0.1 0.3 0.4 0.6 1.0

a[6] 0.3 0.2 0.0 0.2 0.3 0.4 0.7

a[7] 0.8 0.3 0.3 0.6 0.8 1.0 1.5

a[8] 0.9 0.3 0.3 0.7 0.9 1.1 1.7

a[9] 0.2 0.1 0.0 0.1 0.2 0.3 0.5

a[10] 0.4 0.2 0.1 0.3 0.4 0.6 0.9

a[11] 1.3 0.5 0.6 1.0 1.3 1.6 2.4

a[12] 0.3 0.2 0.0 0.2 0.3 0.4 0.7

a[13] 0.4 0.2 0.1 0.3 0.4 0.6 0.9

a[14] 0.4 0.3 0.0 0.2 0.4 0.6 1.1

b[1] -0.9 0.4 -1.5 -1.2 -0.9 -0.6 0.0

b[2] -0.9 0.4 -1.6 -1.3 -1.0 -0.6 0.0

b[3] 0.0 0.4 -0.7 -0.4 0.0 0.3 0.7

b[4] -1.7 0.6 -2.7 -2.1 -1.7 -1.2 -0.5

b[5] -1.1 0.5 -1.8 -1.4 -1.1 -0.8 -0.1

b[6] 0.3 0.4 -0.5 0.0 0.3 0.6 1.0

b[7] -1.6 0.6 -2.6 -2.0 -1.6 -1.2 -0.5

b[8] -1.3 0.6 -2.3 -1.7 -1.4 -0.9 -0.2

b[9] -0.7 0.4 -1.3 -1.0 -0.7 -0.4 0.2

b[10] -1.0 0.5 -1.7 -1.4 -1.0 -0.6 0.0

b[11] -1.9 0.7 -3.3 -2.3 -1.9 -1.5 -0.5

b[12] 0.4 0.4 -0.4 0.0 0.4 0.8 1.0

b[13] -0.9 0.4 -1.6 -1.3 -1.0 -0.7 0.0

b[14] -1.6 0.5 -2.4 -1.9 -1.6 -1.2 -0.5

delta[1] 0.1 0.5 -0.8 -0.4 0.0 0.5 1.0

delta[2] -0.1 0.5 -0.9 -0.5 -0.1 0.2 0.9

delta[3] 0.0 0.5 -0.9 -0.4 0.0 0.4 0.9

delta[4] -0.1 0.5 -0.9 -0.5 -0.2 0.2 0.9

delta[5] -0.1 0.5 -0.9 -0.5 -0.1 0.3 0.9

delta[6] 0.0 0.5 -0.9 -0.4 0.0 0.4 1.0

delta[7] -0.1 0.5 -0.9 -0.6 -0.2 0.3 0.9

delta[8] -0.1 0.5 -0.9 -0.6 -0.2 0.2 0.9

delta[9] -0.1 0.5 -0.9 -0.4 -0.1 0.3 0.8

delta[10] -0.1 0.5 -0.9 -0.5 -0.2 0.3 0.9

delta[11] -0.3 0.6 -1.0 -0.8 -0.5 0.1 0.9

delta[12] 0.0 0.5 -1.0 -0.5 0.0 0.4 0.9

delta[13] 0.0 0.5 -0.8 -0.4 0.0 0.4 1.0

delta[14] -0.1 0.5 -0.9 -0.5 -0.1 0.3 0.9

deviance 3855.8 66.3 3703.9 3814.0 3865.0 3904.0 3963.0

DIC info (using the rule, pD = Dbar-Dhat)

pD = -52.0 and DIC = 3803.7

DIC is an estimate of expected predictive error (lower deviance is better).

Note that for now we just asked for monitoring the parameters a, b and delta. But if it requires could ask for .

7. Finally, for more details in the command bugs can consult Help writing in the line of commands

?bugs

Note. You can specify Bug.directory. The directory that contains the WinBUGS executable. If the global option `R2WinBUGS.bugs.directory` is not `NULL`, it will be used as the default. Also you can specify the program to use, either `winbugs`/`WinBUGS` or `openbugs`/`OpenBUGS`, the latter makes use of function `openbugs` and requires the CRAN package BRugs.

Digite um texto ou endereço de um site ou traduza um documento.

In addition, because of the large number of parameters in IRT models, execution may be delayed!