RJAGSでの正規-正規モデルのシミュレーション

RJAGS によるベイズモデリング

Alicia Johnson

Associate Professor, Macalester College

睡眠研究

$Y_i$ = 3日間の睡眠不足後の反応時間の変化(ms)

$Y_i \sim N(m, s^2)$

RJAGS によるベイズモデリング

事前分布からの示唆

RJAGS によるベイズモデリング

データ(尤度)からの示唆

mean(sleep_study$diff_3)
sd(sleep_study$diff_3)
26.34021
37.20764

データが $Y_i \sim N(m, s^2)$ から生成されると仮定すると、最も起こりやすい値は…

  • $m \approx 26$ ms
  • $s \approx 37$ ms
RJAGS によるベイズモデリング

事後分布からの示唆

RJAGS によるベイズモデリング

正規-正規モデルの定義

sleep_model <- "model{
    # Likelihood model for Y[i]




    # Prior models for m and s


}"  

RJAGS によるベイズモデリング

正規-正規モデルの定義

sleep_model <- "model{
    # Likelihood model for Y[i]




    # Prior models for m and s


}"  

  • $Y_i \sim N(m, s^2)$ ($i = 1,2,\ldots,18$)
RJAGS によるベイズモデリング

正規-正規モデルの定義

sleep_model <- "model{
    # Likelihood model for Y[i]
    for(i in 1:length(Y)) {

    }

    # Prior models for m and s


}"  

  • $Y_i \sim N(m, s^2)$ ($i = 1,2,\ldots,18$)
RJAGS によるベイズモデリング

正規-正規モデルの定義

sleep_model <- "model{
    # Likelihood model for Y[i]
    for(i in 1:length(Y)) {
        Y[i] ~ dnorm(m, s^(-2))
    }

    # Prior models for m and s


}"  

  • $Y_i \sim N(m, s^2)$ ($i = 1,2,\ldots,18$)
    • 注:精度 = 分散$^{-1}$ = $s^{-2}$
RJAGS によるベイズモデリング

正規-正規モデルの定義

sleep_model <- "model{
    # Likelihood model for Y[i]
    for(i in 1:length(Y)) {
        Y[i] ~ dnorm(m, s^(-2))
    }

    # Prior models for m and s
    m ~ dnorm(50, 25^(-2))

}"  

  • $Y_i \sim N(m, s^2)$ ($i = 1,2,\ldots,18$)
    • 注:精度 = 分散$^{-1}$ = $s^{-2}$
  • $m \sim N(50, 25^2)$
RJAGS によるベイズモデリング

正規-正規モデルの定義

sleep_model <- "model{
    # Likelihood model for Y[i]
    for(i in 1:length(Y)) {
        Y[i] ~ dnorm(m, s^(-2))
    }

    # Prior models for m and s
    m ~ dnorm(50, 25^(-2))
    s ~ dunif(0, 200)
}"  

  • $Y_i \sim N(m, s^2)$ ($i = 1,2,\ldots,18$)
    • 注:精度 = 分散$^{-1}$ = $s^{-2}$
  • $m \sim N(50, 25^2)$
  • $s \sim \text{Unif}(0, 200)$
RJAGS によるベイズモデリング

正規-正規モデルのコンパイル

# COMPILE the model    
sleep_jags <- jags.model(textConnection(sleep_model), 
    data = list(Y = sleep_study$diff_3), 
    inits = list(.RNG.name = "base::Wichmann-Hill",
                 .RNG.seed = 1989))
    sleep_study$diff_3
 [1]  71.8798 -18.0269  33.7877 -36.4096  32.5074  74.9082
 [7]  15.9673 -10.8008  29.1938  33.7556  18.8188  -0.7697
[13]  30.0626 125.1784   5.7331  15.2090  11.9091  41.2199
RJAGS によるベイズモデリング

正規-正規モデルのシミュレーション

# COMPILE the model    
sleep_jags <- jags.model(textConnection(sleep_model), 
    data = list(Y = sleep_study$diff_3),
    inits = list(.RNG.name = "base::Wichmann-Hill", 
                 .RNG.seed = 1989))

# SIMULATE the posterior
sleep_sim <- coda.samples(model = sleep_jags, 
    variable.names = c("m", "s"), 
    n.iter = 10000)
RJAGS によるベイズモデリング

正規-正規モデルのシミュレーション

RJAGS によるベイズモデリング

正規-正規モデルのシミュレーション

RJAGS によるベイズモデリング

では、練習しましょう!

RJAGS によるベイズモデリング

Preparing Video For Download...