[英]Manipulating mcmc.list object in R
I have used JAGS called via rjags to produce the mcmc.list object foldD_samples, which contains trace monitors for a large number of stochastic nodes (>800 nodes). 我使用了通过rjags调用的JAGS来生成mcmc.list对象foldD_samples,其中包含大量随机节点(> 800个节点)的跟踪监视器。
I would now like to use R to compute a fairly complicated, scalar-valued function of these nodes, and write the output to an mcmc object so that I can use coda to summarize the posterior and run convergence diagnostics. 我现在想用R来计算这些节点的相当复杂的标量值函数,并将输出写入mcmc对象,这样我就可以使用coda来总结后验和运行收敛诊断。
However, I haven't been able to figure out how get the posterior draws from foldD_samples into a dataframe. 但是,我无法弄清楚如何将后验从foldD_samples绘制到数据帧中。 Any help much appreciated.
任何帮助非常感谢。
Here is the structure of the mcmc.list: 这是mcmc.list的结构:
str(foldD_samples)
List of 3
$ : mcmc [1:5000, 1:821] -0.667 -0.197 -0.302 -0.204 -0.394 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:821] "beta0" "beta1" "beta2" "dtau" ...
..- attr(*, "mcpar")= num [1:3] 4100 504000 100
$ : mcmc [1:5000, 1:821] -0.686 -0.385 -0.53 -0.457 -0.519 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:821] "beta0" "beta1" "beta2" "dtau" ...
..- attr(*, "mcpar")= num [1:3] 4100 504000 100
$ : mcmc [1:5000, 1:821] -0.492 -0.679 -0.299 -0.429 -0.421 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:821] "beta0" "beta1" "beta2" "dtau" ...
..- attr(*, "mcpar")= num [1:3] 4100 504000 100
- attr(*, "class")= chr "mcmc.list"
Cheers, Jacob 干杯,雅各布
As it's a list
structure you can use either of these methods to bind the matrices together. 由于它是一个
list
结构,您可以使用这些方法中的任何一种将矩阵绑定在一起。
do.call(rbind.data.frame, foldD_samples)
or 要么
rbindlist(lapply(foldD_samples, as.data.frame)) # thanks to BenBolker
A mwe 一个mwe
library(rjags)
library(coda)
library(data.table)
mod <- textConnection("model {
A ~ dnorm(0, 1)
B ~ dnorm(0, 1)
}")
# evaluate
mod <- jags.model(mod, n.chains = 4, n.adapt = 50000)
pos <- coda.samples(mod, c("A", "B"), 10000)
out <- do.call(rbind.data.frame, pos)
out2 <- rbindlist(lapply(pos, as.data.frame))
all.equal(out, out2, check.attributes=FALSE)
The answer given by user20650 will certainly work, but it can be quite slow. user20650给出的答案肯定会有效,但可能会很慢。 Also note that as of this writing, rbind_list() is deprecated in place of bind_rows().
另请注意,在撰写本文时,不推荐使用rbind_list()代替bind_rows()。
Something I've written for my own purposes converts the mcmc.list to a "long format" data.frame. 我为自己的目的编写的东西将mcmc.list转换为“长格式”data.frame。 On my machine, it is about 4-7x faster than the above methods, and adds two additional columns: one for the chain number, and one for the step number.
在我的机器上,它比上述方法快4-7倍,并增加了两列:一列用于链号,一列用于步号。
parameter_names <- varnames(mcmc_list)
saved_steps <- as.integer(row.names(mcmc_list[[1]]))
out <- data.frame("chain" = factor(rep(1 : length(mcmc_list), each = length(saved_steps))),
"step" = rep(saved_steps, length(mcmc_list)) )
for (param in parameter_names) {
out[param] <- NA
}
for (a_chain in 1 : length(mcmc_list)) {
out[out$chain == a_chain, parameter_names ] <- as.data.frame(mcmc_list[[a_chain]])
}
return(out)
Working with an mcmc.list object of 3 chains, 50,000 rows total, rbind_list/do.call method: 0.71 sec avg elapsed time my method: 0.12 sec avg elapsed time 使用3个链的mcmc.list对象,总共50,000行,rbind_list / do.call方法:0.71秒平均耗时我的方法:0.12秒平均耗时
Edit: some further reading of code in the library "coda" shows that as.matrix() is much faster. 编辑:进一步阅读库“coda”中的代码表明as.matrix()要快得多。
parameter_names <- varnames(mcmc_list)
saved_steps <- as.integer(row.names(mcmc_list[[1]]))
out <- data.frame("chain" = factor(rep(1 : length(mcmc_list), each = length(saved_steps))),
"step" = rep(saved_steps, length(mcmc_list)) )
out <- cbind(out, as.data.frame(as.matrix(chain_samples)))
Takes on overage 0.03 seconds elapsed time. 超过0.03秒消耗时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.