In [1]:
date()
'Wed Jan 11 12:43:45 2017'
In [2]:
sapply(c("pipeR", "dplyr", "tidyr", "purrr", "ggplot2", "readr"), require, character.only= TRUE)
Loading required package: pipeR
Loading required package: dplyr

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

Loading required package: tidyr
Loading required package: purrr

Attaching package: 'purrr'

The following objects are masked from 'package:dplyr':

    contains, order_by

Loading required package: ggplot2
Loading required package: readr
pipeR
TRUE
dplyr
TRUE
tidyr
TRUE
purrr
TRUE
ggplot2
TRUE
readr
TRUE

11章 空間構造のある階層ベイズモデル

空間相関

  • 個体差は独立ではない
  • 空間上の配置の影響を受ける
  • 近いところとは似ていて,離れると似ていない

11.1 例題: 一次元空間上の個体数分布

  • 調査区画: 50個
  • とある植物の区画ごとの個体数を数えた
  • 局所密度 に類似性がある
In [3]:
load("data/chap11/Y.RData")
ls.str()
m :  Named num [1:50] 2.15 3.25 4.63 6.23 7.99 ...
Y :  num [1:50] 0 3 2 5 6 16 8 14 11 10 ...
In [4]:
options(repr.plot.width = 4,repr.plot.height = 3)
In [5]:
# Fig. 11.2
data_frame(j = seq_along(Y), y = Y) %>>% 
    ggplot(aes(x = j, y = y)) + 
    geom_point(shape = 21)+ 
    theme_bw() +
    theme(panel.grid = element_blank()) + 
    xlab(expression("Position "*italic("j"))) + 
    ylab(expression("Number of individuals  "*italic("y")[italic("j")])) + 
    ylim(c(0, 25))

11.2 階層ベイズモデルに空間構造をくみこむ

個体数 $y_j$ がすべての区画で共通の平均値 $\lambda$ のポアソン分布に従うとする.

$$ p(y_j \mid \lambda) = \frac{\lambda^{y_j}\exp(-\lambda)}{y_j!} $$

標本平均は,

In [6]:
mean(Y)
10.88

10.9 ぐらいなので,分散も同じぐらいになるはずだが,標本分散は,

In [7]:
var(Y)
27.3730612244898

27.4 ぐらいなので,過分散が生じている.

図 11.2 を見ると,個体数 $y_j$ は位置によって変化しているように見える.

区画ごとに異なる $\lambda_j$ をもつとする. 平均個体数 $\lambda_j$ を線形予測子と対数リンク関数を用いて,以下のようにあらわす.

$$\log \lambda_j =\beta + r_j$$

ここで,

  • $\beta$ は切片で大域的なパラメータ => 無情報事前分布
  • $r_j$ は場所差で局所的なパラメータ => 階層事前分布

11.2.1 空間構造のない階層事前分布

場所差 $r_j$ の階層事前分布

  • 10章のようにすると,$r_j$ はどれも独立になる
    • 平均0,標準偏差s の正規分布
$$ p(r_j \mid s) = \frac{1}{\sqrt{2 \pi s^2}} \exp \left( -\frac{r_j^2}{2s^2} \right) $$

*

In [8]:
readLines("chap11-model5.jags") %>>% cat(sep = "\n")
# GLMM 
model
{
  for (j in 1:N.site){
    Y[j] ~ dpois(lambda[j])
    lambda[j] <- exp(beta + r[j])
    r[j] ~ dnorm(0, tau)
  }
  beta ~ dnorm(0, 1.0E-4)
  tau <- 1 / (s * s)
  s ~ dunif(0, 1.0E+4)
}

In [9]:
data.list <- list(
    N.site = length(Y), 
    Y = Y
)
In [10]:
library(rjags)
Loading required package: coda
Linked to JAGS 4.2.0
Loaded modules: basemod,bugs
In [11]:
m <- jags.model(
    file = "chap11-model5.jags", 
    data = data.list,
    n.chain = 3, 
    n.adapt = 100
)
Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 50
   Unobserved stochastic nodes: 52
   Total graph size: 260

Initializing model

In [12]:
post.jags <- coda.samples(m, variable.names = c("r", "s", "beta"), n.iter = 10000, thin = 10)
In [14]:
# saveRDS(post.jags, file = "chap11-post-jags.rds")
In [13]:
post.jags <- readRDS("chap11-post-jags.rds")
In [14]:
options(repr.plot.width = 4, repr.plot.height = 3)
In [15]:
gp.glmm <- (function(){
    beta <- summary(post.jags[, "beta"])$statistics[["Mean"]]
    Y.mean <- exp(beta + summary(post.jags[, grep("r", varnames(post.jags))])$statistics[, "Mean"])
    Y.qnt <- exp(beta + summary(post.jags[, grep("r", varnames(post.jags))])$quantiles[, c("2.5%", "97.5%")])

    data_frame(site = seq_along(Y), 
               Y = Y,
               Y.mean = Y.mean,
               Y.975 = Y.qnt[, 2],
               Y.025 = Y.qnt[, 1]) %>>% 
        ggplot() +
            geom_point(aes(x = site, y = Y)) +
            geom_line(aes(x = site, y = Y.mean)) +
            geom_ribbon(aes(x = site, ymin = Y.025, ymax = Y.975), alpha = 0.3) + 
            theme_bw() + 
            theme(
                panel.grid = element_blank()
            ) + 
            xlab(expression("Position "*italic("j"))) + 
            ylab(expression("Number of individuals  "*italic("y")[italic("j")])) + 
            ylim(c(0, 30))    
})()
gp.glmm

11.2.2 空間構造のある階層事前分布

場所差$r_j$に制約を設ける

  1. 場所差は「近傍」区画の場所差にしか影響されない
  2. 区画$j$の近傍の個数 $n_j$ は有限個であり,どの区画が近傍であるかはあらかじめ設定する
  3. 近傍の直接の影響はどれも等しく,$1 / n_j$

簡単のために,近傍数は2とする

  • 両隣の影響しか受けない
  • 両端については近傍数1

$r_j$ の事前分布を以下のように設定する

$$ p(r_j \mid \mu_j, s) = \sqrt{\frac{n_j}{2 \pi s^2}} \exp \left\{ -\frac{(r_j - \mu_j)^2}{2s^2 / n_j} \right\} $$

ここで,平均 $\mu_j$ は近傍の平均値に等しいとする

$$ \mu_j = \frac{r_{j-1} + r_{j+1}}{2} $$
  • 両端では,$\mu_1 = r_2$,$\mu_{50} = r_49$
  • 標準偏差は $s / \sqrt{n_j}$
  • $s$ はどこでも等しい

条件つき自己回帰(Conditional Auto Regressive, CAR)モデル

のなかでも

intristic Gaussian CAR モデルという

場所差 $\{ r_j \} = \{ r_1, r_2, \ldots, r_{50}\}$ 全体の事前分布である同時分布は,以下のように書ける

  • $j \sim j^\prime$ は$j$と $j^\prime$ が近傍であるようなすべての $\{j, j^\prime\}$ の組み合わせ
$$ p( \{r_j \} \mid s) \propto \exp \left\{ -\frac{1}{2s^2} \sum\limits_{j \sim j^\prime} (r_j - r_{j^\prime})^2 \right\} $$

この同時分布において,$r_j$を除くすべての$\{ r_* \}$ を定数とおくと,条件つき事前分布 $p(r_j \mid \mu_j, s)$ が得られる(Full Conditional 形式).

11.3 空間統計モデルをデータにあてはめる

事前分布は,

$$ p(\beta, s, \{ r_j \} \mid \boldsymbol{Y}) \propto p(\{ r_j \} \mid s) p(s) p(\beta) \prod\limits_j p(y_j \mid \lambda_j) $$
  • データ $y_j$ が得られる確率: 平均 $\lambda_j = \exp (\beta + r_j)$ のポアソン分布
    • $\beta$の事前分布: 無情報事前分布($p(\beta)$)
    • $r_j$ の事前分布: 空間構造を考慮した階層事前分布(同時分布$p(\{ r_j \} \mid s)$)
  • 個々の$r_j$ の条件付事前分布: 平均 $\mu_j$,標準偏差 $s/\sqrt{n_j}$の正規分布 $p(r_j \mid \mu_j, s)$
    • $s$ の事前分布: 無情報事前分布($p(s)$)

WinBUGS を使う場合

  • car.normal() を使うと,Intristic Gaussian CAR モデルを扱える
In [16]:
readLines("chap11-model.jags", encoding = "UTF-8") %>>% cat(sep = "\n")
# CAR, car.normal() in WinBUGS
model
{
  for (j in 1:N.site) {
    Y[j] ~ dpois(mean[j]) # ポアソン分布
    log(mean[j]) <- beta + r[j] # (切片) + (場所差)
  }
  # 場所差 r[j] をCAR model で生成
  # car.normal()
  ## Adj[]: 隣接する場所の番号
  ## Weights[]: Adj[] に対応する重み
  ## Num[]: 隣接数 n_j
  ## tau: 分散の逆数
  r[1:N.site] ~ car.normal(Adj[], Weights[], Num[], tau)
  beta ~ dnorm(0, 1.0E-4)
  tau <- 1 / (s * s) # tau は分散の逆数
  s ~ dunif(0, 1.0E+4)
}

JAGS を使う場合

car.normal() はないが,Intristic CAR モデル は状態空間モデル の形式で表現することができるので,こちらの形式で考える.


時間・空間を含むベイズモデルは,いくつかの形式で表現できる

(詳しくは

などを参照.以降の説明は 『岩波データサイエンス Vol.1』 より)

岩波データサイエンス Vol.1 では以下の4つが紹介されている.

  1. 状態空間モデル
  2. 条件つき分布
  3. マルコフ場モデル
  4. Full Conditional

(マルコフ場モデルはスキップ)

今回の

$$ p(r_j \mid r_{-j}) = \sqrt{\frac{n_j}{2 \pi s^2}} \exp \left\{-\frac{n_j}{2s^2}\left[ r_j - \frac{1}{2}(r_{j-1} + r_{j+1}) \right]^2 \right\} $$

は,各変数についてそれ以外のすべての変数を固定した条件つき分布であり,(4)Full Conditional な形式である

