错误“不在模型中的变量”使用openbugs

时间:2015-04-14 06:34:29

标签: r winbugs

我花了几天时间在openbugs上解锁错误消息但没有成功。使用R2OpenBUGS,我收到此消息:

  

“模型在语法上是正确的   加载的数据(不在模型中的变量:xA,pA,yA,xB,pB等)   变量mu.phi.alpha未定义   模型必须已编译但未更新才能更改RN生成器   BugsCmds:NoCompileInits   必须在生成初始值之前编译模型   必须在更新之前初始化模型   必须在监视器使用之前初始化模型“   必须在使用监视器之前初始化模型

以下是代码:

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

以下是模型:

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))

   }
}

1 个答案:

答案 0 :(得分:0)

感谢您的回复!

以下是仍然生成相同错误的简化代码和模型(即不在模型中的变量:xA,pA,yA,xB,pB等)。

library(R2OpenBUGS)
setwd("C:\\Users\\Tiphaine\\Desktop")

xA = c(100,1,100)
pA = c(0.3,0.7,0.9)
yA = c(100,1,100)
xB = c(100,1,100)
pB = c(0.3,0.7,0.9)
yB = c(100,1,100)
choice = c(1,2,1)

data = list("xA", "pA", "yA","xB","pB","yB")
parameters <- c("alpha", "mu.phi.alpha", "mu.alpha", "sigma.phi.alpha")
inits = function()
 {

 list(mu.phi.alpha = 0.7, sigma.phi.alpha = 1)

 }


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)


# Some useful commands:
print(hierarchical)  # a rough summary
names(hierarchical)  # summarizes the variables
hierarchical$summary # more detailed summary
plot(hierarchical)   # a visual representation

对于模型:

model {


    # (the choice-rule parameter).

    alpha <- phi(alpha.phi)


    # We put group-level normal´s on the individual parameters:

    alpha.phi ~ dnorm(mu.phi.alpha,tau.phi.alpha)I(-3, 3)


}


# 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)


# To obtain the mean of the hyper distribution on the wanted scale:
mu.alpha <- phi(mu.phi.alpha) 




        va[i] <- xA[i]*pA[i]*alpha+yA[i]*(1-pA[i])*alpha
            vb[i] <- xB[i]*pB[i]*alpha+yB[i]*(1-pB[i])*alpha


 C[i]<- choice[i]-1       
C[i] ~ dbern(binval[i])
        binval[i] <- 1/(1+exp(va[i]-vb[i]))

    }
}