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