これは,p.248の注釈9にある通り,有向非巡回グラフ(Directed acyclic graph,DAG)ではないので,JAGS で直接表現できない.

なので,例題を状態空間モデル の形式で表現して,こちらの形式を使ってJAGSで計算する.

一般的なはなし

状態空間モデル

ある隠れた状態 $\{ x_i\} = \{x_1, x_2, \ldots, x_i \}$ から データ $\{ y_i\} = \{y_1, y_2, \ldots, y_i \}$ が観測されるとする.

$x_i$ に システム雑音 $\eta_i$ が加わって $x_{i+1}$ に遷移し,$x_i$ から $y_i$ を観測する際には,観測雑音 $\epsilon_i$ が加わるとすると,

$$ \begin{align} x_{i+1} &= x_i + \eta_i \\ y_i &= x_i + \epsilon_i \end{align} $$

となる.

条件つき分布

(1)の状態空間モデルの形式を,JAGS で書きやすいように, 状態間の移動をあらわす条件つき密度 $p(x_{i+1} \mid x_i )$ と 測定のときの誤差をあらわす条件つき密度 $p(y_i \mid x_i)$ で表現する.

$$ \begin{align} \eta_i &= x_{i+1} - x_i \\ \epsilon_i &= y_i - x_i \end{align} $$

なので,

$$ p(x_{i+1} \mid x_i ) = \frac{1}{\sqrt{2 \pi s^2}} \exp \left( -\frac{1}{2 s^2} (x_{i+1} - x_i)^2 \right)$$$$ p(y_i \mid x_i) = \frac{1}{\sqrt{2 \pi \sigma^2}} \exp \left( -\frac{1}{2 \sigma^2} (y_{i} - x_i)^2 \right) $$

また,初期値 $x_1$ の事前密度 $p(x_1)$を,平均0,分散$r^2$の正規分布とし,$s^2$,$\sigma^2$の事前密度を $p(s^2)$,$p(\sigma^2)$ とすると,ベイズモデルを定義する同時密度は,次のようになる.

$$ p(\{y_i\}, \{x_i\}, s^2, \sigma^2) = p(s^2) p(\sigma^2)p(x_1) \prod\limits^{N}_{i=1}p(y_i \mid x_i) \prod\limits^{N-1}_{i=1}p(x_{i+1} \mid x_i) $$

具体的なはなし

今回の例題だと

  • 観測値 $y$ は平均$\lambda$のポアソン分布に従う
    • $\lambda$ の対数が 状態 $r$ になる
  • 状態 $r$ は標準偏差 $s$ の正規分布にしたがってランダムウォークする
In [17]:
readLines("chap11-model2.jags", encoding = "UTF-8") %>>% cat(sep = "\n")
# CAR, JAGS
model 
{
  for (j in 1:N.site){
    Y[j] ~ dpois(lambda[j])
    lambda[j] <- exp(r[j])
  }
  for (j in 2:N.site){
    r[j] ~ dnorm(r[j - 1], tau)
  }
  r[1] ~ dunif(0, 10)
  tau <- 1 / (s * s)
  s ~ dunif(0, 1.0E+4)
}
In [18]:
data.list2 <- list(
    N.site = length(Y), 
    Y = Y
)
In [19]:
data.list2
$N.site
50
$Y
  1. 0
  2. 3
  3. 2
  4. 5
  5. 6
  6. 16
  7. 8
  8. 14
  9. 11
  10. 10
  11. 17
  12. 19
  13. 14
  14. 19
  15. 19
  16. 18
  17. 15
  18. 13
  19. 13
  20. 9
  21. 11
  22. 15
  23. 18
  24. 12
  25. 11
  26. 17
  27. 14
  28. 16
  29. 15
  30. 9
  31. 6
  32. 15
  33. 10
  34. 11
  35. 14
  36. 7
  37. 14
  38. 14
  39. 13
  40. 17
  41. 8
  42. 7
  43. 10
  44. 4
  45. 5
  46. 5
  47. 7
  48. 4
  49. 3
  50. 1
In [20]:
m2 <- jags.model(
    file = "chap11-model2.jags", 
    data = data.list2,
    n.chain = 3, 
    n.adapt = 100
)
Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 50
   Unobserved stochastic nodes: 51
   Total graph size: 159

Initializing model

In [24]:
post.jags2 <- coda.samples(m2, variable.names = c("r", "s"), n.iter = 10000, thin = 10)
In [25]:
# saveRDS(post.jags2, file = "chap11-post-jags2.rds")
In [21]:
post.jags2 <- readRDS(file = "chap11-post-jags2.rds")
In [23]:
summary(post.jags2)
Iterations = 110:10100
Thinning interval = 10 
Number of chains = 3 
Sample size per chain = 1000 

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

        Mean       SD Naive SE Time-series SE
r[1]   1.028   0.3626 0.006620       0.008382
r[2]   1.187   0.3085 0.005632       0.006794
r[3]   1.357   0.2880 0.005259       0.007210
r[4]   1.646   0.2347 0.004285       0.004829
r[5]   1.928   0.2096 0.003826       0.004078
r[6]   2.280   0.2008 0.003667       0.005134
r[7]   2.293   0.1871 0.003415       0.003467
r[8]   2.440   0.1834 0.003349       0.003399
r[9]   2.450   0.1812 0.003309       0.003306
r[10]  2.506   0.1762 0.003217       0.003290
r[11]  2.701   0.1691 0.003087       0.003087
r[12]  2.796   0.1637 0.002989       0.003068
r[13]  2.767   0.1653 0.003017       0.003212
r[14]  2.853   0.1609 0.002938       0.002938
r[15]  2.865   0.1593 0.002908       0.003010
r[16]  2.813   0.1612 0.002943       0.002944
r[17]  2.699   0.1618 0.002953       0.002961
r[18]  2.598   0.1734 0.003165       0.003166
r[19]  2.535   0.1742 0.003180       0.003250
r[20]  2.440   0.1923 0.003511       0.003768
r[21]  2.496   0.1760 0.003213       0.003213
r[22]  2.625   0.1697 0.003099       0.003192
r[23]  2.684   0.1655 0.003022       0.002957
r[24]  2.587   0.1692 0.003090       0.003073
r[25]  2.564   0.1703 0.003109       0.003181
r[26]  2.670   0.1675 0.003058       0.003192
r[27]  2.651   0.1699 0.003102       0.003151
r[28]  2.648   0.1707 0.003117       0.003220
r[29]  2.560   0.1697 0.003098       0.003351
r[30]  2.371   0.1847 0.003372       0.003727
r[31]  2.291   0.2011 0.003672       0.005042
r[32]  2.438   0.1765 0.003222       0.003222
r[33]  2.391   0.1824 0.003329       0.003217
r[34]  2.416   0.1846 0.003371       0.003454
r[35]  2.450   0.1779 0.003247       0.003247
r[36]  2.367   0.1919 0.003504       0.004874
r[37]  2.497   0.1763 0.003218       0.003560
r[38]  2.540   0.1748 0.003192       0.003308
r[39]  2.531   0.1755 0.003204       0.003203
r[40]  2.522   0.1818 0.003320       0.003949
r[41]  2.257   0.1874 0.003421       0.003484
r[42]  2.102   0.1960 0.003578       0.003579
r[43]  2.017   0.1985 0.003625       0.003626
r[44]  1.800   0.2185 0.003989       0.004914
r[45]  1.703   0.2242 0.004093       0.004420
r[46]  1.636   0.2347 0.004285       0.004482
r[47]  1.588   0.2363 0.004314       0.005283
r[48]  1.418   0.2602 0.004750       0.005194
r[49]  1.265   0.2937 0.005363       0.006015
r[50]  1.131   0.3812 0.006961       0.011552
s     22.782 267.1561 4.877580      14.435658

2. Quantiles for each variable:

        2.5%    25%    50%    75%  97.5%
r[1]  0.2484 0.7892 1.0519 1.2845 1.6705
r[2]  0.5411 0.9962 1.2068 1.3967 1.7300
r[3]  0.7384 1.1884 1.3712 1.5586 1.8559
r[4]  1.1562 1.4965 1.6519 1.8096 2.0803
r[5]  1.5167 1.7887 1.9360 2.0681 2.3351
r[6]  1.9029 2.1471 2.2752 2.4080 2.6870
r[7]  1.9036 2.1724 2.2976 2.4236 2.6482
r[8]  2.0580 2.3230 2.4441 2.5606 2.7868
r[9]  2.0739 2.3357 2.4574 2.5745 2.7915
r[10] 2.1514 2.3933 2.5087 2.6237 2.8477
r[11] 2.3814 2.5903 2.6965 2.8124 3.0371
r[12] 2.4819 2.6859 2.7924 2.9049 3.1246
r[13] 2.4447 2.6589 2.7702 2.8772 3.0892
r[14] 2.5376 2.7441 2.8519 2.9612 3.1612
r[15] 2.5567 2.7604 2.8630 2.9722 3.1850
r[16] 2.4913 2.7043 2.8155 2.9211 3.1280
r[17] 2.3722 2.5971 2.7048 2.8085 3.0049
r[18] 2.2454 2.4818 2.6059 2.7125 2.9235
r[19] 2.1743 2.4230 2.5437 2.6532 2.8648
r[20] 2.0411 2.3203 2.4521 2.5703 2.7814
r[21] 2.1241 2.3849 2.4955 2.6143 2.8325
r[22] 2.2914 2.5109 2.6246 2.7385 2.9530
r[23] 2.3719 2.5714 2.6808 2.7969 3.0189
r[24] 2.2520 2.4783 2.5912 2.7016 2.9079
r[25] 2.2231 2.4516 2.5691 2.6796 2.8842
r[26] 2.3499 2.5543 2.6702 2.7818 3.0017
r[27] 2.3055 2.5408 2.6498 2.7661 2.9824
r[28] 2.3150 2.5328 2.6494 2.7617 2.9779
r[29] 2.2292 2.4496 2.5589 2.6754 2.8922
r[30] 1.9982 2.2514 2.3763 2.4989 2.7108
r[31] 1.8746 2.1720 2.3025 2.4242 2.6470
r[32] 2.0772 2.3219 2.4420 2.5526 2.7819
r[33] 2.0190 2.2745 2.3967 2.5147 2.7420
r[34] 2.0435 2.3018 2.4177 2.5422 2.7785
r[35] 2.0920 2.3312 2.4510 2.5694 2.8020
r[36] 1.9624 2.2561 2.3765 2.4964 2.7208
r[37] 2.1615 2.3791 2.4971 2.6161 2.8465
r[38] 2.1976 2.4220 2.5400 2.6587 2.8740
r[39] 2.1850 2.4128 2.5340 2.6488 2.8706
r[40] 2.1765 2.3992 2.5156 2.6410 2.8857
r[41] 1.8817 2.1353 2.2630 2.3827 2.6139
r[42] 1.7004 1.9782 2.1067 2.2363 2.4764
r[43] 1.6195 1.8856 2.0212 2.1472 2.4017
r[44] 1.3901 1.6615 1.8105 1.9454 2.1975
r[45] 1.2238 1.5648 1.7074 1.8568 2.1105
r[46] 1.1507 1.4881 1.6487 1.7908 2.0770
r[47] 1.1368 1.4339 1.5894 1.7462 2.0435
r[48] 0.8899 1.2494 1.4274 1.5941 1.9018
r[49] 0.6692 1.0809 1.2799 1.4613 1.8022
r[50] 0.3661 0.9255 1.1540 1.3749 1.7606
s     0.1427 0.1946 0.2268 0.2644 0.3682
In [24]:
gelman.diag(post.jags2)
Potential scale reduction factors:

      Point est. Upper C.I.
