I'm implementing the ordered logistic model in the Stan reference manual, and I want to check whether the model is working well by comparing the observed values and the model prediction. The model prediction is nothing like the observed values, I don't know if I have programmed it wrong or if I misunderstand how to check the model fits from an ordered logistic model.
The model code is essentially copied from the manual, I only added the generated quantities part. The whole model file is attached. And here is the generated quantities part I added:
generated quantities {
real<lower=0> y_p[N]; // prediction
{
matrix[N, K] pred; // prediction each case and each class
for (n in 1:N) { // cases
for (k in 1:K) { // classes
pred[n, k] = exp(ordered_logistic_lpmf(k | x[n]*beta, c)) * k;
}
y_p[n] = sum(pred[n]);
}
}
}
Basically what I have done is:
- get the log pmf for each class, exponentialize it;
- multiply it with the observed class value;a
- and sum all the classes for the expected prediction
But the prediction is nothing like the observed values. I have 7 different classes, and here are the observed values:
> table(d$response)
1 2 3 4 5 6 7
1274 909 1071 2323 1462 1445 1446
And the predicted values are all in a much smaller range:
> range((m113_sample$y_p))
[1] 2.673227 4.953754
What am I doing wrong here?