ERROR trying to get slot "mode" from an object (class "try-error") that is not an S4 object

I’m struggling with a couple of error messages. The model runs on my Mac but trips up on GCP instance (Debian, 16 vCPUs, 96 GB memory). When i run the model from RStudio on this instance I get:

‘Error in unserialize(socklist[[n]]) : error reading from connection’

When I run in terminal I get:

Error in FUN(X[[i]], …) :

trying to get slot “mode” from an object (class “try-error”) that is not an S4 object

Calls: sampling -> sampling -> .local -> sapply -> lapply -> FUN

In addition: Warning message:

In parallel::mclapply(1:chains, FUN = callFun, mc.preschedule = FALSE, :

4 function calls resulted in an error

Execution halted

I know this has something to do with the generated quantities chunk, because when I remove it I don’t get the problem. The model and prediction data files have observations in the hundreds.

Any recommendations would be super appreciated!

functions {

// for likelihood estimation
  real dirichlet_multinomial_lpmf(int[] y, vector alpha) {
    real alpha_plus = sum(alpha);
    return lgamma(alpha_plus) + sum(lgamma(alpha + to_vector(y)))
                - lgamma(alpha_plus+sum(y)) - sum(lgamma(alpha));
  }

// for monte-carlo simulation
 int[] dirichlet_multinomial_rng(vector alpha, int N) {
  return multinomial_rng(dirichlet_rng(alpha), N);
}

}

data {
  int<lower = 1> K;                 // num channels
  int<lower = 1> N;                 // num observations
  int<lower = 1> G;                 // num first_level
  int<lower = 1> M;                 // num second_level
  int<lower = 0> y[N, K];           // spend in each channel
  int<lower = 1> first_level[N];    // number of first_level
  int<lower = 1> second_level[N];   // number of second_level
  int<lower = 0> budget[N];         // total campaign budget
  vector[N] log_budget;             // log budget
  int<lower = 1> N_new;             // new num observations
  int<lower = 1> budget_new[N_new]; // campaign budget for simulation
  vector[N_new] log_budget_new;     // log budget
}

parameters {
  matrix<lower = 0>[M,K] beta_0[G];
  matrix<lower = 0>[M,K] beta_1[G];
  matrix<lower = 1>[M,G] kappa;

  matrix<lower = 0>[G,K] mu_beta_0;
  matrix<lower = 0>[G,K] sigma_beta_0;

  vector<lower = 0>[K] mu_g_mu_beta_0;
  vector<lower = 0>[K] mu_g_sig_beta_0;

  vector<lower = 0>[K] sigma_g_mu_beta_0;
  vector<lower = 0>[K] sigma_g_sig_beta_0;


  matrix<lower = 0>[G,K] mu_beta_1;
  matrix<lower = 0>[G,K] sigma_beta_1;

  vector<lower = 0>[K] mu_g_mu_beta_1;
  vector<lower = 0>[K] mu_g_sig_beta_1;

  vector<lower = 0>[K] sigma_g_mu_beta_1;
  vector<lower = 0>[K] sigma_g_sig_beta_1;


  vector<lower = 0>[G] mu_kappa;
  vector<lower = 0>[G] sigma_kappa;

  real<lower=1> mu_g_mu_kappa;
  real<lower=1> mu_g_sig_kappa;
  real<lower=1> sigma_g_mu_kappa;
  real<lower=1> sigma_g_sig_kappa;

}

transformed parameters {
  simplex[K] theta[N];    
  vector[K] x_beta[N];

  // create XB index
  {
  for (n in 1:N) {
    x_beta[n] = to_vector(beta_0[first_level[n]][second_level[n],] + beta_1[first_level[n]][second_level[n],]*log_budget[n]);
    theta[n] = softmax(x_beta[n]);
  }
 }
}

model {

  // priors
  mu_g_mu_beta_0 ~ lognormal(0,0.1);
  mu_g_sig_beta_0 ~ lognormal(0,0.1);

  sigma_g_mu_beta_0 ~ lognormal(0,0.1);
  sigma_g_sig_beta_0 ~ lognormal(0,0.1);

  mu_g_mu_beta_1 ~ lognormal(0,0.1);
  mu_g_sig_beta_1 ~ lognormal(0,0.1);

  sigma_g_mu_beta_1 ~ lognormal(0,0.1);
  sigma_g_sig_beta_1 ~ lognormal(0,0.1);

  mu_g_mu_kappa ~ lognormal(1, 3);
  mu_g_sig_kappa ~ lognormal(1, 3);
  sigma_g_mu_kappa ~ lognormal(1, 3);
  sigma_g_sig_kappa ~ lognormal(1, 3);


  for (g in 1:G){

    to_vector(mu_beta_0[g]) ~ lognormal(mu_g_mu_beta_0, mu_g_sig_beta_0);
    to_vector(sigma_beta_0[g]) ~ lognormal(sigma_g_mu_beta_0, sigma_g_sig_beta_0);
    to_vector(mu_beta_1[g]) ~ lognormal(mu_g_mu_beta_1, mu_g_sig_beta_1);
    to_vector(sigma_beta_1[g]) ~ lognormal(sigma_g_mu_beta_1, sigma_g_sig_beta_1);

    mu_kappa[g] ~ lognormal(mu_g_mu_kappa, mu_g_sig_kappa);
    sigma_kappa[g] ~ lognormal(sigma_g_mu_kappa, sigma_g_sig_kappa);

    for (m in 1:M){
      to_vector(beta_0[g][m]) ~ gamma(mu_beta_0[g], sigma_beta_0[g]);     
      to_vector(beta_1[g][m]) ~ gamma(mu_beta_1[g], sigma_beta_1[g]);
      kappa[m,g] ~ lognormal(mu_kappa[g], sigma_kappa[g]);
    }
  }
  

  for (n in 1:N) {
        y[n] ~ dirichlet_multinomial(kappa[second_level[n], first_level[n]]*theta[n]);
    }
}

generated quantities {
  int<lower=0> channel_sim[G,M,N_new,K];    
  vector[K] theta_new[G,M,N_new];
  
  {
    vector[K] alpha_new[N_new];
    int channel_sim_tmp[N_new,K];
    int channel_sim_tmp2[M,N_new,K];
    for (g in 1:G){
      for (m in 1:M){
        for (n in 1:N_new){
          for(k in 1:K){
            alpha_new[n,k]   = beta_0[g][m,k] + beta_1[g][m,k]*log_budget_new[n];
          }
          theta_new[g,m,n]     = kappa[m,g]*softmax(to_vector(alpha_new[n]));
          channel_sim_tmp[n] = dirichlet_multinomial_rng(theta_new[g,m,n],budget_new[n]); 
        }
        channel_sim_tmp2[m] = channel_sim_tmp;
      }
      channel_sim[g] = channel_sim_tmp2;
    }
  }
}

Try it with chains = 1 to see the underlying error message.

Thanks for your reply - I did this. Rstudio crashes and if I run in terminal it says ‘Segmentation fault’… any ideas?

Thanks

Solved it i think. Had to run sudo Rscript due to some GC memory restriction

Actually this hasn’t worked, it just hid the Segmentation fault (core dumped) error. Any help on this would be hugely appreciated