r[1]       1.004      1.014
r[2]       1.000      1.003
r[3]       1.003      1.012
r[4]       1.003      1.014
r[5]       1.000      1.002
r[6]       1.000      1.001
r[7]       1.001      1.005
r[8]       1.007      1.028
r[9]       1.003      1.015
r[10]      1.001      1.007
r[11]      1.002      1.007
r[12]      1.003      1.009
r[13]      1.001      1.003
r[14]      0.999      0.999
r[15]      1.000      1.000
r[16]      1.002      1.007
r[17]      1.002      1.003
r[18]      1.000      1.001
r[19]      1.001      1.003
r[20]      1.001      1.002
r[21]      1.000      1.003
r[22]      1.002      1.004
r[23]      1.000      1.002
r[24]      1.000      1.003
r[25]      1.002      1.008
r[26]      1.000      1.003
r[27]      1.005      1.017
r[28]      1.001      1.004
r[29]      1.003      1.008
r[30]      1.000      1.003
r[31]      1.000      1.002
r[32]      1.002      1.004
r[33]      1.000      1.000
r[34]      1.003      1.014
r[35]      1.001      1.006
r[36]      1.002      1.004
r[37]      0.999      1.000
r[38]      1.001      1.003
r[39]      1.005      1.012
r[40]      1.006      1.018
r[41]      1.003      1.007
r[42]      1.000      1.003
r[43]      0.999      1.001
r[44]      1.000      1.001
r[45]      1.000      1.001
r[46]      1.000      1.001
r[47]      1.001      1.005
r[48]      1.002      1.011
r[49]      1.006      1.022
r[50]      1.004      1.016
s          1.010      1.035

Multivariate psrf

1.03
In [25]:
varnames(post.jags2)
  1. 'r[1]'
  2. 'r[2]'
  3. 'r[3]'
  4. 'r[4]'
  5. 'r[5]'
  6. 'r[6]'
  7. 'r[7]'
  8. 'r[8]'
  9. 'r[9]'
  10. 'r[10]'
  11. 'r[11]'
  12. 'r[12]'
  13. 'r[13]'
  14. 'r[14]'
  15. 'r[15]'
  16. 'r[16]'
  17. 'r[17]'
  18. 'r[18]'
  19. 'r[19]'
  20. 'r[20]'
  21. 'r[21]'
  22. 'r[22]'
  23. 'r[23]'
  24. 'r[24]'
  25. 'r[25]'
  26. 'r[26]'
  27. 'r[27]'
  28. 'r[28]'
  29. 'r[29]'
  30. 'r[30]'
  31. 'r[31]'
  32. 'r[32]'
  33. 'r[33]'
  34. 'r[34]'
  35. 'r[35]'
  36. 'r[36]'
  37. 'r[37]'
  38. 'r[38]'
  39. 'r[39]'
  40. 'r[40]'
  41. 'r[41]'
  42. 'r[42]'
  43. 'r[43]'
  44. 'r[44]'
  45. 'r[45]'
  46. 'r[46]'
  47. 'r[47]'
  48. 'r[48]'
  49. 'r[49]'
  50. 'r[50]'
  51. 's'
In [26]:
gp.car <- (function(){
    Y.mean <- exp(summary(post.jags2[, grep("r", varnames(post.jags2))])$statistics[, "Mean"])
    Y.qnt <- exp(summary(post.jags2[, grep("r", varnames(post.jags2))])$quantiles[, c("2.5%", "97.5%")])

    data_frame(site = seq_along(Y), 
               Y = Y,
               Y.mean = Y.mean,
               Y.975 = Y.qnt[, 2],
               Y.025 = Y.qnt[, 1]) %>>% 
        ggplot() +
            geom_point(aes(x = site, y = Y)) +
            geom_line(aes(x = site, y = Y.mean)) +
            geom_ribbon(aes(x = site, ymin = Y.025, ymax = Y.975), alpha = 0.3) + 
            theme_bw() + 
            theme(
                panel.grid = element_blank()
            ) + 
            xlab(expression("Position "*italic("j"))) + 
            ylab(expression("Number of individuals  "*italic("y")[italic("j")])) + 
            ylim(c(0, 30))
})()
In [27]:
options(repr.plot.width = 8)
  • 左: 空間相関あり
  • 右: 空間相関なし
In [28]:
library(gridExtra)
Attaching package: 'gridExtra'

The following object is masked from 'package:dplyr':

    combine

In [29]:
grid.arrange(gp.car, gp.glmm, ncol = 2)

11.4 空間統計モデルが作り出す確率場

  • 相互作用する確率変数でうめつくされた空間は確率場と呼ばれる
    • $\{r_i\}$ は マルコフ確率場

隣と似ている度合いが変わると確率場$\{r_i\}$はどう影響されるか

In [30]:
readLines("chap11-model3.jags") %>>% cat(sep = "\n")
# CAR, various s, JAGS
model
{
  for (j in 1:3) {
    for (i in 1:N.site) {
      Y[i, j] ~ dpois(lambda[i, j])
      lambda[i, j] <- exp(r[i, j])
    }
    for (i in 2:N.site){
      r[i, j] ~ dnorm(r[i - 1, j], Tau[j])
    }
    r[1, j] ~ dunif(0, 10)
  }
}
In [31]:
data.list3 <- list(
    N.site = length(Y), 
    Y = matrix(c(Y, Y, Y), length(Y), 3), 
    Tau = c(1000, 20, 0.01)
)
In [32]:
m3 <- jags.model(
    file = "chap11-model3.jags", 
    data = data.list3,
    n.chain = 3, 
    n.adapt = 100
)
Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 150
   Unobserved stochastic nodes: 150
   Total graph size: 460

Initializing model

In [40]:
post.jags3 <- coda.samples(m3, variable.names = c("r"), n.iter = 200, thin = 1)
In [41]:
# saveRDS(post.jags3, file = "chap11-post-jags3.rds")
In [33]:
post.jags3 <- readRDS("chap11-post-jags3.rds")
In [34]:
summary(post.jags3)
Iterations = 101:300
Thinning interval = 1 
Number of chains = 3 
Sample size per chain = 200 

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

           Mean      SD Naive SE Time-series SE
