Convergence Error: Rstan

Here’s my code. My Rhat values are NA for the Lu parameters. According to an other post, it seems to come form the fact that the sampled values are constant troughout the draws. Someone suggested to use the package posterior (https://github.com/stan-dev/posterior) to fix issue, however, I’m not sure how it is supposed to do so. Anyone else has a solution or can explain how I could use posterior. Thanks!

data {
  int<lower=1> N;            //number of observations
  real M[N];                //reaction times
  real X[N];   //predictor (days of sleep deprivation)

  // grouping factor
  int<lower=1> J;                   //number of subjects
  int<lower=1,upper=J> Subject[N];  //subject id
}

parameters {
  vector[2] beta;                   // fixed-effects parameters
  real<lower=0> sigma_e;            // residual std
  vector<lower=0>[2] sigma_u;       // random effects standard deviations

  //declare L_u to be the Choleski factor of a 2x2 correlation matrix
  cholesky_factor_corr[2] L_u;

  matrix[2,J] z_u;                  // random effect matrix
}

transformed parameters {
  // this transform random effects so that they have the correlation
  // matrix specified by the correlation matrix above
   matrix[2,J] u;
   u = diag_pre_multiply(sigma_u, L_u) * z_u;

}

model {
  real mu; // conditional mean of the dependent variable

  //priors
  L_u ~ lkj_corr_cholesky(2); // LKJ prior for the correlation matrix
  to_vector(z_u) ~ normal(0,5);
  sigma_e ~ normal(0, 5);       // prior for residual standard deviation
  beta[1] ~ normal(0, 0.5);   // prior for fixed-effect intercept
  beta[2] ~ normal(0, 2);     // prior for fixed-effect slope

  //likelihood
  for (i in 1:N){
    mu = beta[1] + u[1,Subject[i]] + beta[2]*X[i] + u[2,Subject[i]]*X[i];
    M[i] ~ normal(mu, sigma_e);
  }
}

This is my stan model. However, when I try to run it using my data, it does not work.

Warning messages:
1: The largest R-hat is NA, indicating chains have not mixed.
Running the chains for more iterations may help. See
http://mc-stan.org/misc/warnings.html#r-hat 
2: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
Running the chains for more iterations may help. See
http://mc-stan.org/misc/warnings.html#bulk-ess 
3: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
Running the chains for more iterations may help. See
http://mc-stan.org/misc/warnings.html#tail-ess 

Here the ‘data’ in R:

data=list(N = nrow(MyData),
          X = MyData$x,
          M = MyData$m,
          J = length(unique(MyData$id)),
          Subject = as.numeric(factor(MyData$id, 
          labels = 1:length(unique(MyData$id)))))

Here is the model in R:

fit = stan(file = 'mediationHM.stan', data=data, iter=4000, chains =4).

What am I doing wrong?

Sorry for taking so long to respond. Did you manage to resolve this?

Anyway the problem looks weird: I don’t see any good reason why the draws could be constant except for the sampler completely breaking down. Or is there more to the model? (I don’t see any generated quantities which often are the source of such issues)

When I look at the individual draws the values seem to have constant values. That is someone told me with the updated version of rstan I shouldn’t see the warnings anymore and apparently it’s true. And no, the problem does not stem from the generated quantities block as when I look at the summary of the draws, the values that are a problem are the L_u’s.

Oh, sorry, I missed the Cholesky of the correlation matrix. Since correlation matrix is constrained to have 1s on the diagonal, this means the Cholesky factor is also constrained and some constant values are completely expected there… So you don’t need to worry about this.

Sorry for the confusion.