Reputation: 26
I am trying to code up a binomial Bayesian model and I get the following error:
Error in checkForRemoteErrors(val) : 3 nodes produced errors; first error: RUNTIME ERROR: Compilation error on line 15. Dimension mismatch in subset expression of y
Here is the model:
sink(file = "JAGS_survival_species.txt")
cat("
model {
for (j in 1:Nsp) { ## loop through species
#### Likelihood
for (i in 1:N) {
#### ecological model
logit(p[i, j]) <- b0[j] + b1[j] * log(HT_m[i]) + b2[j] * log(PREVDIA_cm[i]) +
b3[j] * PREVDIA_cm[i] + b4[j] * ANN_DIA_GROWTH_cm[i]
#### observation model
y[i, j] ~ dbinom(p[i, j], N)
}
#### Priors at species level -- all priors normal with a separate mean and sd
b0[j] ~ dnorm(mu.b0, tau.b0)
b1[j] ~ dnorm(mu.b1, tau.b1)
b2[j] ~ dnorm(mu.b2, tau.b2)
b3[j] ~ dnorm(mu.b3, tau.b3)
b4[j] ~ dnorm(mu.b4, tau.b4)
}
####################################################################
# hyperpriors (hierarchical model)
mu.b0 ~ dunif(-10, 10) # flat hyperpriors - for mu between -10 and 10
mu.b1 ~ dunif(-10, 10)
mu.b2 ~ dunif(-10, 10)
mu.b3 ~ dunif(-10, 10)
mu.b4 ~ dunif(-10, 10)
sd.b0 ~ dunif(0, 5)
tau.b0 <- 1/sd.b0^2
sd.b1 ~ dunif(0, 5)
tau.b1 <- 1/sd.b1^2
sd.b2 ~ dunif(0, 5)
tau.b2 <- 1/sd.b2^2
sd.b3 ~ dunif(0, 5)
tau.b3 <- 1/sd.b3^2
sd.b4 ~ dunif(0, 5)
tau.b4 <- 1/sd.b4^2
} # end model
",fill = TRUE)
sink()
Here are the first 500 observations of the dataset I am using:
ds <- structure(list(STATUSCD = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1,
1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1,
1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1,
0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1,
1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1,
1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0,
0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1,
1, 1, 1, 1, 0, 1), SPCD = c(7911, 8781, 8781, 8793, 8644, 8644,
8713, 8644, 7470, 8713, 877, 7808, 7808, 8489, 8644, 6114, 6114,
6114, 7298, 8311, 7290, 6443, 6743, 6114, 7565, 6129, 6129, 7565,
885, 6888, 8786, 8786, 6559, 6114, 6060, 6559, 8650, 6730, 7946,
7565, 986, 6651, 8268, 6114, 6338, 998, 8793, 8344, 8644, 6060,
6338, 8644, 6559, 7290, 6577, 6443, 8713, 6443, 8713, 8020, 7996,
6867, 8701, 8644, 859, 7565, 863, 940, 7306, 6311, 7565, 6693,
854, 7422, 6684, 8850, 8834, 6888, 6443, 7290, 7474, 854, 8169,
6313, 7442, 7717, 8596, 8713, 8121, 8489, 6792, 8311, 7565, 7565,
7565, 7285, 6128, 6128, 8931, 7290, 7290, 8644, 8644, 8344, 8173,
6059, 7565, 303, 6059, 8311, 7317, 854, 6284, 7317, 8020, 8311,
8311, 8020, 6541, 859, 7211, 998, 854, 854, 8781, 6407, 6790,
8311, 7317, 7470, 6114, 854, 7279, 8344, 303, 6255, 6255, 8223,
8344, 8558, 7298, 7470, 986, 986, 986, 989, 8558, 6313, 7994,
6443, 7813, 8558, 885, 6443, 7912, 8713, 7912, 877, 8311, 7098,
6443, 6541, 8701, 8701, 7470, 7474, 908, 6577, 6128, 6255, 6128,
8712, 8644, 6443, 8701, 8311, 7911, 8781, 7911, 8793, 7290, 8644,
8713, 6443, 6559, 859, 7285, 7808, 8713, 8644, 6114, 6114, 6114,
998, 8311, 8650, 6443, 8558, 6445, 6380, 854, 7565, 8850, 6730,
6114, 7474, 7290, 7290, 7565, 6835, 6443, 8572, 7565, 986, 854,
8713, 8558, 6338, 8644, 8644, 6060, 8793, 7290, 6577, 7290, 6559,
6443, 8361, 854, 8713, 8020, 8099, 7635, 6835, 8644, 8425, 7061,
7470, 8713, 7306, 854, 7565, 6255, 8211, 8850, 8873, 7474, 7290,
7474, 6255, 854, 6023, 7442, 7717, 7442, 7990, 8121, 8489, 8558,
7565, 7565, 854, 8768, 6393, 7290, 8644, 8644, 8644, 6389, 8173,
6059, 7565, 8650, 6743, 6661, 852, 852, 8794, 8020, 6283, 6064,
859, 998, 863, 854, 8781, 6407, 885, 6743, 7474, 7470, 7298,
8169, 8220, 8344, 303, 6255, 8223, 8344, 8409, 7298, 7822, 986,
986, 986, 986, 859, 6443, 7285, 7422, 7813, 6403, 8311, 8311,
8713, 8713, 8425, 7061, 8311, 8311, 8644, 6407, 8701, 8701, 7470,
908, 6577, 7565, 7298, 8712, 6443, 6443, 8701, 8305, 7911, 8781,
8781, 8793, 8644, 6114, 6114, 6443, 859, 7470, 7808, 6150, 7994,
6114, 7285, 6114, 6407, 8311, 7290, 6443, 7290, 6445, 6380, 7565,
7939, 7290, 7290, 7474, 7474, 6728, 8701, 6443, 8177, 7565, 854,
8713, 8558, 6338, 888, 8644, 6060, 6888, 7474, 8489, 6559, 6559,
6443, 8216, 6443, 8713, 6284, 6403, 6283, 6835, 8644, 859, 7565,
7565, 940, 8177, 7485, 7565, 8211, 6443, 7474, 6888, 7474, 7290,
7474, 863, 854, 8931, 7682, 7717, 859, 6792, 8713, 8489, 8311,
8425, 854, 8768, 6393, 7290, 8644, 7290, 8644, 6389, 8173, 6384,
8664, 7994, 6661, 852, 8794, 8020, 8311, 8020, 859, 998, 863,
854, 7946, 7285, 6443, 7474, 7470, 8220, 8169, 8344, 6255, 8223,
7565, 8713, 7298, 8701, 986, 986, 986, 986, 7442, 6114, 6064,
6403, 8311, 8311, 6443, 8713, 6790, 8713, 859, 7061, 7829, 6521,
6888, 6888, 8701, 7470, 8644, 8644, 6255, 8712, 8644, 6443, 8305,
7891, 8781, 8781, 7290, 6114, 6114, 6114, 877, 7994, 7808, 6684,
7994, 6114, 859, 877, 6114, 8311, 7290, 7290, 7290, 6380), PREVDIA_cm = c(2.794,
4.064, 5.08, 6.35, 27.178, 13.208, 20.828, 22.098, 25.908, 19.812,
23.622, 12.7, 13.97, 36.83, 53.34, 17.018, 22.098, 23.114, 25.146,
17.272, 36.576, 26.67, 27.686, 13.97, 4.572, 13.208, 14.478,
5.842, 69.596, 28.956, 20.32, 20.574, 19.558, 23.876, 3.048,
4.064, 32.512, 4.318, 3.048, 5.842, 3.048, 14.478, 12.446, 12.7,
14.224, 16.764, 5.334, 2.794, 20.066, 7.112, 84.582, 41.402,
32.004, 23.368, 15.24, 5.334, 3.302, 7.112, 13.208, 18.288, 19.05,
36.068, 14.732, 13.97, 12.954, 31.242, 15.494, 12.7, 5.08, 16.51,
4.064, 2.794, 18.796, 14.732, 5.334, 14.478, 3.302, 14.478, 45.72,
16.256, 24.13, 36.576, 15.24, 17.526, 12.954, 14.986, 13.208,
14.224, 17.78, 35.814, 20.32, 13.208, 13.462, 2.54, 13.208, 15.494,
16.002, 6.096, 16.256, 17.78, 16.002, 28.956, 12.7, 18.796, 12.954,
7.874, 2.54, 8.382, 13.97, 16.764, 2.794, 26.162, 13.716, 2.54,
12.954, 20.32, 20.574, 14.478, 76.2, 3.302, 45.212, 27.94, 14.224,
22.606, 12.446, 3.302, 2.794, 20.574, 22.606, 40.386, 54.102,
30.988, 32.004, 3.81, 3.048, 2.794, 4.318, 4.064, 19.812, 13.716,
17.526, 12.954, 13.97, 19.812, 15.748, 12.446, 16.764, 30.226,
19.05, 17.78, 4.826, 32.258, 56.388, 15.494, 14.224, 13.716,
3.81, 14.224, 15.494, 19.812, 45.466, 2.794, 18.542, 13.462,
34.798, 16.51, 23.114, 20.828, 6.096, 14.478, 18.288, 4.064,
22.86, 18.034, 17.018, 14.478, 3.302, 4.572, 4.826, 4.318, 15.24,
12.7, 22.606, 23.114, 14.986, 20.066, 6.35, 17.78, 21.59, 27.178,
13.208, 17.018, 19.812, 19.304, 17.526, 34.798, 15.748, 42.926,
60.96, 5.08, 13.462, 5.08, 30.988, 30.226, 18.288, 14.986, 14.986,
13.716, 3.048, 19.558, 6.858, 3.81, 6.604, 3.048, 14.986, 14.732,
38.862, 16.51, 3.556, 16.51, 2.794, 15.748, 16.256, 14.732, 30.48,
24.892, 7.62, 2.54, 2.794, 6.35, 18.542, 20.32, 45.466, 8.89,
5.08, 2.794, 4.572, 25.146, 13.716, 4.318, 14.986, 2.54, 3.81,
9.398, 26.67, 41.91, 24.892, 13.716, 19.05, 19.304, 31.496, 27.178,
25.4, 22.86, 48.514, 27.432, 19.558, 30.988, 36.83, 5.08, 7.62,
18.288, 22.352, 16.002, 18.796, 15.24, 16.764, 13.208, 6.096,
18.542, 7.874, 2.54, 7.874, 34.29, 2.794, 2.794, 3.302, 2.794,
14.478, 46.228, 31.75, 16.51, 23.876, 13.97, 16.51, 16.256, 3.81,
66.294, 20.574, 18.034, 25.654, 54.61, 8.636, 3.81, 3.302, 4.826,
4.826, 4.064, 13.208, 14.224, 17.018, 18.796, 17.272, 18.034,
19.812, 15.494, 14.986, 12.7, 21.59, 33.274, 2.794, 17.018, 15.748,
12.954, 3.81, 17.526, 3.556, 13.462, 17.526, 13.97, 17.272, 3.048,
15.748, 15.494, 30.734, 26.924, 20.066, 6.096, 18.542, 3.048,
13.462, 21.59, 13.716, 16.256, 2.54, 5.08, 4.318, 14.224, 20.066,
18.288, 17.526, 26.924, 18.796, 20.32, 17.018, 14.478, 20.574,
14.986, 30.226, 14.986, 6.096, 13.716, 14.224, 35.052, 16.256,
38.1, 6.858, 7.62, 24.638, 17.78, 15.24, 13.208, 24.13, 32.512,
16.764, 3.81, 4.318, 4.318, 18.796, 13.462, 45.466, 24.384, 2.54,
15.24, 9.144, 17.018, 17.272, 35.814, 24.13, 13.462, 3.556, 3.81,
5.08, 10.414, 19.304, 12.7, 13.97, 3.556, 4.064, 8.382, 2.794,
14.986, 23.622, 5.842, 4.318, 3.556, 6.096, 38.354, 22.606, 20.32,
20.574, 13.462, 20.828, 27.94, 29.464, 13.208, 12.7, 17.272,
3.81, 18.796, 12.954, 32.258, 16.002, 3.302, 32.766, 12.7, 20.828,
19.304, 24.892, 12.954, 22.098, 5.334, 14.478, 4.064, 4.064,
20.574, 3.048, 3.302, 3.048, 19.558, 17.018, 16.256, 20.32, 14.478,
14.478, 19.812, 17.526, 22.098, 55.118, 6.604, 12.446, 7.874,
8.382, 2.54, 2.794, 3.556, 7.366, 14.224, 14.224, 32.766, 25.654,
13.716, 18.288, 17.526, 37.338, 13.716, 20.828, 16.51, 14.986,
15.748, 15.494, 21.59, 4.318, 14.224, 4.826, 16.002, 19.304,
26.162, 28.194, 3.302, 14.224, 19.304, 6.096, 2.794, 19.304,
2.794, 14.986, 13.462, 30.734, 2.794, 4.826, 2.794, 17.526, 20.574,
16.51, 18.796, 16.51, 14.986, 12.7, 2.794, 14.478, 23.114, 20.574,
23.876, 9.906, 18.034, 31.496, 18.034, 27.178, 7.62), ANN_DIA_GROWTH_cm = c(0.2032,
0.2032, 0.2032, 0.2032, 0.635, 0.4826, 0.5842, 0.5842, 0.635,
0.5842, 0.4826, 0.4064, 0.4826, 0.5588, 0.4572, 0.4826, 0.5842,
0.635, 0.635, 0.4826, 0.5588, 0.635, 0.635, 0.4826, 0.2032, 0.4826,
0.4826, 0.2032, 0.4826, 0.508, 0.5842, 0.5842, 0.5842, 0.635,
0.2032, 0.2032, 0.508, 0.2032, 0.2032, 0.2032, 0.1524, 0.4826,
0.4064, 0.4064, 0.4826, 0.3556, 0.2032, 0.2032, 0.5842, 0.2032,
0.4572, 0.4572, 0.508, 0.635, 0.4826, 0.2032, 0.2032, 0.2032,
0.4826, 0.5842, 0.5842, 0.5588, 0.4826, 0.4826, 0.3556, 0.508,
0.3556, 0.3556, 0.2032, 0.4826, 0.2032, 0.2032, 0.4064, 0.4826,
0.2032, 0.4826, 0.2032, 0.4826, 0.4318, 0.4826, 0.635, 0.2794,
0.4826, 0.4826, 0.4826, 0.4826, 0.4826, 0.4826, 0.4826, 0.5588,
0.5842, 0.4826, 0.4826, 0.2032, 0.4826, 0.4826, 0.4826, 0.2032,
0.4826, 0.4826, 0.4826, 0.508, 0.4064, 0.5842, 0.4826, 0.4064,
0.2032, 0.4064, 0.4826, 0.4826, 0.2032, 0.4826, 0.4826, 0.2032,
0.4826, 0.5842, 0.5842, 0.4826, 0.4572, 0.1524, 0.4318, 0.4826,
0.3556, 0.4064, 0.4064, 0.2032, 0.2032, 0.5842, 0.5842, 0.4572,
0.4572, 0.4572, 0.508, 0.2032, 0.2032, 0.2032, 0.2032, 0.2032,
0.5842, 0.4826, 0.4826, 0.4826, 0.3556, 0.4064, 0.3556, 0.3556,
0.4826, 0.508, 0.5842, 0.4826, 0.2032, 0.508, 0.4826, 0.4826,
0.4826, 0.4826, 0.2032, 0.3556, 0.4826, 0.5842, 0.4318, 0.2032,
0.5842, 0.4826, 0.5588, 0.4826, 0.4826, 0.5842, 0.2032, 0.4826,
0.5842, 0.2032, 0.5842, 0.5842, 0.4826, 0.4826, 0.2032, 0.2032,
0.2032, 0.2032, 0.4826, 0.4064, 0.5842, 0.635, 0.4826, 0.4064,
0.2032, 0.4826, 0.5842, 0.635, 0.4826, 0.4826, 0.5842, 0.4064,
0.4826, 0.5588, 0.4826, 0.4572, 0.4572, 0.2032, 0.3556, 0.2032,
0.508, 0.508, 0.5842, 0.4826, 0.4826, 0.4826, 0.2032, 0.5842,
0.2032, 0.2032, 0.2032, 0.1524, 0.3556, 0.4826, 0.4572, 0.4826,
0.2032, 0.4826, 0.2032, 0.4826, 0.4826, 0.4826, 0.508, 0.635,
0.2032, 0.2032, 0.1524, 0.2032, 0.5842, 0.5842, 0.4318, 0.4064,
0.2032, 0.2032, 0.2032, 0.635, 0.4826, 0.2032, 0.3556, 0.2032,
0.2032, 0.4064, 0.635, 0.4572, 0.635, 0.4826, 0.5842, 0.5842,
0.4572, 0.635, 0.635, 0.5842, 0.4572, 0.635, 0.5842, 0.508, 0.5588,
0.2032, 0.2032, 0.4064, 0.5842, 0.4826, 0.5842, 0.4826, 0.4826,
0.4826, 0.2032, 0.5842, 0.4064, 0.2032, 0.4064, 0.5588, 0.2032,
0.1524, 0.1524, 0.2032, 0.4826, 0.4318, 0.508, 0.3556, 0.4826,
0.3556, 0.3556, 0.4826, 0.2032, 0.4826, 0.5842, 0.5842, 0.635,
0.4572, 0.4064, 0.2032, 0.2032, 0.2032, 0.2032, 0.2032, 0.4826,
0.4826, 0.4826, 0.5842, 0.3556, 0.4064, 0.4064, 0.3556, 0.3556,
0.4064, 0.5842, 0.5588, 0.2032, 0.4826, 0.4826, 0.4826, 0.2032,
0.4826, 0.2032, 0.4826, 0.4826, 0.4826, 0.4826, 0.2032, 0.4826,
0.4826, 0.508, 0.4826, 0.5842, 0.2032, 0.5842, 0.2032, 0.4826,
0.5842, 0.4826, 0.4826, 0.2032, 0.2032, 0.2032, 0.4826, 0.5842,
0.5842, 0.4826, 0.635, 0.4064, 0.5842, 0.4826, 0.4826, 0.5842,
0.4826, 0.508, 0.4826, 0.2032, 0.4826, 0.4826, 0.5588, 0.4826,
0.5588, 0.2032, 0.2032, 0.635, 0.4826, 0.4826, 0.4826, 0.635,
0.508, 0.4826, 0.2032, 0.2032, 0.2032, 0.4064, 0.4826, 0.4318,
0.635, 0.1524, 0.4826, 0.4064, 0.4826, 0.4826, 0.5588, 0.635,
0.4826, 0.2032, 0.2032, 0.2032, 0.4064, 0.5842, 0.4064, 0.4826,
0.2032, 0.2032, 0.3556, 0.2032, 0.4826, 0.4826, 0.2032, 0.2032,
0.2032, 0.2032, 0.4572, 0.5842, 0.5842, 0.5842, 0.4826, 0.5842,
0.4826, 0.4572, 0.4826, 0.4064, 0.4826, 0.1524, 0.5842, 0.4826,
0.508, 0.4826, 0.2032, 0.4572, 0.4064, 0.5842, 0.5842, 0.635,
0.4826, 0.5842, 0.2032, 0.4826, 0.2032, 0.2032, 0.5842, 0.2032,
0.1524, 0.2032, 0.5842, 0.4826, 0.4826, 0.4064, 0.3556, 0.3556,
0.4064, 0.4826, 0.5842, 0.4572, 0.2032, 0.4064, 0.4064, 0.4064,
0.2032, 0.2032, 0.2032, 0.2032, 0.4826, 0.4826, 0.508, 0.4826,
0.3556, 0.4064, 0.3556, 0.5588, 0.4826, 0.5842, 0.4826, 0.4826,
0.4826, 0.4826, 0.5842, 0.2032, 0.4826, 0.1524, 0.4826, 0.5842,
0.635, 0.508, 0.2032, 0.4826, 0.5842, 0.2032, 0.2032, 0.5842,
0.2032, 0.4826, 0.4826, 0.508, 0.2032, 0.2032, 0.2032, 0.4826,
0.5842, 0.4826, 0.5842, 0.3556, 0.4826, 0.4064, 0.2032, 0.4826,
0.635, 0.4064, 0.4826, 0.4064, 0.5842, 0.508, 0.5842, 0.635,
0.2032), HT_m = c(5.48646671543526, 5.18166300902219, 7.62009266032675,
9.44891489880517, 14.6305779078274, 13.106559375762, 9.1441111923921,
17.9834186783711, 1.82882223847842, 18.2882223847842, 8.22970007315289,
9.44891489880517, 7.92489636673982, 7.92489636673982, 18.5930260911973,
12.4969519629359, 16.4594001463058, 11.5825408436967, 8.83930748597903,
15.8497927334796, 21.0314557425018, 21.641063155328, 11.8873445501097,
11.8873445501097, 6.40087783467447, 9.1441111923921, 8.53450377956596,
6.0960741282614, 11.5825408436967, 14.9353816142404, 11.5825408436967,
11.8873445501097, 14.0209704950012, 10.0585223116313, 3.96244818336991,
3.96244818336991, 12.4969519629359, 3.35284077054377, 3.35284077054377,
9.44891489880517, 2.43842965130456, 8.83930748597903, 7.31528895391368,
10.0585223116313, 2.13362594489149, 11.8873445501097, 4.26725188978298,
3.0480370641307, 14.9353816142404, 7.92489636673982, 21.945866861741,
23.1650816873933, 17.9834186783711, 16.4594001463058, 7.92489636673982,
3.0480370641307, 4.26725188978298, 3.96244818336991, 7.31528895391368,
14.0209704950012, 9.44891489880517, 20.1170446232626, 7.92489636673982,
7.92489636673982, 11.5825408436967, 11.8873445501097, 10.3633260180444,
7.92489636673982, 6.40087783467447, 6.0960741282614, 6.40087783467447,
4.57205559619605, 6.0960741282614, 7.92489636673982, 3.0480370641307,
6.40087783467447, 6.40087783467447, 8.53450377956596, 16.1545964398927,
7.62009266032675, 14.0209704950012, 14.9353816142404, 7.92489636673982,
10.9729334308705, 10.9729334308705, 6.0960741282614, 7.92489636673982,
7.01048524750061, 10.3633260180444, 11.8873445501097, 11.8873445501097,
3.0480370641307, 10.0585223116313, 5.48646671543526, 7.92489636673982,
11.8873445501097, 7.62009266032675, 3.96244818336991, 10.9729334308705,
8.53450377956596, 10.0585223116313, 14.9353816142404, 10.6681297244574,
9.44891489880517, 4.57205559619605, 7.01048524750061, 3.96244818336991,
6.0960741282614, 7.31528895391368, 6.40087783467447, 3.0480370641307,
9.1441111923921, 9.44891489880517, 3.0480370641307, 9.44891489880517,
7.01048524750061, 7.92489636673982, 10.6681297244574, 18.5930260911973,
3.96244818336991, 13.106559375762, 7.01048524750061, 7.92489636673982,
10.0585223116313, 11.2777371372836, 3.96244818336991, 6.40087783467447,
5.79127042184833, 2.13362594489149, 10.3633260180444, 12.4969519629359,
9.44891489880517, 8.53450377956596, 3.0480370641307, 2.74323335771763,
5.48646671543526, 4.57205559619605, 4.26725188978298, 6.0960741282614,
8.22970007315289, 9.44891489880517, 20.1170446232626, 7.01048524750061,
9.75371860521824, 9.44891489880517, 10.9729334308705, 10.3633260180444,
15.5449890270666, 11.5825408436967, 16.4594001463058, 6.0960741282614,
13.106559375762, 12.1921482565228, 14.3257742014143, 9.44891489880517,
11.2777371372836, 5.48646671543526, 10.0585223116313, 4.57205559619605,
10.6681297244574, 10.0585223116313, 3.0480370641307, 6.0960741282614,
7.92489636673982, 7.92489636673982, 8.83930748597903, 11.5825408436967,
8.22970007315289, 3.96244818336991, 7.01048524750061, 5.48646671543526,
4.57205559619605, 16.4594001463058, 14.0209704950012, 10.9729334308705,
8.53450377956596, 4.87685930260912, 4.87685930260912, 8.22970007315289,
3.96244818336991, 10.3633260180444, 12.4969519629359, 9.44891489880517,
17.9834186783711, 11.8873445501097, 14.9353816142404, 7.92489636673982,
10.9729334308705, 11.2777371372836, 17.9834186783711, 12.4969519629359,
14.6305779078274, 11.2777371372836, 8.83930748597903, 6.0960741282614,
17.9834186783711, 20.1170446232626, 19.2026335040234, 21.0314557425018,
6.40087783467447, 5.48646671543526, 7.01048524750061, 12.4969519629359,
18.2882223847842, 9.44891489880517, 11.8873445501097, 11.8873445501097,
8.53450377956596, 4.57205559619605, 9.44891489880517, 5.79127042184833,
4.26725188978298, 9.44891489880517, 2.13362594489149, 7.62009266032675,
7.31528895391368, 10.0585223116313, 2.13362594489149, 3.0480370641307,
4.87685930260912, 3.0480370641307, 13.7161667885881, 10.3633260180444,
8.53450377956596, 7.62009266032675, 20.1170446232626, 3.0480370641307,
3.96244818336991, 3.0480370641307, 4.87685930260912, 14.9353816142404,
8.53450377956596, 20.1170446232626, 4.87685930260912, 4.87685930260912,
4.57205559619605, 6.40087783467447, 15.5449890270666, 7.92489636673982,
6.40087783467447, 7.01048524750061, 3.96244818336991, 3.96244818336991,
5.48646671543526, 10.0585223116313, 10.9729334308705, 16.4594001463058,
7.62009266032675, 12.8017556693489, 8.22970007315289, 10.6681297244574,
7.92489636673982, 13.4113630821751, 9.1441111923921, 14.0209704950012,
10.9729334308705, 10.9729334308705, 6.40087783467447, 17.0690075591319,
7.31528895391368, 7.92489636673982, 13.4113630821751, 10.9729334308705,
10.0585223116313, 9.44891489880517, 10.0585223116313, 14.9353816142404,
8.53450377956596, 4.87685930260912, 4.87685930260912, 7.92489636673982,
3.96244818336991, 6.0960741282614, 11.8873445501097, 3.96244818336991,
4.87685930260912, 6.70568154108754, 4.26725188978298, 9.44891489880517,
14.9353816142404, 11.8873445501097, 10.9729334308705, 7.01048524750061,
8.22970007315289, 9.44891489880517, 11.5825408436967, 3.96244818336991,
17.0690075591319, 13.106559375762, 7.31528895391368, 7.01048524750061,
14.0209704950012, 6.40087783467447, 3.0480370641307, 3.35284077054377,
3.0480370641307, 4.87685930260912, 4.26725188978298, 7.92489636673982,
4.87685930260912, 9.44891489880517, 7.31528895391368, 4.57205559619605,
9.75371860521824, 10.0585223116313, 6.40087783467447, 10.9729334308705,
12.4969519629359, 17.0690075591319, 13.106559375762, 4.57205559619605,
17.6786149719581, 4.57205559619605, 4.26725188978298, 5.79127042184833,
12.1921482565228, 5.18166300902219, 12.1921482565228, 7.62009266032675,
3.35284077054377, 7.92489636673982, 4.87685930260912, 3.96244818336991,
7.92489636673982, 7.92489636673982, 13.106559375762, 9.1441111923921,
7.01048524750061, 13.106559375762, 4.87685930260912, 9.1441111923921,
12.4969519629359, 10.9729334308705, 10.0585223116313, 3.96244818336991,
4.87685930260912, 5.18166300902219, 10.6681297244574, 6.0960741282614,
10.0585223116313, 10.9729334308705, 17.9834186783711, 10.3633260180444,
19.5074372104365, 11.2777371372836, 14.0209704950012, 12.1921482565228,
12.8017556693489, 18.8978297976103, 14.0209704950012, 6.0960741282614,
7.92489636673982, 15.5449890270666, 22.2506705681541, 13.4113630821751,
14.6305779078274, 6.0960741282614, 5.79127042184833, 16.1545964398927,
7.92489636673982, 7.62009266032675, 11.5825408436967, 14.9353816142404,
15.8497927334796, 7.01048524750061, 4.57205559619605, 4.26725188978298,
9.44891489880517, 7.92489636673982, 11.2777371372836, 13.4113630821751,
2.13362594489149, 3.0480370641307, 7.92489636673982, 9.1441111923921,
16.4594001463058, 13.106559375762, 17.9834186783711, 21.945866861741,
17.9834186783711, 2.13362594489149, 4.26725188978298, 3.0480370641307,
7.92489636673982, 10.3633260180444, 7.92489636673982, 9.44891489880517,
5.79127042184833, 4.26725188978298, 9.44891489880517, 6.40087783467447,
13.106559375762, 11.2777371372836, 6.0960741282614, 3.65764447695684,
6.0960741282614, 3.35284077054377, 16.1545964398927, 16.4594001463058,
10.9729334308705, 15.2401853206535, 9.1441111923921, 8.53450377956596,
9.75371860521824, 10.6681297244574, 9.1441111923921, 7.31528895391368,
7.92489636673982, 8.53450377956596, 10.0585223116313, 10.3633260180444,
8.53450377956596, 3.65764447695684, 5.48646671543526, 14.6305779078274,
9.1441111923921, 10.3633260180444, 6.40087783467447, 11.2777371372836,
8.22970007315289, 11.8873445501097, 4.87685930260912, 4.87685930260912,
7.01048524750061, 3.0480370641307, 10.3633260180444, 3.35284077054377,
5.79127042184833, 4.26725188978298, 9.1441111923921, 9.44891489880517,
14.0209704950012, 11.2777371372836, 3.96244818336991, 8.53450377956596,
7.92489636673982, 11.2777371372836, 9.1441111923921, 18.8978297976103,
11.5825408436967, 9.1441111923921, 4.87685930260912, 6.40087783467447,
4.26725188978298, 5.18166300902219, 4.26725188978298, 5.79127042184833,
7.92489636673982, 9.44891489880517, 8.53450377956596, 8.22970007315289,
6.40087783467447, 9.44891489880517, 13.4113630821751, 16.4594001463058,
10.9729334308705, 13.106559375762, 11.8873445501097, 5.48646671543526,
4.57205559619605, 0.91441111923921, 8.53450377956596, 6.40087783467447,
14.0209704950012, 6.40087783467447, 13.106559375762, 13.7161667885881,
16.4594001463058, 11.2777371372836, 6.0960741282614, 6.0960741282614,
7.92489636673982, 2.13362594489149, 2.13362594489149, 11.8873445501097,
3.96244818336991, 12.1921482565228, 12.4969519629359, 14.0209704950012,
3.96244818336991, 6.0960741282614, 4.87685930260912, 9.44891489880517,
10.0585223116313, 7.62009266032675, 10.6681297244574, 9.44891489880517,
10.3633260180444, 13.106559375762, 3.0480370641307, 11.8873445501097,
17.373811265545, 6.70568154108754, 14.0209704950012, 7.62009266032675,
11.5825408436967, 14.9353816142404, 13.4113630821751, 3.96244818336991,
5.48646671543526)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L,
34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L,
60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L,
73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L,
86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L,
99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L,
110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L,
121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L,
132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L,
143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L,
154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L,
165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L,
176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L, 186L,
187L, 188L, 189L, 190L, 191L, 192L, 193L, 194L, 195L, 196L, 197L,
198L, 199L, 200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L,
209L, 210L, 211L, 212L, 213L, 214L, 215L, 216L, 217L, 218L, 220L,
221L, 222L, 223L, 224L, 225L, 226L, 227L, 228L, 229L, 230L, 231L,
232L, 233L, 234L, 235L, 236L, 237L, 238L, 239L, 240L, 241L, 242L,
243L, 244L, 245L, 246L, 247L, 248L, 249L, 250L, 251L, 252L, 253L,
254L, 255L, 256L, 257L, 258L, 259L, 260L, 261L, 262L, 263L, 264L,
265L, 266L, 267L, 268L, 269L, 270L, 271L, 272L, 273L, 274L, 275L,
276L, 277L, 278L, 279L, 280L, 281L, 282L, 283L, 284L, 285L, 286L,
287L, 288L, 289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L,
298L, 299L, 300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 308L,
309L, 310L, 311L, 312L, 313L, 314L, 315L, 316L, 317L, 318L, 319L,
320L, 321L, 322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L, 330L,
331L, 332L, 333L, 334L, 335L, 336L, 337L, 338L, 339L, 340L, 341L,
342L, 343L, 344L, 345L, 346L, 347L, 348L, 349L, 350L, 351L, 352L,
353L, 354L, 355L, 356L, 357L, 358L, 359L, 360L, 361L, 362L, 363L,
364L, 365L, 366L, 367L, 368L, 369L, 370L, 371L, 372L, 373L, 375L,
376L, 377L, 378L, 379L, 380L, 381L, 382L, 383L, 384L, 385L, 386L,
387L, 388L, 389L, 390L, 391L, 392L, 393L, 394L, 395L, 396L, 397L,
398L, 399L, 400L, 401L, 402L, 403L, 404L, 405L, 406L, 407L, 408L,
409L, 410L, 411L, 412L, 413L, 414L, 415L, 416L, 417L, 418L, 419L,
420L, 421L, 422L, 423L, 424L, 425L, 426L, 427L, 428L, 429L, 430L,
432L, 433L, 434L, 435L, 436L, 437L, 438L, 439L, 440L, 441L, 442L,
443L, 444L, 445L, 446L, 447L, 448L, 449L, 450L, 451L, 452L, 453L,
454L, 455L, 456L, 457L, 458L, 459L, 460L, 461L, 462L, 463L, 464L,
465L, 466L, 467L, 468L, 469L, 470L, 471L, 472L, 473L, 474L, 475L,
476L, 477L, 478L, 479L, 480L, 481L, 482L, 483L, 484L, 485L, 486L,
487L, 488L, 489L, 490L, 491L, 492L, 493L, 494L, 495L, 496L, 497L,
498L, 499L, 500L, 501L, 502L, 503L), class = "data.frame")
## make data list
jags.data <- list(y = ds$STATUSCD,
PREVDIA_cm = ds$PREVDIA_cm,
ANN_DIA_GROWTH_cm = ds$ANN_DIA_GROWTH_cm,
HT_m = ds$HT_m,
sp = ds$SPCD,
Nsp = length(unique(ds$SPCD)),
N = length(ds$TREE)
)
And here is how I am calling/ running the model in R
library(R2jags)
# parameters to monitor
par.list <- c("p", "b0", "b1", "b2", "b3", "b4")
## Run model in R2Jags with parallel computing
survival_species.jags = jags.parallel(
data = jags.data,
model.file = "JAGS_survival_species.txt",
parameters.to.save = par.list,
jags.seed = 123, n.chains = 3, n.iter = 9999, n.thin = 20,
)
I suspect is something to do with the nested loops over species and observations and the indexing. I am new to jags/ bugs code, so any help is greatly appreciated. Thanks for your time.
I can get the simpler model to run (with no species coeffects). it's the j loop that has me messed up.
Upvotes: 0
Views: 56
Reputation: 660
As suggested by the comments in response to the question, I think the issue here does have to do with indexing. Briefly, you're passing observations (y
) that are formatted as a vector to a model that is trying to treat them as a matrix (y[i,j]
in your likelihood). Two options are that you could (1) reformat your data so that it is in a matrix or (2) rewrite your model so that you can pass it a vector of observations. I'll take the second approach here, starting with the ds
object that you pasted.
The first thing is to index your groups (species, in this case) so that JAGS knows what to expect. For example, you can do this by creating a reference data frame that assigns each species a unique ID from 1:(number of species). Then join that reference dataframe to your original data (ds
). Each species is now assigned a unique index. Yes, the species were already uniquely identified but the reason I reassign them a new ID will reveal itself below.
library(tidyverse)
referenceDF <- data.frame(sp=unique(ds$SPCD),id=1:length(unique(ds$SPCD)))
ds <- ds %>%
left_join(referenceDF, by = c("SPCD" = "sp") )
Next, make the list of data. Instead of assigning the species its code, you'll use the id we created above. Note also you'll want to set N = nrow(ds)
to make sure you have the right number of observations.
jags.data <- list(y = ds$STATUSCD,
PREVDIA_cm = ds$PREVDIA_cm,
ANN_DIA_GROWTH_cm = ds$ANN_DIA_GROWTH_cm,
HT_m = ds$HT_m,
sp = ds$id,
Nsp = length(unique(ds$SPCD)),
N = nrow(ds)
)
With that new set up for the data in mind, let's rewrite the model. The full code is below but here are the highlights. First, I'll use nested indexing. In the likelihood, our for loop will run over each observation. For each observation in the ecological model, I call up species-specific parameters like b0
by writing b0[sp[i]]
. This says something like, for observation i
, get the species id (which I set up to be a unique number!), and then get the parameter identified by that species id. Second, this equation predicts the value of p[i]
(per observation) that I plug in to our observation model for y[i]
(again, per observation). Third, I write a separate for loop for the priors, which I index separately by species. For example, in modifying your model I assign b0
a unique prior for each j in 1:nSp
. I didn't make any changes to the hyperpriors.
sink(file = "JAGS_survival_species.txt")
cat("
model {
#### Likelihood
for (i in 1:N) { ## loop through observations (species are nested within )
#### ecological model
logit(p[i]) <- b0[sp[i]] + b1[sp[i]] * log(HT_m[i]) + b2[sp[i]] * log(PREVDIA_cm[i]) +
b3[sp[i]] * PREVDIA_cm[i] + b4[sp[i]] * ANN_DIA_GROWTH_cm[i]
#### observation model
y[i] ~ dbinom(p[i], N)
}
for(j in 1:Nsp){
#### Priors at species level -- all priors normal with a separate mean and sd
b0[j] ~ dnorm(mu.b0, tau.b0)
b1[j] ~ dnorm(mu.b1, tau.b1)
b2[j] ~ dnorm(mu.b2, tau.b2)
b3[j] ~ dnorm(mu.b3, tau.b3)
b4[j] ~ dnorm(mu.b4, tau.b4)
}
####################################################################
# hyperpriors (hierarchical model)
mu.b0 ~ dunif(-10, 10) # flat hyperpriors - for mu between -10 and 10
mu.b1 ~ dunif(-10, 10)
mu.b2 ~ dunif(-10, 10)
mu.b3 ~ dunif(-10, 10)
mu.b4 ~ dunif(-10, 10)
sd.b0 ~ dunif(0, 5)
tau.b0 <- 1/sd.b0^2
sd.b1 ~ dunif(0, 5)
tau.b1 <- 1/sd.b1^2
sd.b2 ~ dunif(0, 5)
tau.b2 <- 1/sd.b2^2
sd.b3 ~ dunif(0, 5)
tau.b3 <- 1/sd.b3^2
sd.b4 ~ dunif(0, 5)
tau.b4 <- 1/sd.b4^2
} # end model
",fill = TRUE)
sink()
In summary, to get your model to run, I (1) re-indexed the species, (2) modified the list of data, and (3) revised the model to take advantage of the new indexing. For more information about nested indexing in JAGS, I recommend https://masonfidino.com/nested_indexing/. This ran for me, using R2jags and rjags - I hope it works for you!
Upvotes: 0