gpt4 book ai didi

r - 如何在 BRugs 中找到后验模式

转载 作者:行者123 更新时间:2023-12-02 01:52:31 25 4
gpt4 key购买 nike

我正在尝试使用 R 包“BRugs”来实现 Gibbs 采样器,但是生成后验汇总统计的函数(例如 samplesStats())仅返回均值和中位数。是否可以提取后验模态?

最佳答案

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

关于r - 如何在 BRugs 中找到后验模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21868230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com