RBi 技术文档
最近发现了一个包libbi(Library For Bayesian inference )[ http://libbi.org ] 特别喜欢(
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查看