Hello all,
Apologies if this is just a stupid error on my part, but I was wondering if I could get some more eyes to help me figure out why my generated quantities block is breaking the sampler. I am trying to fit a random intercept/slope logistic regression with covariance matrix for grouped bernoulli trial data. I *think* the model itself is working ok, but the minute I added a generated quantities block to calculate the
log_lik for subsequent use with 'loo', the sampler stopped working (the exact same model file runs fine and returns reasonable looking results if I comment out the GQ block). With the GQ block included, the model still compiles fine, but immediately throws an error and returns no samples. At the same time, nearly the same GQ block (just without the [grp[]] indexing) works fine for a reduced model with no covariance matrix. Can anybody help me identify what I'm doing here that would give the sampler such a hard time and how to fix? I've copied the model and error message below.
Thanks in advance for any help or advice.
- C
THE MODEL:
data {
int<lower=1> N; // Number of observations
int<lower=0> P; // Number of predictors
int<lower=0> J; // Number of groups (Runs)
int<lower=0> K; // Number of group-level predictors
int<lower=1,upper=J> grp[N]; // Group index for each observation
int<lower=0> nT[N]; // Number of trials
int<lower=0> nS[N]; // Number of successes
matrix [N,P] X; // Predictor matrix
row_vector[K] Z[N]; // Group predictor matrix (row_vector for efficiency)
}
parameters {
cholesky_factor_corr[K] L_run; // Cholesky factor of Run r.e. correlations
vector<lower=0>[K] tau_run; // standard deviations of unconditional Run r.e. dist
vector[K] u[J]; // spherical Run random effects
vector [P] beta; // Vector of fixed effect estimates
real<lower=0> sigma_y; // error scale
}
transformed parameters {
matrix[K,K] corrs;
corrs <- tcrossprod(L_run); // for monitoring Run correlations
}
model {
// Local variables
vector[N] mu;
matrix[K,K] Lambda_run;
vector[K] gamma[J];
// Priors for covariance matrix
tau_run ~ cauchy(0,5);
L_run ~ lkj_corr_cholesky(2);
Lambda_run <- diag_pre_multiply(tau_run,L_run);
for (i in 1:J) {
u[i] ~ normal(0,1);
gamma[i] <- Lambda_run * u[i];
}
// Priors
beta ~ normal(0,3);
sigma_y ~ cauchy(0,5);
// Likelihood
for (i in 1:N) {
mu[i] <- X[i] * beta + Z[i] * gamma[grp[i]];
}
nS ~ binomial_logit(nT, mu);
}
generated quantities {
vector[N] log_lik;
vector[K] gamma[J];
for (i in 1:N) {
log_lik[i] <- binomial_logit_log(nS[i], nT[i], X[i]*beta + Z[i]*gamma[grp[i]]);
}
}
THE ERROR MESSAGE:
> ## Call to STAN
> mat4 <- stan(data = data.list,
file = './Stan/mat-logistic-1Z-cov.stan',
chains = 1,
iter = numSavedSteps,
thin = thinSteps,
save_dso = TRUE
)
COMPILING THE C++ CODE FOR MODEL 'mat-logistic-1Z-cov' NOW.
starting worker pid=28067 on localhost:11870 at 15:11:51.074
starting worker pid=28076 on localhost:11870 at 15:11:51.213
starting worker pid=28085 on localhost:11870 at 15:11:51.354
starting worker pid=28094 on localhost:11870 at 15:11:51.493
SAMPLING FOR MODEL 'mat-logistic-1Z-cov' NOW (CHAIN 1).
Error : Exception thrown at line 64:
stan::math::binomial_logit_log: Probability parameter is nan, but must be finite!
error occurred during calling the sampler; sampling not done
here are whatever error messages were returned
[[1]]
Stan model 'mat-logistic-1Z-cov' does not contain samples.
Warning message:In .local(object, ...) : some chains had errors; consider specifying chains = 1 to debug
>
I also tried providing intial values for the sampler. Again, this worked fine with the GQ commented out, but gave the same error if I included the GQ.
inits <- list(
list(L_run = matrix(runif(16^2,-0.5,0.5), nrow=16,ncol=16)),
list(tau_run = runif(16,0.1,2)),
list(u = matrix(runif((8*16),-1,1),nrow=8,ncol=16)),
list(beta = runif(2,-1,1)),
list(sigma_y = runif(1,0.1,2))
)