我正在尝试使用 R 包“BRugs”来实现 Gibbs 采样器,但是产生汇总后验统计数据的函数,例如 samplesStats(),只返回平均值和中位数。是否可以提取后验模式?
问问题
874 次
1 回答
2
该功能将为您提供完整的 MCMC,以使用帮助文件samplesSample
中的示例进行说明...BRugs
### Step by step example: ###
library("BRugs") # loading BRugs
## Prepare the example files in a temporary directory
exfiles <- dir(options()$OpenBUGSExamples, pattern="^Rats.*txt$", full.names=TRUE)
ok <- file.copy(exfiles, tempdir())
## Now setting the working directory to the temporary one:
oldwd <- setwd(tempdir())
## some usual steps (like clicking in WinBUGS):
modelCheck("Ratsmodel.txt") # check model file
modelData("Ratsdata.txt") # read data file
modelCompile(numChains=2) # compile model with 2 chains
modelInits(rep("Ratsinits.txt", 2)) # read init data file
modelUpdate(1000) # burn in
samplesSet(c("alpha0", "alpha")) # alpha0 and alpha should be monitored
modelUpdate(1000) # 1000 more iterations ....
可以提取 MCMC 样本,比如alpha
节点,并通过以下方式对它做任何你想做的事情,
alpha0<-samplesSample("alpha0")
hist(alpha0)
于 2014-02-19T10:01:29.363 回答