在 jags / rjags / runjags 中修剪 mcmc.list

trimming mcmc.list in jags / rjags / runjags

我在 R 中将 runjags 模型的输出作为 mcmc.list。下面是生成 3 个 1,000 个样本链的代码。我想 trim 所有 12 个链到最后 400 个样本。我可以拆开链并将链输出的矩阵保存在列表中,但它不再是 mcmc.list 并且我无法弄清楚如何将它变回 mcmc.list.

这里是 运行 runjags 模型的一些数据,并将输出转换为 mcmc.list:

y <- rnorm(100)

jags.model ="
model {
#model
for (i in 1:N){
      y[i] ~ dnorm(y.hat[i], tau) 
  y.hat[i] <- m0
}

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

jags.data <- list(y = y, N = length(y))
jags.out <- runjags::run.jags(jags.model,
                              data = jags.data,
                              n.chains = 3,
                              adapt = 100,
                              burnin = 100,
                              sample = 1000,
                              monitor = c('m0'))
z <- coda::as.mcmc.list(jags.out)

最简单的方法是使用 window:

z2 <- window(z, start=601, end=1000, thin=1)
summary(z2)

另请参阅:

?window.mcmc.list

或者,您可以使用 as.mcmc 和 as.mcmc.list 将您的(列表)截断矩阵转换回 mcmc.list 对象:

library('coda')
z3 <- as.mcmc.list(lapply(z, function(x) as.mcmc(x[601:1000,])))
summary(z3)

但如果我是你,我会坚持使用 window!

马特