我正在尝试使用R包" BRugs"实现Gibbs采样器,但是产生摘要后验统计数据的函数,例如samplesStats(),只返回均值和中位数。是否有可能提取后验模式?
答案 0 :(得分:2)
使用samplesSample
帮助文件中的示例,BRugs
函数将为您提供完整的MCMC,以便进行说明...
### 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)