Первый пример с jags

Создаём реальность!

# скажем knitr'у, что надо кэшировать все куски при повторном запуске всё
# будет быстро!
opts_chunk$set(cache = TRUE)


set.seed(3)  # 3 - Любимое Анино число
n.obs <- 300  # количество наблюдений
chain.len <- 10000  # длина цепи
x <- cos(1:n.obs) * 8
eps <- rnorm(n.obs, mean = 0, sd = 4)
y <- 2 + 3 * x + eps

Оцениваем модель

setwd("~/science/probability/mc201/jags_1/")
library(rjags)
## Loading required package: coda
## Loading required package: lattice
## linking to JAGS 3.2.0
## module basemod loaded
## module bugs loaded
library(ggplot2)
library(ggmcmc)
## Loading required package: plyr
## Loading required package: reshape
## Attaching package: 'reshape'
## The following object(s) are masked from 'package:plyr':
## 
## rename, round_any
## Warning: replacing previous import 'rename' when loading 'reshape'
## Warning: replacing previous import 'round_any' when loading 'reshape'
model <- jags.model("simple_regression.jags", data = list(y = y, x = x, n = n.obs), 
    n.chains = 2, n.adapt = 1000)
## Compiling model graph
##    Resolving undeclared variables
##    Allocating nodes
##    Graph Size: 1211
## 
## Initializing model

result <- coda.samples(model, variable.names = c("beta1", "beta2", "sigma2"), 
    n.iter = 1e+05)

df <- ggs(result)

Строим график апостериорного распределения

ggs_density(df)

plot of chunk unnamed-chunk-3