简体   繁体   English

Rjags中的分层混合模型

[英]Hierarchical Mixture Model in Rjags

This is my first time using rjags, and I'm trying to fit some count data Y. I'm using a hierarchical mixture model as follows: 这是我第一次使用rjags,我正在尝试拟合一些计数数据Y。我正在使用如下的分层混合模型:

Y ~ p*Poisson(N*lambda1) + (1-p)*Poisson(N*lambda2)
lambda1 ~ Gamma(a,b)
lambda2 ~ Lognormal(c,d)
a ~ Gamma(1,1)
b ~ Gamma(1,1)
c ~ Normal(0,1)
d ~ Gamma(1,1)

Here, Y is the count data that I observe, and N is known. 在这里,Y是我观察到的计数数据,而N是已知的。

I wrote up a simple rjags model that I've been playing around with. 我写了一个我一直在玩的简单的Rjags模型。 However, when testing on simple simulated data, I'm getting really bad results. 但是,在对简单的模拟数据进行测试时,我得到的结果确实很差。 Here is my code to generate the simulated data and run the model: 这是我的代码,用于生成模拟数据并运行模型:

a <- 0.5
b <- 0.5
c <- -10
d <- 1

lambda1 <- rgamma(30,a,b)
lambda2 <- rlnorm(70,c,d)
counts <- rpois(100,1000*c(lambda1,lambda2))

model_string <- "model{
  # Likelihood 
  for (i in 1:n) {
    mu1[i] <- N*lambda1[i]
    mu2[i] <- N*lambda2[i]
    lambda1[i] ~ dgamma(a,b)
    lambda2[i] ~ dlnorm(c,d)
    m[i] ~ dcat(mprior[])
    mu[i] <- equals(m[i],1)*mu1[i] + equals(m[i],2)*mu2[i]
    Y[i] ~ dpois(mu[i])
  }
  # Prior
  mprior[1] <- 0.5
  mprior[2] <- 0.5
  a ~ dgamma(1,1)
  b ~ dgamma(1,1)
  c ~ dnorm(0,1)
  d ~ dgamma(1,1)
}"

model <- jags.model(textConnection(model_string), 
                    data = list(Y=counts,N=1000,n=100))
update(model,10000)
samp <- coda.samples(model, 
                     variable.names=c("a","b","c","d","m"), 
                     n.iter=20000)

print(colMeans(samp[[1]])[1:4])

After running this, the posterior estimates of a, b, c, d are quite poor, and the component assignments m also don't match up well with the true assignments. 运行此命令后,a,b,c,d的后验估计非常差,并且组件分配m与真实分配也不太匹配。 I also notice that plotting the chains don't look great, even when increasing the number of iterations. 我还注意到,即使增加迭代次数,绘制链看起来也不是很好。

Any advice? 有什么建议吗? I'm not sure if I'm fitting the mixture in the best way. 我不确定我是否以最佳方式拟合混合物。 I'm also definitely open to changing my priors on a, b, c, d if there are other distributions that will be easier to work with. 如果还有其他更易于使用的发行版,我也绝对愿意更改a,b,c,d上的先验。

In your JAGS model lambda depends on i, which probably was not your intention. 在您的JAGS模型中,lambda取决于i,这可能不是您的意图。 The model can not estimate parameters using such a definition as basically there are too many lambdas or put another way each draw follows it's own distribution. 该模型无法使用这样的定义来估计参数,因为基本上有太多的lambda,或者换句话说每个绘图遵循其自己的分布。

Maybe model should look more like 也许模型应该看起来更像

lambda1 ~ dgamma(a,b)
lambda2 ~ dlnorm(c,d)

Or if you really want to have separate lambdas, then you would need many more draws per lambda for posterior distribution to make sense. 或者,如果您真的想拥有单独的lambda,则每个lambda都需要更多次抽奖,以便进行后验分配。 Right now you are just estimating with one data point per lambda. 现在,您仅估算每个lambda一个数据点。

Same applies to categorical distribution you use for the mixture. 同样适用于您用于混合物的分类分布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM