因为具有所有情节的runjags对象都是too big,所以我尝试用plot=FALSE
进行run.jags
,将结果的runjags
对象保存到文件中,在新的R会话中恢复它(作为out
),然后通过
out.with_summaries <- extend.jags(out, sample = 0, adapt = 0)
(有关此技巧,请参见此处讨论:https://stackoverflow.com/a/21859618/684229)
然而,,由于未知的原因,它再次重新编译和调整模型!,即使我设置了sample = 0, adapt = 0
!
require(runjags)
t1 <- proc.time()
out.sum <- extend.jags(out, sample = 0, adapt = 0)
# Re-compiling rjags model and adapting...
# Calculating the Gelman-Rubin statistic for 4 variables....
# Convergence may have failed for this run for 4 parameters after 500
# iterations (multi-variate psrf = 214.873)
# Finished running the simulation
t2 <- proc.time()
print(t2 - t1)
# user system elapsed
# 345.67 0.08 352.30
仅仅绘制图表就需要相当长的时间,这是相当烦人的。同样的情况是,当我用图解计算runjags对象,然后尝试摆脱它们来存储runjag对象小的时候:
t1 <- proc.time()
out.no_sum <- extend.jags(out.sum, sample = 0, adapt = 0, summarise=FALSE, plot=FALSE)
# Loading required package: rjags
# Loading required package: coda
# Loading required package: lattice
# Linked to JAGS 3.3.0
# Loaded modules: basemod,bugs
# Re-compiling rjags model and adapting...
# Finished running the simulation
t2 <- proc.time()
print(t2 - t1)
# user system elapsed
# 327.53 0.05 329.73
关于如何解决这个问题(除了编写我自己的绘图功能),有什么建议吗?
警告:第二次运行同一个runjags对象上的extend.jags
函数已经很快了。但是,如果您保存runjags对象并在新会话中再次加载它,extend.jags
将再次慢下来。runjags
或JAGS似乎正在缓存某些东西(但不在原始runjags对象中)。
发布于 2014-09-22 00:54:45
这个extend.jags
函数调用很慢,因为模型正在重新编译(在您的例子中,它实际上并没有适应,尽管有一些误导的消息)。这是因为您正在使用保存对象中的rjags方法--这意味着模型必须重新加载到内存中,并准备从内存中进行采样(即使您实际上并不想从它中取样)。第二次调用extend.jags
时不会发生这种情况,因为它已经编译好了。
以这种方式使用extend.jags确实有点麻烦--下一个版本的runjags将提供一种更干净的方法。同时,如果指定adapt=0, sample=0, method='simple'
,则应防止重新编译JAGS对象。
编辑:如runjags的帮助文件中所述,使用lattice::traceplot
或densityplot
(或两者兼而有之)重新创建情节更有效。若要提取MCMC对象,请使用as.mcmc.list(runjags_object)
--这还允许您在必要时提取特定变量,请参阅?as.mcmc.list.runjags。
https://stackoverflow.com/questions/25958736
复制