简体   繁体   English

从R中的runjags对象中删除一个链

[英]Remove a chain from a runjags object in R

I have a runjags object that has two chains that mixed very well (chains 1 and 3), and one that did not (chain 2). 我有一个runjags对象,有两个链很好地混合(链1和3),一个没有(链2)。 How can I go about trimming the runjags object to just contain chains 1 and 3? 我怎样才能修改runjags对象只包含链1和3?

Here is a reproducible example of generating a JAGS model using runjags (although, the chains here mix fine). 这是一个使用runjag生成JAGS模型的可重现示例(尽管这里的链条混合得很好)。

library(runjags)

#generate the data
x <- seq(1,10, by = 0.1)
y <- x + rnorm(length(x))

#write a jags model
j.model = "
model{
#this is the model loop.
for(i in 1:N){
y[i] ~dnorm(y.hat[i], tau)
y.hat[i] <- m*x[i]
}

#priors
m ~ dnorm(0, .0001)
tau <- pow(sigma, -2)
sigma ~ dunif(0, 100)
}
"

#put data in a list.
data = list(y=y, x=x, N=length(y))

#run the jags model.
jags.out <- run.jags(j.model,
                     data = data,
                     n.chains=3,
                     monitor=c('m'))

One way to achieve this is to convert the runjags object to a mcmc.list , then remove the chain using the following code: 实现此目的的一种方法是将runjags对象转换为mcmc.list ,然后使用以下代码删除链:

trim.jags <- as.mcmc.list(jags.out)
trim.jags <- mcmc.list(trim.jags[[1]], trimjags[[3]])

However, once converted in this direction, the data cannot be put back into the runjags format. 但是,一旦在这个方向转换,数据就不能放回runjags格式。 I would really like a solution that keeps the output in the runjags format, as my current workflows rely on that formatting generated by the runjags summary output. 我真的想要一个将输出保持为runjags格式的解决方案,因为我当前的工作流程依赖于runjags摘要输出生成的格式。

Take a look at the (admittedly not very obviously named) divide.jags function: 看一下(肯定不是很明显的名字)divide.jags函数:

jags_13 <- divide.jags(jags.out, which.chains=c(1,3))                     
jags_13
extend.jags(jags_13)
# etc

Hopefully this does exactly what you want. 希望这完全符合您的要求。

Matt 马特

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

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