R Studio Crashes when sampling (code included)

The example comes from the rethinking package. It is a multi-level model with varying slopes and intercepts.

Basically, R studio crashes when I execute this code. It’s done this on two computers. Currently using R 3.5.1 on Windows 10 with rethinking 1.59 and rstan 2.12.2.


## R code 13.1
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

## R code 13.2
Mu <- c( a , b )

## R code 13.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 13.4
matrix( c(1,2,3,4) , nrow=2 , ncol=2 )

## R code 13.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 13.6
N_cafes <- 20

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

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

## R code 13.9
plot( a_cafe , b_cafe , col=rangi2 ,
      xlab="intercepts (a_cafe)" , ylab="slopes (b_cafe)" )

# overlay population distribution
library(ellipse)
for ( l in c(0.1,0.3,0.5,0.8,0.99) )
  lines(ellipse(Sigma,centre=Mu,level=l),col=col.alpha("black",0.2))

## R code 13.10
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 )

## R code 13.11
R <- rlkjcorr( 1e4 , K=2 , eta=2 )
dens( R[,1,2] , xlab="correlation" )

## R code 13.12
m13.1 <- map2stan(
  alist(
    wait ~ dnorm( mu , sigma ),
    mu <- a_cafe[cafe] + b_cafe[cafe]*afternoon,
    c(a_cafe,b_cafe)[cafe] ~ dmvnorm2(c(a,b),sigma_cafe,Rho),
    a ~ dnorm(0,10),
    b ~ dnorm(0,10),
    sigma_cafe ~ dcauchy(0,2),
    sigma ~ dcauchy(0,2),
    Rho ~ dlkjcorr(2)
  ) ,
  data=d ,
  iter=5000 , warmup=2000 , chains=2 )

I should add that there is a warning when I execute the code:

DIAGNOSTIC(S) FROM PARSER:
    Info (non-fatal):
    Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
    If it does, you need to include a target += statement with the log absolute determinant of the Jacobian of the transform.
    Left-hand-side of sampling statement:
        v_a_cafeb_cafe ~ multi_normal(...)

I’ve also tested using the experimental rethinking package, version 1.81.

Here is my session info / makevars info:

R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rethinking_1.81      rstan_2.18.2         StanHeaders_2.18.0-1 ggplot2_3.1.0       

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0         bindr_0.1.1        compiler_3.5.2     pillar_1.3.1       plyr_1.8.4         prettyunits_1.0.2 
 [7] remotes_2.0.2      tools_3.5.2        digest_0.6.18      pkgbuild_1.0.2     pkgload_1.0.2      lattice_0.20-38   
[13] memoise_1.1.0      tibble_2.0.1       gtable_0.2.0       pkgconfig_2.0.2    rlang_0.3.1        cli_1.0.1         
[19] rstudioapi_0.9.0   curl_3.3           yaml_2.2.0         mvtnorm_1.0-8      loo_2.0.0          bindrcpp_0.2.2    
[25] coda_0.19-2        gridExtra_2.3      withr_2.1.2        dplyr_0.7.8        desc_1.2.0         fs_1.2.6          
[31] devtools_2.0.1     stats4_3.5.2       tidyselect_0.2.5   rprojroot_1.3-2    grid_3.5.2         inline_0.3.15     
[37] glue_1.3.0         R6_2.3.0           processx_3.2.1     sessioninfo_1.1.1  purrr_0.2.5        callr_3.1.1       
[43] magrittr_1.5       MASS_7.3-51.1      matrixStats_0.54.0 backports_1.1.3    scales_1.0.0       ps_1.3.0          
[49] usethis_1.4.0      assertthat_0.2.0   colorspace_1.4-0   lazyeval_0.2.1     munsell_0.5.0      crayon_1.3.4 

MakeVars

CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native

Hi everyone, I resolved this issue by removing “-march=native -mtune=native” from my makevars file. I guess this is an R issue, but I gotta post somewhere baby!

1 Like