RJAGS에서 정규-정규 모형 시뮬레이션

RJAGS로 배우는 Bayesian 모델링

Alicia Johnson

Associate Professor, Macalester College

수면 연구

$Y_i$ = 3일간 수면 부족 후 반응 시간 변화(ms)

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

RJAGS로 배우는 Bayesian 모델링

사전 분포에서 얻은 인사이트

RJAGS로 배우는 Bayesian 모델링

데이터(및 가능도)에서 얻은 인사이트

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로 배우는 Bayesian 모델링

사후 분포 인사이트

RJAGS로 배우는 Bayesian 모델링

정규-정규 모형 정의

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




    # Prior models for m and s


}"  

RJAGS로 배우는 Bayesian 모델링

정규-정규 모형 정의

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




    # Prior models for m and s


}"  

  • $Y_i \sim N(m, s^2)$ ($i$ in ${1,2,\ldots,18}$)
RJAGS로 배우는 Bayesian 모델링

정규-정규 모형 정의

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$ in ${1,2,\ldots,18}$)
RJAGS로 배우는 Bayesian 모델링

정규-정규 모형 정의

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$ in ${1,2,\ldots,18}$)
    • 참고: 정밀도 = 분산$^{-1}$ = $s^{-2}$
RJAGS로 배우는 Bayesian 모델링

정규-정규 모형 정의

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$ in ${1,2,\ldots,18}$)
    • 참고: 정밀도 = 분산$^{-1}$ = $s^{-2}$
  • $m \sim N(50, 25^2)$
RJAGS로 배우는 Bayesian 모델링

정규-정규 모형 정의

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$ in ${1,2,\ldots,18}$)
    • 참고: 정밀도 = 분산$^{-1}$ = $s^{-2}$
  • $m \sim N(50, 25^2)$
  • $s \sim \text{Unif}(0, 200)$
RJAGS로 배우는 Bayesian 모델링

정규-정규 모형 컴파일

# 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로 배우는 Bayesian 모델링

정규-정규 모형 시뮬레이션

# 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로 배우는 Bayesian 모델링

정규-정규 모형 시뮬레이션

RJAGS로 배우는 Bayesian 모델링

정규-정규 모형 시뮬레이션

RJAGS로 배우는 Bayesian 모델링

연습해 봅시다!

RJAGS로 배우는 Bayesian 모델링

Preparing Video For Download...