Stan now (mostly) working on RStudio Cloud

There is a function in knitr that strips out the R code from an Rmd
file, so it is fairly easy to get both.

1 Like

The number of kids who donā€™t know R or Stan but do know enough about probability / statistics / computation to have any interest in and appreciation for what Stan is doing is a very small set. But for them you presumably just want HTML that has the plots but hides the code.

One possible audience is people who donā€™t know statistics but who could think this is cool. People such as Alan Edelman.

I love this and Iā€™m happy with Akiā€™s knitr-like .R file. I have just 2 concerns:

  1. The session takes a long time to load. Is that because the .rds and .html files are so big, in which case should we remove them from the saved session?

  2. If load time is proportional to file sizes, then this is a concern if weā€™d like to put in several models in the same session. Perhaps we should talk with JJ about him giving us some number of separate sessions: one for the regression model, one for the golf example, etc etc. This is all good publicity for Rstudio, so I donā€™t see why heā€™d say no to this request.

  3. The other concern is compilation time. Yes, I realize this conflicts with item 1 above. But, seeing the online version makes me worry that, for new users especially, compilation time is a barrier, as it makes it difficult to play around with models.

I donā€™t think it has to do with file sizes in projects; see https://community.rstudio.com/t/performance-issues/14019 . So, we should continue to precompile the examples.

Here is the afore mentioned blog post: https://andrewgelman.com/2018/10/12/stan-on-the-web-for-free-thanks-to-rstudio/

1 Like

Iā€™ve succesfully complied two simpler models, but the following and other models with similar complexity fail to compile

writeLines(readLines("binomb.stan"))
data {
  int<lower=0> N;
  int<lower=0> y;
}
parameters {
  real alpha;
}
transformed parameters {
  real theta;
  theta = inv_logit(alpha);
}
model {
  alpha ~ normal(0,1.5);
  y ~ binomial_logit(N,alpha);
}
d_bin <- list(N = 100, y = 70)
fit_bin <- stan(file = 'binomb.stan', data = d_bin)

I am guessing that one should compile when the server is under less load.

but

so itā€™s not particularily useful as is. Have you asked RStudio what is the price for more resources when running a course to avoid problems like this?

It is in alpha so they are not yet selling it. For a workshop, the person in charge should compile the models in advance when everyone else is asleep. But I will ask RStudio if they will install a version of clang++ that supports ThinLTO and maybe we can compile a few more models within the resource limits.

Itā€™s difficult to pre-compile those models that students write or modify during the courseā€¦

This binomial model compiles for me on Saturday, although RStudio seems open to adding a better compiler.

This is excellent news and is really a great step in terms of encouraging the adoption of Stan. Up until now Iā€™ve been reluctant to run tutorial sessions on Stan because of the difficulties of getting it installed on our University machines, but this changes everything.

After playing around with my own copy of the project and one thing Iā€™m noticing is that compilation is really finicky. Often itā€™ll work on the first attempt, but subsequent attempts will give a bunch of clang errors. Sometimes, restarting R (using Session -> Restart R) will work, but other times it will just refuse to work. Is there anything that can be done about this, or is it just a limitation of the present implementation?

I think it depends on the load that happens to be present at that moment. We are trying to improve it a little bit. It is always possible to serialize a compiled model if you specify auto_write = TRUE in the call to sampling or rstan_options(auto_write = TRUE) at the outset. I was able to compile almost 500 examples just by waiting until late at night Eastern time.

I was encouraged to try out the RStudio Cloud implementation of Stan through the Statistical Rethinking course using my MacBook Pro. I installed the rethinking package (+dependencies) and updated a few of the existing packages (not sure if supposed to do that). The rethinking code works fine; however, when I try to run the examples (as recommended) I get the following:

library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
?stan_demo()
fit1 <- stan_demo(68)
stan_demo(model = character(68), 
          method = c("sampling"))
# chose model 68 when prompted
# > "N" <-
#   +   8
# 
# > "sigma_y" <-
#   +   c(15,10,16,11,9,11,10,18)
# 
# > "y" <-
#   +   c(28,8,-3,7,-1,1,18,12)
# Error in compileCode(f, code, language = language, verbose = verbose) : 
#   Compilation ERROR, function(s)/method(s) not created! clang version 5.0.0-3~16.04.1 (tags/RELEASE_500/final)
# Target: x86_64-pc-linux-gnu
# Thread model: posix
# InstalledDir: /usr/bin
# Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5
# Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0
# Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6
# Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.0.0
# Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5
# Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0
# Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
# Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.0.0
# Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0
# Candidate multilib: .;@m64
# Selected multilib: .;@m64
# "/usr/lib/llvm-5.0/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-llvm-bc -flto=thin -flto-unit -disable-free -
#   In addition: Warning message:
#   In system(cmd, intern = !verbose) :
#   running command '/opt/R/3.5.2/lib/R/bin/R CMD SHLIB file11165b009b1.cpp 2> file11165b009b1.cpp.err.txt' had status 1
# Error in sink(type = "output") : invalid connection

Any thoughts? Is it OK to update/add packages or should one stick to what is provided?

I got a similar error message and I think itā€™s due to the current RAM/memory limit (1GB) in RStudio Cloud.

I have a simmilar error now on iPad pro with google chrome.
My device RAM is 4GB.

Why dosenā€™t work?

It is not related to the RAM on your machine; it is related to the 1GB RAM limit imposed by RStudio Cloud. I think Stan 2.18.x is not going to work with those restrictions and I will have to downgrade everything to Stan 2.17.x.

1 Like

Oh, I had misunderstand. Thank you. I wil try it!

Thanks everyone.