r[1,1]   2.3772 0.11304 0.004615       0.026603
r[2,1]   2.3881 0.10965 0.004476       0.027653
r[3,1]   2.4068 0.10653 0.004349       0.027028
r[4,1]   2.4345 0.10100 0.004123       0.024418
r[5,1]   2.4661 0.09940 0.004058       0.025225
r[6,1]   2.5025 0.10059 0.004107       0.027585
r[7,1]   2.5316 0.10087 0.004118       0.028329
r[8,1]   2.5636 0.10195 0.004162       0.027207
r[9,1]   2.5899 0.10145 0.004142       0.027897
r[10,1]  2.6173 0.10058 0.004106       0.029495
r[11,1]  2.6474 0.10185 0.004158       0.035515
r[12,1]  2.6719 0.09838 0.004017       0.032816
r[13,1]  2.6926 0.09697 0.003959       0.027790
r[14,1]  2.7090 0.09462 0.003863       0.025108
r[15,1]  2.7211 0.08942 0.003651       0.019470
r[16,1]  2.7275 0.08440 0.003446       0.019391
r[17,1]  2.7282 0.08200 0.003348       0.018844
r[18,1]  2.7269 0.08135 0.003321       0.020526
r[19,1]  2.7230 0.08294 0.003386       0.017946
r[20,1]  2.7203 0.08312 0.003393       0.018191
r[21,1]  2.7221 0.08092 0.003303       0.020552
r[22,1]  2.7215 0.07784 0.003178       0.019361
r[23,1]  2.7189 0.07988 0.003261       0.018265
r[24,1]  2.7133 0.08095 0.003305       0.017117
r[25,1]  2.7086 0.08242 0.003365       0.019413
r[26,1]  2.7047 0.08314 0.003394       0.018543
r[27,1]  2.6989 0.08598 0.003510       0.021605
r[28,1]  2.6928 0.08979 0.003666       0.021331
r[29,1]  2.6814 0.09094 0.003713       0.023234
r[30,1]  2.6666 0.09259 0.003780       0.025325
r[31,1]  2.6545 0.09016 0.003681       0.026499
r[32,1]  2.6471 0.09111 0.003720       0.027052
r[33,1]  2.6344 0.09401 0.003838       0.026944
r[34,1]  2.6204 0.09468 0.003865       0.025226
r[35,1]  2.6056 0.09807 0.004004       0.027715
r[36,1]  2.5899 0.10211 0.004169       0.027054
r[37,1]  2.5782 0.10501 0.004287       0.030364
r[38,1]  2.5633 0.10957 0.004473       0.027538
r[39,1]  2.5450 0.11479 0.004686       0.026700
r[40,1]  2.5252 0.11989 0.004894       0.027765
r[41,1]  2.4965 0.12489 0.005099       0.029031
r[42,1]  2.4703 0.12962 0.005292       0.031310
r[43,1]  2.4461 0.13435 0.005485       0.034832
r[44,1]  2.4221 0.14125 0.005766       0.038130
r[45,1]  2.4056 0.14951 0.006104       0.046058
r[46,1]  2.3894 0.15434 0.006301       0.044845
r[47,1]  2.3779 0.15953 0.006513       0.050143
r[48,1]  2.3663 0.16105 0.006575       0.046451
r[49,1]  2.3559 0.16200 0.006614       0.045179
r[50,1]  2.3513 0.16107 0.006576       0.042494
r[1,2]   1.0846 0.30675 0.012523       0.027956
r[2,2]   1.2170 0.25661 0.010476       0.027177
r[3,2]   1.3814 0.23776 0.009707       0.022681
r[4,2]   1.6494 0.20888 0.008527       0.022509
r[5,2]   1.9293 0.19457 0.007943       0.020354
r[6,2]   2.2677 0.19117 0.007805       0.015789
r[7,2]   2.2812 0.19726 0.008053       0.017561
r[8,2]   2.4253 0.18257 0.007453       0.016845
r[9,2]   2.4485 0.17711 0.007231       0.012847
r[10,2]  2.5130 0.17668 0.007213       0.013190
r[11,2]  2.6942 0.16397 0.006694       0.013811
r[12,2]  2.7755 0.15486 0.006322       0.011889
r[13,2]  2.7675 0.16148 0.006593       0.011570
r[14,2]  2.8569 0.16546 0.006755       0.012913
r[15,2]  2.8637 0.15671 0.006398       0.011993
r[16,2]  2.8073 0.15256 0.006228       0.010284
r[17,2]  2.6942 0.15823 0.006460       0.010207
r[18,2]  2.5952 0.15590 0.006365       0.010309
r[19,2]  2.5294 0.16936 0.006914       0.011280
r[20,2]  2.4519 0.16923 0.006909       0.011574
r[21,2]  2.5197 0.16393 0.006692       0.010955
r[22,2]  2.6387 0.16195 0.006611       0.012025
r[23,2]  2.6980 0.15391 0.006283       0.009468
r[24,2]  2.5882 0.15816 0.006457       0.011743
r[25,2]  2.5650 0.16695 0.006816       0.011144
r[26,2]  2.6467 0.16897 0.006898       0.013529
r[27,2]  2.6328 0.16276 0.006645       0.011361
r[28,2]  2.6378 0.16051 0.006553       0.011585
r[29,2]  2.5631 0.18349 0.007491       0.015095
r[30,2]  2.3958 0.18917 0.007723       0.015629
r[31,2]  2.3001 0.18263 0.007456       0.016991
r[32,2]  2.4324 0.17365 0.007089       0.013703
r[33,2]  2.3906 0.16515 0.006742       0.012355
r[34,2]  2.4092 0.17084 0.006974       0.014513
r[35,2]  2.4311 0.17971 0.007337       0.013868
r[36,2]  2.3602 0.19344 0.007897       0.018718
r[37,2]  2.4946 0.18295 0.007469       0.014656
r[38,2]  2.5423 0.17576 0.007175       0.015729
r[39,2]  2.5220 0.15928 0.006502       0.013846
r[40,2]  2.5027 0.17784 0.007260       0.016214
r[41,2]  2.2655 0.19153 0.007819       0.021552
r[42,2]  2.1106 0.19849 0.008103       0.016024
r[43,2]  2.0001 0.19829 0.008095       0.016979
r[44,2]  1.7770 0.20186 0.008241       0.016584
r[45,2]  1.6931 0.21255 0.008677       0.019940
r[46,2]  1.6415 0.22743 0.009285       0.023183
r[47,2]  1.5906 0.22813 0.009314       0.021344
r[48,2]  1.4248 0.24241 0.009896       0.026750
r[49,2]  1.2693 0.26991 0.011019       0.032954
r[50,2]  1.1449 0.30807 0.012577       0.029395
r[1,3]   0.4443 0.33700 0.013758       0.021511
r[2,3]   0.9255 0.59175 0.024158       0.027650
r[3,3]   0.4933 0.74590 0.030451       0.043977
r[4,3]   1.5031 0.41361 0.016886       0.021095
r[5,3]   1.7077 0.42940 0.017530       0.021565
r[6,3]   2.7246 0.25597 0.010450       0.012595
r[7,3]   1.9735 0.38711 0.015804       0.020652
r[8,3]   2.5929 0.25987 0.010609       0.012307
r[9,3]   2.3672 0.31407 0.012822       0.014312
r[10,3]  2.2673 0.34013 0.013886       0.015436
r[11,3]  2.8069 0.24710 0.010088       0.013332
r[12,3]  2.9156 0.21885 0.008934       0.012974
r[13,3]  2.5685 0.28936 0.011813       0.014968
r[14,3]  2.9154 0.23844 0.009734       0.012841
r[15,3]  2.9147 0.23834 0.009730       0.011166
r[16,3]  2.8707 0.22803 0.009309       0.012442
r[17,3]  2.6703 0.26099 0.010655       0.012386
r[18,3]  2.5229 0.26518 0.010826       0.014262
r[19,3]  2.5302 0.29139 0.011896       0.016231
r[20,3]  2.1151 0.34411 0.014048       0.020729
r[21,3]  2.3240 0.30995 0.012654       0.016403
r[22,3]  2.6608 0.25991 0.010611       0.011811
r[23,3]  2.8692 0.24280 0.009912       0.013542
r[24,3]  2.4323 0.28139 0.011488       0.014155
r[25,3]  2.3506 0.31286 0.012772       0.015392
r[26,3]  2.8023 0.23646 0.009653       0.010595
r[27,3]  2.5986 0.28029 0.011443       0.014868
r[28,3]  2.7427 0.24917 0.010172       0.013875
r[29,3]  2.6506 0.25935 0.010588       0.013859
r[30,3]  2.1357 0.34182 0.013955       0.016629
r[31,3]  1.6730 0.44631 0.018221       0.025769
r[32,3]  2.6793 0.25078 0.010238       0.010792
r[33,3]  2.2433 0.32844 0.013408       0.019290
r[34,3]  2.3594 0.28658 0.011700       0.013881
r[35,3]  2.6069 0.31427 0.012830       0.019480
r[36,3]  1.8990 0.34921 0.014256       0.017597
r[37,3]  2.6236 0.27463 0.011212       0.014002
r[38,3]  2.6241 0.26802 0.010942       0.012476
r[39,3]  2.5344 0.26763 0.010926       0.014468
r[40,3]  2.8041 0.22455 0.009167       0.011035
r[41,3]  2.0428 0.34219 0.013970       0.015358
r[42,3]  1.8532 0.42411 0.017314       0.028061
r[43,3]  2.2445 0.33903 0.013841       0.016749
r[44,3]  1.2536 0.51073 0.020851       0.026066
r[45,3]  1.4831 0.47380 0.019343       0.025014
r[46,3]  1.4875 0.48703 0.019883       0.022831
r[47,3]  1.8589 0.41811 0.017069       0.019474
r[48,3]  1.2253 0.54648 0.022310       0.030208
r[49,3]  0.8373 0.66182 0.027019       0.037650
r[50,3] -0.5464 1.23721 0.050509       0.071745

2. Quantiles for each variable:

             2.5%      25%     50%    75% 97.5%
