R goes really slow implemented Rstan. Don't know what happened

Hi everyone,

My Rstudio gets stuck once the Rstan is implemented, for example the “Check” button on the Rstan interface takes forever to run. The “rstan::” function takes forever too. Also, after I library(rstan), “::” function for all the packages get stuck. When I create a new project and avoid any steps related to rstan, everything goes fine. I have tried to update my R and Rstudio to the latest version but it didn’t help.

I believe the code is proper. paste it anyway:

data{
  int obs;                      // number of observation in the long vector
  int N;                        // number of individuals
  vector [N] size_t0;           // size of focal plants
  vector [N] growth;            // growth of focal plants, response
  int n_nb[N];
  vector [obs] size_vector;        // leng vector of neighbor sizes
  vector [obs] dist_vector;        // long vector of pairwise distances
  int pos[N];
}
transformed data{
}
parameters{
  real alpha;                       // Intercept  
  real beta;                        // Effect of target plant size on response  
  real sigma;                       // global SD  
  real<lower=0> a1;                 // size dependent effect of the neighbor    
  real a3;                          // net crowding effect  
  real<lower=0> a2;                 // spatial scale  
  }
transformed parameters{
  vector[N] kernel;  
  vector[N] mu;
  vector[obs] size_vec;
  vector[obs] dist_vec;
  for (i in 1:obs){
      dist_vec[i]=dist_vector[i]^2; 
      size_vec[i]=size_vector[i]^a1;
    }
  for(n in 1:N)
      kernel[n]=sum(exp(log(segment(size_vec, pos[n], n_nb[n]))-
                          (segment(dist_vec, pos[n], n_nb[n])*a2)));
  
  for(n in 1:N)
      mu[n]=alpha+size_t0[n]*beta+a3*kernel[n];
}
model{
  alpha~normal(0,5);   // uninformed priors centered at zero;
  beta~normal(0,5);
  a1~normal(0,5);
  a2~normal(0,5);
  a3~normal(0,5);
  sigma~exponential(1);
  
  growth ~ normal(mu,sigma);
}

Operating System: MAC Catalina 10.15.6
RStan Version: 2.21.2
Session info:
setting value
version R version 4.0.3 (2020-10-10)
os macOS Catalina 10.15.6
system x86_64, darwin17.0
ui RStudio
language (EN)
collate zh_CN.UTF-8
ctype zh_CN.UTF-8
tz Asia/Shanghai
date 2020-12-27

Thanks!

2 Likes

Hi, Yangkang and welcome to the community!

if it’s any comfort, using cmdstan I can compile this model. Try installing cmdstanr,

install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

and then install cmdstan with,

install_cmdstan()

and then compile model with,

m0 <- cmdstan_model("foo.stan")
1 Like

Thank you torkar. I will try it. But still a pity that I can’t use the “Check” button to confirm my code before running it.

That could be an RStudio problem so better check with them?

Yes, I will use CmdStan before I figure it out. If I’m the only one to have this problem, must be something wrong with my RStudio. Thanks for your advise.

There’s another recent thread where a few of us have reported the same thing. I suspect it’s an rstudio bug but haven’t figured out how to make a reproducible example for a hug report.

3 Likes

I have a similar problem with you some months ago. The reason is that rstan will try to download a script from GitHub which is inaccessible in China. See code on this page stanc.R.

Try rstan_options ( javascript = FALSE)

4 Likes

Thanks! It’s exactly the solution. Wish I can remember to type this every time : )

It should be a permanent option so you only need to type once. At least I just use it once.