Error when using the brm()

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 (21.4 KB)

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

2 Likes

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.

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

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.

I would recommend using cor_car. See ?car in brms 2.13+

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.