2014-02-19 48 views
3

我想使用R包「BRugs」來實現Gibbs採樣器,但是生成摘要後驗統計的函數(如samplesStats())僅返回平均值和中位數。是否有可能提取後驗模式?如何在BRugs中找到後驗模式

+2

如果您保存了MCMC輸出(原始MCMC樣本 - 你米ight需要在BRugs的調用中指定你想要這些保存...?),那麼你可以使用'density()'來找到一個近似模式。也就是說,'密度(樣本)$ x [密度(樣本)$ y ==最大(密度(樣本)$ y)]''。如果有多個參數要估算模式,請將樣本排列在數組中,並使用上面的代碼'apply'。 –

+1

或'dd < - 密度(樣本); DD $ X [which.max(DD $ Y)]' –

回答

2

samplesSample功能會給你充分MCMC,說明,使用在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) 

enter image description here