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

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

1 Like

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?

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

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

delta = 0.99,

That should be:

control=list(adapt_delta=0.99),

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

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

1 Like