stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.04k stars 269 forks source link

Error reading from connection when controls are added to the model #953

Open maxwaldo opened 3 years ago

maxwaldo commented 3 years ago

Summary:

The issue arise when I try to add control in a model. While I can have rstan up and running, when I try to increase the delta with the command "control=list(adapt_delta=0.9)" it throws the error: "Error in unserialize(socklist[[n]]) : error reading from connection". I was wondering whether there might be an issue when pasting the controls to the model.

Description:

I have a Stan model that returns some divergence - 3 for 2000 iterations in four chains. I already used non-centric parametrisation but the only way to get ride of these divergence is to slightly increase the step size for the NUTS algorithm. While there was no issue to do so in the past, it was recently impossible to do. I install rstan on my Mac and my Window computer and the issues are persistent in both cases.

To be clear: the model runs as long as I don't specify the "control" parameter. If I specify a control parameter with an empty list the model still runs. As long as I try to specify any specifics in the control parameter - adapt_delta, max_treedepth, etc. - the sampling is not done and the error "Error in unserialize(socklist[[n]]) : error reading from connection". This issue did not exist in the past and is persistent across operating systems (Mac and windows).

Reproducible Steps:

Here is an example of the "eight school model"

model_code <- "
  data {
    int<lower=0> J;          // number of schools 
    real y[J];               // estimated treatment effects
    real<lower=0> sigma[J];  // s.e. of effect estimates 
  }
  parameters {
    real mu; 
    real<lower=0> tau;
    vector[J] eta;
  }
  transformed parameters {
    vector[J] theta;
    theta = mu + tau * eta;
  }
  model {
    target += normal_lpdf(eta | 0, 1);
    target += normal_lpdf(y | theta, sigma);
  }

"

model.data <- list(J = 8,
                   y = c(28,  8, -3,  7, -1,  1, 18, 12),
                   sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

fit.working <- stan(
  model_code =model_code,  
  data = model.data,
  chains = 4,   
  warmup = 1000,  
  iter = 2000, 
  cores = 4
)

## Here the model runs.
# Message of the outputs: 
# Warning messages:
# 1: There were 2 divergent transitions after warmup. See
# http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
# to find out why this is a problem and how to eliminate them. 
# 2: Examine the pairs() plot to diagnose sampling problems

fit.notworking <- stan(
  model_code =model_code,  
  data = model.data,
  chains = 4,   
  warmup = 1000,  
  iter = 2000, 
  cores = 4,   
  control=list(adapt_delta=0.9) ## Or any otehr control
)

## Here the model does not run:
# Meassage output: 
# SAMPLING FOR MODEL '8e0b427f57af3571c0dcc2e3d408b556' NOW (CHAIN 1).
# Error in unserialize(socklist[[n]]) : error reading from connection
# 
# SAMPLING FOR MODEL '8e0b427f57af3571c0dcc2e3d408b556' NOW (CHAIN 2).
# 
# SAMPLING FOR MODEL '8e0b427f57af3571c0dcc2e3d408b556' NOW (CHAIN 3).

fit.also.working <- stan(
  model_code =model_code,  
  data = model.data,
  chains = 4,   
  warmup = 1000,  
  iter = 2000, 
  cores = 4,
  control=list()
)

## Here the model also runs
# Output message
# Warning messages:
# 1: There were 3 divergent transitions after warmup. See
# http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
# to find out why this is a problem and how to eliminate them. 
# 2: Examine the pairs() plot to diagnose sampling problems

RStan Version:

packageVersion("rstan")
[1] ‘2.21.2’

R Version:

R 4.1.0

Operating System:

OS X 11.4 but also on a window computer.

terryschulz-sys commented 3 years ago

Hi max waldo, You are spot on. I am having the same problem with Bayesian Models code 10-26. Adding errors in measurements to the code and controls to minimize them results in the same error. Have same R and RStan versions and Big Sur 11.5.2. I suspect RStan is the culprit, because I ran the same code with introduced measurement errors and controls some time ago and got nearly the same results given in the book. Thanks for your input. As a rank amateur I began to think I had made one or more code errors. Is there a way to obtain the previous RStan package? Never had a problem with it.

terryschulz-sys commented 3 years ago

I stumbled onto a solution. I went to Stan home page , click on quick start guide, go down page to "If you are using R version 3.x on a Mac, go to here and click it (trust me!) Follow instructions to install RStan from source and all down to and including install.packages("rstan", type = "source" ) . After pages of screen output you have rstan version 2.21.2 that works with control. I assume you already have a C++ tool chain installed. I have C++ 12.0 from command line tools. Cheers.