简体   繁体   English

修剪mcmc.list中的尖齿/ rjags / runjags

[英]trimming mcmc.list in jags / rjags / runjags

I have the output of a runjags model in R as an mcmc.list . 我在R中有一个runjags模型的输出作为mcmc.list Below is code to generate 3 chains of 1,000 samples. 以下是生成3个链的1,000个样本的代码。 I'd like to trim all 12 chains to the last 400 samples. 我想将所有12条链修剪到最后400个样本。 I can pull apart the chains and save matrices of chain output in a list, but its no longer an mcmc.list and I can't figure out how to turn it back into an mcmc.list. 我可以拆开链并将链输出矩阵保存在列表中,但是它不再是mcmc.list而且我不知道如何将其转换回mcmc.list。

Here are some data to run a runjags model, and convert the output to a 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)

The easiest way to do this is using window: 最简单的方法是使用window:

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

See also: 也可以看看:

?window.mcmc.list

Alternatively, you could use as.mcmc and as.mcmc.list to convert your (list of) truncated matrices back into an mcmc.list object: 另外,您可以使用as.mcmc和as.mcmc.list将截断矩阵的列表转换回mcmc.list对象:

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

But I'd stick with using window if I were you! 但是如果我是你,我会坚持使用窗户!

Matt 马特

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

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