Rhat calculation fails in for sentence?

When I use stan() in for sentences then sometimes, R hat and n_eff and se_mean does not be calculated even if such a chain is not constant. The following code use 6 fitted models and in the third model, R hat does not calculated. If the code does not reproduce this, then please chainge 6 or 3. It sometimes occurs and sometimes does not occur.

My original code is too bit to show, so, I made some simple example. (I guess, it still includes redundant stuff.) The code is the following. In this code, 6 fittted model objects (x[[i]], i= 1,..., 6) are created. In the third fitted model object, x[[3]], some R hat is not calculated as follows . In this example, a chains of such a prametere looks non-constant.

          mean se_mean   sd    2.5%     25%     50%     75%   97.5% n_eff Rhat

A[11]     0.71    0.02 0.16    0.39    0.64    0.72    0.82    0.98    65 0.99
A[12]     0.72     NaN 0.17    0.38    0.60    0.72    0.84    1.01   NaN  NaN
A[13]     0.70    0.02 0.17    0.27    0.61    0.72    0.84    0.94    98 1.01
A[14]     0.71    0.01 0.12    0.42    0.65    0.73    0.79    0.89    98 1.00
A[15]     0.71     NaN 0.18    0.40    0.58    0.74    0.83    0.94   NaN  NaN
A[16]     0.70    0.02 0.16    0.34    0.62    0.73    0.80    0.92    97 0.99
A[17]     0.71     NaN 0.20    0.27    0.58    0.71    0.86    1.08   NaN  NaN
A[18]     0.71     NaN 0.15    0.40    0.61    0.73    0.82    0.95   NaN  NaN
A[19]     0.73    0.01 0.12    0.51    0.64    0.74    0.82    0.93    98 1.00
A[20]     0.71    0.01 0.13    0.47    0.61    0.70    0.79    0.93    98 0.98
A[21]     0.72    0.01 0.13    0.46    0.65    0.73    0.81    0.91    98 1.00
A[22]     0.69    0.01 0.14    0.38    0.61    0.71    0.77    0.94    98 1.00
A[23]     0.70    0.01 0.13    0.45    0.62    0.70    0.79    0.94    98 0.98
A[24]     0.72    0.02 0.14    0.38    0.66    0.71    0.78    0.95    52 1.03
A[25]     0.69    0.01 0.14    0.37    0.63    0.69    0.77    0.95    98 0.99
A[26]     0.67    0.02 0.19    0.19    0.55    0.71    0.79    0.95    94 1.00
A[27]     0.71    0.02 0.17    0.39    0.60    0.74    0.84    0.99    91 0.99
A[28]     0.70    0.02 0.17    0.34    0.59    0.71    0.84    0.93    98 0.99
A[29]     0.67    0.01 0.14    0.34    0.58    0.69    0.78    0.91    98 0.98
A[30]     0.71     NaN 0.19    0.34    0.60    0.72    0.84    0.98   NaN  NaN
lp__   -911.38    0.89 3.43 -918.23 -913.60 -911.20 -908.68 -905.89    15 1.12

But, when I show the trace plot of one of such a parameter, e.g., A[30], trace plot looks good. I am worried if the example does not replicate such a phenomenon.

Please Copy and paste the following R-code.




x <- list()


for (k in 1:6) {
  
  stanmodelcode <- "
data {
  int<lower=0> N;
  real y[N];
} 

parameters {
  real mu[30];
} 

model {
for(i in 1:30){
  target += normal_lpdf(mu[i] | 0, 10);
  target += normal_lpdf(y  | mu[i], 1);
  }
}


generated quantities{

real A[30];
for(i in 1:30){
 A[i] = sqrt(mu[i]+1 );
  }

}

"
  
  y <- rnorm(20) 
  dat <- list(N = 20, y = y); 
  
  
  x[[k]] <-  stan(model_code = stanmodelcode, model_name = "example", 
                        data = dat, iter = 111, chains = 1, verbose = F ) 
  
}

x[[3]]

rstan::traceplot(x[[3]], pars=c("A[30]"))

Based on the example it looks like the sampler isn’t sufficiently exploring the parameter space. If you run the model with the reccomended four chains, 2000 iterations, and 1000 warmup does it still exhibit these issues?

1 Like

Thank you for your reply.
I didn’t reproduce the problem by the above code.
In the above code, I mistake the square root A[i] = sqrt(mu[i]+1 ) of some parameter mu[i] which is sometimes nagative.
For negative numbers, Stan does not calculate its square root in MCMC sampling and it causes NaN in Rhat and etc.
In the following figure, I found that MCMC skipped the calculation.

If I replace A[i] = sqrt(mu[i]+1 ) by A[i] = sqrt(mu[i]^2+1 ), then NaN does not occur.
I cannot reproduce my original problem by simple codes.

[image]

Thank you