Hi all,
I am working on a hierarchical random coefficient(random slope) model using Stan. Since I am new to Stan, I take baby steps by adding levels to the model step by step.For the base model, I assign random coefficient beta a distribution, and it works fine. However, when I try to add individual level predictors to model beta, I got error.
Here is my base model code which works fine, although quite slow(several hours)
data {
int<lower=0> N; // total observation
int<lower=0> I; // subjects
int<lower=0> sellerid[N]; // ID variable
int<lower=0> y[N]; // number of return customer for seller i
real<lower=0,upper=1> x1[N];
real x2[N];
}
parameters {
vector[3] beta[I]; //I*3 matrix
real mu_beta[3]; //means for beta's normal distribution
real<lower=0,upper=100> sigma_b[3];
real<lower=0, upper=100> sigma;
real<lower=0> phi; // neg. binomial dispersion parameter
real<lower=0> mu_y[I]; // mean Y for each individual
}
model {
//prior
phi ~ cauchy(0, 20);
for (k in 1:3){
mu_beta[k] ~ normal(0, 10);
sigma_b[k] ~ uniform(0,100);
for (i in 1:I)
beta[i,k] ~ normal(mu_beta[k], sigma_b[k]);
}
sigma ~ uniform(0,100);
//likelihood
for(n in 1:N){
y[n] ~ neg_binomial_2_log(mu_y[sellerid[n]], phi);
}
for(i in 1:I){
mu_y[i]~ normal( beta[sellerid[i],1] + beta[sellerid[i],2] * x1[i] + beta[sellerid[i],3]* x2[i],sigma);
}
}
Then I want to add individual level attributes to model random coefficient beta, I got warning message:
Warning (non-fatal):
Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
If so, you need to call increment_log_prob() with the log absolute determinant of the Jacobian of the transform.
Left-hand-side of sampling statement:
to_vector(sigma_b) ~ cauchy(...)
beta[k] ~ normal(...)
mu_y[i] ~ normal(...)error message
and also error messgaes:
[1] "Error : "
[2] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"
[3] "validate transformed params: mu_y[k0__] is 1.#QNAN, but must be greater than or equal to 0"
[4] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"
[5] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."
[6] "Rejecting initial value:"
[7] " Error evaluating the log probability at the initial value."
Here is my stan code which doesn't work:
data {
int<lower=0> N; // total observation
int<lower=0> I; // subjects
int<lower=0> sellerid[N]; // ID variable
int<lower=0> y[N]; // number of return customer for seller i
vector[3] Z[I]; //an i by 3 matrix of coefficient predictors
real x1[N];
real x2[N];
int<lower=1> nvar; // Number of beta coefficients nvar=3
int<lower=1> nz; // Number of z coefficients for beta nz=3
}
parameters {
matrix[3,3] delta;
real<lower=0, upper=100> sigma;
real<lower=0> phi; // neg. binomial dispersion parameter
}
transformed parameters {
vector[3] beta[I]; //I*3 matrix
vector[N] y_hat;
real mu_beta[3];
real sigma_b[3];
real<lower=0> mu_y[I]; // mean Y for each individual
for (n in 1:N){
y_hat[n] <- beta[sellerid[n],1] + beta[sellerid[n],2] * x1[n] + beta[sellerid[n],3]* x2[n];
}
for (i in 1:I){
for (k in 1:3){
mu_beta[k] <- Z[i,1]*delta[1,k] + Z[i,2]*delta[2,k] + Z[i,3]*delta[3,k];
}
}
}
model {
to_vector(sigma_b) ~ cauchy(0,5);
for (k in 1:3){
beta[k] ~ normal(mu_beta[k], sigma_b[k]);
}
//prior
phi ~ cauchy(0, 5);
sigma ~ normal(0,100);
to_vector(delta) ~ cauchy(0,5);
//likelihood
for(n in 1:N){
y[n] ~ neg_binomial_2_log(mu_y[sellerid[n]], phi);
}
for(i in 1:I){
mu_y[i]~ normal( y_hat[i],sigma);
}
}
I appreciate any advices on fixing the model. Also, how should I code it in a way that will make Stan run faster?
Thanks!!