37.
RStan
• Rstan・・・RのStanインターフェース
– C++への変換、コンパイルから、実行までを担当
– 結果はstanfit関数に格納
– 可視化の関数あり
Rstanまわりの構造
stan ()
stan ()
Stan code
stanc()
C++ code
plot()
exe
stan_model()
S4:stanfit
sampling()
traceplot()
extract()
38.
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での実行結果が返される
41.
前回のモデルを利用
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)
42.
Rからの並列計算
#parallel processing of
library(doSNOW)
library(foreach)
cl<-makeCluster(4)
registerDoSNOW(cl)
doSNOWとforeachを使用
4パラで計算
#parallel processing each chain of stan
sflist1<-foreach(i=1:10,.packages='rstan') %dopar% {
stan(fit = fit1, data=dat, chains = 1, chain_id = i, refresh = -1)
}
#merging the chains
f3<-sflist2stanfit(sflist1)
foreachで並列処理、
Stanfitクラスのリストを取得する。
Sflist2stanfitでリストをマージする。
Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.
Be the first to comment