Hi all, I’m having a persistent error when trying to specify this model, and the line-numbers aren’t very helpful. Would anyone be willing to point me towards a solution?
Here’s my code:
data {
int<lower=1> N; // number of tableaux -- that is to say, number of datapoints
int<lower=0> max_num_candidates;
array[N] int<lower=2, upper=max_num_candidates> J; // vector of num_candidate in each tableau
int<lower=1> K; // number of constraints; this is constant across tableaux
array[K] int<lower=0, upper=1> is_constraint_hierarhical;
int<lower=1> L; // number of levels of the random effect
array[N] int<lower=1, upper=L> ll; // an array telling you which level of the random effect a given UR belongs to.
array[N] int<lower=1,upper=max_num_candidates> Y; // index of winning candidate in each tableau
array[N] matrix[max_num_candidates, K] violations; // violations
} parameters {
array[K] real mu; // the vector of constraint weights, coefficients - group level
array[K] real<lower=0> sigma; // the variance on these guys
array[L] vector[K] beta; // a vector of random-effect-level coefficients that are drawn from real_mu, one for each leve lof the random effect. these are the random-effect-level offsets // means drawn from the group-level real mu
//matrix[L, K] beta;
}
model {
for (constraint_index in 1:K) { // for each constraint -
mu[constraint_index] ~ normal(0, 100); // draw its mean from a hyperprior distribution
if (is_constraint_hierarhical[constraint_index] == 1) { // then do the thing where you draw a group-level random offset and put it in the relevant slot in the item-by-constraint coeff matrix
for (l in 1:L) { // then for each level of the random effect
beta[l, constraint_index] ~ normal(mu[constraint_index], sigma[constraint_index]); // draw the value of the level-specific offset for that constraint from a distribution centered on for each offset from the group-level for that constraint
}
}
else { // otherwise just skip one level of hierarchy and put the mu - the group-level - in.
for (l in 1:L) { // then for each level of the random effect
beta[l, constraint_index] = mu[constraint_index]; // just slot in the group-level mu.
}
}
}
//print(size(beta));
for (i in 1:N) { // then for each datapoint
//relevant_intercept_index = ll[n];
Y[i] ~ categorical_logit(block(violations[i], 1, 1, J[i], K)* beta[ll[i]]); // take the datapoint n, get the random effect group it belongs to - ll - then index into the matrix of betas to find that row, and multiply it by the constraint violation matrix - this is what needs to be blocked on my end - for that one.
}
}
generated quantities {
array[N] int sim_winners;
for (i in 1:N){
sim_winners[i] = categorical_logit_rng(block(violations[i], 1, 1, J[i], K)*beta[ll[i]]);
}
}
And I’m getting the error:
Semantic error in 'string', line 27, column 8 to column 58:
Cannot assign to global variable 'beta' declared in previous blocks.
The model compiles and fits fine when K is non-hierarchically-structured:
data {
int<lower=1> N; // number of tableaux -- that is to say, number of datapoints
int<lower=0> max_num_candidates;
array[N] int<lower=2, upper=max_num_candidates> J; // vector of num_candidate in each tableau
int<lower=1> K; // number of constraints; this is constant across tableaux
array[N] int<lower=1,upper=max_num_candidates> Y; // index of winning candidate in each tableau
array[N] matrix[max_num_candidates, K] violations; // violations
}
parameters {
vector<lower=0>[K] beta; // attribute effects // constraint weights; should be just one
}
model {
beta ~ normal(0,1);
for (i in 1:N) // look up "block" - just looks at a specific part of a matrix
//local_violations = ; // this is supposed to get the i'th tableau's candidates and violations from the stack violations (which is num_tableaux * max_num_candidates (padded out) * num_constraints in size), and pull out only the first J[i] rows, which are just those that are nonzero and not padded out so relevant here, and then all columns (the K constraints, which are constant for all tableaux)
Y[i] ~ categorical_logit(block(violations[i], 1, 1, J[i], K)*beta); // so this gets just one instance, one token, at a time. could we ramp up to do multinomial and not categorical?
}
generated quantities {
array[N] int sim_winners;
for (i in 1:N)
sim_winners[i] = categorical_logit_rng(block(violations[i], 1, 1, J[i], K)*beta);
}
So I assume something odd is going on in the way I tried to enrich it with hierarchical structure.
Thanks very much, I appreciate any help I can get in advance!
EDIT: I realize the error I originally had was to do with an extra parenthesis (deep sigh…), but after that I solved a few more random issues, and am still stuck on the one above; I think it has to do with mixing ~
and =
when populating the beta
matrix, but I don’t really know another way around it. any suggestions would be very welcome!
EDIT: I found I could get rid of the error by replacing the beta[l, constraint_index] = mu[constraint_index]
with something hacky like beta[l, constraint_index] ~ normal(mu[constraint_index], 0.00000001)
but that doesn’t really feel like a good solution; so I’d still really appreciate any help with this folks might have!