JAGS中的逻辑回归

时间:2016-07-10 04:19:20

标签: bayesian jags

我是贝叶斯分析的新手。我有一个带有二元响应变量的分层模型。只有一个预测变量(分类),它有3个级别:HLL,LHL和LLL。我通过虚拟编码所有这些级别来准备我的数据文件。我的型号规格如下:

cat("

model{
  for(i in 1:Ny){
    y[i] ~ dbern(mu[s[i]])
  }
for(j in 1:Ns){
mu[j] <- ilogit(b0[j] + b1*HLL[j] + b2*LHL[j] + b3*LLL[j])

b0[j] ~ dnorm(mu0, sigma0)
b1[j] ~ dnorm(mu1, sigma1)
b2[j] ~ dnorm(mu2, sigma2)
b3[j] ~ dnorm(mu3, sigma3)

  }

mu0 ~ dnorm(0, 0.001)
sigma0 ~ dunif(0, 100)

mu1 ~ dnorm(0, 0.001)
sigma1 ~ dunif(0, 100)

mu2 ~ dnorm(0, 0.001)
sigma2 ~ dunif(0, 100)

mu3 ~ dnorm(0, 0.001)
sigma3 ~ dunif(0, 100)
}
", fill = TRUE, file = "generalModel.txt")

错误

基本上,我想得到HLL和LHL的估计值(使用LLL作为参考级别)。这个模型没有运行,我不知道为什么。这是错误消息:

Calling 3 simulations using the parallel method...
Following the progress of chain 1 (the program will wait for all chains to finish
before continuing):
Welcome to JAGS 4.2.0 on Sun Jul 10 00:10:00 2016
JAGS is free software and comes with ABSOLUTELY NO WARRANTY
Loading module: basemod: ok
Loading module: bugs: ok
. . Reading data file data.txt
. Compiling model graph
   Resolving undeclared variables
   Allocating nodes
RUNTIME ERROR:
Invalid vector argument to ilogit
Deleting model
. Reading parameter file inits1.txt
Can't set RNG name. No model!
Can't set initial values. No model!
. Can't initialize. No model!
. Adaptation skipped: model is not in adaptive mode.
. Updating 1000
-------------------------------------------------| 1000
Can't update. No model!

. Can't set monitor. No model!
. Can't set monitor. No model!
. Can't set monitor. No model!
. Can't set monitor. No model!
. Updating 10000
-------------------------------------------------| 10000
Can't update. No model!

. No model
. Can't dump CODA output. No model!
. Can't dump samplers. No model!
. Updating 0
Can't update. No model!
Can't update. No model!
. Deleting model
. 
All chains have finished
Note: the model did not require adaptation
Error in runjags.readin(directory = startinfo$directory, silent.jags = silent.jags,  : 
  All the simulations appear to have crashed - check the model output in failed.jags() for clues
In addition: Warning messages:
1: No initial values were provided - JAGS will use the same initial values for all chains 
2: You attempted to start parallel chains without setting different PRNG for each chain, which is not recommended.  Different .RNG.name values have been added to each set of initial values. 
Note: Either one or more simulation(s) failed, or there was an error in processing
the results.  You may be able to retrieve any successful simulations using:
results.jags("/private/var/folders/nv/gznh75k93cv1wp35q1hvkkg00000gn/T/RtmpYRkQYd/runjagsfiles7c8d79109b99",
recover.chains=TRUE)
See the help file for that function for possible options.
To remove failed simulation folders use cleanup.jags() - this will be run
automatically when the runjags package is unloaded

仅限拦截模型正常工作

我成功运行了与上面相同的仅限拦截模型。在这种情况下,我运行一个模型,比如说只使用LLL而另一个模型只使用HLL。然后我绘制了两个后验的差异,结果几乎与glmer()模型中HLL的估计一致,其中LLL是ref。水平。

cat("

model{
  for(i in 1:Ny){
    y[i] ~ dbern(mu[s[i]])
  }
  for(j in 1:Ns){
    mu[j] <- ilogit(b0[j])
    b0[j] ~ dnorm(mu0, sigma)
  }

  mu0 ~ dnorm(0, 0.001)
  sigma ~ dunif(0, 100)
}
", fill = TRUE, file = "modelA.txt")

有什么想法吗?谢谢!

2 个答案:

答案 0 :(得分:4)

输出的重要部分是这个错误:

RUNTIME ERROR:
Invalid vector argument to ilogit

这来自您模型的以下部分:

mu[j] <- ilogit(b0[j] + b1*HLL[j] + b2*LHL[j] + b3*LLL[j])

参数b1,b2和b3在其下方的循环中由j索引,因此b1 * HLL [j]产生向量(长度为Ns),这是ilogit无法处理的。你可能想在这一行中用j索引b1等,即:。

mu[j] <- ilogit(b0 + b1[j]*HLL[j] + b2[j]*LHL[j] + b3[j]*LLL[j])

请注意,我已经将索引放在b0上以获得固定的截距,否则模型可能不会快速收敛,但是一旦模型的其余部分工作,您可以随后将其添加回来。这个和你想要的东西之间可能存在其他差异,但是如果没有更多关于你想要做什么的信息,我很难说。例如,您可能不想用b来索引b0,b1,b2和b3,而是想要在循环外定义单个b0,b1,b2和b3 - 这不会虽然是分层的,你说你想要的。

其他几点:

1)我会非常小心以下类型的先验:

sigma0 ~ dunif(0, 100)

对于sigma0来说,这可能是一个非常有用的信息,并且倾向于将参数拉向更高的值。

2)确保加载GLM模块 - 这将允许块更新参数,这将改善收敛性

3)这是一个更小的观点,但写作更为习惯:

logit(mu[j]) <- ....

比:

mu[j] <- ilogit(...)

除了使您更清楚地知道您正在编写GLM之外,它还会更改错误消息JAGS为您提供的内容可能会为您提供有关编码错误原因的更多线索。

答案 1 :(得分:3)

另外(在上一个答案的扩展中),请注意JAGS中标准偏差的参数化。 dnorm是通过精度而非标准偏差来参数化的。这样写得更清楚(IMO):

b~dnorm(mu,1 / sigma ^ 2)#Notice 1 / sigma ^ 2,not sigma

sigma~dunif(0,100)

通常将统一先验放在sigma上,而不是精确度。

[想把它添加为评论而不是答案,但系统不会让我。]