Comments (12)
Thanks for the report. This happens only when the model definitions are the same? If so, maybe related to how Stan reuses the compiled model binary.
from rethinking.
A little experimentation later, still not sure what is causing the problem. But using the previous map2stan result seems to bypass it:
m2 <- map2stan(
alist(
pulled_left ~ dbinom(1,theta),
logit(theta) <- a + bp*prosoc_left + bpc*condition*prosoc_left ,
a ~ dnorm(0,10),
bp ~ dnorm(0,10),
bpc ~ dnorm(0,10)
) ,
data=d, chains=1, cores=1 )
m2_2 <- map2stan( m2 ,data=d, chains=1, cores=1 )
Hopefully that's a useful workaround for others, until I can figure out the core issue.
from rethinking.
I think you are correct that stan reusing the compiled model leads to the failure. If you delete the relevant files from the tmp directory, the same model will run twice.
library(rethinking)
data(chimpanzees)
# don't want any variables with NAs
d <- list(
pulled_left = chimpanzees$pulled_left ,
prosoc_left = chimpanzees$prosoc_left ,
condition = chimpanzees$condition ,
actor = as.integer( chimpanzees$actor ) ,
blockid = as.integer( chimpanzees$block )
)
# RStan fit
m2 <- map2stan(
alist(
pulled_left ~ dbinom(1,theta),
logit(theta) <- a + bp*prosoc_left + bpc*condition*prosoc_left ,
a ~ dnorm(0,10),
bp ~ dnorm(0,10),
bpc ~ dnorm(0,10)
) ,
data=d, chains=1, cores=1 )
# Clean up tmpdir ########
tmp_dir <- tempdir()
model_code <- m2@stanfit@stanmodel@model_code
model_name2 <- attr(model_code, "model_name2")
unlink(paste0(tmp_dir, "/", model_name2, c(".rds", ".stan")))
# Seems to work ok without removing the dso, but just in case
dso_name <- m2@stanfit@stanmodel@dso@dso_filename
unlink(paste0(tmp_dir, "/", model_name2, c(".cpp", ".o", ".so")))
m2_2 <- map2stan(
alist(
pulled_left ~ dbinom(1,theta),
logit(theta) <- a + bp*prosoc_left + bpc*condition*prosoc_left ,
a ~ dnorm(0,10),
bp ~ dnorm(0,10),
bpc ~ dnorm(0,10)
) ,
data=d, chains=1, cores=1 )
from rethinking.
I haven't had any luck figuring out the root cause. I thought perhaps it was a mismatch between the model name that map2stan uses and the model name internal to the stanfit object and module. But making those identical didn't change anything.
It's perhaps related to this general bug with stan, in which it keeps links to removed objects:
https://groups.google.com/forum/#!category-topic/stan-users/general/Bg8HhyB6qG4
from rethinking.
I can reliably prevent the segfault by simply making a copy of the stanfit object in the global environment:
library(rethinking)
flist <- alist(
y ~ dnorm(mu,1),
mu <- a + b*x,
a ~ dnorm(0,10),
b ~ dnorm(0,1)
)
set.seed(1)
x <- rnorm(100)
y <- rnorm(100,-0.1*x,1)
d <- list(x=x,y=y)
m1 <- map2stan( flist , data=d , chains=1 )
fit <- m1@stanfit
m2 <- map2stan( flist , data=d , chains=1 )
As soon as the fit
copy is removed, it'll segfault again:
rm(fit)
m2 <- map2stan( flist , data=d , chains=1 )
No idea what this means.
from rethinking.
Not surprising, because it's a Windows fix, but this commit stan-dev/rstan@6a61721 does not fix the crash on OS X.
Your code above leads to crash with recompiled rstan from the develop tree:
devtools::install_github("stan-dev/rstan", ref = "develop", subdir = "rstan/rstan", dependencies = TRUE)
from rethinking.
I've been working through the excellent Rethinking book and have encountered the same problem.
Everything worked well until I got to the map2stan examples in Chapter 10 and then R started to crash whenever I run a given map2stan more than once. (When I call map2stan with a 'map' model as the input R crashes the first time I run the code.)
(Also, as an aside, the function logistic doesn't seem to exist in the rethinking package. Or at least I can't find it.)
from rethinking.
I just pushed to the Experimental branch a solution to this issue. It's not a long-term solution, because it prevents Stan from reusing the compiled model. It forces recompilation. But it does stop the segfaults.
https://github.com/rmcelreath/rethinking/tree/Experimental
from rethinking.
from rethinking.
Works well. I know it's not an ideal fix, but for me, recompiling models is less aggravating than starting over completely after a segfault.
from rethinking.
Experimental branch work-around is working for people:
library(devtools)
install_github( "rmcelreath/rethinking" , ref="Experimental" )
from rethinking.
Kinda fixed for now, through workaround. Closing the issue, but still worrying about the underlying cause.
from rethinking.
Related Issues (20)
- 2nd edition errata: p. 445, discussion of Fig. 14.5 describes 1st edition Fig. 13.5
- Feature request: Add a template for shifted lognormal distribution HOT 2
- Does the rethinking package work using the newest version of R - 4.2.2? HOT 1
- Nevermind HOT 2
- Eliminating variable breaks ulam model HOT 2
- Smooth functions of a variable - modeling syntax HOT 2
- Problem with R code 9.14 on Mac OS
- problems loading rethinking HOT 1
- Zero Inflated Negative Binomial Model HOT 1
- difference in n_eff of population mean depending on model specification HOT 3
- Comparison length > 1 in `cv_data_slice`.
- character limit in translating to stan with ulam()? HOT 3
- Problem with defining imputed variable as a vector in generated stan code HOT 2
- link() not handling id variable values not in original data HOT 2
- Gamma parameterisation
- sim() from model with missing value imputation breaks with error "function merge_missing not found" HOT 2
- 'reject' stan command conflicts with UCBadmit data
- Chapter 16 Lynx Hare Model Change with Stan 2.33.0? HOT 10
- How to construct a spline model using ulam() HOT 4
- cannot install rethinking HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rethinking.