I have spent several days to unlock an error message on openbugs without success. Using R2OpenBUGS, I get this message:
"model is syntactically correct data loaded (variables not in the model: xA, pA, yA, xB, pB and others) variable mu.phi.alpha is not defined model must have been compiled but not updated to be able to change RN generator BugsCmds:NoCompileInits model must be compiled before generating initial values model must be initialized before updating model must be initialized before monitors used" model must be initialized before monitors used
Here is the code:
library(R2OpenBUGS)
setwd("C:\\Users\\Tiphaine\\Desktop")
mydata<-read.csv("C:\\Users\\Tiphaine\\Desktop\\TEST3.csv")
mydata
xA <- mydata$xA1
pA <- mydata$pA1
yA <- mydata$yA1
xB <- mydata$xB1
pB <- mydata$pB1
yB <- mydata$yB1
choice <-mydata$Choice
data = list("xA", "pA", "yA","xB","pB","yB")
parameters <- c("alpha", "mu.phi.alpha", "mu.alpha", "sigma.phi.alpha", "beta", "mu.phi.beta", "mu.beta", "sigma.phi.beta", "gamma", "mu.phi.gamma", "mu.gamma", "sigma.phi.gamma", "delta", "mu.phi.delta", "mu.delta", "sigma.phi.delta", "lambda", "lmu.lambda", "mu.lambda", "lsigma.lambda", "luce", "lmu.luce", "mu.luce", "lsigma.luce")
inits = function()
{
list(mu.phi.alpha = 0.7, sigma.phi.alpha = 1, mu.phi.beta = 0.7,
sigma.phi.beta = 1,
mu.phi.gamma = 0.7, sigma.phi.gamma = 1, mu.phi.delta = 0.7,
sigma.phi.delta = 1,
lmu.lambda = 0, lsigma.lambda = 0.5, lmu.luce = 0,
sigma.phi.luce = 0.5)
}
model.file="model5.txt"
hierarchical = bugs(data, inits, parameters,model.file, n.chains=1, n.iter=10000, n.burnin=1000, n.thin=10,DIC=T, codaPkg=F, debug=T)
print(hierarchical) # a rough summary
names(hierarchical) # summarizes the variables
hierarchical$summary # more detailed summary
plot(hierarchical) # a visual representation
Here is the model:
model {
alpha <- phi(alpha.phi)
luce <- exp(lluce)
lambda <- exp(llambda)
# We put group-level normal“s on the individual parameters:
alpha.phi ~ dnorm(mu.phi.alpha,tau.phi.alpha)I(-3, 3)
lluce ~ dnorm(lmu.luce, ltau.luce)
llambda ~ dnorm(lmu.lambda, ltau.lambda)
# Here priors for the hyperdistributions are defined:
mu.phi.alpha ~ dnorm(0,1)
tau.phi.alpha <- pow(sigma.phi.alpha,-2)
sigma.phi.alpha ~ dunif(0,10)
lmu.lambda ~ dunif(-2.3, 1.61)
ltau.lambda <- pow(lsigma.lambda,-2)
lsigma.lambda ~ dunif(0,1.13)
lmu.luce ~ dunif(-2.3, 1.61)
ltau.luce <- pow(lsigma.luce,-2)
lsigma.luce ~ dunif(0,1.13)
# To obtain the mean of the hyper distribution on the wanted scale:
mu.alpha <- phi(mu.phi.alpha)
mu.lambda <- exp(lmu.lambda)
mu.luce <- exp(lmu.luce)
for (i in 1:10) {
a[i]<-step(yA1[i])+lambda*(step(yA1[i])-1)
b[i]<-step(yB1[i])+lambda*(step(yB1[i])-1)
va[i] <- pow(xA[i],alpha)*pA[i]+ a[i]*pow(abs(yA[i]),alpha*(1-pA[i])
vb[i] <- pow(xB[i],alpha)*pB[i]+ b[i]*pow(abs(yB[i]),alpha)*(1-pB[i])
C[i]<- Choice[i]-1
C[i] ~ dbern(binval[i])
binval[i] <- 1/(1+exp((vb[i]-va[i])*luce))
}
}