# Statistical Rethinking Simple Models Crashing

**URL:** <https://discourse.mc-stan.org/t/statistical-rethinking-simple-models-crashing/17360>\
**Category:** RStan\
**Tags:** rstan\
**Created:** [August 10, 2020, 10:51pm UTC](https://discourse.mc-stan.org/t/statistical-rethinking-simple-models-crashing/17360 "2020-08-10T22:51:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tjburch](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tjburch/32/8993_2.png) [@tjburch](https://discourse.mc-stan.org/u/tjburch)\
**Post date:** [August 10, 2020, 10:51pm UTC](https://discourse.mc-stan.org/t/statistical-rethinking-simple-models-crashing/17360/1 "2020-08-10T22:51:43Z")

</div>

I’m working through Statistical rethinking right now in order to learn Stan, and having issues where fitting most MCMC models crash my R session. In some cases they’ll fit and the session crashes after running a few more code blocks, in some it crashes when fitting. I assume this is a memory issue, but I can’t imagine straightforward models would crash my session like this. I’m using a 2015 dual-core 8gb macbook pro. Is this common?

This is a persisting problem, but the most recent example is model 14.1:

```
m14.1 <- ulam(
    alist(
        wait ~ normal( mu , sigma ),
        mu <- a_cafe[cafe] + b_cafe[cafe]*afternoon,
        c(a_cafe,b_cafe)[cafe] ~ multi_normal( c(a,b) , Rho , sigma_cafe ),
        a ~ normal(5,2),
        b ~ normal(-1,0.5),
        sigma_cafe ~ exponential(1),
        sigma ~ exponential(1),
        Rho ~ lkj_corr(2)
    ) , data=d , chains=2 , cores=1)

```

This crashes the session on fitting. I also tried fitting with pure Stan, not the ulam overlay:

````
```{r}
try01 <- as.character("
data{
  vector[200] wait;
  int afternoon[200];
  int cafe[200];
}
parameters{
  vector[20] b_cafe;
  vector[20] a_cafe;
  real a;
  real b;
  vector<lower=0>[2] sigma_cafe;
  real<lower=0> sigma;
  corr_matrix[2] Rho;
}
model{
  vector[200] mu;
  Rho ~ lkj_corr( 2 );
  sigma ~ exponential( 1 );
  sigma_cafe ~ exponential( 1 );
  b ~ normal( -1 , 0.5 );
  a ~ normal( 5 , 2 );
  {
    vector[2] YY[20];
    vector[2] MU;
    MU = [a , b]';
    for ( j in 1:20 ) YY[j] = [a_cafe[j] , b_cafe[j] ]';
    YY ~ multi_normal( MU , quad_form_diag(Rho , sigma_cafe) );
  }
  for ( i in 1:200 ) {
    mu[i] = a_cafe[cafe[i]] + b_cafe[cafe[i]] * afternoon[i];
  }
  wait ~ normal( mu , sigma );
}
")
m14.1 <- stan( model_code = try01, data=d, chains=2)

````

which also crashes for me. I’ve tried running from Rstudio and from command line. Full disclosure, I’m doing it within the context of an Rmarkdown script, so I’m running as:

```
Rscript -e "rmarkdown::render('ch14.Rmd', clean=TRUE)

```

Relevant session info:

```
 version R version 4.0.1 (2020-06-06)
 os macOS Catalina 10.15.4      
 system x86_64, darwin19.5.0        
 ui RStudio      

rethinking_2.01 
 rstan * 2.21.2 2020-07-27 [1] CRAN (R 4.0.1)

```

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [August 11, 2020, 7:37am UTC](https://discourse.mc-stan.org/t/statistical-rethinking-simple-models-crashing/17360/2 "2020-08-11T07:37:05Z")

</div>

Welcome to the Stan community!

The below works for me with `rethinking v2.12`, `StanHeaders v2.21.0.5`, and `rstan v2.21.2` on `OS X` (I’ve cut’n pasted the code from the book, i.e., edition 2). It takes some time to compile but then it samples for, like, a second. Like a warm knife through butter :)

```
a <- 3.5 # average morning wait time
b <- (-1) # average difference afternoon wait time
sigma_a <- 1 # std dev in intercepts
sigma_b <- 0.5 # std dev in slopes
rho <- (-0.7) # correlation between intercepts and slopes

Mu <- c( a , b )

## R code 14.3
cov_ab <- sigma_a*sigma_b*rho
Sigma <- matrix( c(sigma_a^2,cov_ab,cov_ab,sigma_b^2) , ncol=2 )

## R code 14.4
matrix( c(1,2,3,4) , nrow=2 , ncol=2 )

## R code 14.5
sigmas <- c(sigma_a,sigma_b) # standard deviations
Rho <- matrix( c(1,rho,rho,1) , nrow=2 ) # correlation matrix

# now matrix multiply to get covariance matrix
Sigma <- diag(sigmas) %*% Rho %*% diag(sigmas)

## R code 14.6
N_cafes <- 20

## R code 14.7
library(MASS)
set.seed(5) # used to replicate example
vary_effects <- mvrnorm( N_cafes , Mu , Sigma )

## R code 14.8
a_cafe <- vary_effects[,1]
b_cafe <- vary_effects[,2]

set.seed(22)
N_visits <- 10
afternoon <- rep(0:1,N_visits*N_cafes/2)
cafe_id <- rep( 1:N_cafes , each=N_visits )
mu <- a_cafe[cafe_id] + b_cafe[cafe_id]*afternoon
sigma <- 0.5 # std dev within cafes
wait <- rnorm( N_visits*N_cafes , mu , sigma )
d <- data.frame( cafe=cafe_id , afternoon=afternoon , wait=wait )

set.seed(867530)
m14.1 <- ulam(
    alist(
        wait ~ normal( mu , sigma ),
        mu <- a_cafe[cafe] + b_cafe[cafe]*afternoon,
        c(a_cafe,b_cafe)[cafe] ~ multi_normal( c(a,b) , Rho , sigma_cafe ),
        a ~ normal(5,2),
        b ~ normal(-1,0.5),
        sigma_cafe ~ exponential(1),
        sigma ~ exponential(1),
        Rho ~ lkj_corr(2)
    ) , data=d , chains=4 , cores=4 )

```

---

<div class="post-metadata">

**Author:** ![tjburch](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tjburch/32/8993_2.png) [@tjburch](https://discourse.mc-stan.org/u/tjburch)\
**Post date:** [August 11, 2020, 1:08pm UTC](https://discourse.mc-stan.org/t/statistical-rethinking-simple-models-crashing/17360/3 "2020-08-11T13:08:31Z")

</div>

Yep, looks like you’re right… It appears all of this runs fine if typed straight into an R console, or in a standard R script, it just dies in the context of Rmd, for whatever reason. Frustrating, I’m surprised the execution is different on memory, but nice to have a workable solution.

Thanks for checking! Sorry for the static with this being ~trivial.

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [August 11, 2020, 6:23pm UTC](https://discourse.mc-stan.org/t/statistical-rethinking-simple-models-crashing/17360/4 "2020-08-11T18:23:36Z")

</div>

Perhaps using the latest support for `cmdstanr` in `rethinking` could be an option?

```
> devtools::install_github("stan-dev/cmdstanr")
> install_cmdstan()

```

restart, and then run `ulam()` with option `cmdstan=TRUE`.

---

<div class="post-metadata">

**Author:** ![tjburch](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tjburch/32/8993_2.png) [@tjburch](https://discourse.mc-stan.org/u/tjburch)\
**Post date:** [August 13, 2020, 4:57pm UTC](https://discourse.mc-stan.org/t/statistical-rethinking-simple-models-crashing/17360/5 "2020-08-13T16:57:52Z")

</div>

Just wanted to come back and thank you for this! I was bashing my head against my computer today running into the problem again, and finally got cmdstan running and everything seems to be working like a charm.

Thanks so much!
