Error in as.character.default() on my computer but compiles successfully on my teammates' computers

I have been fitting a series of models in rstan over the past 6 weeks, but all of a sudden in the past couple days or so my computer has failed to compile models that just a couple days ago would compile and run perfectly fine. The error message is “Error in as.character.default(X[[i]], …) :
no method for coercing this S4 class to a vector”, which does not seem to appear in any Stan related programming issue that I could find.

Two of my teammates were able to compile this model, and even after I reinstalled everything (RStudio, R, and the user-installed packages) I am still running into this issue. When I compile smaller Stan programs my computer is able to do it, but this larger program has started to give me this error. My instructor suggested that it could be when Stan tries to convert the S4 stan model and pass it to another function where it is compiled into C++ instructions, and that some string parsing related error was occuring. I am running Windows 10 on a 64-bit Dell XPS 17. If anyone had any insight into possible causes if this issue I would really appreciate it. Thank you.

EDIT: I think that a solution to this issue is creating a new file with a name that I haven’t used before and simply copying the code over to that file. There have been three different times where I’ve been able to solve the issue with this approach. Does anyone have an idea what’s going on? My only guess is that if I use a file name that I’ve already used then maybe there is a compiled version saved to the hard drive that causes Stan to make a mistake. However if I use a new name then maybe Stan can’t make that mistake. In the mean time I deleted any old rds files and from now on while compile my models with save_dso and auto_write equal to false.

data {
  int<lower=1> N;
  int<lower=1> num_transitivity;
  int<lower=1> num_degree;
  int<lower=1> num_t_ROI;
  
  row_vector[3] transitivity_spatial[num_transitivity];
  row_vector[3] degree_spatial[num_degree];
  
  matrix[N,num_transitivity] transitivity_covariates;
  matrix[N,num_degree] degree_covariates;
  matrix[N,num_t_ROI] t_ROI_covariates;
  
  int<lower=0,upper=1> labels[N];
  
  int<lower=1> N_valid;
  matrix[N_valid,num_transitivity] transitivity_covariates_valid;
  matrix[N_valid,num_degree] degree_covariates_valid;
  matrix[N_valid,num_t_ROI] t_ROI_covariates_valid;
}
transformed data {
  real delta = 1e-9;
}
parameters {
  real intercept_label;
  
  real<lower=0> transitivity_rho;
  real<lower=0> transitivity_alpha;
  vector[num_transitivity] transitivity_eta;
  
  real<lower=0> degree_rho;
  real<lower=0> degree_alpha;
  vector[num_degree] degree_eta;
  
  vector[num_t_ROI] t_ROI_beta;
}
transformed parameters {
  vector[num_transitivity] transitivity_beta;
  matrix[num_transitivity, num_transitivity] K_transitivity;
  matrix[num_transitivity, num_transitivity] L_K_transitivity;
  
  vector[num_degree] degree_beta;
  matrix[num_degree, num_degree] K_degree;
  matrix[num_degree, num_degree] L_K_degree;
  
  K_transitivity = cov_exp_quad(transitivity_spatial, transitivity_alpha, transitivity_rho);
  K_degree = cov_exp_quad(degree_spatial, degree_alpha, degree_rho);

  // diagonal elements
  for (n in 1:num_transitivity) {
    K_transitivity[n, n] = K_transitivity[n, n] + delta;
  }
  
  // diagonal elements
  for (n in 1:num_degree) {
    K_degree[n, n] = K_degree[n, n] + delta;
  }

  L_K_transitivity = cholesky_decompose(K_transitivity);
  transitivity_beta = L_K_transitivity * transitivity_eta;
  
  L_K_degree = cholesky_decompose(K_degree);
  degree_beta = L_K_degree * degree_eta;
}
model {
  vector[N] x_beta;
  for (n in 1:N) {
    x_beta[n] = intercept_label + dot_product(degree_beta,degree_covariates[n])
                                + dot_product(transitivity_beta,transitivity_covariates[n])
                                + dot_product(t_ROI_beta,t_ROI_covariates[n]);
  }
  
  intercept_label ~ normal(0, 10);
  
  transitivity_rho ~ inv_gamma(3, 3);
  transitivity_alpha ~ std_normal();
  transitivity_eta ~ std_normal();
  
  degree_rho ~ inv_gamma(3, 3);
  degree_alpha ~ std_normal();
  degree_eta ~ std_normal();
  
  for (d in 1:num_t_ROI) {
    t_ROI_beta ~ normal(0,1);
  }
  
  labels ~ bernoulli_logit(x_beta);
}
1 Like

Hi, sorry for not getting to you earlier.

Not sure about the specific problem, but which version of rstan are you using? Unfortunately the version on CRAN is a bit behind the development (for mostly stupid reasons, we are working towards a solution). Current version can however be installed via Repository for distributing (some) stan-dev R packages | r-packages . Does the problem persist with latest rstan?

No problem at all! I resolved the issue but forgot to edit my post with the solution and close it, if that’s an option. I’m pretty sure that I at some point tried to save the model, whose source code was for example model1.stan, using saveRDS() and the file name “model1.stan” instead of “model1.rds”. I remember at one point I did this and then changed it, but I didn’t connect the dots until later. I think what caused the issue is that it would try to get the compiled model from the source code and then throw an error.