More Related Content Similar to StanTutorial (7) StanTutorial26. ラットデータ
• WinBUGS example volume Iに
入っているサンプルデータ
(http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/Vol1.pdf)
•
•
•
•
Gelfand et al (1990)
週別の生後間もないラットの体重のモデル
行:個別のラット(N=30)
列:体重計測日(M=5)
From http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/Vol1.pdf
27. データを用意します。
rats.data.R
N <-30
T <-5
y <-structure(c(151, 145, 147, 155, 135, 159, 141, 159, 177, 134,
160, 143, 154, 171, 163, 160, 142, 156, 157, 152, 154, 139, 146,
157, 132, 160, 169, 157, 137, 153, 199, 199, 214, 200, 188, 210,
189, 201, 236, 182, 208, 188, 200, 221, 216, 207, 187, 203, 212,
203, 205, 190, 191, 211, 185, 207, 216, 205, 180, 200, 246, 249,
263, 237, 230, 252, 231, 248, 285, 220, 261, 220, 244, 270, 242,
248, 234, 243, 259, 246, 253, 225, 229, 250, 237, 257, 261, 248,
219, 244, 283, 293, 312, 272, 280, 298, 275, 297, 350, 260, 313,
273, 289, 326, 281, 288, 280, 283, 307, 286, 298, 267, 272, 285,
286, 303, 295, 289, 258, 286, 320, 354, 328, 297, 323, 331, 305,
338, 376, 296, 352, 314, 325, 358, 312, 324, 316, 317, 336, 321,
334, 302, 302, 323, 331, 345, 333, 316, 291, 324), .Dim = c(30,
5))
x <- c(8.0, 15.0, 22.0, 29.0, 36.0)
ファイルの場所
xbar <-22.0
<Rのライブラリーインストール先>rstan¥include¥
stansrc¥models¥bugs_examples¥vol1¥rats
30. Stanプログラムはブロックからなる
• Stanの骨子となるプログラム
• modelブロック以外はオプショナル
この順は
固定
data {
model {
... declarations ...
... declarations ... statements ...
}
}
transformed data {
generated quantities {
... declarations ... statements ...... declarations ... statements ...
}
}
parameters {
... declarations ...
}
transformed parameters {
... declarations ... statements ...
}
31. Stan Program has specific blocks.
data
• 入力データの宣言、デー
タは最初にロードされる
transformed
data
• データに対してStan側で
変換を行う
parameters
• 結果として出力するパラ
メータを宣言する。
34. Stan Program has specific blocks.
Transformed
• パラメータを変換する
Parameters
Model
Generated
Quantities
• モデル記述部、必須
• 全ての変数を参照可能。1
サンプルにつき実行される。
35. transfromed parametersブロック
transformed parameters {
分散を標準偏差
real<lower=0> sigma_y; // sigma in original…
に変換
real<lower=0> sigma_alpha;
real<lower=0> sigma_beta;
sigma_y <- sqrt(sigmasq_y);
sigma_alpha <- sqrt(sigmasq_alpha);
sigma_beta <- sqrt(sigmasq_beta);
}
36. modelブロック
model {
超パラメータを
mu_alpha ~ normal(0, 100);
サンプリング
mu_beta ~ normal(0, 100);
sigmasq_y ~ inv_gamma(0.001, 0.001);
sigmasq_alpha ~ inv_gamma(0.001, 0.001);
sigmasq_beta ~ inv_gamma(0.001, 0.001);
alpha ~ normal(mu_alpha, sigma_alpha); // vectorized
超パラメータを
beta~ normal(mu_beta, sigma_beta); // vectorized
用いてパラメータ
for (n in 1:N)
をサンプリング
for (t in 1:T)
y[n,t] ~ normal(alpha[n] + beta[n] * (x[t] - xbar), sigma_y);
}
個体差を含む体重に誤差を加えてサンプリング
39. Rでの実行
#set to dir which contains source file
STAN_HOME<-<STAN_HOME>
dirpath<-paste0(STAN_HOME, path_to_dir)
#load data to list:dat
データはリス
source(paste0(dirpath, "/rats.data.R"))
ト形式でStan
dat<-list(y=y, x=x, xbar=xbar, N=N, T=T)
に渡せる。
#fit1:to simulate the model as one liner
fit1 <- stan(file = paste0(dirpath, "/rats.stan"), data = dat,
iter = 1000, chains = 4)
apply(extract(fit1)$alpha,2, median)
Stanfitクラス、Stanでの実行結果が返される
42. 前回のモデルを利用
Once a model is fitted, we can use the fitted
result as an input to fit the model with other
data or settings. This would save us time of
compiling the C++ code for the model
https://code.google.com/p/stan/wiki/RStanGettingStarted
#fit again using the previous fit result
fit3<-stan(fit=fit1, data = dat, iter = 400, chains = 4)