RBi Package---技术文档学习

RBi包是用于与LibBi进行交互的R语言工具,支持加载、运行和分析状态空间模型。它提供了bi_model和libbi两个类,用于模型管理和运行推断。包内提供了bi_read和bi_write方法来处理NetCDF文件,以及get_traces函数分析MCMC轨迹。通过libbi类,可以进行模型的采样、拟合和预测。例如,SIR模型可以通过bi_generate_dataset生成数据,使用PMCMC方法进行参数估计。此外,RBi还支持保存和加载libbi对象,便于后续分析。
摘要由CSDN通过智能技术生成


最近发现了一个包libbi(Library For Bayesian inference )[ http://libbi.org ] 特别喜欢( lunwenzhizaojiqi ),它支持R接口😃,遂找到R的相关文档,学习了一下(在R中为RBi package),以下是简单的翻译记录,文中所有代码都已运行实现,进一步的使用需要学习更多案例。

LibBi is used for Bayesian inference over state-space models, including simulation, filtering and smoothing for state estimation, and optimisation and sampling for parameter estimation.

更多细节请看网址:
http://ydl.oregonstate.edu/pub/cran/web/packages/rbi/vignettes/introduction.html

1、Getting started

The RBi package requires R (>= 3.2.0) as well as the packages:

  • reshape2
  • ncdf4
  • data.table

The easiest way to install the latest stable version of RBi is via CRAN. The package name is rbi (all lowercase):

install.packages('rbi')
library('rbi')

The RBi package mainly provides two classes: bi_model and libbi. The bi_model class is used to load, view and manipulate LibBi model files. The libbi class is used to run LibBi and perform inference.

RBi包主要提供两类:bi_model和libbi。bi_model类被用于加载,预览,操作LibBi模型文件。libbi类被用于运行LibBi和运行推断。

The package also provides two methods for interacting with the NetCDF files used by LibBi, bi_read and bi_write. Lastly, it provides a get_traces function to analyse Markov-chain Monte Carlo (MCMC) traces using the coda package.

这个包还提供了与LibBi使用的NetCDF文件交互的两种方法:bi_read和bi_write。最后,它提供了一个get_traces函数来使用coda包分析Markov-chain Monte Carlo (MCMC)跟踪。

2、The bi_model class

As an example, we consider a simplified version of the SIR model discussed in Del Moral et al. (2014). This is included with the RBi package and can be loaded with
作为一个例子,我们考虑Del Moral等人(2014)中讨论的SIR模型的简化版本。这包含在RBi包中,可以加载

model_file <- system.file(package="rbi", "SIR.bi")
SIRmodel <- bi_model(model_file) # load model

Other ways of implementing a (deterministic or stochastic) SIR model can be found in the collection of SIR models for LibBi, where you also find how to load them into a bi_model object, e.g. SIRmodel. Feel free to run the commands below with different versions of the model.
其他实现(确定性或随机)SIR模型的方法可以在LibBi的SIR模型集合中找到,在那里你还可以找到如何将它们加载到bi_model对象中,例如SIRmodel。请随意使用模型的不同版本运行下面的命令。

加载下面命令可以看到模型详细说明:

SIRmodel

get_block()获取函数块

get_block(SIRmodel, "parameter")

## [1] "p_rep ~ uniform(0,1)" "p_R0 ~ uniform(1,3)"

var_names() 获取变量名

var_names(SIRmodel, type="state")

## [1] "S" "I" "R" "Z"

The fix method fixes a variable to one value. This can be useful, for example, to run the deterministic equivalent of a stochastic model for testing purposes:

fix方法将一个变量固定为一个值。这是有用的,例如,为了测试目的运行随机模型的确定性等价:

det_SIR_model <- fix(SIRmodel, n_transmission=0, n_recovery=0)

3、Generating a dataset

首先从SIR模型中创建一个数据集

set.seed(1001912)
SIRdata <- bi_generate_dataset(SIRmodel, end_time=16*7, 
noutputs=16)

This simulates the model a single time from time 0 until time 16*7 (say, 16 weeks with a daily time step), producing 16 outputs (one a week)

模拟模型单个时间从0到时间16*7,(16周以天为时间步骤),产生16个输出(一周)

Also note that LibBi compiles the model code only the first time it is run. If you run the command above a second time, it should run much faster.

还要注意,LibBi只在模型代码第一次运行时才编译它。如果您第二次运行上述命令,它应该会运行得快得多。

The bi_generate_dataset function returns a libbi object:

The generated dataset can be viewed and/or stored in a variable using bi_read:

生成的数据集可以使用bi_read查看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值