# Error in FUN(X\[\[i\]\], ...) : trying to get slot "mode" from an object (class "try-error") that is not an S4 object when trying to fit beta binomial model on Ubuntu server

**URL:** <https://discourse.mc-stan.org/t/error-in-fun-x-i-trying-to-get-slot-mode-from-an-object-class-try-error-that-is-not-an-s4-object-when-trying-to-fit-beta-binomial-model-on-ubuntu-server/23191>\
**Category:** Modeling\
**Created:** [June 25, 2021, 8:30am UTC](https://discourse.mc-stan.org/t/error-in-fun-x-i-trying-to-get-slot-mode-from-an-object-class-try-error-that-is-not-an-s4-object-when-trying-to-fit-beta-binomial-model-on-ubuntu-server/23191 "2021-06-25T08:30:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aitorme](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/aitorme/32/12072_2.png) [@aitorme](https://discourse.mc-stan.org/u/aitorme)\
**Post date:** [June 25, 2021, 8:30am UTC](https://discourse.mc-stan.org/t/error-in-fun-x-i-trying-to-get-slot-mode-from-an-object-class-try-error-that-is-not-an-s4-object-when-trying-to-fit-beta-binomial-model-on-ubuntu-server/23191/1 "2021-06-25T08:30:58Z")

</div>

Hi there,  
I am trying to fit a beta binomial model in brms and I get the following error; I’ve seen a similar error posted in Nov18 but I’m running this on a server with Ubuntu, 16 CPU and 64 RAM and I expect the source of the error to be different:

```
Error in FUN(X[[i]], ...) : 
  trying to get slot "mode" from an object (class "try-error") that is not an S4 object 
Calls: brm ... eval -> .fun -> .fun -> .local -> sapply -> lapply -> FUN
In addition: Warning message:
In parallel::mclapply(1:chains, FUN = callFun, mc.preschedule = FALSE, :
  6 function calls resulted in an error
Execution halted

```

this is the model I’m trying to fit:

```
brm_fit <- 
  brm(
    formula = 
      distance | vint(trials) 
    ~ 1 
    + (1 | person) 
    + (1 | word),
    data=data,
    family = beta_binomial2,
    stanvars = stanvars,
    prior = priors,
    cores = 6,
    silent = F,
    seed = 42,
    chains = 6,
    iter = 6000,
    warmup = 3000,
    delta = 0.99,
    save_pars = save_pars(all = TRUE)
  )

```

I prepared the family the following way:

```
beta_binomial2 <- custom_family(
  "beta_binomial2", dpars = c("mu", "phi"),
  links = c("logit", "log"), lb = c(NA, 0),
  type = "int", vars = "vint1[n]"
)

stan_funs <- "
  real beta_binomial2_lpmf(int y, real mu, real phi, int T) {
    return beta_binomial_lpmf(y | T, mu * phi, (1 - mu) * phi);
  }
  int beta_binomial2_rng(real mu, real phi, int T) {
    return beta_binomial_rng(T, mu * phi, (1 - mu) * phi);
  }
"

stanvars <- stanvar(scode = stan_funs, block = "functions")

log_lik_beta_binomial2 <- function(i, prep) {
  mu <- prep$dpars$mu[, i]
  phi <- prep$dpars$phi
  trials <- prep$data$vint1[i]
  y <- prep$data$Y[i]
  beta_binomial2_lpmf(y, mu, phi, trials)
}

posterior_predict_beta_binomial2 <- function(i, prep, ...) {
  mu <- prep$dpars$mu[, i]
  phi <- prep$dpars$phi
  trials <- prep$data$vint1[i]
  beta_binomial2_rng(mu, phi, trials)
}

posterior_epred_beta_binomial2 <- function(prep) {
  mu <- prep$dpars$mu
  trials <- prep$data$vint1
  trials <- matrix(trials, nrow = nrow(mu), ncol = ncol(mu), byrow = TRUE)
  mu * trials
}

```

thanks in advance

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [July 2, 2021, 8:45am UTC](https://discourse.mc-stan.org/t/error-in-fun-x-i-trying-to-get-slot-mode-from-an-object-class-try-error-that-is-not-an-s4-object-when-trying-to-fit-beta-binomial-model-on-ubuntu-server/23191/2 "2021-07-02T08:45:35Z")

</div>

In `rstan`, some error messages are hidden when running multiple chains, could you post the output of running the same model with `chains = 1, cores = 1`?

---

<div class="post-metadata">

**Author:** ![aitorme](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/aitorme/32/12072_2.png) [@aitorme](https://discourse.mc-stan.org/u/aitorme)\
**Post date:** [July 13, 2021, 8:22am UTC](https://discourse.mc-stan.org/t/error-in-fun-x-i-trying-to-get-slot-mode-from-an-object-class-try-error-that-is-not-an-s4-object-when-trying-to-fit-beta-binomial-model-on-ubuntu-server/23191/3 "2021-07-13T08:22:53Z")

</div>

hi there,  
I did it what you suggested and now I get this error:  
`Error: passing unknown arguments: delta.`

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [July 13, 2021, 8:35am UTC](https://discourse.mc-stan.org/t/error-in-fun-x-i-trying-to-get-slot-mode-from-an-object-class-try-error-that-is-not-an-s4-object-when-trying-to-fit-beta-binomial-model-on-ubuntu-server/23191/4 "2021-07-13T08:35:06Z")

</div>

I believe the error is caused by this part of your `brm` call:

```r
delta = 0.99,

```

That should be:

```
control=list(adapt_delta=0.99),

```

---

<div class="post-metadata">

**Author:** ![aitorme](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/aitorme/32/12072_2.png) [@aitorme](https://discourse.mc-stan.org/u/aitorme)\
**Post date:** [July 13, 2021, 9:19am UTC](https://discourse.mc-stan.org/t/error-in-fun-x-i-trying-to-get-slot-mode-from-an-object-class-try-error-that-is-not-an-s4-object-when-trying-to-fit-beta-binomial-model-on-ubuntu-server/23191/5 "2021-07-13T09:19:46Z")

</div>

thanks, I tried this with 1 chain and now I get the following warnings:  
`Warning messages:`

`1: There were 2436 divergent transitions after warmup. See`

`https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup`

`to find out why this is a problem and how to eliminate them.`

`2: There were 3564 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10. See`

`https://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded`

`3: Examine the pairs() plot to diagnose sampling problems`  
`4: The largest R-hat is 1.41, indicating chains have not mixed.`

`Running the chains for more iterations may help. See`

`https://mc-stan.org/misc/warnings.html#r-hat`

`5: 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`

`https://mc-stan.org/misc/warnings.html#bulk-ess`

`6: 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`

`https://mc-stan.org/misc/warnings.html#tail-ess`

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [July 13, 2021, 9:22am UTC](https://discourse.mc-stan.org/t/error-in-fun-x-i-trying-to-get-slot-mode-from-an-object-class-try-error-that-is-not-an-s4-object-when-trying-to-fit-beta-binomial-model-on-ubuntu-server/23191/6 "2021-07-13T09:22:48Z")

</div>

That indicates issues with your model specification, rather than an installation issue. Try looking at different families or parameterisations
