I have been building some time series panel model in stan, similar in style to the one detailed here:

In short, I structured my Moving Average model in panel and then temporal order, and then iterated back the length of the panel to gather the residuals from the previous observations.

I now actually have multiple panels (or groups) over which I wish to gather residuals. Using the classic classroom example, I may already incorporate historic residuals from the student’s performance but now want to incorporate the residuals from the class performance as well. So simply ‘iterating back’ the length of the previous observations in the group won’t work, as I can’t order the data structure sequentially for both the student and class.

Instead I’ve tried providing the index of the previous observations for the class (and gather the residuals for the class in a for loop within the models), and kept the data structure in panel and then temporal order for each student. This has the unintended consequence that when viewing the data by class the observations are not in temporal order.

When running the model, I get ‘log probability evaluates to negative infinity’ errors, I suspect because stan can’t gather residuals from observations that appear after the observation currently being sampled in the data structure.

I suspect the solution is therefore to order the whole dataset temporally (and not temporally within panels) and then do the index-forloop approach for gathering every previous residual for each panel. This will require quite an overhaul of the current code however and would like to get confirmation that my current understanding and proposed solutions are valid before proceeding.

Is my understanding correct?