Error in parsing model: base type mismatch in assignment

Hi,

I am new to Stan and am trying to construct a hierarchical model as per the following requirements:

response Y ~ neg_binomial_2( mu , phi )

where, mu = a + b*log(X)

a and b are drawn from the hyper-distribution that has a mean defined by a linear relationship with third variable (see model file, covariate U). However, I am getting the following error when I run the model:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:

base type mismatch in assignment; variable name = u_gamma_t, type = real; right-hand side type=row vector
  error in 'model21844a1e2888_c1d0a5d3f064fe151ed16c4649a622e1' at line 41, column 15
  -------------------------------------------------
    39: a_trap ~ normal(0,sigma_trap);
    40: for (j in 1:J)
    41: u_gamma_t[j] = (U[j]*gamma);
                      ^
    42: beta ~ multi_normal(u_gamma_t, Sigma_beta);
  -------------------------------------------------

PARSER EXPECTED: <expression assignable to left-hand side>
Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'c1d0a5d3f064fe151ed16c4649a622e1' due to the above error.

There might be something very basic in the coding that I am missing. I’ve pasted my code below. Any help will be much appreciated!

Thank you for your time.

sts.ndd = "
data{
int<lower=1> N; // rows of observations
int<lower=0> Y[N]; // observed response
int<lower=1> K; //no.of predictors
int<lower=1> J; //no.of species
int<lower=1> L; // num group predictors
matrix[N,K] X; // individual predictors/seed numbers
matrix[J,L] U; // group predictors
int<lower=1, upper=J> jj[N]; // group for species	
int<lower=1> y; 						//number of  years
int<lower=1,upper=y> yy[N]; // group for year
int<lower=1> T; 						//number of  traps
int<lower=1,upper=T> tt[N]; // group for trap
}

parameters{
corr_matrix[K] Omega; //  correlation matrix
vector<lower=0>[K] tau; //  scale
vector[K] beta[J]; // indiv coeffs per species
matrix[L,K] gamma; // group coeffs 
vector[y] a_year;  					// year intercepts
real<lower=0> sigma_year;   // year sd
vector[T] a_trap;  					// trap intercepts
real<lower=0> sigma_trap;   // trap sd
}

model{
vector[N] x_beta_jj; // mean expectation
vector[K] u_gamma_t; //
matrix[K,K] Sigma_beta;
Sigma_beta = quad_form_diag(Omega,tau);
tau ~ cauchy(0,5);
Omega ~ lkj_corr(2.0); // prior covariance
to_vector(gamma) ~ normal(0,1);

// random effect priors
a_year ~ normal(0, sigma_year);
a_trap ~ normal(0,sigma_trap);
for (j in 1:J)
u_gamma_t[j] = (U[j]*gamma);
beta ~ multi_normal(u_gamma_t, Sigma_beta);

phi ~ gamma(0.01, 0.01); 

// Likelihood
for (n in 1:N)
x_beta_jj[n] = (X[n] * beta[jj[n]] + a_year[yy[n]] + a_trap[tt[n]]); //log location
sdl ~ neg_binomial_2_log(x_beta_jj, phi);
}

"

Best,
Meghna

I think you meant to declare vector[K] u_gamma_t[J]; in your model block.

Hi Ben,

Thanks for your response. I had initially declared it as you mentioned, but
was getting the same error. Which is why I had changed to the current
declaration.

Might there be an issue with how the data is provided? U is currently a
on-column matrix. Should I provide it as a column vector instead?

Thanks,
Meghna

Yes

Thanks Ben.

It appears that declaring row_vector[K] u_gamma_t[J]; works. The model did compile but I got the following runtime error:

   Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  variable does not exist; processing stage=data initialization; variable name=yy; base type=int
In addition: Warning messages:
1: In FUN(X[[i]], ...) : data with name yy is not numeric and not used
2: In FUN(X[[i]], ...) : data with name tt is not numeric and not used
trying deprecated constructor; please alert package maintainer
Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  no valid constructor available for the argument list
failed to create the sampler; sampling not done

So, as I understand, the grouping variables (in this case yy and tt) need to given as integers to Stan, not as factors. Is that right?

I don’t understand the error about deprecated constructor. I formulated my model based on instructions in the latest vignette. Apologies if I have missed something here.

Yes

Don’t worry about that.

Providing yy and tt as integers appears to be working.

Thank you very much for your help Ben! Appreciate the quick feedback.

Dear stan community. I am having a similar issue and have tried the solutions given here to no avail. I have therefore posted it as a separate question at SYNTAX ERROR, MESSAGE(S) FROM PARSER: Variable definition base type mismatch, variable declared as base type: vector variable definition has base.

Thank you in advance for any help.