r[1,1]   2.182520  2.28711  2.3775 2.4636 2.601
r[2,1]   2.198216  2.29720  2.3849 2.4712 2.590
r[3,1]   2.227055  2.31978  2.4020 2.4879 2.612
r[4,1]   2.275625  2.35171  2.4320 2.5092 2.646
r[5,1]   2.320767  2.38483  2.4551 2.5396 2.682
r[6,1]   2.352158  2.42331  2.4805 2.5737 2.725
r[7,1]   2.371579  2.45936  2.5098 2.6087 2.749
r[8,1]   2.402606  2.48714  2.5465 2.6411 2.779
r[9,1]   2.421651  2.51316  2.5728 2.6697 2.790
r[10,1]  2.446041  2.54175  2.6059 2.6999 2.803
r[11,1]  2.465742  2.57676  2.6339 2.7258 2.831
r[12,1]  2.484258  2.61004  2.6591 2.7452 2.854
r[13,1]  2.503912  2.62950  2.6867 2.7598 2.879
r[14,1]  2.534427  2.64441  2.7038 2.7693 2.900
r[15,1]  2.546499  2.66187  2.7136 2.7761 2.912
r[16,1]  2.574178  2.67260  2.7219 2.7801 2.923
r[17,1]  2.577998  2.67135  2.7273 2.7793 2.912
r[18,1]  2.556101  2.67181  2.7291 2.7806 2.901
r[19,1]  2.563921  2.66483  2.7240 2.7759 2.912
r[20,1]  2.555212  2.66861  2.7215 2.7681 2.901
r[21,1]  2.576931  2.66857  2.7210 2.7675 2.889
r[22,1]  2.588836  2.66974  2.7195 2.7687 2.886
r[23,1]  2.587380  2.66328  2.7112 2.7743 2.906
r[24,1]  2.577151  2.65873  2.6977 2.7630 2.887
r[25,1]  2.567445  2.65327  2.6931 2.7564 2.893
r[26,1]  2.569537  2.65059  2.6876 2.7580 2.886
r[27,1]  2.569908  2.63929  2.6823 2.7475 2.886
r[28,1]  2.553286  2.62995  2.6779 2.7433 2.877
r[29,1]  2.546744  2.61770  2.6597 2.7291 2.894
r[30,1]  2.538794  2.60542  2.6436 2.7113 2.903
r[31,1]  2.528607  2.59687  2.6281 2.6950 2.881
r[32,1]  2.518996  2.58963  2.6247 2.6775 2.862
r[33,1]  2.501814  2.57211  2.6162 2.6658 2.852
r[34,1]  2.487624  2.55587  2.5960 2.6556 2.854
r[35,1]  2.456509  2.54045  2.5823 2.6514 2.844
r[36,1]  2.426262  2.52178  2.5639 2.6481 2.825
r[37,1]  2.407072  2.50833  2.5538 2.6484 2.806
r[38,1]  2.371737  2.48400  2.5399 2.6456 2.796
r[39,1]  2.334819  2.46919  2.5226 2.6335 2.769
r[40,1]  2.281238  2.44838  2.5080 2.6184 2.763
r[41,1]  2.263678  2.41364  2.4809 2.5924 2.724
r[42,1]  2.229205  2.38736  2.4479 2.5606 2.715
r[43,1]  2.208900  2.35602  2.4225 2.5449 2.710
r[44,1]  2.192374  2.32726  2.3961 2.5095 2.722
r[45,1]  2.177361  2.29734  2.3803 2.4966 2.731
r[46,1]  2.135164  2.27770  2.3776 2.4846 2.740
r[47,1]  2.125852  2.25868  2.3693 2.4779 2.734
r[48,1]  2.101167  2.24887  2.3645 2.4551 2.724
r[49,1]  2.098438  2.23140  2.3612 2.4437 2.718
r[50,1]  2.085205  2.23308  2.3448 2.4397 2.708
r[1,2]   0.504617  0.85827  1.1070 1.2986 1.663
r[2,2]   0.679536  1.05316  1.2243 1.3906 1.682
r[3,2]   0.888632  1.23756  1.3917 1.5294 1.888
r[4,2]   1.262871  1.50990  1.6477 1.7747 2.091
r[5,2]   1.573755  1.79155  1.9297 2.0567 2.333
r[6,2]   1.883320  2.14439  2.2705 2.4026 2.614
r[7,2]   1.887247  2.15385  2.2761 2.4049 2.644
r[8,2]   2.044298  2.30789  2.4221 2.5423 2.777
r[9,2]   2.119503  2.32139  2.4458 2.5672 2.808
r[10,2]  2.163455  2.39005  2.5162 2.6389 2.837
r[11,2]  2.345392  2.58410  2.6993 2.8047 2.998
r[12,2]  2.476801  2.67217  2.7765 2.8795 3.081
r[13,2]  2.433907  2.66690  2.7723 2.8829 3.060
r[14,2]  2.536946  2.74459  2.8615 2.9694 3.171
r[15,2]  2.556649  2.76081  2.8696 2.9777 3.166
r[16,2]  2.533896  2.69211  2.7959 2.9207 3.103
r[17,2]  2.391976  2.58314  2.6888 2.8021 2.992
r[18,2]  2.299989  2.48428  2.5899 2.7088 2.891
r[19,2]  2.199945  2.41762  2.5360 2.6290 2.884
r[20,2]  2.127007  2.33305  2.4557 2.5734 2.760
r[21,2]  2.215933  2.41520  2.5086 2.6176 2.841
r[22,2]  2.332706  2.53231  2.6357 2.7380 2.976
r[23,2]  2.392071  2.58671  2.7011 2.8081 2.978
r[24,2]  2.290681  2.47195  2.5842 2.7008 2.869
r[25,2]  2.231117  2.45742  2.5767 2.6820 2.877
r[26,2]  2.325461  2.52598  2.6552 2.7634 2.964
r[27,2]  2.292840  2.52298  2.6452 2.7347 2.957
r[28,2]  2.328200  2.52197  2.6346 2.7380 2.955
r[29,2]  2.238331  2.43498  2.5557 2.6741 2.964
r[30,2]  2.056545  2.25983  2.3918 2.5245 2.771
r[31,2]  1.954247  2.17074  2.3054 2.4223 2.661
r[32,2]  2.101134  2.31513  2.4368 2.5555 2.749
r[33,2]  2.056399  2.28243  2.3954 2.4932 2.720
r[34,2]  2.062515  2.29349  2.4082 2.5340 2.721
r[35,2]  2.087256  2.30891  2.4270 2.5527 2.783
r[36,2]  1.944982  2.23287  2.3727 2.4944 2.707
r[37,2]  2.130806  2.37385  2.4980 2.6176 2.843
r[38,2]  2.199723  2.42642  2.5401 2.6529 2.866
r[39,2]  2.221305  2.41349  2.5198 2.6291 2.841
r[40,2]  2.161195  2.38147  2.5037 2.6216 2.847
r[41,2]  1.920625  2.12391  2.2477 2.3844 2.656
r[42,2]  1.716738  1.98250  2.0999 2.2390 2.509
r[43,2]  1.580546  1.85932  2.0133 2.1374 2.359
r[44,2]  1.357574  1.64936  1.7746 1.9056 2.161
r[45,2]  1.225077  1.55949  1.6944 1.8386 2.093
r[46,2]  1.195778  1.48615  1.6528 1.8084 2.063
r[47,2]  1.148180  1.43534  1.5980 1.7551 2.033
r[48,2]  0.957998  1.25129  1.4266 1.5967 1.890
r[49,2]  0.747026  1.09579  1.2711 1.4559 1.770
r[50,2]  0.519589  0.90952  1.1444 1.3628 1.701
r[1,3]   0.009629  0.17485  0.3744 0.6506 1.183
r[2,3]  -0.411920  0.57654  0.9770 1.3296 1.938
r[3,3]  -1.198216  0.04774  0.5932 0.9822 1.745
r[4,3]   0.575012  1.24939  1.5370 1.7798 2.258
r[5,3]   0.803352  1.42588  1.7255 2.0117 2.484
r[6,3]   2.239267  2.55574  2.7400 2.9067 3.193
r[7,3]   1.084395  1.74839  2.0071 2.2299 2.636
r[8,3]   2.056210  2.41620  2.6187 2.7670 3.044
r[9,3]   1.708184  2.16875  2.3744 2.5729 2.964
r[10,3]  1.529929  2.03429  2.2906 2.5097 2.904
r[11,3]  2.260301  2.64733  2.8219 2.9813 3.237
r[12,3]  2.481040  2.77521  2.9331 3.0835 3.312
r[13,3]  1.991406  2.37845  2.5801 2.7842 3.069
r[14,3]  2.432101  2.74920  2.9151 3.0718 3.358
r[15,3]  2.445858  2.74849  2.9245 3.0918 3.355
r[16,3]  2.393093  2.71567  2.8872 3.0267 3.275
r[17,3]  2.174862  2.48952  2.6815 2.8224 3.177
r[18,3]  1.931819  2.36145  2.5313 2.6933 3.013
r[19,3]  1.864865  2.33899  2.5489 2.7392 3.034
r[20,3]  1.359730  1.92405  2.1480 2.3368 2.707
r[21,3]  1.686434  2.12123  2.3331 2.5419 2.888
r[22,3]  2.106379  2.50260  2.6802 2.8322 3.101
r[23,3]  2.367941  2.71210  2.8792 3.0498 3.295
r[24,3]  1.875957  2.23910  2.4581 2.6293 2.939
r[25,3]  1.670070  2.14451  2.3677 2.5674 2.909
r[26,3]  2.314458  2.65006  2.8196 2.9758 3.217
r[27,3]  2.050715  2.41247  2.6109 2.7916 3.107
r[28,3]  2.207959  2.58609  2.7539 2.9240 3.221
r[29,3]  2.117793  2.48778  2.6756 2.8219 3.140
r[30,3]  1.432188  1.89728  2.1391 2.3789 2.776
r[31,3]  0.741114  1.40578  1.7047 1.9833 2.453
r[32,3]  2.190002  2.51937  2.6747 2.8460 3.148
r[33,3]  1.538408  2.04818  2.2702 2.4588 2.854
r[34,3]  1.772792  2.15998  2.3735 2.5486 2.881
r[35,3]  1.967653  2.40079  2.6201 2.8266 3.221
r[36,3]  1.185802  1.66943  1.8998 2.1428 2.538
r[37,3]  2.037711  2.44676  2.6321 2.8004 3.142
r[38,3]  2.110660  2.46565  2.6309 2.7957 3.107
r[39,3]  1.969091  2.36883  2.5537 2.7134 2.996
r[40,3]  2.391699  2.64688  2.7903 2.9690 3.240
r[41,3]  1.283503  1.83198  2.0722 2.2786 2.651
r[42,3]  0.939305  1.57482  1.9001 2.1604 2.583
r[43,3]  1.456985  2.04821  2.2670 2.4661 2.833
r[44,3]  0.112312  0.91203  1.2971 1.6078 2.099
r[45,3]  0.447473  1.18763  1.5247 1.8194 2.238
r[46,3]  0.440559  1.21019  1.5250 1.8186 2.314
r[47,3]  0.904927  1.60301  1.8780 2.1458 2.593
r[48,3]  0.150188  0.86859  1.2328 1.6299 2.220
r[49,3] -0.532854  0.41864  0.8974 1.2940 2.029
r[50,3] -3.502635 -1.20133 -0.3580 0.3100 1.373
In [35]:
str(post.jags3)
List of 3
 $ : mcmc [1:200, 1:150] 2.59 2.62 2.61 2.65 2.61 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:150] "r[1,1]" "r[2,1]" "r[3,1]" "r[4,1]" ...
  ..- attr(*, "mcpar")= num [1:3] 101 300 1
 $ : mcmc [1:200, 1:150] 2.64 2.62 2.64 2.59 2.59 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:150] "r[1,1]" "r[2,1]" "r[3,1]" "r[4,1]" ...
  ..- attr(*, "mcpar")= num [1:3] 101 300 1
 $ : mcmc [1:200, 1:150] 2.6 2.6 2.58 2.62 2.61 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:150] "r[1,1]" "r[2,1]" "r[3,1]" "r[4,1]" ...
  ..- attr(*, "mcpar")= num [1:3] 101 300 1
 - attr(*, "class")= chr "mcmc.list"
  • s = 0.0316
In [36]:
options(repr.plot.width = 4)
In [37]:
post.jags3 %>>% purrr::map_df(function(x){as_data_frame(x)}) %>>% dplyr::sample_n(3) %>>% 
    select(ends_with("1]")) %>>% t() %>>% as_data_frame() %>>% 
    purrr::dmap(function(x){exp(x)}) %>>% 
    mutate(site = seq_along(Y), Y = Y) %>>% 
    ggplot() + 
        geom_point(aes(x = site, y = Y)) + 
        geom_line(aes(x = site, y = V1), colour = gray(0.6)) + 
        geom_line(aes(x = site, y = V2), colour = gray(0.65)) + 
        geom_line(aes(x = site, y = V3), colour = gray(0.7)) + 
        theme_bw() + theme(panel.grid = element_blank()) + 
        xlab(expression("Position "*italic("j"))) + 
        ylab(expression("Number of individuals  "*italic("y")[italic("j")])) + 
        ylim(c(0, 25))
  • s = 0.224
