Windowsユーザの方は以下の点にご注意ください
Rのサイトはこちらです。Rの最新バージョンは3.5.2ですので,ご準備ください。
RStudioのサイトはこちらです。RStudioの最新バージョンは1.1.463ですので,ご準備ください。OSに応じて最新版のインストーラをダウンロードし,実行してください。
RおよびRStudioのインストールが終われば,RStudioを起動し,次のコードを実行してください。
関連パッケージも含め,多くのパッケージが導入されますのでしばらくお待ちください。
install.packages('tidyverse')
install.packages('summarytools')
install.packages('ggrepel')
install.packages('rstan')
install.packages('bayesplot')
install.packages('brms')
install.packages('MASS')
install.packages('smacof')
すでにRやRStudio,パッケージの導入をしたことがある人も,次のコードでパッケージを最新版にアップデートすることができますので,ご確認ください。
update.packages(ask=F)
環境の準備ができれば,次のコードをコピー&ペーストで実行してください。
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
stancode <- "
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // s.e. of effect estimates
}
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);
}
"
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))
model.ex <- stan_model(model_code = stancode, model_name = "school")
fit.samp <- sampling(model.ex, data = schools_dat, iter = 1000,
chains = 4)
## Warning: There were 1 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
fit.samp
## Inference for Stan model: school.
## 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 8.10 0.16 5.13 -1.32 4.59 8.08 11.32 18.71 974 1.00
## tau 6.53 0.18 5.17 0.34 2.66 5.36 9.12 19.31 827 1.01
## eta[1] 0.42 0.02 0.95 -1.47 -0.20 0.44 1.06 2.24 2035 1.00
## eta[2] -0.01 0.02 0.85 -1.74 -0.56 0.00 0.55 1.60 2062 1.00
## eta[3] -0.21 0.02 0.93 -2.02 -0.82 -0.23 0.40 1.61 1719 1.00
## eta[4] -0.05 0.02 0.84 -1.69 -0.61 -0.04 0.48 1.63 1826 1.00
## eta[5] -0.38 0.02 0.85 -2.05 -0.95 -0.42 0.16 1.34 1887 1.00
## eta[6] -0.22 0.02 0.90 -1.91 -0.82 -0.26 0.39 1.53 1885 1.00
## eta[7] 0.33 0.02 0.87 -1.47 -0.22 0.34 0.91 2.00 1367 1.00
## eta[8] 0.05 0.02 0.93 -1.77 -0.60 0.06 0.67 1.84 1849 1.00
## theta[1] 11.68 0.21 8.18 -1.38 6.31 10.58 15.72 30.93 1523 1.00
## theta[2] 8.04 0.14 6.27 -5.29 3.98 8.16 12.15 20.17 1972 1.00
## theta[3] 6.31 0.17 7.49 -10.72 2.18 6.78 11.14 20.01 1949 1.00
## theta[4] 7.66 0.14 6.49 -5.13 3.75 7.52 11.60 21.34 2112 1.00
## theta[5] 5.18 0.15 6.15 -7.64 1.43 5.55 9.34 16.41 1772 1.00
## theta[6] 6.10 0.15 6.85 -9.46 2.05 6.42 10.47 19.24 2096 1.00
## theta[7] 10.64 0.17 6.76 -1.31 6.04 10.11 14.57 25.71 1523 1.00
## theta[8] 8.63 0.20 7.88 -7.00 3.97 8.43 12.85 25.97 1610 1.00
## lp__ -39.41 0.10 2.51 -45.17 -40.94 -39.23 -37.61 -35.09 680 1.01
##
## Samples were drawn using NUTS(diag_e) at Fri Mar 8 17:11:11 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).
なにやら上記のような文字列が出ていたら準備完了です。
Rのコードについては,本サイトからコピー&ペーストで使ってもらって結構ですが,Rの基本的な使い方,RStudioの基本的な使い方,ggplotによる描画の方法などについて,「RユーザのためのRStudio入門」 を参考に予習しておいていただけると,理解が進むと思います。