RStan Getting Started (Japanese) - PrinceWangR/rstan GitHub Wiki
<wiki:toc max_depth="3" />
ãã®ææžã¯RStan Getting Startedãæ¥æ¬èªã«ç¿»èš³ãããã®ã§ãã
äžã®èª¬æã®ã»ãšãã©ã¯RStanã®äžèšã®ããŒãžã§ã³ã«é¢ãããã®ã§ãã
RStanã¯StanãR (http://www.r-project.org/) ããå©çšããããã®ã€ã³ã¿ãŒãã§ãŒã¹ã§ãããã®ææžã§ã¯ä»¥äžã®èª¬æãããŸã:
Stanããã®ææ³ã®ãããªãæ å ±ã欲ããæã¯ä»¥äžãåç §ããŠãã ãã:
Rã®ããŒãžã§ã³ã¯3.0.2ããã以éãå¿
èŠã§ãïŒMacã®å ŽåãããXcodeãã¢ããã°ã¬ãŒãããŠãããšRã®ã»ããæ§ã
ãªã©ã€ãã©ãªã®åã€ã³ã¹ããŒã«ãå¿
èŠã«ãªããããããŸããïŒãããããRStanã¯3.2.0ããåã®ããŒãžã§ã³ã ãšäœããã®ç¹ã§ããŸãåããªãããšãç¥ãããŠããŸãïŒggplot2ã§ã®ããããã倱æããã§ãããããŸããrstan.package.skeleton
é¢æ°ãå¿
èŠãªãã¡ã€ã«ãããŠã³ããŒãããªããªã©ïŒãããŒãžã§ã³3.0.2ãšããã®ã¯ãææ°ã®Rãçšæã§ããªããããªãªã¢ãŒããµãŒããŒã§äœæ¥ããããã«å¿
èŠãªããŒãžã§ã³ã§ããRStanã¯Rã®ææ°ã®å®å®æ¿ãšdevelopçã§ãã¹ãããŠããŸãããããããªãã管çè
æš©éãæã£ãŠãããªããç¹ã«RStanã®ã¯ãŒã¯ã·ã§ããã«åå ããå ŽåãRã®ææ°çã䜿ããªãçç±ã¯ãããŸãããææ°çã®Rã¯ä»¥äžããå
¥æã§ããŸãã
å·Šã®ã¡ãã¥ãŒã®ãDownloadãã®ãšããã«ãããCRANããã¯ãªãã¯ããŠããããããã©ãŒãµã€ããéžã³ãŸãïŒhttp://cran.rstudio.com/ ããªã¹ã¹ã¡ããŸããããã¯æãè¿ããŠä¿¡é Œã§ãããã©ãŒãµã€ããžãªãã€ã¬ã¯ãããŠãããããã§ãïŒãããããããªãã®OS(Windowsã»Linuxã»Mac)ã«ããããŠãªã³ã¯ãã¯ãªãã¯ããŸããWindowsã ãšããŠã³ããŒãã®åã«ãbaseãããã±ãŒãžãéžã¶ããã²ãšæéããããŸãã
LinuxãšMacã®å Žåã¯Rã®ã³ãã³ãã©ã€ã³ãšGUIãã€ã³ã¹ããŒã«ããŸããããããã©ã«ãã®èšå®ã®ãŸãŸã§å€§äžå€«ã§ãã
å¿ é ã§ã¯ãããŸããããã»ãšãã©ã®ãŠãŒã¶ã«ã¯RStudioã®ããŒãžã§ã³0.99.441ããã以éãã€ã³ã¹ããŒã«ããããšããªã¹ã¹ã¡ããŸãã
https://www.rstudio.com/products/rstudio/
.stanãã¡ã€ã«ïŒStanã®ã¢ãã«ãæžããã¡ã€ã«ïŒã«ãå®ç§ã§ã¯ãããŸããã察å¿ããŠããŸãã
- Macã®å ŽåãRStan Mac OS X Prerequisite Installation Instructions ãèŠãŠãã ããã
- Windowsã®å ŽåãHow to install Rtools on Windows ãèŠãŠãã ããã
- Linuxã®å Žåã䜿ã£ãŠãããã£ã¹ããªãã¥ãŒã·ã§ã³ã®ããã±ãŒãžãããŒãžã£ãŒã䜿ã£ãŠãã«ãã«å¿ èŠãªãã®ãšæè¿ã®g++ãclang++ãã€ã³ã¹ããŒã«ããŠãã ããã
ãã®å°ç¯ã«æžããŠããããšã¯å¿ é ã§ã¯ãããŸããããããªããŠãRStanã¯åãããã§ããã§ãèšå®ããããšã匷ããªã¹ã¹ã¡ããŸããããå人ã®Makevarsãã¡ã€ã«ããŸã æã£ãŠããªããªããã®ãªã³ã¯å ã«åŸã£ãŠäœããŸãããã 以äžã®æé ã¯Rãèµ·åããŠããã®Makevarsãã¡ã€ã«ãRã§äœãäŸã§ãïŒR GUIã§ãã¿ãŒããã«äžã§èµ·åããRã§ããªã¹ã¹ã¡ããRStudioãèµ·åããŠå®è¡ããŠãOKã§ãïŒã
dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, "Makevars")
if (!file.exists(M)) file.create(M)
cat("\nCXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function",
file = M, sep = "\n", append = TRUE)
ã²ãšã€æ³šæããããŸããRã®ããã±ãŒãžã®äžã«ã¯ã³ã³ãã€ã«ã®èšå®ãå¹³å¡ãªå Žåã ãããã¹ãããŠãããã®ããã£ãŠãäžã§èšå®ããããã«ã³ã³ãã€ã«ã®æé©åã¬ãã«ã3ã«ãããšãããããããã±ãŒãžããœãŒã¹ããã€ã³ã¹ããŒã«ããæã«äžå ·åãã§ããããããŸããã
ããg++ã®ããŒãžã§ã³ã4.9ããã以éãªãã°ïŒMacã§ã¯çããã§ããïŒã以äžãRäžã§å®è¡ããããšããªã¹ã¹ã¡ããŸãã
cat("\nCXXFLAGS+=-flto -ffat-lto-objects",
file = M, sep = "\n", append = TRUE)
ããã«ãOS Xã®å Žåã ã ïŒclang++ã¯ã€ã³ã¹ããŒã«ãããšããŠïŒä»¥äžãRäžã§å®è¡ããå¿ èŠããããŸãã
cat("\nCC=clang", "CXX=clang++ -arch x86_64 -ftemplate-depth-256",
file = M, sep = "\n", append = TRUE)
Rã®ããŒãžã§ã³3.3.xã䜿ããšãC++11ããµããŒãããŠããg++ 4.9.xã«å¯Ÿå¿ããWindowsçšRtoolsãããŠã³ããŒãããŠäœ¿ãããšãã§ããŸããä»ã®ãšããStanã§ã¯g++ 4.6ãšããããåã®ããŒãžã§ã³ã§ã¯C++11ããµããŒãããŠããŸãããããããg++ã®4.6ããããšã®ããŒãžã§ã³ãšclang++ã®æè¿ã®ããŒãžã§ã³ã䜿ããšStanã§ãC++11ã¯äœ¿ãããšã¯æããŸãã
C++11ã䜿ããã©ããã«é¢ããããããRtools33ïŒããã以éïŒã䜿ããªãã以äžãäžåºŠå®è¡ããå¿
èŠããããŸãã
ïŒèš³æ³šïŒããRtoolsãC:\Rtools
ã«ã€ã³ã¹ããŒã«ããå Žåã¯äžèŠã§ããCãã©ã€ãçŽäžã§ãªããšããã«ã€ã³ã¹ããŒã«ããå Žåã以äžã®ããã«.Rprofile
ã§BINPREF
ãå®çŸ©ããããããã¯Windowsã®ç°å¢å€æ°ãçŽæ¥ç·šéããŠèšå®ããããããã¯(Rã®ã€ã³ã¹ããŒã«ãã©ã«ã)\etc\x64\Makeconf
ã®19è¡ç®ãããã®BINPREF
ãç·šéããå¿
èŠããããŸãïŒ
cat('Sys.setenv(BINPREF = "C:/Rtools/mingw_$(WIN)/bin/")',
file = file.path(Sys.getenv("HOME"), ".Rprofile"),
sep = "\n", append = TRUE)
ããg++ã®version 6ããã以äžã䜿ãå Žåã Stanã«é¢ä¿ããªãèŠåãåºåãããã®ãæ¢ããããšæããããããŸãããããã«ã¯æ¬¡ãå®è¡ããŸãã
cat("\nCXXFLAGS += -Wno-ignored-attributes -Wno-deprecated-declarations",
file = M, sep = "\n", append = TRUE)
以äžãå®è¡ããããšã§ãèšå®ãæ£ããã確ãããããšãã§ããŸãã
cat(readLines(M), sep = "\n")
ããæ£ãããªããã°ããã¡ã€ã«ã®ãã¹ã以äžã§ç¢ºãããŠããã®ãã¡ã€ã«ãããã¹ããšãã£ã¿ã§éããŠç·šéããããšãã§ããŸãã
cat(M)
-
Rãèµ·åããŸãïŒR GUIã§ãã¿ãŒããã«äžã§èµ·åããRã§ããªã¹ã¹ã¡ããRStudioãèµ·åããŠå®è¡ããŠãOKã§ãïŒã
-
ãœãŒã¹ãããã«ãããå Žåã ãïŒOS XãWindowsã§ã¯çãããšæããŸããïŒããã«ãã«äœã³ã¢äœ¿ããæå®ããŸããäŸãã°4ã³ã¢äœ¿ããããªãã°ã以äžãRäžã§å®è¡ããŸãã
Sys.setenv(MAKEFLAGS = "-j4")
- rstanããã±ãŒãžã®ææ°çãšãããäŸåããŠããããã±ãŒãžã以äžã®ããã«æ瀺çã«æå®ããŠã€ã³ã¹ããŒã«ããŸãã
# ããhttpsã䜿ã£ãããŠã³ããŒãã«å¯Ÿå¿ããŠããªããªãã°ãhttpsã®ãsããåã£ãŠå®è¡ããŠãã ãã
install.packages('rstan', repos = 'https://cloud.r-project.org/', dependencies = TRUE)
ããã§ãããŸããããªãå ŽåããœãŒã¹ããã€ã³ã¹ããŒã«ããããšãè©ŠããŠããããããããŸããã
install.packages("rstan", type = "source")
-
Rã®åèµ·å ã€ã³ã¹ããŒã«ã®åŸã¯ãã¶ãRã®åèµ·åãå¿ èŠã§ãã ãããŠrstanããã±ãŒãžãåŒã³åºãåã«ä»¥åã®ããŒãžã§ã³ã®RStanã«ãã£ãŠäœãããäœããã®ãªããžã§ã¯ããåæã«loadãããªãã確ãããŠãã ããã
-
Rã§ä»¥äžãå®è¡ããŠãC++ãåãã確ãããŠãã ããã
fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , '
return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
')
fx( 2L, 5 ) # 10ã«ãªãã¯ãã§ã
ããã±ãŒãžåã¯ãrstanãã§ããã ãã library(rstan)
ã§rstanãloadããå¿
èŠããããŸãã
library(rstan) # ã¹ã¿ãŒãã¢ããã®ã¡ãã»ãŒãžã衚瀺ããã
ã¹ã¿ãŒãã¢ããã®ã¡ãã»ãŒãžã«ããããã«ããããã«ãã³ã¢ã§ã¡ã¢ãªãååã«ããããŒã«ã«ãã·ã³ã§rstanã䜿ã£ãŠäžŠåã§æšå®èšç®ãå®è¡ããããå Žåã«ã¯ã以äžãå®è¡ããŸãã
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
ãããã®optionã¯ãããããåã³ã³ãã€ã«ãããªããŠããããã«ã³ã³ãã€ã«åŸã®Stanããã°ã©ã ãããŒããã£ã¹ã¯ã«ä¿åããããšãè€æ°ã®ãã«ã³ãé£éã䞊åã§å®è¡ããããšãæå®ããŠããŸãã
ããã¯Gelman et al (2003)ã®5.5ç¯ã«ããäŸã§ãã8ã€ã®åŠæ ¡ã«ãããæè²ã®å¹æãç 究ãããã®ã§ããç°¡åã®ããããã®äŸããeight schoolsããšåŒã³ãŸãã
ã¯ããã«ããã®ã¢ãã«ã8schools.stan
ãšããååã§ä¿åããŸããäžèº«ã¯ä»¥äžã®éãã§ãïŒãã¡ã€ã«ã¯ããã§èŠãããšãã§ããŸãïŒã
data {
int<lower=0> J; // åŠæ ¡ã®æ°
real y[J]; // æšå®ãããŠããæè²ã®å¹æ
real<lower=0> sigma[J]; // æè²ã®å¹æã®æšæºèª€å·®
}
parameters {
real mu;
real<lower=0> tau;
real eta[J];
}
transformed parameters {
real theta[J];
for (j in 1:J)
theta[j] = mu + tau * eta[j];
}
model {
target += normal_lpdf(eta | 0, 1);
target += normal_lpdf(y | theta, sigma);
}
ãã®ã¢ãã«ã§ã¯ãtheta
ãparametersãšããŠçŽæ¥çã«å®£èšãã代ããã«ãmu
ãšeta
ã䜿ã£ãŠtheta
ãäœãtransformed parametersã«ããŠããŸãããã®ããã«ãã©ã¡ãŒã¿åããããšã§ããå¹ççã«ãµã³ããªã³ã°ã§ããŸãã8schools.stan
ãäœæ¥ãã£ã¬ã¯ããªã«çœ®ãããšãããšã以äžã®Rã³ãŒãã§ããŒã¿ãçšæãããŠã¢ãã«ããã£ããã£ã³ã°ãããããšãã§ããŸãã
schools_dat <- list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18))
fit <- stan(file = '8schools.stan', data = schools_dat,
iter = 1000, chains = 4)
stan
é¢æ°ã®model_code
åŒæ°ã䜿ãããšã§Stanã®ã¢ãã«ãRã³ãŒãå
ã®æååã§æå®ããããšãã§ããŸããããããªãããããã¯ãªã¹ã¹ã¡ã§ããŸããã
stan
é¢æ°ããè¿ãããfit
ãªããžã§ã¯ãã¯stanfit
ã¯ã©ã¹ã®S4ãªããžã§ã¯ãã§ãã print
ã»plot
ã»pairs
ã®ãããªé¢æ°ã¯ãã£ããããçµæãšé¢é£ããŠããããã®ããšã®ã³ãŒãã䜿ã£ãŠfit
ã®äžã®çµæãåãåºãããšãã§ããŸããprint
ã¯ã¢ãã«ã®ãã©ã¡ãŒã¿ã«å ããŠlp__
ãšããååã®å¯Ÿæ°äºåŸç¢ºçïŒlog-posteriorïŒã®èŠçŽã衚瀺ããŸãïŒãã®ããšã®åºåäŸãèŠãŠãã ããïŒãä»ã®é¢æ°ãstanfit
ã¯ã©ã¹ã®è©³çŽ°ãç¥ãããå Žåã¯ãstanfit
ã¯ã©ã¹ã®ãã«ããèŠãŠãã ããã
ç¹ã«ãMCMCãµã³ãã«ãåŸãã«ã¯stanfit
ãªããžã§ã¯ãã«å¯ŸããŠextract
é¢æ°ã䜿ããŸãã extract
ã¯stanfit
ãªããžã§ã¯ããããèå³ãããã©ã¡ãŒã¿ã®arraysã®listãããã¯1ã€ã®arrayãšããŠMCMCãµã³ãã«ãåãåºããŸããããã«ãas.array
ãšas.matrix
ãšããS3é¢æ°ãå®çŸ©ãããŠããŸãïŒRäžã§help("as.array.stanfit")
ã䜿ã£ãŠãã«ããèŠãŠãã ããïŒã
print(fit)
plot(fit)
pairs(fit, pars = c("mu", "tau", "lp__"))
la <- extract(fit, permuted = TRUE) # arraysã®listãè¿ã
mu <- la$mu
### iterations, chains, parametersã®3次å
arrayãè¿ã
a <- extract(fit, permuted = FALSE)
### stanfitãªããžã§ã¯ãã«S3é¢æ°ã®as.arrayïŒãas.matrixïŒã䜿ã
a2 <- as.array(fit)
m <- as.matrix(fit)
> print(fit, digits = 1)
Inference for Stan model: schools_code.
4 chains, each with iter=1000; warmup=500; thin=1;
post-warmup draws per chain=500, total post-warmup draws=2000.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
mu 7.9 0.2 4.9 -2.1 4.5 7.9 11.0 17.8 422 1
tau 6.3 0.3 5.0 0.2 2.5 5.2 8.9 18.7 214 1
eta[1] 0.4 0.0 0.9 -1.5 -0.2 0.4 1.0 2.1 928 1
eta[2] 0.0 0.0 0.9 -1.8 -0.6 0.0 0.5 1.8 1640 1
eta[3] -0.2 0.0 1.0 -2.1 -0.8 -0.2 0.4 1.8 1243 1
eta[4] 0.0 0.0 0.9 -1.7 -0.6 0.0 0.6 1.7 1421 1
eta[5] -0.3 0.0 0.9 -2.0 -1.0 -0.4 0.3 1.5 883 1
eta[6] -0.2 0.0 0.9 -2.0 -0.8 -0.2 0.4 1.6 926 1
eta[7] 0.4 0.0 0.9 -1.4 -0.2 0.4 0.9 2.1 969 1
eta[8] 0.1 0.0 1.0 -1.8 -0.6 0.1 0.7 2.0 1365 1
theta[1] 11.4 0.3 8.1 -1.4 5.9 10.3 15.2 30.6 574 1
theta[2] 7.7 0.2 6.1 -3.7 3.9 7.8 11.4 19.5 762 1
theta[3] 5.8 0.3 7.9 -12.1 1.8 6.5 10.5 19.9 715 1
theta[4] 8.0 0.2 6.5 -5.4 3.9 8.1 12.3 20.2 977 1
theta[5] 5.0 0.3 6.7 -10.3 1.3 5.7 9.5 16.5 667 1
theta[6] 6.0 0.2 6.6 -8.4 2.0 6.2 10.2 18.6 976 1
theta[7] 10.8 0.3 6.8 -1.1 6.2 10.2 14.9 26.0 596 1
theta[8] 8.6 0.3 7.9 -6.1 4.0 8.1 12.6 27.7 629 1
lp__ -5.0 0.1 2.6 -10.7 -6.6 -4.8 -3.1 -0.5 367 1
Samples were drawn using NUTS2 at Fri Apr 12 22:09:54 2013.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at
convergence, Rhat=1).
ããã«ãBUGSïŒãJAGSïŒãšåæ§ã«ãCmdStanïŒStanã®ã³ãã³ãã©ã€ã³ã€ã³ã¿ãŒãã§ãŒã¹ïŒã䜿ãã«ã¯ãã¹ãŠã®ããŒã¿ãRã®dumpãã¡ã€ã«ã®åœ¢åŒã§æã£ãŠããå¿
èŠããããŸããdumpãã¡ã€ã«ãããå Žåãrstanã«ã¯read_rdump
ãšããé¢æ°ããããããã䜿ã£ãŠå
šãŠã®ããŒã¿ã1ã€ã®Rã®ååä»ããªã¹ããšããŠèªã¿èŸŒãããšãã§ããŸããäŸãã°ãäœæ¥ãã£ã¬ã¯ããªã«ä»¥äžã®äžèº«ãæ〠"8schools.rdump" ãšããååã®ãã¡ã€ã«ããããšããŸãã
J <- 8
y <- c(28, 8, -3, 7, -1, 1, 18, 12)
sigma_y <- c(15, 10, 16, 11, 9, 11, 10, 18)
ãããããšä»¥äžã®ããã« "8schools.rdump" ããããŒã¿ãèªã¿èŸŒãããšãã§ããŸãã
schools_dat <- read_rdump('8schools.rdump')
dumpãã¡ã€ã«ã¯Rã®source
é¢æ°ã䜿ã£ãŠèªã¿èŸŒãããšãã§ããdumpãã¡ã€ã«å
ã®åå€æ°ã¯ã°ããŒãã«ç°å¢å
ã®ãªããžã§ã¯ããšãªããŸãããã®å ŽåãStanãå®è¡ããæã®stan
é¢æ°ã®data
åŒæ°ãçç¥ããããšãã§ããŸããstan
é¢æ°ã¯ 8schools.stan ã®dataãããã¯ã«ããå€æ°åãšåãååã®ãªããžã§ã¯ããåŒã³åºãããç°å¢ããæ¢ãããã§ããããªãã¡ã
source('8schools.rdump')
fit <- stan(file = '8schools.stan', iter = 1000, chains = 4)
Ratsã®äŸã人æ°ã®ããäŸã§ãã äŸãã°ãããã«OpenBUGSããŒãžã§ã³ãããããªãªãžãã«ã¯Gelfand et al (1990)ã§ãã30å¹ã®ã©ããã®äœéã1é±éããš5é±éã«ããã£ãŠèšé²ããããŒã¿ã§ããããŒã¿ã¯ä»¥äžã®è¡šã®ããã«ãªã£ãŠãããäœéãèšé²ããæ¥ä»ãè¡šãã®ã«x
ã䜿ã£ãŠããŸãããã®äŸãè©Šãã®ã«ãªã³ã¯å
ã®ããŒã¿ãšã¢ãã«ã®ã³ãŒãã䜿ãããšãã§ããŸãïŒrats.txtãšrats.stanïŒã
Rat | x=8 | x=15 | x=22 | x=29 | x=36 | Rat | x=8 | x=15 | x=22 | x=29 | x=36 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 151 | 199 | 246 | 283 | 320 | 16 | 160 | 207 | 248 | 288 | 324 | |
2 | 145 | 199 | 249 | 293 | 354 | 17 | 142 | 187 | 234 | 280 | 316 | |
3 | 147 | 214 | 263 | 312 | 328 | 18 | 156 | 203 | 243 | 283 | 317 | |
4 | 155 | 200 | 237 | 272 | 297 | 19 | 157 | 212 | 259 | 307 | 336 | |
5 | 135 | 188 | 230 | 280 | 323 | 20 | 152 | 203 | 246 | 286 | 321 | |
6 | 159 | 210 | 252 | 298 | 331 | 21 | 154 | 205 | 253 | 298 | 334 | |
7 | 141 | 189 | 231 | 275 | 305 | 22 | 139 | 190 | 225 | 267 | 302 | |
8 | 159 | 201 | 248 | 297 | 338 | 23 | 146 | 191 | 229 | 272 | 302 | |
9 | 177 | 236 | 285 | 350 | 376 | 24 | 157 | 211 | 250 | 285 | 323 | |
10 | 134 | 182 | 220 | 260 | 296 | 25 | 132 | 185 | 237 | 286 | 331 | |
11 | 160 | 208 | 261 | 313 | 352 | 26 | 160 | 207 | 257 | 303 | 345 | |
12 | 143 | 188 | 220 | 273 | 314 | 27 | 169 | 216 | 261 | 295 | 333 | |
13 | 154 | 200 | 244 | 289 | 325 | 28 | 157 | 205 | 248 | 289 | 316 | |
14 | 171 | 221 | 270 | 326 | 358 | 29 | 137 | 180 | 219 | 258 | 291 | |
15 | 163 | 216 | 242 | 281 | 312 | 30 | 153 | 200 | 244 | 286 | 324 |
y <- read.table('https://raw.github.com/wiki/stan-dev/rstan/rats.txt', header = TRUE)
x <- c(8, 15, 22, 29, 36)
xbar <- mean(x)
N <- nrow(y)
T <- ncol(y)
rats_fit <- stan(file = 'https://raw.githubusercontent.com/stan-dev/example-models/master/bugs_examples/vol1/rats/rats.stan')
éçºããŒã ãBUGS exampleïŒWinBUGSã«ä»å±ããŠããäŸïŒã®å€§éšåããã®ä»ã®ã¢ãã«ãStanã§äœæããŠããã以äžãå®è¡ããããšã§è©Šãããšãã§ããŸãã
model <- stan_demo()
ãã®ããšã¯ã¢ãã«ã®äŸããããã¢ããã§åºãŠãããªã¹ãããéžãã§ãããŸããã¯ãããŠstan_demo()
ãåŒã¶ãšãã¯ããããã®äŸãããŠã³ããŒããããã©ãããèããŠããŸããoption 1ãéžãã§rstanãã€ã³ã¹ããŒã«ããããã£ã¬ã¯ããªã«ä¿åããããã«ããŸãããããããããšããŸããã€ãå®è¡ããæã«åããŠã³ããŒãããªããŠããã¿ãŸããå«ãŸããmodel
ãªããžã§ã¯ãã¯stanfit
ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãªã®ã§ããããã«å¯ŸããŠããprint
ã»plot
ã»pairs
ã»extract
ãåŒã³åºãããšãã§ããŸãã
RStanã«é¢ãããããªã詳现ã¯rstanããã±ãŒãžã®vignetteã«å«ãŸããããã¥ã¡ã³ãã§èŠãããšãã§ããŸããäŸãã°ãhelp(stan)
ãhelp("stanfit-class")
ã䜿ãããšã§stan
é¢æ°ãS4ã¯ã©ã¹ã§ããstanfit
ã®ãã«ããèŠãããšãã§ããŸãã
Stanã®ãµã³ãã©ãŒããªããã£ãã€ã¶ãŒãStanã®ææ³ã«é¢ãã詳现ãç¥ãããæã«ã¯Stan's modeling language manualãèŠãŠãã ããã
ããã«ãStanã®äœ¿ãæ¹ãè°è«ãããå Žåã«ã¯Stan User's Mailing listã«äŸãæçš¿ãããã(R)Stanã«ã€ããŠè³ªåããŠãããã§ããããå©ããå¿ èŠãªæã¯ã以äžã«é¢ããååãªæ å ±ãããããŠèšè¿°ããããšãéèŠã§ãã
- Stanã®ã¢ãã«ã³ãŒã
- ããŒã¿
- å®è¡ããã®ã«å¿ èŠãªRã³ãŒã
-
stan
é¢æ°ãåŒã¶ãšãã«verbose=TRUE
ãã€cores=1
ãæå®ããŠåºåããããšã©ãŒã¡ãã»ãŒãžã®dump - C++ã³ã³ãã€ã©ã®ããŒãžã§ã³ããã
gcc
ã䜿ã£ãŠãããªãã°g++ -v
ã§åŸãããšãã§ããŸãã - Räžã§
sessionInfo
é¢æ°ã§åŸãããšãã§ããRã«é¢ããæ å ±
- Gelman, A., Carlin, J. B., Stern, H. S., and Rubin, D. B. (2003). Bayesian Data Analysis, CRC Press, London, 2nd Edition.
- The Stan Development Team (2015). Stan Modeling Language User's Guide and Reference Manual.
- Gelfand, A. E., Hills S. E., Racine-Poon, A., and Smith A. F. M. (1990). "Illustration of Bayesian Inference in Normal Data Models Using Gibbs Sampling", Journal of the American Statistical Association, 85, 972-985.
- Stan
- R
- BUGS
- OpenBUGS
- JAGS
- Rcpp