I am trying to rewrite a GLM model in Stan (based on a non-Bayesian model someone else in my company wrote in MATLAB many years ago). There are a set of multiple parameters that are all related – all of them are also 0-1 (but not necessarily exclusive).
In the MATLAB model, these parameters are subject to a constraint that sets their overall trend to be 0 (i.e. if one were to apply a linear regression to coefficients g1, g2, g3, g4, g5, (with x = 1, 2, 3, 4, 5), the slope would be 0). It does this by adding an extra row to the model matrix, with entries as 0 for every variable except for g1-g5. Those have entries c1-c5, set as \sum_{i=1}^{5}(c_i *g_i) = 0. They then wrote a new IRLS function that solves for the coefficients while keeping this constraint.
Is there any way to do something like this in STAN – either just set a linear constraint on a set of coefficients, or add a row of dummy predictors to the model matrix, and solve it all at the same time?
Sorry if the above was unclear, I can try to explain further if necessary.
Thanks for responding so clearly. This answer seems to have worked!
One follow-up (for now):
In the above example, there are 5 coefficients. If I want to change the function so that there are K coefficients (i.e. K as input data), is there a way I can rewrite the part that you put in the transformed data block to be dynamic?
I tried
int z0 = (K-1)/2;
row_vector[K-1] z = [(-z0):(z0-1)];
but that did not work – I received the error:
error in 'model1e4c3bd969f0_loglinear_constr_w_prior' at line 14, column 22
-------------------------------------------------
12: transformed data {
13: int z0 = (K-1)/2;
14: row_vector[K-1] z = [(-z0):(z0-1)];
^
-------------------------------------------------
PARSER EXPECTED: <expression>
Do you know if there is a way to do it dynamically? If not, I can think of a way, where I just create the vector in R, and then bring it in within data, but it would be more elegant to do it in Stan directly.
PARSER EXPECTED: <expression>
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model 'loglinear_constr_w_prior' due to the above error.
Yup. No more informative info there. Often there is a Stan compiler message after that, which is why I asked. (On the forums, it’s good just to paste whole output for that reason; hard to tell what’s being censored and that often has important info. Here, not so much.)
Someone will need to dig in order to figure this one out.
@AJF, just looked at the thread again. Can’t really help without the model. It’s a syntax error, so gotta have something that we can run to help debug. If you post the full program, I’ll take a look.
what the parser is saying is that that what you’ve got on the right-hand side isn’t an expression - what you’re trying to do is use Stan’s slice indexing operator :, but that’s not allowed here.
cf. Stan language manual, section 4.3 “Vector, Matrix, and Array Expressions” :
Vector Expressions
Square brackets may be wrapped around a sequence of comma separated primitive expressions to produce a row vector expression.
It should have also given you a line number and pointer to the exact location in the source file where it was expecting to find an expression but instead found something else.