In [38]:
post.jags3 %>>% purrr::map_df(function(x){as_data_frame(x)}) %>>% dplyr::sample_n(3) %>>% 
    select(ends_with("2]")) %>>% t() %>>% as_data_frame() %>>% 
    purrr::dmap(function(x){exp(x)}) %>>% 
    mutate(site = seq_along(Y), Y = Y) %>>% 
    ggplot() + 
        geom_point(aes(x = site, y = Y)) + 
        geom_line(aes(x = site, y = V1), colour = gray(0.6)) + 
        geom_line(aes(x = site, y = V2), colour = gray(0.65)) + 
        geom_line(aes(x = site, y = V3), colour = gray(0.7)) + 
        theme_bw() + theme(panel.grid = element_blank()) + 
        xlab(expression("Position "*italic("j"))) + 
        ylab(expression("Number of individuals  "*italic("y")[italic("j")])) + 
        ylim(c(0, 25))
  • s = 10.0
In [39]:
post.jags3 %>>% purrr::map_df(function(x){as_data_frame(x)}) %>>% dplyr::sample_n(3) %>>% 
    select(ends_with("3]")) %>>% t() %>>% as_data_frame() %>>% 
    purrr::dmap(function(x){exp(x)}) %>>% 
    mutate(site = seq_along(Y), Y = Y) %>>% 
    ggplot() + 
        geom_point(aes(x = site, y = Y)) + 
        geom_line(aes(x = site, y = V1), colour = gray(0.6)) + 
        geom_line(aes(x = site, y = V2), colour = gray(0.65)) + 
        geom_line(aes(x = site, y = V3), colour = gray(0.7)) + 
        theme_bw() + theme(panel.grid = element_blank()) + 
        xlab(expression("Position "*italic("j"))) + 
        ylab(expression("Number of individuals  "*italic("y")[italic("j")])) + 
        ylim(c(0, 25))

11.5 空間相関モデルと欠測のあるデータ

状態空間モデルの場合は,モデルを少し書き換える必要がある (どこが欠測かを明示する必要がある)

空間相関あり

In [40]:
readLines("chap11-model4.jags") %>>% cat(sep = "\n")
# CAR with NA, JAGS
model
{
  for (j in 1:N.obs){
    Y.obs[j] ~ dpois(lambda[j])
    lambda[j] <- exp(r[Idx.obs[j]])
  }
  for(j in 2:N.site){
    r[j] ~ dnorm(r[j - 1], tau)
  }
  r[1] ~ dunif(0, 10)
  tau <- 1 / (s * s)
  s ~ dunif(0, 1.0E+4)
}
In [41]:
data.list4 <- (function(){
    Idx.obs <- c(1:50)[-c(6, 9, 12, 13, 26:30)]
    Y.obs <- Y[Idx.obs]
    list(
        N.site = length(Y), 
        Idx.obs = Idx.obs, 
        Y.obs = Y.obs, 
        N.obs = length(Y.obs)
    )    
})()
In [42]:
m4 <- jags.model(
    file = "chap11-model4.jags", 
    data = data.list4,
    n.chain = 3, 
    n.adapt = 100
)
Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 41
   Unobserved stochastic nodes: 51
   Total graph size: 183

Initializing model

In [54]:
post.jags4 <- coda.samples(
    m4, 
    c("r", "s"), 
    n.iter = 10000, 
    thin = 10
)
In [30]:
# saveRDS(post.jags4, file = "chap11-post-jags4.rds")
In [43]:
post.jags4 <- readRDS("chap11-post-jags4.rds")
In [44]:
summary(post.jags4)
Iterations = 110:10100
Thinning interval = 10 
Number of chains = 3 
Sample size per chain = 1000 

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

        Mean       SD Naive SE Time-series SE
r[1]   0.948   0.3651 0.006666       0.008338
r[2]   1.107   0.3106 0.005671       0.006707
r[3]   1.249   0.3010 0.005496       0.007672
r[4]   1.506   0.2535 0.004629       0.005368
r[5]   1.722   0.2415 0.004409       0.004942
r[6]   1.249 210.2152 3.837987       3.515816
r[7]   2.126   0.2161 0.003946       0.003940
r[8]   2.370   0.2077 0.003792       0.003957
r[9]   3.506 178.5927 3.260641       3.006242
r[10]  2.483   0.1941 0.003544       0.003643
r[11]  2.679   0.1908 0.003483       0.003599
r[12]  2.387 212.3963 3.877808       1.957238
r[13]  8.031 155.1208 2.832105       3.775996
r[14]  2.872   0.1742 0.003180       0.003194
r[15]  2.879   0.1615 0.002948       0.003093
r[16]  2.820   0.1644 0.003001       0.002945
r[17]  2.703   0.1713 0.003128       0.003108
r[18]  2.597   0.1723 0.003145       0.003527
r[19]  2.523   0.1789 0.003266       0.003235
r[20]  2.427   0.1920 0.003505       0.003610
r[21]  2.484   0.1825 0.003332       0.003333
r[22]  2.612   0.1732 0.003163       0.003158
r[23]  2.672   0.1773 0.003237       0.003364
r[24]  2.532   0.1823 0.003328       0.003328
r[25]  2.451   0.2116 0.003863       0.003977
r[26] -3.975 249.9620 4.563662       5.856539
r[27]  3.529 326.8967 5.968290       2.869816
r[28]  7.695 339.9632 6.206850       6.022406
r[29]  8.392 351.9526 6.425747       8.122596
r[30] 10.113 203.5726 3.716711       5.313560
r[31]  2.233   0.2367 0.004322       0.006454
r[32]  2.422   0.1930 0.003524       0.003623
r[33]  2.386   0.1858 0.003393       0.003439
r[34]  2.416   0.1850 0.003378       0.003378
r[35]  2.457   0.1910 0.003487       0.003485
r[36]  2.358   0.1944 0.003550       0.004151
r[37]  2.505   0.1830 0.003340       0.003303
r[38]  2.551   0.1827 0.003336       0.003283
r[39]  2.539   0.1833 0.003346       0.003347
r[40]  2.529   0.1870 0.003413       0.003492
r[41]  2.254   0.1937 0.003537       0.003673
r[42]  2.097   0.2003 0.003657       0.003960
r[43]  2.016   0.2056 0.003753       0.003625
r[44]  1.788   0.2316 0.004228       0.005349
r[45]  1.701   0.2277 0.004157       0.004149
r[46]  1.636   0.2278 0.004159       0.004373
r[47]  1.585   0.2415 0.004408       0.005118
r[48]  1.403   0.2563 0.004679       0.005063
r[49]  1.237   0.2988 0.005455       0.006192
r[50]  1.090   0.4125 0.007531       0.012155
s     18.377 240.6874 4.394330      11.758850

2. Quantiles for each variable:

        2.5%    25%    50%    75%  97.5%
r[1]  0.2025 0.7073 0.9561 1.2096 1.6240
r[2]  0.4879 0.8975 1.1187 1.3271 1.6813
r[3]  0.6465 1.0630 1.2657 1.4540 1.7876
r[4]  0.9923 1.3407 1.5175 1.6791 1.9881
r[5]  1.2132 1.5697 1.7304 1.8895 2.1670
r[6]  1.3723 1.7567 1.9380 2.1038 2.4339
r[7]  1.6914 1.9854 2.1305 2.2679 2.5497
r[8]  1.9581 2.2301 2.3676 2.5131 2.7814
r[9]  1.9474 2.2674 2.4220 2.5801 2.9188
r[10] 2.0891 2.3526 2.4910 2.6157 2.8528
r[11] 2.3081 2.5526 2.6790 2.8042 3.0472
r[12] 2.2370 2.5711 2.7403 2.9022 3.2910
r[13] 2.2992 2.6380 2.8017 2.9670 3.3056
r[14] 2.5220 2.7581 2.8731 2.9871 3.2144
r[15] 2.5597 2.7724 2.8810 2.9875 3.1995
r[16] 2.5000 2.7128 2.8210 2.9286 3.1519
r[17] 2.3636 2.5889 2.7051 2.8197 3.0350
r[18] 2.2549 2.4831 2.5988 2.7147 2.9241
r[19] 2.1610 2.4043 2.5291 2.6434 2.8715
r[20] 2.0100 2.3046 2.4382 2.5579 2.7698
r[21] 2.1115 2.3670 2.4904 2.6046 2.8335
r[22] 2.2673 2.5013 2.6139 2.7231 2.9518
r[23] 2.3367 2.5534 2.6687 2.7904 3.0316
r[24] 2.1724 2.4061 2.5348 2.6555 2.8856
r[25] 2.0233 2.3116 2.4552 2.5959 2.8359
r[26] 1.7847 2.2210 2.4142 2.6080 3.0122
r[27] 1.6390 2.1575 2.3901 2.6158 3.1101
r[28] 1.6105 2.1193 2.3600 2.5851 3.1163
r[29] 1.6006 2.0973 2.3251 2.5452 3.0274
r[30] 1.5973 2.0830 2.2871 2.4884 2.8958
r[31] 1.7608 2.0948 2.2493 2.3904 2.6542
r[32] 2.0515 2.2901 2.4236 2.5504 2.7998
r[33] 2.0204 2.2642 2.3882 2.5104 2.7387
r[34] 2.0407 2.2961 2.4217 2.5404 2.7658
r[35] 2.0827 2.3295 2.4592 2.5818 2.8238
r[36] 1.9606 2.2386 2.3679 2.4937 2.7126
r[37] 2.1485 2.3792 2.5048 2.6282 2.8590
r[38] 2.1991 2.4297 2.5532 2.6737 2.9040
r[39] 2.1807 2.4134 2.5391 2.6638 2.8969
r[40] 2.1760 2.4027 2.5218 2.6534 2.8967
r[41] 1.8733 2.1272 2.2587 2.3841 2.6195
r[42] 1.6875 1.9708 2.1023 2.2357 2.4695
r[43] 1.6166 1.8778 2.0182 2.1534 2.4180
r[44] 1.3120 1.6480 1.8015 1.9373 2.2091
r[45] 1.2392 1.5514 1.7075 1.8559 2.1261
r[46] 1.1852 1.4896 1.6392 1.7863 2.0701
r[47] 1.0918 1.4308 1.5912 1.7436 2.0256
r[48] 0.8747 1.2372 1.4176 1.5810 1.8671
r[49] 0.6267 1.0500 1.2470 1.4459 1.7722
r[50] 0.3122 0.8688 1.1145 1.3402 1.7470
s     0.1529 0.2040 0.2377 0.2801 0.3845
In [45]:
data.list4$Idx.obs
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 7
  7. 8
  8. 10
  9. 11
  10. 14
  11. 15
  12. 16
  13. 17
  14. 18
  15. 19
  16. 20
  17. 21
  18. 22
  19. 23
  20. 24
  21. 25
  22. 31
  23. 32
  24. 33
  25. 34
  26. 35
  27. 36
  28. 37
  29. 38
  30. 39
  31. 40
  32. 41
  33. 42
  34. 43
  35. 44
  36. 45
  37. 46
  38. 47
  39. 48
  40. 49
  41. 50
