How to apply arbitrary constraints on functions of parameters as bounds of uniform priors in a stan model

Dear stan community. I am trying to specify noninformative uniform priors in my stan model whose bounds are based on arbitrary constraints on functions of parameters.

I need help to address some issues in my approach including the error discussed here Error in parsing model: base type mismatch in assignment because the solutions given there are not working/applicable for my case.

I have specified uniform priors with proper bounds because I think that makes more sense given the model constraints. Thanks, @bgoodri for raising that point and for all other very helpful insights. So far I have not parameterized alpha_0 into exp_alpha_0 as discussed there because I need exp(alpha_0) to be able to generalize the model.

#error

SYNTAX ERROR, MESSAGE(S) FROM PARSER:

variable definition base type mismatch, variable declared as base type: vector variable definition has base: real error in ‘model5150539c2a83_m’ at line 27, column 31

25: model {
26: // priors
27: vector[N] right = exp(alpha_0);
                                  ^
28: real rb = max(inv(right));

Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model ‘m’ due to the above error.
In addition: Warning message:
In readLines(file, warn = TRUE) :
incomplete final line found on ‘~\stan\m.stan’

#line 27

vector[N] right = exp(alpha_0);

#model

data{
int<lower=0> N;  
int<lower=0> ncases[N];                         
int<lower=0> A[N];                               
int<lower=0> B[N];                                 
int<lower=0> nn[N];                               
 }

parameters {
real<upper = 0> alpha_0;
real<lower = -1, upper = inv(exp(alpha_0))> alpha_1;
real<lower = -1, upper = inv(exp(alpha_0))> alpha_2;
real<lower = -1 - alpha_1 - alpha_2, upper = inv(exp(alpha_0))> alpha_3;
  }

transformed parameters {
vector[N] pp_hat;  
for (i in 1:N) {  
pp_hat[i] = exp(alpha_0) * (1 + alpha_1*A[i] + alpha_2*B[i] + alpha_3*A[i]*B[i]);
}
}

model {
vector[N] right = exp(alpha_0);
real rb = max(inv(right));
vector[N] left = -1 - alpha_1 - alpha_2;
real lb = min(left));
alpha_0 ~ uniform(-1e6, 0);
alpha_1 ~ uniform(-1, rb);
alpha_2 ~ uniform(-1, rb);
alpha_3 ~ uniform(lb, rb);
for (i in 1:N)
  ncases ~ binomial(nn, pp_hat);
  
}

Thanks in advance for any help.

the syntax error is telling you that you cannot assign a scalar real value to a vector. if you want all elements of the vector to contain the same value, you can use function rep_vector

you’ve got a few more errors in this program - same problem again on line 27, extra paren on line 28, undeclared variable ncases on line 34. those are the syntax errors. others may have more insight into the correctness of this approach.

1 Like

EDIT: Thanks, @mitzimorris. After implementing the recommended approach to assign a scalar real value to a vector and correcting the other issues you identified in my model, it works fine now.