我犹豫要不要问这个问题,因为我觉得答案会很明显和愚蠢,但是知道错误的含义是什么,我仍然找不到源。
我正在运行 ICAR 模型,因此不得不参加 winBUGS 速成课程,因为 rJAGS 无法轻松运行这些模型(据我所知)。每当我运行此代码时,模型都会在抛出警告“陷阱:索引超出范围”之前进行编译。当我关闭 WinBUGS 并返回 R 时,什么都没有保存(不出所料)。我已经从我的 for 循环中检查了我的索引,并与相关数据进行了匹配,一切似乎都排好了。任何见解都会受到欢迎。
相关代码和数据源如下:
library(R2WinBUGS)
##########Likelihood
sink("mod1.txt")
cat("model{
for(j in 1:nhhs){
y[j] ~ dnorm(theta[msoa[j]], prec.y)
}
for(i in 1:nmsoas){
theta[i] <- alpha + S[i]
}
S[1:nmsoas] ~ car.normal(adj[], weights[], num[], prec.S)
##########Priors
alpha ~ dunif(-10000, 10000)
sigma.S ~ dunif(0.0001, 1000)
sigma.y ~ dunif(0.0001, 1000)
prec.S <- pow(sigma.S, -2)
prec.y <- pow(sigma.y, -2)
var.y <- pow(sigma.y, 2)
var.S.un <- sd(S[1:nmsoas]) * sd(S[1:nmsoas])
vpc <- var.S.un/(var.S.un + var.y) * 100
},
fill = TRUE
")
sink()
data <- [[See data list below -- note it includes adjacency data created directly from a shapefile]]
params <- c("alpha", "sigma.y", "sigma.S", "S", "vpc")
inits <- function () {list(alpha = 200, sigma.y = 50, sigma.S = 20, S = rep(0, 109))}
nc <- 2
ni <- 50000
nb <- 10000
nt <- 1
bugs.out <- bugs(data = data,
inits = inits,
parameters.to.save = params,
model.file="mod1.txt",
n.chains=nc,
n.iter=ni,
n.burnin=nb,
n.thin=nt,
debug=TRUE,
DIC=TRUE,
bugs.directory = "C:\\winbugs\\winbugs14_full_patched\\WinBUGS14",
working.directory=getwd())
数据
list(nhhs = 760L, nmsoas = 109, y = c(501, 616, 472, 816, 637,
500, 506, 560, 542, 447, 644, 522, 487, 275, 720, 564, 568, 696,
679, 649, 557, 541, 459, 218, 522, 443, 432, 281, 388, 485, 587,
429, 482, 434, 291, 379, 381, 414, 530, 496, 404, 395, 490, 614,
448, 446, 589, 642, 525, 657, 593, 394, 497, 338, 615, 675, 420,
347, 522, 445, 710, 446, 525, 453, 369, 470, 254, 587, 455, 657,
488, 369, 501, 413, 373, 566, 475, 530, 539, 456, 515, 580, 777,
371, 689, 650, 759, 427, 500, 489, 401, 591, 586, 536, 632, 521,
322, 579, 476, 595, 572, 677, 526, 695, 566, 696, 574, 596, 535,
686, 416, 436, 783, 488, 551, 524, 535, 540, 650, 555, 508, 770,
550, 593, 605, 727, 609, 614, 518, 571, 456, 391, 503, 298, 481,
296, 420, 397, 542, 631, 353, 816, 390, 570, 329, 369, 676, 452,
311, 272, 499, 448, 473, 402, 336, 385, 627, 438, 348, 698, 530,
452, 603, 583, 408, 737, 488, 342, 502, 546, 808, 533, 577, 510,
478, 645, 775, 979, 813, 841, 455, 806, 685, 418, 732, 441, 631,
371, 410, 352, 439, 507, 375, 557, 319, 369, 680, 373, 551, 452,
551, 711, 698, 459, 214, 1229, 878, 854, 747, 848, 718, 831,
673, 487, 939, 1117, 736, 539, 934, 791, 439, 800, 669, 729,
559, 1176, 471, 323, 533, 541, 395, 173, 435, 577, 503, 499,
575, 372, 667, 529, 618, 659, 558, 420, 675, 243, 401, 432, 442,
571, 475, 648, 580, 555, 689, 752, 250, 671, 643, 530, 866, 632,
650, 850, 1001, 747, 626, 492, 642, 536, 470, 546, 401, 809,
518, 632, 233, 586, 346, 607, 546, 462, 664, 428, 442, 393, 432,
620, 689, 779, 558, 680, 659, 394, 902, 641, 741, 563, 455, 427,
557, 347, 674, 418, 639, 624, 469, 521, 359, 505, 583, 481, 421,
399, 466, 586, 418, 457, 330, 524, 663, 687, 602, 266, 560, 557,
456, 521, 392, 599, 482, 337, 477, 679, 481, 605, 630, 521, 479,
393, 530, 335, 477, 419, 205, 297, 549, 429, 537, 654, 610, 523,
491, 429, 621, 515, 323, 433, 242, 429, 169, 551, 356, 377, 403,
481, 488, 477, 363, 284, 381, 448, 371, 453, 334, 459, 474, 425,
424, 616, 516, 551, 348, 456, 555, 434, 227, 474, 250, 375, 624,
542, 597, 506, 457, 362, 656, 404, 334, 479, 498, 574, 345, 364,
358, 362, 363, 370, 544, 330, 361, 465, 512, 323, 349, 452, 502,
365, 247, 561, 534, 319, 383, 443, 929, 1146, 469, 648, 676,
459, 279, 617, 346, 547, 629, 524, 669, 854, 720, 1000, 766,
1244, 856, 701, 609, 154, 386, 520, 614, 248, 860, 599, 575,
441, 458, 709, 686, 295, 459, 690, 542, 935, 797, 699, 601, 642,
494, 694, 822, 763, 478, 354, 555, 629, 750, 394, 619, 601, 759,
576, 526, 408, 502, 526, 933, 947, 555, 443, 304, 677, 436, 943,
467, 617, 1048, 392, 581, 392, 586, 424, 550, 500, 423, 602,
749, 839, 394, 563, 774, 452, 252, 477, 473, 617, 553, 643, 467,
567, 474, 669, 428, 556, 387, 514, 484, 293, 473, 628, 569, 621,
421, 775, 612, 532, 846, 485, 646, 792, 690, 554, 537, 653, 736,
653, 870, 639, 533, 740, 283, 715, 494, 416, 319, 397, 608, 359,
187, 528, 386, 385, 378, 527, 618, 457, 454, 667, 555, 567, 501,
718, 449, 710, 537, 399, 542, 557, 512, 669, 237, 324, 319, 500,
665, 676, 537, 583, 559, 646, 633, 472, 538, 561, 603, 319, 193,
530, 345, 389, 546, 401, 450, 665, 552, 553, 474, 357, 315, 472,
750, 528, 521, 542, 452, 325, 792, 431, 445, 271, 451, 451, 450,
262, 678, 676, 550, 342, 553, 441, 433, 629, 357, 446, 419, 512,
313, 422, 599, 554, 386, 565, 216, 460, 583, 423, 485, 291, 596,
259, 450, 458, 649, 516, 914, 426, 678, 199, 470, 529, 343, 552,
509, 584, 369, 355, 268, 425, 315, 535, 488, 735, 345, 618, 418,
310, 547, 313, 357, 374, 485, 343, 586, 320, 485, 477, 335, 404,
379, 554, 619, 337, 581, 472, 267, 398, 530, 496, 186, 488, 521,
417, 373, 664, 440, 448, 328, 304, 285, 408, 586, 625, 607, 580,
426, 328, 452, 209, 453, 684, 505, 618, 547, 433, 748, 520, 408,
539, 340, 638, 593, 419, 403, 569, 336, 345, 509, 407, 646, 505,
679, 592, 249, 363, 680, 606, 399, 352, 649, 419, 1195, 1189,
517, 644, 559, 959, 406, 295, 903, 751, 723, 500), msoa = c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L,
22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L, 25L, 25L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 27L, 27L, 27L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 32L, 32L, 32L,
32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 34L, 34L, 34L, 34L, 34L,
34L, 34L, 34L, 34L, 34L, 34L, 34L, 36L, 36L, 36L, 36L, 36L, 36L,
36L, 37L, 37L, 37L, 37L, 37L, 37L, 38L, 38L, 38L, 38L, 38L, 38L,
38L, 38L, 38L, 39L, 39L, 39L, 39L, 39L, 39L, 39L, 39L, 39L, 39L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L,
43L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 45L,
45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 46L, 46L,
46L, 46L, 46L, 46L, 46L, 46L, 46L, 47L, 47L, 47L, 47L, 47L, 47L,
47L, 47L, 47L, 47L, 48L, 48L, 48L, 48L, 48L, 48L, 48L, 48L, 48L,
48L, 48L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L,
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 51L, 51L,
51L, 51L, 51L, 52L, 52L, 52L, 52L, 52L, 52L, 52L, 52L, 52L, 52L,
52L, 52L, 53L, 53L, 53L, 53L, 53L, 53L, 53L, 53L, 54L, 54L, 54L,
54L, 54L, 54L, 54L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L,
55L, 55L, 55L, 55L, 56L, 56L, 56L, 56L, 56L, 56L, 57L, 57L, 57L,
57L, 57L, 57L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 59L, 59L,
59L, 59L, 59L, 59L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 61L, 61L,
61L, 61L, 61L, 61L, 62L, 62L, 62L, 62L, 62L, 62L, 62L, 62L, 63L,
63L, 63L, 63L, 63L, 63L, 63L, 64L, 64L, 64L, 64L, 64L, 64L, 64L,
65L, 65L, 65L, 65L, 65L, 65L, 65L, 65L, 65L, 66L, 66L, 66L, 66L,
66L, 66L, 66L, 67L, 67L, 67L, 67L, 67L, 67L, 67L, 67L, 68L, 68L,
68L, 68L, 68L, 68L, 68L, 69L, 69L, 69L, 69L, 69L, 69L, 69L, 70L,
70L, 70L, 70L, 70L, 70L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 72L,
72L, 72L, 72L, 72L, 72L, 72L, 73L, 73L, 73L, 73L, 73L, 73L, 74L,
74L, 74L, 74L, 74L, 74L, 74L, 74L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 76L, 76L, 76L, 76L, 76L, 76L, 77L, 77L, 77L,
77L, 77L, 77L, 79L, 79L, 79L, 79L, 79L, 79L, 79L, 79L, 80L, 80L,
80L, 80L, 80L, 80L, 81L, 81L, 81L, 81L, 81L, 81L, 82L, 82L, 82L,
82L, 82L, 82L, 82L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 85L,
85L, 85L, 85L, 85L, 85L, 85L, 86L, 86L, 86L, 86L, 86L, 86L, 86L,
87L, 87L, 87L, 87L, 87L, 87L, 87L, 87L, 87L, 89L, 89L, 89L, 89L,
89L, 89L, 89L, 90L, 90L, 90L, 90L, 90L, 90L, 90L, 91L, 91L, 91L,
91L, 91L, 91L, 92L, 92L, 92L, 92L, 92L, 92L, 93L, 93L, 93L, 93L,
93L, 93L, 94L, 94L, 94L, 94L, 94L, 94L, 95L, 95L, 95L, 95L, 95L,
95L, 95L, 95L, 95L, 97L, 97L, 97L, 97L, 97L, 97L, 97L, 98L, 98L,
98L, 98L, 98L, 98L, 99L, 99L, 99L, 99L, 99L, 99L, 100L, 100L,
101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 102L, 102L,
102L, 102L, 102L, 102L, 102L, 103L, 103L, 103L, 103L, 103L, 103L,
103L, 104L, 104L, 104L, 104L, 104L, 104L, 104L, 104L, 105L, 105L,
105L, 105L, 105L, 105L, 105L, 106L, 106L, 106L, 106L, 106L, 106L,
107L, 107L, 107L, 107L, 107L, 107L, 108L, 108L, 108L, 108L, 108L,
108L, 109L, 109L, 109L, 109L, 109L, 109L), num = c(1, 6, 3, 4,
3, 1, 6, 1, 5, 5, 7, 2, 3, 4, 1, 5, 3, 5, 3, 4, 1, 5, 6, 4, 4,
3, 4, 8, 4, 6, 4, 6, 7, 7, 2, 5, 5, 9, 6, 3, 5, 5, 4, 3, 9, 2,
4, 3, 5, 7, 4, 4, 2, 1, 3, 2, 3, 4, 3, 4, 4, 2, 7, 4, 5, 3, 5,
4, 5, 5, 7, 3, 4, 8, 4, 4, 7, 5, 6, 6, 4, 4, 3, 6, 3, 5, 3, 5,
3, 4, 4, 6, 3, 4, 4, 3, 4, 6, 5, 4, 6, 6, 7, 5, 5, 6, 5, 4, 3
), weights = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1), adj = c(2L, 1L, 3L, 4L, 11L, 23L,
24L, 2L, 4L, 22L, 2L, 3L, 20L, 24L, 9L, 25L, 26L, 26L, 10L, 16L,
18L, 96L, 101L, 106L, 9L, 5L, 8L, 13L, 23L, 25L, 7L, 11L, 12L,
15L, 16L, 2L, 10L, 13L, 14L, 17L, 23L, 24L, 10L, 14L, 9L, 11L,
17L, 11L, 12L, 20L, 24L, 10L, 7L, 10L, 19L, 20L, 22L, 11L, 13L,
23L, 7L, 19L, 22L, 101L, 106L, 16L, 18L, 22L, 4L, 14L, 16L, 22L,
23L, 3L, 16L, 18L, 19L, 20L, 2L, 9L, 11L, 17L, 21L, 25L, 2L,
4L, 11L, 14L, 5L, 9L, 23L, 26L, 5L, 6L, 25L, 28L, 29L, 30L, 34L,
27L, 29L, 31L, 32L, 33L, 60L, 70L, 75L, 27L, 28L, 32L, 34L, 27L,
34L, 35L, 36L, 37L, 40L, 28L, 32L, 33L, 39L, 28L, 29L, 31L, 34L,
39L, 50L, 28L, 31L, 38L, 39L, 74L, 75L, 81L, 27L, 29L, 30L, 32L,
37L, 42L, 50L, 30L, 36L, 30L, 35L, 37L, 41L, 45L, 30L, 34L, 36L,
42L, 45L, 33L, 39L, 43L, 44L, 46L, 49L, 52L, 81L, 86L, 31L, 32L,
33L, 38L, 43L, 50L, 30L, 41L, 47L, 36L, 40L, 45L, 47L, 51L, 34L,
37L, 45L, 48L, 50L, 38L, 39L, 49L, 50L, 38L, 49L, 52L, 36L, 37L,
41L, 42L, 47L, 48L, 51L, 53L, 55L, 38L, 86L, 40L, 41L, 45L, 51L,
42L, 45L, 55L, 38L, 43L, 44L, 50L, 52L, 32L, 34L, 39L, 42L, 43L,
49L, 55L, 41L, 45L, 47L, 53L, 38L, 44L, 49L, 56L, 45L, 51L, 56L,
45L, 48L, 50L, 52L, 54L, 59L, 61L, 63L, 60L, 63L, 64L, 68L, 57L,
61L, 62L, 28L, 58L, 64L, 70L, 57L, 59L, 63L, 65L, 59L, 66L, 57L,
58L, 61L, 65L, 67L, 68L, 74L, 58L, 60L, 68L, 70L, 61L, 63L, 66L,
67L, 71L, 62L, 65L, 69L, 63L, 65L, 71L, 74L, 77L, 58L, 63L, 64L,
74L, 66L, 71L, 72L, 73L, 76L, 28L, 60L, 64L, 74L, 75L, 65L, 67L,
69L, 76L, 77L, 79L, 82L, 69L, 73L, 78L, 69L, 72L, 76L, 78L, 33L,
63L, 67L, 68L, 70L, 75L, 77L, 80L, 28L, 33L, 70L, 74L, 69L, 71L,
73L, 78L, 67L, 71L, 74L, 79L, 80L, 82L, 84L, 72L, 73L, 76L, 79L,
83L, 71L, 77L, 78L, 82L, 83L, 84L, 74L, 77L, 81L, 84L, 85L, 86L,
33L, 38L, 80L, 86L, 71L, 77L, 79L, 84L, 78L, 79L, 84L, 77L, 79L,
80L, 82L, 83L, 85L, 80L, 84L, 86L, 38L, 46L, 80L, 81L, 85L, 88L,
89L, 90L, 87L, 89L, 91L, 97L, 98L, 87L, 88L, 92L, 87L, 92L, 94L,
104L, 88L, 92L, 97L, 105L, 89L, 90L, 91L, 94L, 99L, 105L, 95L,
98L, 103L, 90L, 92L, 99L, 104L, 93L, 96L, 101L, 103L, 7L, 95L,
101L, 88L, 91L, 98L, 102L, 88L, 93L, 97L, 100L, 102L, 103L, 92L,
94L, 104L, 105L, 108L, 98L, 102L, 103L, 107L, 7L, 18L, 95L, 96L,
103L, 106L, 97L, 98L, 100L, 105L, 107L, 108L, 93L, 95L, 98L,
100L, 101L, 106L, 107L, 90L, 94L, 99L, 108L, 109L, 91L, 92L,
99L, 102L, 108L, 7L, 18L, 101L, 103L, 107L, 109L, 100L, 102L,
103L, 106L, 109L, 99L, 102L, 104L, 105L, 104L, 106L, 107L))