4. stanファイル
data {
int<lower=0> N;
int<lower=0> x[N];
}
parameters {
real<lower=0, upper=1> theta;
}
model {
x ~ bernoulli(theta);
}
generated quantities{
real p; #4回中3回以上成功する確率
real beta;
int pred; #2回成功するまでの総試行数
p = 1*pow(theta, 4)*pow((1-theta),0)+4*pow(theta, 3)*pow((1-theta),1);
beta = theta/(1-theta);
pred = neg_binomial_rng(2, beta)+2;
}
5. Rスクリプト
scr <- "model774.stan"
N <- 16
x <- c(0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1)
data <- list(N=N,x=x)
par <- c("p", "pred")
fit <- stan(file=scr, model_name=scr, data=data, pars=par, verbose=F,
seed=123, chains=1, warmup=5000, iter=100000)
library(ggmcmc) #チャート出力用ライブラリ
ggmcmc(ggs(fit), file='fit-ggmcmc.pdf')
6. 結果
> fit
Inference for Stan model: model774.stan.
4 chains, each with iter=1e+05; warmup=10000; thin=1;
post-warmup draws per chain=90000, total post-warmup draws=360000.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
p 0.13 0.00 0.11 0.01 0.05 0.11 0.19 0.41 236584 1
pred 6.80 0.01 5.33 2.00 3.00 5.00 8.00 21.00 317352 1
lp__ -11.97 0.00 0.73 -14.03 -12.14 -11.69 -11.51 -11.46 164598 1
Samples were drawn using NUTS(diag_e) at Fri Jan 27 19:50:09 2017.
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).