Chapter 10 Exercises
Students' goals
Solutions
In Example 10.3.4 (Students' goals),
a) extend the model to allow the gender effect b.boy to vary between schools, giving a random coefficient model. Use the Comparison Tool to compare the odds ratio, and the probabilities that a boy (or a girl) prefers sports, between schools
b) use the DIC to compare this model with the original fixed coefficient model.
c) use conflict p-values
(see Section 10.7, Example 10.7.4)
to assess whether there are any schools whose effects are inconsistent with the random effects distribution.
a) The code added in bold shows how the original model is changed to add random gender effects.
model {
for (i in 1:npupil) {
Goals[i] ~ dcat(p[i,])
Goals.fix[i] ~ dcat(p.fix[i,])
for (k in 1:3) {
p[i,k] <- q[i,k]/sum(q[i,])
log(q[i,k]) <- a[i,k]
p.fix[i,k] <- q.fix[i,k]/sum(q.fix[i,])
log(q.fix[i,k]) <- a.fix[i,k]
}
a[i,1] <- b[School[i], 1] +
b.boy[School[i]]
*Gender[i]
a[i,2] <- b[School[i], 2]
a[i,3] <- 0
a.fix[i,1] <- b.fix[School[i], 1] + b.boy.fix[School[i]]*Gender[i]
a.fix[i,2] <- b.fix[School[i], 2]
a.fix[i,3] <- 0
}
for (j in 1:nschool) {
b[j,1] ~ dnorm(mu[1], tau[1])
mub2[j] <- mu[2] + s[2]/s[1]*cor*(b[j,1] - mu[1])
b[j,2] ~ dnorm(mub2[j], taub2)
b[j, 3] <- 0
for (k in 1:3) { egirl[j,k] <- exp(b[j,k]) }
eboy[j,1] <- exp(b[j,1] +
b.boy[j]
); for (k in 2:3) { eboy[j,k] <- exp(b[j,k]) }
for (k in 1:3) {
p.girl[j,k] <- egirl[j,k] / sum(egirl[j,])
p.boy[j,k] <- eboy[j,k] / sum(eboy[j,])
}
b.boy[j] ~ dnorm(mu.boy, tau.boy)
or.boy[j] <- exp(b.boy[j])
b.pred[j,1] ~ dnorm(mu[1], tau[1])
b.pred[j,2] ~ dnorm(mub2[j], taub2)
b.boy.pred[j] ~ dnorm(mu.boy, tau.boy)
b.fix[j,1] ~ dnorm(0, 0.0001)
b.fix[j,2] ~ dnorm(0, 0.0001)
b.boy.fix[j] ~ dnorm(0, 0.0001)
p.base.fix[j,1] <- step(b.fix[j,1] - b.pred[j,1])
p.base.fix[j,2] <- step(b.fix[j,2] - b.pred[j,2])
p.boy.fix[j] <- step(b.boy.fix[j] - b.boy.pred[j])
}
vb2 <- (1 - cor*cor)*v[2]
tau[1] <- 1/v[1]
taub2 <- 1/vb2
for (k in 1:2) {
mu[k] ~ dnorm(0, 0.0001)
v[k] <- s[k]*s[k]
s[k] ~ dunif(0, 100)
}
cor ~ dunif(0, 1)
mu.boy ~ dnorm(0, 0.0001)
sig.boy ~ dunif(0, 10); tau.boy <- 1 / pow(sig.boy, 2)
}
list(mu=c(0,0), s=c(1,1), cor=0.5, mu.boy=0, sig.boy=1)
The gender odds ratio is fairly homogeneous between schools, with posterior means of around 3.
The biggest odds ratio is for school 6 (Brown Middle) where over 50% of boys preferred sports, compared to about 10% of girls. Though given the extent of the credible intervals around each school-specific odds ratio, this seems consistent with sampling variability.
The probabilities of preferring sports for a boy in each of the nine schools are shown in the plot below, and below again for a girl. Note the schools are in upside-down order compared to Figure 10.5 in the book, which was plotted in R. The probabilities are substantively the same as in the hierarchical model without random coefficients.
(note: use right click on plot, Properties, Axis Bounds to make x axis the same as for boys)
b) The DIC of 952 suggests that adding random coefficients doesn't improve the predictive ability of the model, compared to the hierarchical model with random intercepts but not random coefficients (DIC 951)
Dbar = post.mean of -2logL; Dhat = -2LogL at post.mean of stochastic nodes
Dbar Dhat pD DIC
Goals 937.052 921.775 15.277 952.328
total 937.052 921.775 15.277 952.328
c) To test whether there are any schools (e.g. school 6) that are inconsistent with the normal distribution for the random coefficients model, see the
code added in red
in the model above. The resulting conflict p-values (
p.base.fix
and
p.boy.fix
) are all around 0.5 (see the posterior summary statistics below) suggesting all schools are consistent with this model.
node mean sd MC error 2.5% median 97.5% start sample
cor 0.7268 0.2447 0.001997 0.115 0.8042 0.9935 5000 390002
mu[1] -1.535 0.3223 0.003277 -2.183 -1.535 -0.9015 5000 390002
mu[2] -0.4928 0.2135 0.00118 -0.9037 -0.4995 -0.04512 5000 390002
mu.boy 1.04 0.3053 0.004377 0.4312 1.041 1.633 5000 390002
or.boy[1] 2.774 1.335 0.01407 0.7749 2.613 5.826 5000 390002
or.boy[2] 3.086 1.301 0.01528 1.342 2.851 6.232 5000 390002
or.boy[3] 2.757 1.044 0.01338 1.184 2.614 5.188 5000 390002
or.boy[4] 2.97 1.178 0.01396 1.234 2.792 5.796 5000 390002
or.boy[5] 3.326 1.641 0.01614 1.33 2.994 7.439 5000 390002
or.boy[6] 4.301 2.295 0.02671 1.846 3.673 10.43 5000 390002
or.boy[7] 3.345 1.498 0.01693 1.487 3.047 7.039 5000 390002
or.boy[8] 2.773 1.069 0.01317 1.113 2.636 5.272 5000 390002
or.boy[9] 2.718 1.193 0.0137 0.8665 2.578 5.499 5000 390002
p.base.fix[1,1] 0.5058 0.5 8.116E-4 0.0 1.0 1.0 5000 390002
p.base.fix[1,2] 0.501 0.5 7.996E-4 0.0 1.0 1.0 5000 390002
p.base.fix[2,1] 0.506 0.5 7.98E-4 0.0 1.0 1.0 5000 390002
p.base.fix[2,2] 0.5021 0.5 8.005E-4 0.0 1.0 1.0 5000 390002
p.base.fix[3,1] 0.5053 0.5 7.898E-4 0.0 1.0 1.0 5000 390002
p.base.fix[3,2] 0.5017 0.5 8.219E-4 0.0 1.0 1.0 5000 390002
p.base.fix[4,1] 0.5054 0.5 7.856E-4 0.0 1.0 1.0 5000 390002
p.base.fix[4,2] 0.5002 0.5 8.043E-4 0.0 1.0 1.0 5000 390002
p.base.fix[5,1] 0.5056 0.5 8.074E-4 0.0 1.0 1.0 5000 390002
p.base.fix[5,2] 0.5012 0.5 8.152E-4 0.0 1.0 1.0 5000 390002
p.base.fix[6,1] 0.5062 0.5 7.96E-4 0.0 1.0 1.0 5000 390002
p.base.fix[6,2] 0.5026 0.5 8.0E-4 0.0 1.0 1.0 5000 390002
p.base.fix[7,1] 0.5064 0.5 7.887E-4 0.0 1.0 1.0 5000 390002
p.base.fix[7,2] 0.5048 0.5 8.318E-4 0.0 1.0 1.0 5000 390002
p.base.fix[8,1] 0.5072 0.4999 8.015E-4 0.0 1.0 1.0 5000 390002
p.base.fix[8,2] 0.5006 0.5 7.719E-4 0.0 1.0 1.0 5000 390002
p.base.fix[9,1] 0.5049 0.5 8.337E-4 0.0 1.0 1.0 5000 390002
p.base.fix[9,2] 0.5032 0.5 8.087E-4 0.0 1.0 1.0 5000 390002
p.boy[1,1] 0.3613 0.122 0.001122 0.1393 0.3548 0.6173 5000 390002
p.boy[1,2] 0.3358 0.08841 7.601E-4 0.1894 0.3252 0.5355 5000 390002
p.boy[1,3] 0.3028 0.09673 0.001207 0.1273 0.2992 0.4956 5000 390002
p.boy[2,1] 0.2146 0.05296 3.093E-4 0.1191 0.2118 0.3254 5000 390002
p.boy[2,2] 0.2491 0.04212 2.991E-4 0.1685 0.2486 0.3334 5000 390002
p.boy[2,3] 0.5363 0.05763 4.418E-4 0.4251 0.5356 0.6513 5000 390002
p.boy[3,1] 0.211 0.05007 3.05E-4 0.1199 0.2089 0.3152 5000 390002
p.boy[3,2] 0.2745 0.03979 2.487E-4 0.2005 0.2731 0.3572 5000 390002
p.boy[3,3] 0.5144 0.05165 3.41E-4 0.4141 0.5144 0.6152 5000 390002
p.boy[4,1] 0.3588 0.08028 6.105E-4 0.2109 0.3553 0.5245 5000 390002
p.boy[4,2] 0.3021 0.05744 4.563E-4 0.2008 0.298 0.4253 5000 390002
p.boy[4,3] 0.3392 0.06757 7.284E-4 0.2119 0.3374 0.4735 5000 390002
p.boy[5,1] 0.296 0.08778 4.733E-4 0.1453 0.2882 0.4899 5000 390002
p.boy[5,2] 0.2516 0.05324 3.177E-4 0.1495 0.2511 0.3608 5000 390002
p.boy[5,3] 0.4524 0.08033 5.352E-4 0.2943 0.4531 0.6104 5000 390002
p.boy[6,1] 0.4011 0.08507 6.939E-4 0.2516 0.3952 0.5823 5000 390002
p.boy[6,2] 0.2383 0.04864 3.718E-4 0.1463 0.2373 0.3376 5000 390002
p.boy[6,3] 0.3607 0.06537 5.221E-4 0.2337 0.3605 0.4888 5000 390002
p.boy[7,1] 0.2371 0.06103 3.736E-4 0.1304 0.2325 0.3692 5000 390002
p.boy[7,2] 0.2094 0.04436 4.293E-4 0.1251 0.2086 0.2968 5000 390002
p.boy[7,3] 0.5535 0.06438 5.733E-4 0.4286 0.5535 0.6793 5000 390002
p.boy[8,1] 0.2625 0.06554 4.174E-4 0.1417 0.2599 0.3994 5000 390002
p.boy[8,2] 0.2874 0.04573 2.989E-4 0.2048 0.2845 0.3854 5000 390002
p.boy[8,3] 0.4501 0.05962 4.014E-4 0.3333 0.4501 0.567 5000 390002
p.boy[9,1] 0.1979 0.06946 4.764E-4 0.07354 0.1946 0.3427 5000 390002
p.boy[9,2] 0.2887 0.05009 3.398E-4 0.1992 0.2852 0.3979 5000 390002
p.boy[9,3] 0.5134 0.06758 5.224E-4 0.3808 0.5134 0.6458 5000 390002
p.boy.fix[1] 0.4956 0.5 7.733E-4 0.0 0.0 1.0 5000 390002
p.boy.fix[2] 0.4957 0.5 7.765E-4 0.0 0.0 1.0 5000 390002
p.boy.fix[3] 0.4972 0.5 7.998E-4 0.0 0.0 1.0 5000 390002
p.boy.fix[4] 0.497 0.5 7.936E-4 0.0 0.0 1.0 5000 390002
p.boy.fix[5] 0.4981 0.5 8.041E-4 0.0 0.0 1.0 5000 390002
p.boy.fix[6] 0.4947 0.5 8.092E-4 0.0 0.0 1.0 5000 390002
p.boy.fix[7] 0.496 0.5 7.89E-4 0.0 0.0 1.0 5000 390002
p.boy.fix[8] 0.496 0.5 8.396E-4 0.0 0.0 1.0 5000 390002
p.boy.fix[9] 0.4952 0.5 8.02E-4 0.0 0.0 1.0 5000 390002
p.girl[1,1] 0.1884 0.06963 7.699E-4 0.08556 0.1774 0.3528 5000 390002
p.girl[1,2] 0.4278 0.08772 8.996E-4 0.2833 0.419 0.6196 5000 390002
p.girl[1,3] 0.3838 0.09995 0.001317 0.1879 0.3859 0.5661 5000 390002
p.girl[2,1] 0.0894 0.02974 3.616E-4 0.03916 0.08695 0.154 5000 390002
p.girl[2,2] 0.2889 0.04609 3.431E-4 0.1987 0.289 0.379 5000 390002
p.girl[2,3] 0.6217 0.05505 5.204E-4 0.5154 0.6209 0.7301 5000 390002
p.girl[3,1] 0.09568 0.02869 3.402E-4 0.04663 0.09373 0.1576 5000 390002
p.girl[3,2] 0.3147 0.04216 2.676E-4 0.234 0.3141 0.4005 5000 390002
p.girl[3,3] 0.5896 0.04935 4.073E-4 0.4922 0.5896 0.6855 5000 390002
p.girl[4,1] 0.1718 0.05091 5.989E-4 0.09217 0.1654 0.2882 5000 390002
p.girl[4,2] 0.3904 0.06147 5.735E-4 0.2813 0.3866 0.52 5000 390002
p.girl[4,3] 0.4379 0.07153 8.679E-4 0.2958 0.4391 0.5722 5000 390002
p.girl[5,1] 0.1238 0.04173 3.943E-4 0.05731 0.1183 0.2215 5000 390002
p.girl[5,2] 0.3138 0.05826 3.901E-4 0.1978 0.3139 0.4314 5000 390002
p.girl[5,3] 0.5625 0.07172 5.727E-4 0.4175 0.5632 0.7029 5000 390002
p.girl[6,1] 0.1514 0.04518 5.236E-4 0.07934 0.1456 0.2546 5000 390002
p.girl[6,2] 0.3375 0.0522 3.559E-4 0.2379 0.3362 0.4451 5000 390002
p.girl[6,3] 0.5111 0.06362 5.959E-4 0.3826 0.5126 0.6327 5000 390002
p.girl[7,1] 0.09323 0.03012 3.617E-4 0.04308 0.09025 0.1599 5000 390002
p.girl[7,2] 0.249 0.05003 5.046E-4 0.1523 0.2486 0.3454 5000 390002
p.girl[7,3] 0.6578 0.05829 6.612E-4 0.544 0.6582 0.7699 5000 390002
p.girl[8,1] 0.1222 0.03414 3.599E-4 0.06507 0.1186 0.199 5000 390002
p.girl[8,2] 0.3422 0.04754 3.148E-4 0.254 0.34 0.4425 5000 390002
p.girl[8,3] 0.5356 0.05555 4.447E-4 0.4232 0.5371 0.6409 5000 390002
p.girl[9,1] 0.09069 0.03121 3.359E-4 0.03669 0.08865 0.1575 5000 390002
p.girl[9,2] 0.3275 0.0513 3.653E-4 0.2329 0.325 0.4377 5000 390002
p.girl[9,3] 0.5818 0.05921 5.093E-4 0.462 0.5824 0.6959 5000 390002
s[1] 0.6625 0.3286 0.004298 0.1014 0.6215 1.429 5000 390002
s[2] 0.478 0.2447 0.002787 0.09566 0.4449 1.054 5000 390002
sig.boy 0.3674 0.2939 0.004177 0.0161 0.3048 1.092 5000 390002