简单介绍使用 JAGS 进行蒙特卡罗模拟

本文介绍了如何使用JAGS进行蒙特卡罗模拟,讲解了使用R2jags包来实现贝叶斯模型的主要步骤,并通过一系列示例,包括硬币翻转、药物试验等,逐步展示模型构建、参数估计和诊断。同时强调了初始值设置、链的收敛性和多链使用的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction to Monte Carlo simulation using JAGS

This practical gives an introduction to the Monte Carlo method using JAGS (Just Another Gibbs Sampler). First we have to install some R packages that act as frontenda for JAGS:

  • R2jags allows fitting JAGS models from within R (depend on rjags);
  • coda provides functions for output analysis and diagnostics for MCMC;
  • lattice provides functions for visualisation of an MCMC object;
  • MCHCvis in an R packages used to visualise, manipulate, and summarise MCMC output.
install.packages('R2jags', dependencies = TRUE) # install coda too
# install,packages('coda', dependencies = TRUE)
install.packages('lattice', dependencies = TRUE)
install.packages('MCMvis', dependencies = TRUE)
library(R2jags)
library(MCMCvis)
library(coda)
library(lattice)

Introduce

The main steps of fitting a Baysian model using R2jags are the following:

  1. Define the JAGS model (as a function);
  2. Prepare the data , which should be passed on as a list. Note that this list should include other known variables not in the data, like the length N of the data vector, when this N is used in the JAGS model definition (e.g. in a for loop);
  3. Identify the parameters whose posterior distribution are of interest.
  4. Define starting values for JAGS. We need starting values for all the stochastic nodes (unknoen parameters, missing data) in each chain. Initial values should be passed on as a list. Note that JAGS runs without user-defined starting values, in which cases it generates its own initial values. But this is not recommended, especially for complex models;
  5. Fit the model in JAGS.
    The most important arguments of the #jags# function includes:
    data (data list), inits (list of initial values),
    parameters.to.save (parameters of interest),
    n.chains (number of MC chains - each gives a sequence of simulated values),
    n.iter (number of iterations in the sampling process),
    n.burnin (number of initial samples to discard),
    model.file (the JAGS model definition);
  6. Convergence diagnostics. Update if convergence hasn’t reached;
  7. Once we are satisfied that the chains have converged, we can start the statistical inference. E.g. we can plot the posterior density, get point estimates and interval estimates by extracting the posterior mean and credible intervals.

To demonstrate the structure of a Baysian model fit using R2jags we will consider some examples with increasing complexity.
For reproducibility we should always set the seed, e.g. set.seed(45621)

Coin example

Suppose we want to known the probability of getting 8 or more heads when we toss a fair coin 10 times.
When we toss a fair coin 10 times, the number of heads Y follows a binomial distribution with parameters n=10 and p=0.5. We are interested in P ( Y > = 8 ) P(Y>=8) P(Y>=8).
First we represent this model using the BUGS language. Recall that ~ is used to represent stochastic dependent, and <- is used to represent logical dependence.

# model definition
jags.model.coin<- function(){
    Y ~ dbin(0.5,10) # our data model
    P8 <- ifelse(Y>7, 1, 0) # the probability of interest
}

For each iteration a sample from B i n o m ( 10 , 0.5 ) Binom(10,0.5) Binom(10,0.5) is drawn, and it’s checked whether this sample is strictly greater than 7 or not (since the distribution is discrete, this is equivalent to > = 8 >=8 >=8). For example, a run of length five could give us

Y = ( 5 , 5 , 8 , 4 , 9 ) Y = (5,5,8,4,9) Y=(5,5,8,4,9)
P 8 = ( 0 , 0 , 1 , 0 , 1 ) P8 = (0,0,1,0,1) P8=(0,0,1,0,1)

In this simple example we have no data, and since this is a Monte Carlo sampling froma known distribution, there is no real need to provide initial va

Doing Bayesian Data Analysis: A Tutorial with R, JAGS, and Stan, Second Edition provides an accessible approach for conducting Bayesian data analysis, as material is explained clearly with concrete examples. Included are step-by-step instructions on how to carry out Bayesian data analyses in the popular and free software R and WinBugs, as well as new programs in JAGS and Stan. The new programs are designed to be much easier to use than the scripts in the first edition. In particular, there are now compact high-level scripts that make it easy to run the programs on your own data sets. The book is divided into three parts and begins with the basics: models, probability, Bayes’ rule, and the R programming language. The discussion then moves to the fundamentals applied to inferring a binomial probability, before concluding with chapters on the generalized linear model. Topics include metric-predicted variable on one or two groups; metric-predicted variable with one metric predictor; metric-predicted variable with multiple metric predictors; metric-predicted variable with one nominal predictor; and metric-predicted variable with multiple nominal predictors. The exercises found in the text have explicit purposes and guidelines for accomplishment. This book is intended for first-year graduate students or advanced undergraduates in statistics, data analysis, psychology, cognitive science, social sciences, clinical sciences, and consumer sciences in business. Accessible, including the basics of essential concepts of probability and random sampling Examples with R programming language and JAGS software Comprehensive coverage of all scenarios addressed by non-Bayesian textbooks: t-tests, analysis of variance (ANOVA) and comparisons in ANOVA, multiple regression, and chi-square (contingency table analysis) Coverage of experiment planning R and JAGS computer programming code on website Exercises have explicit purposes and guidelines for accomplishment Provides step-by-step instructions on how to conduct Bayesian data analyses in the popular and free software R and WinBugs
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值