【发布时间】:2016-02-16 17:46:58
【问题描述】:
我使用通过 rjags 调用的 JAGS 来生成 mcmc.list 对象 foldD_samples,其中包含大量随机节点(>800 个节点)的跟踪监视器。
我现在想使用 R 计算这些节点的相当复杂的标量值函数,并将输出写入 mcmc 对象,以便我可以使用 coda 总结后验并运行收敛诊断。
但是,我无法弄清楚如何将 foldD_samples 中的后绘制图放入数据框中。非常感谢任何帮助。
这是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"
干杯, 雅各布
【问题讨论】:
-
rjags 中可能有方法,但我似乎记得你可以做
do.call(rbind.data.frame, foldD_samples)。或许胖子使用效率更高data.table::rbindlist -
ps 您可以在列表中应用
code.samples,而无需强制转换为数据框 -
感谢 user20650 !
do.call(rbind.data.frame, foldD_samples)运作良好。如果这样发布,我很乐意接受这个作为答案。data.table::rbindlist不接受 mcmc.list 对象作为输入。还要注意后记中“coda”的假定拼写错误“代码”。 -
rbindlist(lapply(foldD_samples,as.data.frame))可能会工作......如果效率对你很重要。
标签: r data-manipulation mcmc jags