rmcelreath / rethinking

Statistical Rethinking course and book package
2.16k stars 605 forks source link

Invalid class “ulam” object: invalid object for slot "coef" #203

Open evgenyneu opened 4 years ago

evgenyneu commented 4 years ago

Hi, I'm getting an error when running example code 9.12 from Statistical Rethinking 2 (Compiled February 28, 2019). Note that the code ran successfully for the first time, but after that it fails.

library(rethinking)
data(rugged)
d <- rugged
d$log_gdp <- log(d$rgdppc_2000)
dd <- d[ complete.cases(d$rgdppc_2000) , ]
dd$log_gdp_std <- dd$log_gdp / mean(dd$log_gdp)
dd$rugged_std <- dd$rugged / max(dd$rugged)
dd$cid <- ifelse( dd$cont_africa==1 , 1 , 2 )

dat_slim <- list(
    log_gdp_std = dd$log_gdp_std,
    rugged_std = dd$rugged_std,
    cid = as.integer( dd$cid )
)

m9.1 <- ulam(
    alist(
        log_gdp_std ~ dnorm( mu , sigma ) ,
        mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
        a[cid] ~ dnorm( 1 , 0.1 ) ,
        b[cid] ~ dnorm( 0 , 0.3 ) ,
        sigma ~ dexp( 1 )
    ),
    data=dat_slim , chains=1 )

precis( m9.1 , depth=2 )

Here is the output

$ Rscript ex9.12.r 
Loading required package: rstan
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.19.2, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
Loading required package: parallel
Loading required package: dagitty
rethinking (Version 1.93)

Attaching package: ‘rethinking’

The following object is masked from ‘package:stats’:

    rstudent

SAMPLING FOR MODEL 'f3314e777e4c586121dcc9de98266129' NOW (CHAIN 1).
Chain 1: 
Chain 1: Gradient evaluation took 4.1e-05 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.41 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1: 
Chain 1: 
Chain 1: Iteration:   1 / 1000 [  0%]  (Warmup)
[1] "Error in sampler$call_sampler(args_list[[i]]) : "
[2] "  c++ exception (unknown reason)"                
error occurred during calling the sampler; sampling not done
Stan model 'f3314e777e4c586121dcc9de98266129' does not contain samples.
Error in validObject(.Object) : 
  invalid class “ulam” object: invalid object for slot "coef" in class "ulam": got class "NULL", should be or extend class "numeric"
Calls: ulam -> new -> initialize -> initialize -> validObject
Execution halted

I just installed rethinking library today:

library(devtools)
devtools::install_github("evgenyneu/rethinking",ref="Experimental")

I'm on macOS Catalina 10.15.1, R version 3.6.1

Also note, that the following code works every time:

library(rethinking)
rstan_options(auto_write = TRUE)
y <- c(-1,1)
set.seed(11)

m9.3 <- ulam(
    alist(
        y ~ dnorm( mu , sigma ) ,
        mu <- alpha ,
        alpha ~ dnorm( 1 , 10 ) ,
        sigma ~ dexp( 1 )
    ),
    data=list(y=y) , chains=2 )

precis( m9.3 )
ssp3nc3r commented 4 years ago

c++ exception (unknown reason) has been a known issue, and the instructions to fix it is in the first post to the thread at https://discourse.mc-stan.org/t/dealing-with-catalina-ii/11802.

evgenyneu commented 4 years ago

@ssp3nc3r thank you so much, the issue is fixed. I needed to do two things

  1. Install macos-rtools-3.2.0.pkg.

  2. Add the following line to the end of .R/Makevars file:

SHLIB_CXX14LDFLAGS+=-Wl,-rpath,/Library/Frameworks/R.framework/Resources/lib /Library/Frameworks/R.framework/Resources/lib/libc++abi.1.dylib

My full .R/Makevars is this:

# clang: start
CFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CCFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CXXFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CPPFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include

SHLIB_CXXLDFLAGS+=-Wl,-rpath,${R_HOME}/lib ${R_HOME}/lib/libc++abi.1.dylib
SHLIB_CXX14LDFLAGS+=-Wl,-rpath,/Library/Frameworks/R.framework/Resources/lib /Library/Frameworks/R.framework/Resources/lib/libc++abi.1.dylib
# clang: end