# Error when using the brm()

**URL:** <https://discourse.mc-stan.org/t/error-when-using-the-brm/15954>\
**Category:** brms\
**Created:** [June 15, 2020, 9:43am UTC](https://discourse.mc-stan.org/t/error-when-using-the-brm/15954 "2020-06-15T09:43:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel\_yao](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/daniel_yao/32/8347_2.png) [@daniel\_yao](https://discourse.mc-stan.org/u/daniel_yao)\
**Post date:** [June 15, 2020, 9:43am UTC](https://discourse.mc-stan.org/t/error-when-using-the-brm/15954/1 "2020-06-15T09:43:24Z")

</div>

Hi everyone,

I tried to build a poisson regression model using the brm() and the command  
brm\_fit \<- brm(grid\_diversity ~ grid\_bio1 + grid\_bio4 + grid\_bio11 + grid\_bio12 + grid\_bio15 + grid\_speciation + grid\_aridityIndex + grid\_wd, data=test, family=poisson()). The response variable was count of species. Other variables were numeric.

**But I got the error:**  
Compiling the C++ model  
Start sampling

SAMPLING FOR MODEL ‘3453be8fad0bec5136a860e9f38761fa’ NOW (CHAIN 1).  
Chain 1: Rejecting initial value:  
Chain 1: Log probability evaluates to log(0), i.e. negative infinity.  
Chain 1: Stan can’t start sampling from this initial value.  
Chain 1: Rejecting initial value:  
Chain 1: Log probability evaluates to log(0), i.e. negative infinity.  
Chain 1: Stan can’t start sampling from this initial value.  
…  
Chain 1: Rejecting initial value:  
Chain 1: Log probability evaluates to log(0), i.e. negative infinity.  
Chain 1: Stan can’t start sampling from this initial value.  
Chain 1:  
Chain 1: Initialization between (-2, 2) failed after 100 attempts.  
Chain 1: Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.  
[1] “Error in sampler$call\_sampler(args\_list[[i]]) : Initialization failed.”  
[1] “error occurred during calling the sampler; sampling not done”

I can ran the glm(grid\_diversity ~ grid\_bio1 + grid\_bio4 + grid\_bio11 + grid\_bio12 + grid\_bio15 + grid\_speciation + grid\_aridityIndex + grid\_wd, data=test, family=poisson()). The response variable in my data didn’t have zero. And I also tried the family = zero\_inflated\_poisson() in the brm(), but still got the same error as above. I uploaded the data I used.

Operation system I used: windows10  
brms version: 2.13.0  
R version: 3.6.1

Thank you very much if anyone has some ideas about my problem.

[brm\_test\_dataset\_20200615.csv](https://discourse.mc-stan.org/uploads/short-url/g0Sx4YtffYrTXtbZVerBHEtWLSK.csv) (21.4 KB)

---

<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:** [June 15, 2020, 9:48am UTC](https://discourse.mc-stan.org/t/error-when-using-the-brm/15954/2 "2020-06-15T09:48:21Z")

</div>

Try scaling your predictors to roughly unit scale and/or set argument inits = 0.

---

<div class="post-metadata">

**Author:** ![daniel\_yao](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/daniel_yao/32/8347_2.png) [@daniel\_yao](https://discourse.mc-stan.org/u/daniel_yao)\
**Post date:** [June 17, 2020, 1:02pm UTC](https://discourse.mc-stan.org/t/error-when-using-the-brm/15954/3 "2020-06-17T13:02:25Z")

</div>

Thanks very much for your help! I have fixed this problem now by scaling the predictors.

Besides, does brms package have argument to count the spatial autocorrelation in model? I have searched around in this discussion website and found few replies you and other people answered. But seems all of these replies based on the stan package, which were a little difficult to understand and use than the brms package.

---

<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:** [June 17, 2020, 1:14pm UTC](https://discourse.mc-stan.org/t/error-when-using-the-brm/15954/4 "2020-06-17T13:14:36Z")

</div>

Can you elaborate a bit more on your question, please?

---

<div class="post-metadata">

**Author:** ![daniel\_yao](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/daniel_yao/32/8347_2.png) [@daniel\_yao](https://discourse.mc-stan.org/u/daniel_yao)\
**Post date:** [June 17, 2020, 1:29pm UTC](https://discourse.mc-stan.org/t/error-when-using-the-brm/15954/5 "2020-06-17T13:29:53Z")

</div>

I’m looking at whether environmental variables selected in the analysis are significant important to the species richness in each grid in South Ameirca. All predictors are numeric and the response is count of species richness in each grid. Now, I’m going to test whether the spatial autocorrelation would influence the species richness pattern in grids. So, it needs to compare model including spatial autocorrelation with model doesn’t include, like you have replied in the topic other people posted. I wonder whether brms package have included this kind of argument since the time that you have replied in other topics. And does cor\_sar fit my problem? Thank you.

---

<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:** [June 17, 2020, 1:31pm UTC](https://discourse.mc-stan.org/t/error-when-using-the-brm/15954/6 "2020-06-17T13:31:26Z")

</div>

I would recommend using cor\_car. See ?car in brms 2.13+

---

<div class="post-metadata">

**Author:** ![daniel\_yao](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/daniel_yao/32/8347_2.png) [@daniel\_yao](https://discourse.mc-stan.org/u/daniel_yao)\
**Post date:** [June 19, 2020, 1:11am UTC](https://discourse.mc-stan.org/t/error-when-using-the-brm/15954/7 "2020-06-19T01:11:23Z")

</div>

Hi, Paul, thanks for your help.  
I added the cor\_car() into the model, but the model has run near 40 hours and not finish yet. It displays:

> Click the Refresh button to see progress of the chains  
> starting worker pid=19640 on localhost:11403 at 22:02:53.235  
> starting worker pid=20428 on localhost:11403 at 22:02:53.532  
> starting worker pid=4700 on localhost:11403 at 22:02:53.854  
> starting worker pid=20152 on localhost:11403 at 22:02:54.175
> 
> SAMPLING FOR MODEL ‘8315da991ace9148954f0990563a6bcd’ NOW (CHAIN 1).  
> Chain 1:  
> Chain 1: Gradient evaluation took 0.189 seconds  
> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 1890 seconds.  
> Chain 1: Adjust your expectations accordingly!  
> Chain 1:  
> Chain 1:  
> Chain 1: Iteration: 1 / 30000 [0%] (Warmup)
> 
> SAMPLING FOR MODEL ‘8315da991ace9148954f0990563a6bcd’ NOW (CHAIN 2).  
> Chain 2:  
> Chain 2: Gradient evaluation took 0.224 seconds  
> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 2240 seconds.  
> Chain 2: Adjust your expectations accordingly!  
> Chain 2:  
> Chain 2:  
> Chain 2: Iteration: 1 / 30000 [0%] (Warmup)
> 
> SAMPLING FOR MODEL ‘8315da991ace9148954f0990563a6bcd’ NOW (CHAIN 3).  
> Chain 3:  
> Chain 3: Gradient evaluation took 0.278 seconds  
> Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 2780 seconds.  
> Chain 3: Adjust your expectations accordingly!  
> Chain 3:  
> Chain 3:  
> Chain 3: Iteration: 1 / 30000 [0%] (Warmup)
> 
> SAMPLING FOR MODEL ‘8315da991ace9148954f0990563a6bcd’ NOW (CHAIN 4).  
> Chain 4:  
> Chain 4: Gradient evaluation took 0.203 seconds  
> Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 2030 seconds.  
> Chain 4: Adjust your expectations accordingly!  
> Chain 4:  
> Chain 4:  
> Chain 4: Iteration: 1 / 30000 [0%] (Warmup)

**The code I used is:**

> dist.mat ← as.matrix(dist(cbind(nw\_grid\_dt3$ctr\_long, nw\_grid\_dt3$ctr\_lat)))  
> head(dist.mat)  
> str(dist.mat)  
> W ← matrix(0, nrow = nrow(dist.mat), ncol = ncol(dist.mat))  
> W[W \<= 3700] ← 1
> 
> #Any distances less than or equal to 3700 m (which is also the distance of 2 arc-degrees at the equator) is considered ‘adjacent’  
> rownames(W) ← nw\_grid\_dt3$grid\_id
> 
> library(brms)  
> brm\_stand\_spat\_fit ← brm(grid\_diversity ~ stand\_bio1 + stand\_bio4 + stand\_bio11 + stand\_bio12 + stand\_bio15 + stand\_speciation + stand\_aridityIndex + stand\_wd, data = nw\_grid\_dt3,  
> family = poisson(), sample\_prior = TRUE,  
> autocor = cor\_car(W, formula = ~ 1|grid\_id),  
> iter = 30000, warmup = 15000, cores = 8,  
> thin = 10, save\_all\_pars = TRUE, seed = T,  
> control = list(adapt\_delta=0.99, max\_treedepth = 15))

It was said that improving the parameters of model could accelerate the model running. But I’m also worried about whether I used wrong arguments or set unreasonable value to arguments in the model. So as your opinion, what should I do to improve the model running? The total data has 1518 rows. And I’m using the version 2.13.0 of the brms package.
