|
1 | | -# Bones: latent trait model for multiple ordered |
2 | | -# categorical responses |
3 | | -## http://www.openbugs.info/Examples/Bones.html |
4 | | - |
5 | | - |
6 | | -## Note: |
7 | | -## 1. Since it is just the response that is |
8 | | -## modelled as categorical distribution, |
9 | | -## we should be able to run the model now except handling |
10 | | -## the missing. However, the data structure is a bit |
11 | | -## difficult to deal with (Allowing some redundancy |
12 | | -## in the transformed parameters (Q here), the model |
13 | | -## is fine in Stan. |
14 | | -## 2. The missing data is recoded as `-1`, which is |
15 | | -## not modeled for `gamma` as in the OpenBUGS example |
16 | | -## and not modeled for `grade`. |
17 | | - |
| 1 | +/** |
| 2 | + * Bones: latent trait model for multiple ordered |
| 3 | + * categorical responses |
| 4 | + * http://www.openbugs.info/Examples/Bones.html |
| 5 | + * |
| 6 | + * |
| 7 | + * Note: |
| 8 | + * 1. Since it is just the response that is |
| 9 | + * modelled as categorical distribution, |
| 10 | + * we should be able to run the model now except handling |
| 11 | + * the missing. However, the data structure is a bit |
| 12 | + * difficult to deal with (Allowing some redundancy |
| 13 | + * in the transformed parameters (Q here), the model |
| 14 | + * is fine in Stan. |
| 15 | + * 2. The missing data is recoded as `-1`, which is |
| 16 | + * not modeled for `gamma` as in the OpenBUGS example |
| 17 | + * and not modeled for `grade`. |
| 18 | + */ |
18 | 19 |
|
19 | 20 | data { |
20 | 21 | int<lower=0> nChild; |
21 | 22 | int<lower=0> nInd; |
22 | | - real gamma[nInd, 4]; // -1 indicates NA in original R dump data (bones.data.R.0) |
| 23 | + real gamma[nInd, 4]; // -1 if missing |
23 | 24 | real delta[nInd]; |
24 | 25 | int<lower=0> ncat[nInd]; |
25 | | - int grade[nChild, nInd]; // -1 indicates NA in original R dump data (bones.data.R.0) |
| 26 | + int grade[nChild, nInd]; // -1 if missing |
26 | 27 | } |
27 | | - |
28 | | - |
29 | 28 | parameters { |
30 | 29 | real theta[nChild]; |
31 | 30 | } |
32 | | - |
33 | 31 | model { |
34 | 32 | real p[nChild, nInd, 5]; |
35 | 33 | real Q[nChild, nInd, 4]; |
36 | 34 | theta ~ normal(0.0, 36); |
37 | | - for(i in 1:nChild) { |
38 | | - # Probability of observing grade k given theta |
| 35 | + for (i in 1:nChild) { |
| 36 | + // Probability of observing grade k given theta |
39 | 37 | for (j in 1:nInd) { |
40 | | - # Cumulative probability of > grade k given theta |
41 | | - for (k in 1:(ncat[j] - 1)) { |
| 38 | + // Cumulative probability of > grade k given theta |
| 39 | + for (k in 1:(ncat[j] - 1)) |
42 | 40 | Q[i, j, k] <- inv_logit(delta[j] * (theta[i] - gamma[j, k])); |
43 | | - } |
44 | | - |
45 | 41 | p[i, j, 1] <- 1 - Q[i, j, 1]; |
46 | 42 | for (k in 2:(ncat[j] - 1)) |
47 | 43 | p[i, j, k] <- Q[i, j, k - 1] - Q[i, j, k]; |
48 | 44 | p[i, j, ncat[j]] <- Q[i, j, ncat[j] - 1]; |
49 | | - // grade[i, j] - 1 ~ categorical(p[i, j, 1:ncat[j]]) |
50 | | - |
51 | | - // We use lp__ instead, since grade[i, j] has categorical distribution |
52 | | - // with varying dimension. |
53 | | - // If grade[i, j] = -1, it is missing, no contribution for lp__ then. |
54 | | - if (grade[i, j] != -1) { |
55 | | - lp__ <- lp__ + log(p[i, j, grade[i, j]]); |
56 | | - } |
| 45 | + |
| 46 | + // incement log probability directly because grade[i, j] |
| 47 | + // has categorical distribution with varying dimension. |
| 48 | + // for missing grade[i, j] = -1, there is no log prob |
| 49 | + // contribution |
| 50 | + if (grade[i, j] != -1) |
| 51 | + increment_log_prob(log(p[i, j, grade[i, j]])); |
57 | 52 | } |
58 | 53 | } |
59 | 54 | } |
|
0 commit comments