In [46]:
missing.intervals <- purrr::map2(c(6, 9, 12, 13, 26:30) - 0.5, 
                                 c(6, 9, 12, 13, 26:30) + 0.5, 
                                 function(xmin, xmax){
                                     geom_rect(mapping = aes_(xmin = xmin, xmax = xmax, 
                                                              ymin = -Inf, ymax = Inf), 
                                               fill = gray(0.9, alpha = 0.9))
  })
In [47]:
gp.car.na <- (function(){
    Y.mean <- exp(summary(post.jags4[, grep("r", varnames(post.jags4))])$statistics[, "Mean"])
    Y.med <- exp(summary(post.jags4[, grep("r", varnames(post.jags4))])$quantiles[, c("50%")])    
    Y.qnt <- exp(summary(post.jags4[, grep("r", varnames(post.jags4))])$quantiles[, c("2.5%", "97.5%")])
    p.fill <- rep("black", 50)
    p.fill[data.list4$Idx.obs] <- "white"
    data_frame(site = seq_along(Y), 
           Y = Y,
           Y.mean = Y.mean,
           Y.med = Y.med, 
           Y.975 = Y.qnt[, 2],
           Y.025 = Y.qnt[, 1]) %>>% 
    ggplot() +
        missing.intervals + 
        geom_point(aes(x = site, y = Y), shape = 21, fill = p.fill) +
        geom_line(aes(x = site, y = Y.mean), linetype = "dotted") +
        geom_line(aes(x = site, y = Y.med)) +    
        geom_ribbon(aes(x = site, ymin = Y.025, ymax = Y.975), alpha = 0.3) + 
        theme_bw() + 
        theme(
            panel.grid = element_blank()
        ) + 
        xlab(expression("Position "*italic("j"))) + 
        ylab(expression("Number of individuals  "*italic("y")[italic("j")])) + 
        scale_y_continuous(breaks = seq(0, 25, 5), limits = c(0, 30))
})()

空間相関なし

In [48]:
readLines("chap11-model5.jags") %>>% cat(sep="\n")
# GLMM 
model
{
  for (j in 1:N.site){
    Y[j] ~ dpois(lambda[j])
    lambda[j] <- exp(beta + r[j])
    r[j] ~ dnorm(0, tau)
  }
  beta ~ dnorm(0, 1.0E-4)
  tau <- 1 / (s * s)
  s ~ dunif(0, 1.0E+4)
}

In [49]:
data.list5 <- (function(){
    Y.na <- Y
    Y.na[c(6, 9, 12, 13, 26:30)] <- NA
 list(
     N.site = length(Y), 
     Y = Y.na
 )   
})()
data.list5
$N.site
50
$Y
  1. 0
  2. 3
  3. 2
  4. 5
  5. 6
  6. NA
  7. 8
  8. 14
  9. NA
  10. 10
  11. 17
  12. NA
  13. NA
  14. 19
  15. 19
  16. 18
  17. 15
  18. 13
  19. 13
  20. 9
  21. 11
  22. 15
  23. 18
  24. 12
  25. 11
  26. NA
  27. NA
  28. NA
  29. NA
  30. NA
  31. 6
  32. 15
  33. 10
  34. 11
  35. 14
  36. 7
  37. 14
  38. 14
  39. 13
  40. 17
  41. 8
  42. 7
  43. 10
  44. 4
  45. 5
  46. 5
  47. 7
  48. 4
  49. 3
  50. 1
In [50]:
m5 <- jags.model(
    file = "chap11-model5.jags", 
    data = data.list5,
    n.chain = 3, 
    n.adapt = 100
)
Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 41
   Unobserved stochastic nodes: 61
   Total graph size: 260

Initializing model

In [51]:
post.jags5 <- coda.samples(
    m5, 
    c("r", "beta", "s"), 
    n.iter = 10000,
    thin = 10
)
In [52]:
# saveRDS(post.jags5, file = "chap11-post-jags5.rds")
In [53]:
post.jags5 <- readRDS("chap11-post-jags5.rds")
In [54]:
gelman.diag(post.jags5)
Potential scale reduction factors:

      Point est. Upper C.I.
beta       1.003      1.006
r[1]       1.001      1.001
r[2]       1.000      1.001
r[3]       1.000      1.002
r[4]       1.000      1.002
r[5]       1.003      1.011
r[6]       1.001      1.003
r[7]       0.999      0.999
r[8]       0.999      0.999
r[9]       1.002      1.008
r[10]      1.001      1.006
r[11]      0.999      1.000
r[12]      1.000      1.004
r[13]      1.002      1.010
r[14]      1.000      1.003
r[15]      0.999      1.001
r[16]      1.001      1.007
r[17]      1.002      1.007
r[18]      1.003      1.011
r[19]      1.000      1.001
r[20]      1.003      1.013
r[21]      1.000      1.001
r[22]      1.001      1.005
r[23]      1.002      1.005
r[24]      0.999      1.000
r[25]      1.009      1.022
r[26]      1.002      1.008
r[27]      1.003      1.004
r[28]      1.000      1.000
r[29]      1.006      1.024
r[30]      1.005      1.018
r[31]      1.001      1.007
r[32]      1.000      1.001
r[33]      1.001      1.006
r[34]      1.002      1.008
r[35]      1.001      1.004
r[36]      1.002      1.011
r[37]      1.000      1.000
r[38]      1.004      1.017
r[39]      1.001      1.005
r[40]      1.002      1.004
r[41]      1.003      1.013
r[42]      1.001      1.006
r[43]      1.001      1.003
r[44]      1.000      1.001
r[45]      1.001      1.005
r[46]      1.001      1.004
r[47]      1.001      1.003
r[48]      1.002      1.007
r[49]      0.999      0.999
r[50]      1.000      1.003
s          1.000      1.000

Multivariate psrf

1.03
In [55]:
summary(post.jags5)
Iterations = 110:10100
Thinning interval = 10 
Number of chains = 3 
Sample size per chain = 1000 

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

          Mean       SD Naive SE Time-series SE
beta   2.18630   0.1841 0.003362       0.009730
r[1]  -4.85434  58.3257 1.064876       2.437015
r[2]  -0.56185   0.3624 0.006617       0.007876
r[3]  -0.68950   0.3772 0.006887       0.006382
r[4]  -0.37650   0.3466 0.006328       0.008958
r[5]  -0.27573   0.3401 0.006210       0.010076
r[6]  -0.19725 203.7513 3.719972       3.322483
r[7]  -0.08623   0.3303 0.006030       0.011300
r[8]   0.33044   0.3063 0.005592       0.011516
r[9]   6.02288 220.1842 4.019994       4.490146
r[10]  0.07044   0.3185 0.005815       0.010747
r[11]  0.50029   0.3008 0.005491       0.011026
r[12] -0.23717 186.4666 3.404399       2.780172
r[13] 11.70626 285.5807 5.213967       7.443496
r[14]  0.60480   0.2965 0.005414       0.012297
r[15]  0.59983   0.2937 0.005362       0.012296
r[16]  0.54862   0.2944 0.005376       0.010657
r[17]  0.38367   0.3030 0.005532       0.011453
r[18]  0.27115   0.3126 0.005708       0.011840
r[19]  0.27208   0.3091 0.005643       0.011323
r[20] -0.01181   0.3239 0.005914       0.011426
r[21]  0.12342   0.3154 0.005758       0.011612
r[22]  0.38474   0.3075 0.005615       0.011419
r[23]  0.54351   0.2987 0.005454       0.011603
r[24]  0.19902   0.3173 0.005792       0.011405
r[25]  0.13489   0.3185 0.005815       0.011019
r[26]  6.28863 285.0061 5.203476       6.144938
r[27] -1.52092 206.8059 3.775742       2.417851
r[28]  2.73324 218.5468 3.990101       2.678018
r[29] -0.84427 198.1821 3.618293       5.732829
r[30]  1.04259 293.7464 5.363051       8.054607
r[31] -0.26211   0.3308 0.006039       0.008886
r[32]  0.38844   0.3057 0.005581       0.012007
r[33]  0.05863   0.3203 0.005847       0.011954
r[34]  0.13381   0.3164 0.005777       0.010903
r[35]  0.32896   0.3122 0.005700       0.010959
r[36] -0.18276   0.3340 0.006097       0.009235
r[37]  0.33244   0.3108 0.005674       0.011360
r[38]  0.32422   0.3079 0.005622       0.012517
r[39]  0.27873   0.3137 0.005727       0.012192
r[40]  0.48924   0.3017 0.005508       0.011351
r[41] -0.09284   0.3336 0.006090       0.010911
r[42] -0.16895   0.3460 0.006317       0.011372
r[43]  0.05966   0.3210 0.005860       0.010968
r[44] -0.46383   0.3700 0.006755       0.010051
r[45] -0.35932   0.3520 0.006426       0.009872
r[46] -0.37007   0.3501 0.006392       0.010159
r[47] -0.17751   0.3428 0.006258       0.010395
r[48] -0.47639   0.3599 0.006571       0.009918
r[49] -0.57417   0.3632 0.006631       0.007806
r[50] -0.82602   0.4047 0.007389       0.008124
s     18.17506 243.1229 4.438797      10.685127

2. Quantiles for each variable:

          2.5%      25%        50%      75%    97.5%
