我跑进去了JAGS
,我得到了一个巨大的列表(这个例子的命名结果)。runjags
R
每当我访问时,默认石英设备中都会弹出results$density
两个(每个参数一个)。lattice plots
我需要将这些与par(mfrow=c(2, 1))
类似的方法结合起来,并将它们发送到pdf device
.
我试过的都没有用。有任何想法吗?
我试过dev.print
, pdf()
withdev.off()
等,但没有运气。
这是一种通过操纵格子结构来抛弃“V1”面板的方法:
p1 <- results$density$c
p2 <- results$density$m
p1$layout <- c(1,1)
p1$index.cond[[1]] <- 1 # remove second index
p1$condlevels[[1]] <- "c" # remove "V1"
class(p1) <- "trellis" # overwrite class "plotindpages"
p2$layout <- c(1,1)
p2$index.cond[[1]] <- 1 # remove second index
p2$condlevels[[1]] <- "m" # remove "V1"
class(p2) <- "trellis" # overwrite class "plotindpages"
library(grid)
layout <- grid.layout(2, 1, heights=unit(c(1, 1), c("null", "null")))
grid.newpage()
pushViewport(viewport(layout=layout))
pushViewport(viewport(layout.pos.row=1))
print(p1, newpage=FALSE)
popViewport()
pushViewport(viewport(layout.pos.row=2))
print(p2, newpage=FALSE)
popViewport()
popViewport()
一罐 c.trellis() 结果 http://img142.imageshack.us/img142/3272/ctrellisa.png
例如,对于包含的示例run.jags
,请使用以下命令检查列表的结构
sink("results_str.txt")
str(results$density)
sink()
然后你会看到名为layout的组件。可以使用设置每个变量的两个图的布局
results$density$m$layout <- c(1,2)
print(results$density$m)
可以使用包中的c.trellis
方法组合不同参数的图latticeExtra
。
class(results$density$m) <- "trellis" # overwrite class "plotindpages"
class(results$density$c) <- "trellis" # overwrite class "plotindpages"
library("latticeExtra")
update(c(results$density$m, results$density$c), layout=c(2,2))
c.trellis http://img88.imageshack.us/img88/6481/ctrellis.png 的输出
另一种方法是使用grid
视口:
library("grid")
results$density$m$layout <- c(2,1)
results$density$c$layout <- c(2,1)
class(results$density$m) <- "trellis"
class(results$density$c) <- "trellis"
layout <- grid.layout(2, 1, heights=unit(c(1, 1), c("null", "null")))
grid.newpage()
pushViewport(viewport(layout=layout))
pushViewport(viewport(layout.pos.row=1))
print(results$density$m, newpage=FALSE)
popViewport()
pushViewport(viewport(layout.pos.row=2))
print(results$density$c, newpage=FALSE)
popViewport()
popViewport()
组合图的最简单方法是使用存储在 results$mcmc 中的结果:
# prepare data, see source code of "run.jags"
thinned.mcmc <- combine.mcmc(list(results$mcmc),
collapse.chains=FALSE,
return.samples=1000)
print(densityplot(thinned.mcmc[,c(1,2)], layout=c(1,2),
ylab="Density", xlab="Value"))