# Model with many (correlated) varying-slopes: Sampling not done

**URL:** <https://discourse.mc-stan.org/t/model-with-many-correlated-varying-slopes-sampling-not-done/6189>\
**Category:** brms\
**Created:** [October 25, 2018, 11:32am UTC](https://discourse.mc-stan.org/t/model-with-many-correlated-varying-slopes-sampling-not-done/6189 "2018-10-25T11:32:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rongmu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/rongmu/32/3359_2.png) [@rongmu](https://discourse.mc-stan.org/u/rongmu)\
**Post date:** [October 25, 2018, 11:32am UTC](https://discourse.mc-stan.org/t/model-with-many-correlated-varying-slopes-sampling-not-done/6189/1 "2018-10-25T11:32:33Z")

</div>

I was trying to fit the below model with brms:

```
fit.1 <- brm(y ~ A * B * C * D + E + (A * B * C * D | g1) + (1 | g2),
           data = df, family = lognormal,
           cores = 1, chains = 1, iter = 300)

```

`A`, `B`, `C` and `D` are factors and `E` is a covariate. This is the dumped data set [data.R](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/4/47a5281790cd71ff0e149ea637437d9432d8a5ca.R) (410.3 KB).

During sampling, I got errors:

```
Rejecting initial value:
  Log probability evaluates to log(0), i.e. negative infinity.
  Stan can't start sampling from this initial value.

```

```
Iteration: 1 / 300 [0%] (Warmup)
Exception: model39d27bec92db_a2c0a7231ba0e427628b016846ca7749_namespace::write_array: y is not positive definite. (in 'model39d27bec92db_a2c0a7231ba0e427628b016846ca7749' at line 143)

```

After the sampling ended, I got an error saying that sampling was not done:

```
error occurred during calling the sampler; sampling not done

```

However, if I remove the correlation of varying intercepts and varying slopes, the model converges successfully:

```
fit.2 <- brm(y ~ A * B * C * D + E + (A * B * C * D || g1) + (1 | g2),
           data = df, family = lognormal,
           cores = 1, chains = 1, iter = 300)

```

Do you know what was wrong with the first model?

Thank you!

* * *

The full errors after sampling:

```
 [1] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"                                          
 [2] "Exception: lognormal_lpdf: Scale parameter is inf, but must be finite! (in 'model39d27bec92db_a2c0a7231ba0e427628b016846ca7749' at line 137)"           
 [3] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"                  
 [4] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."                                                
 [5] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"                                          
 [6] "Exception: lognormal_lpdf: Scale parameter is inf, but must be finite! (in 'model39d27bec92db_a2c0a7231ba0e427628b016846ca7749' at line 137)"           
 [7] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"                  
 [8] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."                                                
 [9] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"                                          
[10] "Exception: lognormal_lpdf: Scale parameter is inf, but must be finite! (in 'model39d27bec92db_a2c0a7231ba0e427628b016846ca7749' at line 137)"           
[11] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"                  
[12] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."                                                
[13] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"                                          
[14] "Exception: multiply: A[37] is -nan, but must not be nan! (in 'model39d27bec92db_a2c0a7231ba0e427628b016846ca7749' at line 79)"                          
[15] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"                  
[16] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."                                                
[17] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"                                          
[18] "Exception: lognormal_lpdf: Scale parameter is inf, but must be finite! (in 'model39d27bec92db_a2c0a7231ba0e427628b016846ca7749' at line 137)"           
[19] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"                  
[20] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."                                                
[21] "Error in sampler$call_sampler(args_list[[i]]) : "                                                                                                        
[22] " Exception: model39d27bec92db_a2c0a7231ba0e427628b016846ca7749_namespace::write_array: y is not positive definite. (in 'model39d27bec92db_a2c0a7231ba0e427628b016846ca7749' at line 143)"

error occurred during calling the sampler; sampling not done

```

- Operating System: Ubuntu 18.04.1
- brms Version: 2.5.0

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [October 25, 2018, 8:15pm UTC](https://discourse.mc-stan.org/t/model-with-many-correlated-varying-slopes-sampling-not-done/6189/2 "2018-10-25T20:15:29Z")

</div>

This is likely a problem with the initial values being too extreme. Try out `inits = 0` or, if that fails, `init_r = 0.1` (or even some smaller value).

---

<div class="post-metadata">

**Author:** ![rongmu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/rongmu/32/3359_2.png) [@rongmu](https://discourse.mc-stan.org/u/rongmu)\
**Post date:** [October 26, 2018, 10:11am UTC](https://discourse.mc-stan.org/t/model-with-many-correlated-varying-slopes-sampling-not-done/6189/3 "2018-10-26T10:11:59Z")

</div>

The model converged without problems after setting `inits = 0`.  
Thank you so much for your advice!

---

<div class="post-metadata">

**Author:** ![Guido\_Biele](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/guido_biele/32/3414_2.png) [@Guido\_Biele](https://discourse.mc-stan.org/u/Guido_Biele)\
**Post date:** [October 26, 2018, 10:49am UTC](https://discourse.mc-stan.org/t/model-with-many-correlated-varying-slopes-sampling-not-done/6189/4 "2018-10-26T10:49:31Z")

</div>

Note that when you set `inits = 0` you no longer have over-dispersed starting values between chains. This is relevant, because to conclude convergence across multiple chains, these need to start from different points, which is not the case when you set `inits = 0`.

If you want to assure convergence across multiple chains from Rhat values you need to set `init_r = r` or similar values as Paul also recommended. What value of r is reasonable depends on the values of your variables. (If you specify interactions, the range of main and interaction terms is relevant)

---

<div class="post-metadata">

**Author:** ![rongmu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/rongmu/32/3359_2.png) [@rongmu](https://discourse.mc-stan.org/u/rongmu)\
**Post date:** [October 26, 2018, 2:01pm UTC](https://discourse.mc-stan.org/t/model-with-many-correlated-varying-slopes-sampling-not-done/6189/5 "2018-10-26T14:01:03Z")

</div>

Thank you so much!  
I didn’t know the importance of different initial values for multiple chains!  
I don’t know much about initial values, I searched through the Stan manual and it says:

> [Zero initial values] can be helpful for diagnosis, and may also be a good starting point for sampling. Once a model is running, multiple chains with more diffuse starting points can help diagnose problems with convergence …

So, different starting points should be better. I tried out `init_r = 0.1` and it worked for my model.

However, I still don’t understand how to decide a reasonable range for initial values.  
Is that based on one’s expectation of the values of parameters?

Most of my predictors are factors and the only covariate has a range within [-1, 1].  
The model with default initial values only fails when the multilevel correlations are modeled, does it mean that the failure was due to extreme initial values for the correlation parameters?  
Also, the estimated multilevel correlations are very small for my model. Is this the reason that smaller initial values are recommended?

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [October 26, 2018, 3:44pm UTC](https://discourse.mc-stan.org/t/model-with-many-correlated-varying-slopes-sampling-not-done/6189/6 "2018-10-26T15:44:00Z")

</div>

It’s because the model contains so many terms that the default initial values over all the included parameters sum up to some extremely implausible values with a density very close to zero so that log() underflows to - infinity. You don’t need to worry about this I think as long as smaller `init_r` values solve it for you.

---

<div class="post-metadata">

**Author:** ![rongmu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/rongmu/32/3359_2.png) [@rongmu](https://discourse.mc-stan.org/u/rongmu)\
**Post date:** [October 27, 2018, 11:20am UTC](https://discourse.mc-stan.org/t/model-with-many-correlated-varying-slopes-sampling-not-done/6189/7 "2018-10-27T11:20:25Z")

</div>

Now I understand the concepts behind smaller `init_r` values. Thank you so much!