beta   1.96405  2.13322  2.2022710  2.26689  2.39235
r[1]  -1.95647 -1.21272 -0.9303432 -0.67032 -0.23869
r[2]  -1.32680 -0.78280 -0.5381991 -0.32800  0.07006
r[3]  -1.45964 -0.91791 -0.6732627 -0.43411 -0.03315
r[4]  -1.06955 -0.59309 -0.3741477 -0.15909  0.26371
r[5]  -0.92082 -0.47868 -0.2822527 -0.07338  0.33665
r[6]  -1.10133 -0.34287 -0.0009818  0.32874  1.04803
r[7]  -0.70835 -0.28478 -0.0863232  0.10267  0.49645
r[8]  -0.20342  0.14771  0.3191801  0.49893  0.85487
r[9]  -1.06917 -0.33699 -0.0006821  0.34352  1.09482
r[10] -0.51183 -0.12706  0.0684431  0.24854  0.64686
r[11] -0.01705  0.31757  0.4940715  0.66307  1.03040
r[12] -1.02274 -0.30974  0.0105114  0.34534  1.05618
r[13] -1.07641 -0.32004  0.0192519  0.35214  1.06198
r[14]  0.08416  0.42949  0.5949882  0.75717  1.11347
r[15]  0.10882  0.42424  0.5869125  0.75154  1.08921
r[16]  0.05158  0.36905  0.5355752  0.70492  1.07335
r[17] -0.14663  0.20469  0.3736711  0.54925  0.88892
r[18] -0.26001  0.08196  0.2590278  0.44243  0.81675
r[19] -0.27175  0.07203  0.2682283  0.44947  0.80459
r[20] -0.60846 -0.20588 -0.0196482  0.17117  0.58064
r[21] -0.45931 -0.07021  0.1155934  0.30084  0.67938
r[22] -0.13307  0.19057  0.3669881  0.54791  0.93203
r[23]  0.02709  0.36303  0.5263727  0.70395  1.05335
r[24] -0.37640  0.01256  0.1925729  0.37429  0.73285
r[25] -0.42956 -0.05967  0.1269073  0.31435  0.69169
r[26] -1.10258 -0.32822  0.0164656  0.36999  1.11110
r[27] -1.08722 -0.34918  0.0081166  0.35164  1.06683
r[28] -1.13980 -0.33364  0.0082074  0.34167  1.13737
r[29] -1.11113 -0.35757 -0.0087234  0.34440  1.03680
r[30] -1.04310 -0.31802  0.0062800  0.33491  1.12152
r[31] -0.90465 -0.47186 -0.2620992 -0.05723  0.34437
r[32] -0.13580  0.20362  0.3766411  0.54940  0.94499
r[33] -0.51763 -0.13778  0.0529511  0.23651  0.62912
r[34] -0.44125 -0.06788  0.1308167  0.32100  0.69113
r[35] -0.24827  0.14276  0.3180395  0.50333  0.86151
r[36] -0.80484 -0.38754 -0.1800133  0.01753  0.43430
r[37] -0.20310  0.14470  0.3201755  0.50153  0.87634
r[38] -0.18265  0.13745  0.3081373  0.48851  0.86146
r[39] -0.26311  0.08761  0.2629961  0.44976  0.81521
r[40] -0.01596  0.30937  0.4791147  0.64825  1.01476
r[41] -0.70767 -0.29762 -0.0957593  0.10467  0.51054
r[42] -0.80418 -0.38297 -0.1717151  0.04613  0.44234
r[43] -0.50403 -0.14479  0.0506631  0.24717  0.63230
r[44] -1.15853 -0.68795 -0.4579810 -0.22960  0.19855
r[45] -1.04403 -0.58341 -0.3549202 -0.14524  0.28007
r[46] -1.02799 -0.58988 -0.3661480 -0.15262  0.26420
r[47] -0.81915 -0.38462 -0.1801724  0.01694  0.43585
r[48] -1.17401 -0.70068 -0.4681168 -0.24205  0.15418
r[49] -1.31822 -0.79262 -0.5598561 -0.34362  0.07526
r[50] -1.67388 -1.05360 -0.7975924 -0.54969 -0.12436
s      0.33842  0.44143  0.5027808  0.57106  0.73752
In [56]:
gp.glmm.na <- (function(){
    beta.mean <- summary(post.jags5[, "beta"])$statistics[["Mean"]]
    beta.med <- summary(post.jags5[, "beta"])$quantiles[["50%"]]
    Y.mean <- exp(beta.mean + summary(post.jags5[, grep("r", varnames(post.jags5))])$statistics[, "Mean"])
    Y.med <- exp(beta.med + summary(post.jags5[, grep("r", varnames(post.jags5))])$quantiles[, c("50%")])
    Y.qnt <- exp(beta.med + summary(post.jags5[, grep("r", varnames(post.jags5))])$quantiles[, c("2.5%", "97.5%")])
    p.fill <- rep("black", 50)
    p.fill[data.list4$Idx.obs] <- "white"
    data_frame(site = seq_along(Y), 
           Y = Y,
           Y.mean = Y.mean,
           Y.med = Y.med,
           Y.975 = Y.qnt[, 2],
           Y.025 = Y.qnt[, 1]) %>>% 
    ggplot() +
        missing.intervals + 
        geom_point(aes(x = site, y = Y), shape = 21, fill = p.fill) +
        geom_line(aes(x = site, y = Y.mean), linetype = "dotted") +
        geom_line(aes(x = site, y = Y.med)) +
        geom_ribbon(aes(x = site, ymin = Y.025, ymax = Y.975), alpha = 0.3) + 
        theme_bw() + 
        theme(
            panel.grid = element_blank()
        ) + 
        xlab(expression("Position "*italic("j"))) + 
        ylab(expression("Number of individuals  "*italic("y")[italic("j")])) + 
        scale_y_continuous(breaks = seq(0, 25, 5), limits = c(0, 30))
})()
In [57]:
options(repr.plot.width = 8, repr.plot.height = 3)
  • 左: 空間相関あり
  • 右: 空間相関なし

(点線は事後平均,線は中央値)

In [58]:
grid.arrange(gp.car.na, gp.glmm.na, ncol = 2)

11.6 まとめ

  • 空間構造のあるデータでは空間相関を考慮した統計モデルを用いる
  • 空間相関のある場所差を生成する intristic Gaussian CAR モデルは WinBUGS の car.normal() で扱える
  • intristic Gaussian CARモデルは状態空間モデルとしても表現できる
    • JAGS で扱う場合にはこちら
  • 空間相関のある場所差は確率場を使って表現できる
  • 空間相関を考慮した階層ベイズモデルは欠測のあるデータを予測する用途にも使える
In [59]:
devtools::session_info()
Session info -------------------------------------------------------------------
Packages -----------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.2 (2016-10-31)
 system   x86_64, mingw32             
 ui       RTerm                       
 language (EN)                        
 collate  Japanese_Japan.932          
 tz       Asia/Tokyo                  
 date     2017-01-11                  

 package    * version date       source                            
 assertthat   0.1     2013-12-06 CRAN (R 3.2.1)                    
 Cairo        1.5-9   2015-09-26 CRAN (R 3.2.2)                    
 coda       * 0.18-1  2015-10-16 CRAN (R 3.2.3)                    
 colorspace   1.2-7   2016-10-11 CRAN (R 3.3.2)                    
 crayon       1.3.2   2016-06-28 CRAN (R 3.3.1)                    
 DBI          0.5-1   2016-09-10 CRAN (R 3.2.5)                    
 devtools     1.12.0  2016-06-24 CRAN (R 3.3.1)                    
 digest       0.6.10  2016-08-02 CRAN (R 3.3.1)                    
 dplyr      * 0.5.0   2016-06-24 CRAN (R 3.2.5)                    
 evaluate     0.10    2016-10-11 CRAN (R 3.3.2)                    
 ggplot2    * 2.2.0   2016-11-11 CRAN (R 3.3.2)                    
 gridExtra  * 2.2.1   2016-02-29 CRAN (R 3.2.5)                    
 gtable       0.2.0   2016-02-26 CRAN (R 3.2.5)                    
 IRdisplay    0.4.4   2016-08-02 CRAN (R 3.3.1)                    
 IRkernel     0.7     2016-11-03 Github (IRkernel/IRkernel@6e6c2ec)
 jsonlite     1.1     2016-09-14 CRAN (R 3.2.5)                    
 labeling     0.3     2014-08-23 CRAN (R 3.2.1)                    
 lattice      0.20-34 2016-09-06 CRAN (R 3.3.2)                    
 lazyeval     0.2.0   2016-06-12 CRAN (R 3.2.5)                    
 magrittr     1.5     2014-11-22 CRAN (R 3.2.1)                    
 memoise      1.0.0   2016-01-29 CRAN (R 3.2.3)                    
 munsell      0.4.3   2016-02-13 CRAN (R 3.2.5)                    
 pbdZMQ       0.2-4   2016-09-22 CRAN (R 3.3.1)                    
 pipeR      * 0.6.1.3 2016-04-04 CRAN (R 3.3.1)                    
 plyr         1.8.4   2016-06-08 CRAN (R 3.2.5)                    
 purrr      * 0.2.2   2016-06-18 CRAN (R 3.2.5)                    
 R6           2.2.0   2016-10-05 CRAN (R 3.3.2)                    
 Rcpp         0.12.7  2016-09-05 CRAN (R 3.2.5)                    
 readr      * 1.0.0   2016-08-03 CRAN (R 3.2.5)                    
 repr         0.9     2016-07-24 CRAN (R 3.3.1)                    
 rjags      * 4-6     2016-02-19 CRAN (R 3.3.2)                    
 scales       0.4.1   2016-11-09 CRAN (R 3.3.2)                    
 stringi      1.1.2   2016-10-01 CRAN (R 3.3.2)                    
 stringr      1.1.0   2016-08-19 CRAN (R 3.2.5)                    
 tibble       1.2     2016-08-26 CRAN (R 3.2.5)                    
 tidyr      * 0.6.0   2016-08-12 CRAN (R 3.2.5)                    
 uuid         0.1-2   2015-07-28 CRAN (R 3.3.1)                    
 withr        1.0.2   2016-06-20 CRAN (R 3.2.5)