I have a model with a coefficient (theta
) that I want to vary by two other categorical variables, group
and time
. Suppose there are G=20
groups and T=10
time points, then I can declare the theta
parameter as
matrix[T, G] theta ;
The group
and time
indices are passed in as data, as integer vectors of length N
(number of observations). In the model step, the easy way to index the particular theta
element that corresponds to each observation would be with a for
loop in the model
step, e.g.
real theta_use[N] ;
for(n in 1:N){
theta_use[n] = theta[time[n], group[n]];
}
It seems this could be done more efficiently with matrix algebra, but I can’t figure out the best way. Any suggestions? I was thinking that I could pass in (from R) two “map” matrices of dummy variables, time_map
and group_map
, that are N\times T and N\times G respectively (no intercept). I’m just not sure what to do with these in Stan.
FWIW, this is just a small part of a much larger program, but I eliminated the rest of the details as they are not necessary for understanding this issue.
Thanks,
Jon