以下のURLからの続き
http://rpubs.com/koyo/BD1991reanal
// データ設定
data {
int<lower=0> nef; // nef は効果指標数
real es[nef]; // [ ]内は効果指標数
real <lower = 0 > se[nef]; // [ ]内は効果指標数
}
// 推定パラメータの設定
parameters {
real d[nef]; // 各研究の効果 [ ]内は効果指標数
real <lower = 0 > sigma [nef]; // 各研究効果の標準誤差 [ ]内は効果指標数
real mu; // 共通効果
}
// モデルの指定
model {
mu ~ uniform (-10, 10);
d ~ normal (mu, sigma);
sigma ~ student_t (4, 0, 5);
es ~ normal(d, se);
}
library(rstan)
scr <- "metaex11.stan"
bd1991 <- read.csv("mgdata.csv")
data <- list(N = bd1991$n, es = bd1991$d, se = bd1991$se, nef = nrow(bd1991))
par <- c("d", "mu")
ite <- 100000
war <- 50000
cha <- 4
see <- 1234
dig <- 3
fit11 <- stan(file = scr, model_name = scr, data = data, pars = par, verbose = F,
seed = see, chains = cha, warmup = war, iter = ite)
print(fit11, pars = par, digits_summary = dig)
## Inference for Stan model: metaex11.stan.
## 4 chains, each with iter=1e+05; warmup=50000; thin=1;
## post-warmup draws per chain=50000, total post-warmup draws=2e+05.
##
## mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
## d[1] 0.753 0.003 0.300 0.192 0.545 0.742 0.950 1.376 11193 1.001
## d[2] 0.589 0.002 0.262 0.071 0.419 0.585 0.760 1.114 11369 1.000
## d[3] 0.431 0.003 0.264 -0.100 0.258 0.439 0.606 0.943 10198 1.001
## d[4] -0.167 0.002 0.263 -0.677 -0.346 -0.170 0.012 0.350 11641 1.001
## d[5] 0.449 0.002 0.236 -0.026 0.295 0.454 0.606 0.908 23006 1.000
## d[6] 0.967 0.003 0.305 0.402 0.752 0.958 1.174 1.577 12499 1.000
## d[7] 1.194 0.004 0.356 0.516 0.947 1.192 1.436 1.893 7922 1.000
## d[8] 0.819 0.003 0.313 0.239 0.603 0.803 1.023 1.465 11150 1.000
## d[9] -0.174 0.004 0.384 -0.931 -0.436 -0.169 0.094 0.553 10470 1.000
## d[10] 0.299 0.002 0.212 -0.129 0.159 0.305 0.444 0.700 19346 1.000
## d[11] -0.092 0.003 0.266 -0.612 -0.273 -0.091 0.087 0.432 9331 1.001
## d[12] 0.717 0.002 0.208 0.321 0.577 0.710 0.854 1.136 17335 1.000
## d[13] 0.836 0.003 0.392 0.110 0.568 0.815 1.086 1.656 24323 1.000
## d[14] 0.365 0.003 0.360 -0.387 0.139 0.379 0.599 1.058 13858 1.000
## d[15] 0.603 0.001 0.133 0.348 0.512 0.602 0.692 0.866 10927 1.001
## d[16] 0.425 0.001 0.119 0.188 0.345 0.426 0.507 0.655 12945 1.001
## d[17] 0.502 0.002 0.263 -0.026 0.331 0.503 0.673 1.018 15312 1.000
## mu 0.524 0.002 0.199 0.126 0.401 0.522 0.648 0.923 16735 1.000
##
## Samples were drawn using NUTS(diag_e) at Fri Mar 15 17:24:08 2019.
## 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).
rw <- subset(bd1991, fb == "rw")
data <- list(N = rw$n, es = rw$d, se = rw$se, nef = nrow(rw))
scr <- "metaex11.stan"
par <- c("d", "mu")
ite <- 100000
war <- 50000
cha <- 4
see <- 1234
dig <- 3
fit.rw <- stan(file = scr, model_name = scr, data = data, pars = par, verbose = F,
seed = see, chains = cha, warmup = war, iter = ite)
print(fit.rw, pars = par, digits_summary = dig)
## Inference for Stan model: metaex11.stan.
## 4 chains, each with iter=1e+05; warmup=50000; thin=1;
## post-warmup draws per chain=50000, total post-warmup draws=2e+05.
##
## mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
## d[1] -0.213 0.002 0.256 -0.716 -0.386 -0.213 -0.041 0.290 25164 1
## d[2] 0.290 0.003 0.406 -0.497 0.012 0.288 0.566 1.082 19733 1
## mu 0.042 0.008 1.926 -4.284 -0.664 0.019 0.764 4.374 53687 1
##
## Samples were drawn using NUTS(diag_e) at Fri Mar 15 17:24:25 2019.
## 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).
ca <- subset(bd1991, fb == "ca")
data <- list(N = ca$n, es = ca$d, se = ca$se, nef = nrow(ca))
scr <- "metaex11.stan"
par <- c("d", "mu")
ite <- 100000
war <- 50000
cha <- 4
see <- 1234
dig <- 3
fit.ca <- stan(file = scr, model_name = scr, data = data, pars = par, verbose = F,
seed = see, chains = cha, warmup = war, iter = ite)
print(fit.ca, pars = par, digits_summary = dig)
## Inference for Stan model: metaex11.stan.
## 4 chains, each with iter=1e+05; warmup=50000; thin=1;
## post-warmup draws per chain=50000, total post-warmup draws=2e+05.
##
## mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
## d[1] 0.786 0.007 0.294 0.208 0.586 0.786 0.983 1.369 1673 1.002
## d[2] 0.609 0.011 0.265 0.098 0.422 0.611 0.788 1.131 602 1.008
## d[3] 0.446 0.005 0.270 -0.095 0.271 0.443 0.632 0.968 3367 1.001
## d[4] 0.964 0.014 0.298 0.425 0.756 0.956 1.161 1.569 456 1.010
## d[5] 1.216 0.008 0.346 0.560 0.972 1.219 1.447 1.915 1838 1.003
## d[6] 0.811 0.027 0.332 0.069 0.607 0.818 1.027 1.455 151 1.026
## d[7] -0.171 0.005 0.378 -0.930 -0.424 -0.163 0.080 0.562 6353 1.001
## d[8] 0.297 0.005 0.221 -0.138 0.147 0.305 0.444 0.721 1799 1.003
## d[9] -0.124 0.017 0.279 -0.671 -0.312 -0.120 0.065 0.417 266 1.014
## d[10] 0.738 0.002 0.204 0.332 0.601 0.743 0.866 1.145 8938 1.002
## d[11] 0.841 0.024 0.401 0.010 0.578 0.830 1.104 1.655 273 1.015
## d[12] 0.618 0.001 0.131 0.358 0.530 0.620 0.703 0.875 23405 1.000
## mu 0.629 0.012 0.265 0.097 0.464 0.634 0.795 1.149 517 1.007
##
## Samples were drawn using NUTS(diag_e) at Fri Mar 15 17:25:12 2019.
## 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).
ex <- subset(bd1991, fb == "ex")
data <- list(N = ex$n, es = ex$d, se = ex$se, nef = nrow(ex))
scr <- "metaex11.stan"
par <- c("d", "mu")
ite <- 100000
war <- 50000
cha <- 4
see <- 1234
dig <- 3
fit.ex <- stan(file = scr, model_name = scr, data = data, pars = par, verbose = F,
seed = see, chains = cha, warmup = war, iter = ite)
print(fit.ex, pars = par, digits_summary = dig)
## Inference for Stan model: metaex11.stan.
## 4 chains, each with iter=1e+05; warmup=50000; thin=1;
## post-warmup draws per chain=50000, total post-warmup draws=2e+05.
##
## mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
## d[1] 0.426 0.002 0.241 -0.059 0.279 0.409 0.582 0.914 10573 1.002
## d[2] 0.414 0.002 0.117 0.183 0.344 0.406 0.491 0.651 3631 1.003
## d[3] 0.504 0.003 0.271 -0.047 0.329 0.524 0.669 1.046 7926 1.001
## mu 0.442 0.004 0.970 -1.601 0.164 0.399 0.730 2.503 53675 1.000
##
## Samples were drawn using NUTS(diag_e) at Fri Mar 15 17:25:31 2019.
## 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).
// データ設定
data {
int<lower=0> nef_rw; // 正誤
real es_rw[nef_rw];
real <lower = 0 > se_rw[nef_rw];
int<lower=0> nef_ca; // 正答
real es_ca[nef_ca];
real <lower = 0 > se_ca[nef_ca];
int<lower=0> nef_ex; // 説明
real es_ex[nef_ex];
real <lower = 0 > se_ex[nef_ex];
}
// 推定パラメータの設定
parameters {
real d_rw[nef_rw]; // 正誤
real <lower = 0 > sigma_rw [nef_rw];
real mu_rw;
real d_ca[nef_ca]; // 正答
real <lower = 0 > sigma_ca [nef_ca];
real mu_ca;
real d_ex[nef_ex]; // 説明
real <lower = 0 > sigma_ex [nef_ex];
real mu_ex;
}
// モデルの指定
model {
mu_rw ~ uniform (-10, 10); // 正誤
d_rw ~ normal (mu_rw, sigma_rw);
sigma_rw ~ student_t (4,0,5);
es_rw ~ normal (d_rw, se_rw);
mu_ca ~ uniform (-10, 10); // 正答
d_ca ~ normal(mu_ca, sigma_ca);
sigma_ca ~ student_t(4,0,5);
es_ca ~ normal(d_ca, se_ca);
mu_ex ~ uniform (-10, 10); // 説明
d_ex ~ normal (mu_ex, sigma_ex);
sigma_ex ~ student_t (4,0,5);
es_ex ~ normal (d_ex, se_ex);
}
// 統合後効果量の比較
generated quantities{
real delta_rw_ca;
real delta_rw_ex;
real delta_ca_ex;
real p_delta_rw_ca;
real p_delta_rw_ex;
real p_delta_ca_ex;
delta_rw_ca = mu_rw - mu_ca;
delta_rw_ex = mu_rw - mu_ex;
delta_ca_ex = mu_ca - mu_ex;
p_delta_rw_ca = step(delta_rw_ca);
p_delta_rw_ex = step(delta_rw_ex);
p_delta_ca_ex = step(delta_ca_ex);
}
library(rstan)
bd1991 <- read.csv("mgdata.csv")
rw <- subset(bd1991, fb == "rw")
ca <- subset(bd1991, fb == "ca")
ex <- subset(bd1991, fb == "ex")
data <- list(N_rw = rw$n, es_rw = rw$d, se_rw = rw$se, nef_rw = nrow(rw),
N_ca = ca$n, es_ca = ca$d, se_ca = ca$se, nef_ca = nrow(ca),
N_ex = ex$n, es_ex = ex$d, se_ex = ex$se, nef_ex = nrow(ex))
scr <- "metaex12.stan"
par <- c("d_rw", "d_ca", "d_ex",
"mu_rw", "mu_ca", "mu_ex",
"delta_rw_ca", "delta_rw_ex", "delta_ca_ex",
"p_delta_rw_ca", "p_delta_rw_ex", "p_delta_ca_ex")
ite <- 100000
war <- 50000
cha <- 4
see <- 1234
dig <- 3
fit12 <- stan(file = scr, model_name = scr, data = data, pars = par, verbose = F,
seed = see, chains = cha, warmup = war, iter = ite)
## Warning: There were 40658 divergent transitions after warmup. Increasing adapt_delta above 0.8 may help. See
## http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
## Warning: Examine the pairs() plot to diagnose sampling problems
print(fit12, pars = par, digits_summary = dig)
## Inference for Stan model: metaex12.stan.
## 4 chains, each with iter=1e+05; warmup=50000; thin=1;
## post-warmup draws per chain=50000, total post-warmup draws=2e+05.
##
## mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff
## d_rw[1] -0.208 0.003 0.255 -0.708 -0.380 -0.207 -0.035 0.289 10328
## d_rw[2] 0.293 0.005 0.400 -0.481 0.020 0.294 0.567 1.074 5834
## d_ca[1] 0.772 0.003 0.294 0.208 0.574 0.764 0.962 1.372 7736
## d_ca[2] 0.617 0.003 0.265 0.086 0.444 0.619 0.790 1.136 11110
## d_ca[3] 0.456 0.003 0.273 -0.098 0.277 0.464 0.641 0.983 7136
## d_ca[4] 0.978 0.004 0.296 0.420 0.773 0.968 1.177 1.572 6826
## d_ca[5] 1.204 0.005 0.343 0.558 0.962 1.203 1.435 1.886 5528
## d_ca[6] 0.839 0.003 0.304 0.263 0.631 0.831 1.041 1.458 11392
## d_ca[7] -0.179 0.006 0.392 -0.957 -0.442 -0.183 0.094 0.565 4353
## d_ca[8] 0.295 0.003 0.222 -0.146 0.147 0.295 0.448 0.724 6531
## d_ca[9] -0.099 0.003 0.263 -0.621 -0.274 -0.099 0.080 0.414 10536
## d_ca[10] 0.734 0.002 0.208 0.333 0.594 0.732 0.871 1.153 8528
## d_ca[11] 0.852 0.004 0.389 0.097 0.594 0.837 1.102 1.652 7663
## d_ca[12] 0.617 0.002 0.135 0.353 0.526 0.615 0.707 0.881 4745
## d_ex[1] 0.434 0.002 0.251 -0.066 0.266 0.434 0.605 0.926 13157
## d_ex[2] 0.419 0.001 0.120 0.183 0.338 0.420 0.499 0.655 9322
## d_ex[3] 0.492 0.003 0.281 -0.057 0.304 0.490 0.676 1.053 10176
## mu_rw 0.034 0.011 1.866 -4.185 -0.625 0.019 0.727 4.257 30201
## mu_ca 0.637 0.003 0.264 0.091 0.481 0.641 0.799 1.159 9119
## mu_ex 0.433 0.010 0.971 -1.628 0.128 0.440 0.756 2.436 8649
## delta_rw_ca -0.603 0.011 1.884 -4.863 -1.319 -0.611 0.135 3.609 28427
## delta_rw_ex -0.399 0.013 2.097 -5.050 -1.314 -0.409 0.542 4.217 27308
## delta_ca_ex 0.204 0.009 1.003 -1.833 -0.194 0.194 0.585 2.319 13607
## p_delta_rw_ca 0.285 0.004 0.451 0.000 0.000 0.000 1.000 1.000 10262
## p_delta_rw_ex 0.374 0.006 0.484 0.000 0.000 0.000 1.000 1.000 7264
## p_delta_ca_ex 0.638 0.005 0.481 0.000 0.000 1.000 1.000 1.000 10536
## Rhat
## d_rw[1] 1.000
## d_rw[2] 1.000
## d_ca[1] 1.000
## d_ca[2] 1.000
## d_ca[3] 1.001
## d_ca[4] 1.001
## d_ca[5] 1.001
## d_ca[6] 1.001
## d_ca[7] 1.001
## d_ca[8] 1.001
## d_ca[9] 1.000
## d_ca[10] 1.001
## d_ca[11] 1.001
## d_ca[12] 1.001
## d_ex[1] 1.000
## d_ex[2] 1.000
## d_ex[3] 1.000
## mu_rw 1.000
## mu_ca 1.000
## mu_ex 1.001
## delta_rw_ca 1.000
## delta_rw_ex 1.000
## delta_ca_ex 1.000
## p_delta_rw_ca 1.000
## p_delta_rw_ex 1.001
## p_delta_ca_ex 1.000
##
## Samples were drawn using NUTS(diag_e) at Fri Mar 15 17:29:09 2019.
## 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).