SSブログ

rjagsの使い方メモ [統計]

1. rjagsのインストール (Mac OS X)


  1. まず、pkg-configをインストールしておく。
    cd pkg-config-x.xx
    ./configure
    make
    sudo make install
  2. rjags_1.0.1-1.tar.gzをダウンロード。
  3. sudo R CMD INSTALL rjags_1.0.1-1.tar.gz

2. rjagsの使用


  1. rjagsパッケージの読み込み。
    library(rjags)
  2. R2WinBUGSにある例題を使用する。
    library(R2WinBUGS)
    data(schools)
  3. 準備。
    model.file <- system.file(package = "R2WinBUGS",
        "model", "schools.txt")
    J <- nrow(schools)
    y <- schools$estimate
    sigma.y <- schools$sd
    data <- list (J=J, y=y, sigma.y=sigma.y)
    init1 <- list(
        theta = rnorm(J, 0, 100),
        mu.theta = rnorm(1, 0, 100),
        sigma.theta = runif(1, 0, 100))
    init2 <- list(
        theta = rnorm(J, 0, 100),
        mu.theta = rnorm(1, 0, 100),
        sigma.theta = runif(1, 0, 100))
    init3 <- list(
        theta = rnorm(J, 0, 100),
        mu.theta = rnorm(1, 0, 100),
        sigma.theta = runif(1, 0, 100))
    parameters <- c("theta", "mu.theta", "sigma.theta")
    model <- jags.model(model.file, data,
        inits = list(init1, init2, init3), nchain = 3, n.adapt = 2000)
  4. 実行する。
    samples <- coda.samples(model, parameters, 2000)
  5. 結果。
    summary(samples)
    Iterations = 2001:4000
    Thinning interval = 1 
    Number of chains = 3 
    Sample size per chain = 2000 

    1. Empirical mean and standard deviation for each variable,
       plus standard error of the mean:

                  Mean    SD Naive SE Time-series SE
    mu.theta     7.923 5.145  0.06643         0.2437
    sigma.theta  6.043 5.314  0.06861         0.3649
    theta[1]    11.033 8.262  0.10667         0.3712
    theta[2]     7.885 6.124  0.07905         0.2291
    theta[3]     6.430 7.499  0.09681         0.2588
    theta[4]     7.584 6.413  0.08279         0.2300
    theta[5]     5.547 6.255  0.08075         0.2392
    theta[6]     6.368 6.645  0.08579         0.2353
    theta[7]    10.223 6.757  0.08723         0.3042
    theta[8]     8.399 7.692  0.09931         0.2578

    2. Quantiles for each variable:

                    2.5%   25%    50%    75% 97.5%
    mu.theta     -1.5973 4.586  7.926 11.138 18.14
    sigma.theta   0.1618 1.971  4.688  8.545 20.11
    theta[1]     -1.9667 5.868 10.089 14.719 31.38
    theta[2]     -4.0934 4.003  7.919 11.560 20.65
    theta[3]    -10.5457 2.158  7.018 11.023 20.60
    theta[4]     -5.2593 3.531  7.681 11.359 20.61
    theta[5]     -8.5426 1.488  6.128  9.851 16.83
    theta[6]     -8.5811 2.288  6.897 10.686 18.81
    theta[7]     -1.2334 5.903  9.764 13.920 25.37
    theta[8]     -6.1549 3.864  8.186 12.310 25.20


.RNG.nameを指定するとaddress 0x0, cause 'non-existent physical address'になるのは環境依存なのか。


タグ:jags rjags R
nice!(0)  コメント(0)  トラックバック(0) 
共通テーマ:学問

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

Facebook コメント

トラックバック 0