Brms/rstan on R 4.0

  1. Kill RStudio.
  2. Make sure the file .Rprofile in your home directory contains exactly this,
if (Sys.getenv("RSTUDIO") == "1" && !nzchar(Sys.getenv("RSTUDIO_TERM")) && 
    Sys.info()["sysname"] == "Darwin") {
  parallel:::setDefaultClusterOptions(setup_strategy = "sequential")
}
  1. Start RStudio

Done, done, and done. Same error. Just shoot me now.

Well, then we better wait for Ben before I get a heart attack ;)

lol. Many thanks again for your kindness and help. Will Ben be aware of this issue?

Does it run fine if you run it without Rstudio? Say, just in the terminal?

One more thing you can try (for troubleshooting): restart RStudio and run this line:

parallel:::setDefaultClusterOptions(setup_strategy = "sequential")

Then start your rstan or brms code.

I just ran it both in terminal and RStudio and it ran both times. I have no idea why. That said, I used a different, simpler model: BRM1 <- brm(weight ~ Diet, data = ChickWeight). I don’t want to get too excited, so I’ll try running the more complex model now.

The more complex model did not run on either RStudio or R.
Error message from R: Compiling the C++ model
Start sampling
Error in makePSOCKcluster(names = spec, …) :
Cluster setup failed. 2 of 2 workers failed to connect.

This is the data for the complex model:

HtWtDataGenerator <- function(nSubj, rndsd = NULL, maleProb = 0.50) {
  # Random height, weight generator for males and females. Uses parameters from
  # Brainard, J. & Burmaster, D. E. (1992). Bivariate distributions for height and
  # weight of men and women in the United States. Risk Analysis, 12(2), 267-275.
  # Kruschke, J. K. (2011). Doing Bayesian data analysis:
  # A Tutorial with R and BUGS. Academic Press / Elsevier.
  # Kruschke, J. K. (2014). Doing Bayesian data analysis, 2nd Edition:
  # A Tutorial with R, JAGS and Stan. Academic Press / Elsevier.
  
  # require(MASS)
  
  # Specify parameters of multivariate normal (MVN) distributions.
  # Men:
  HtMmu   <- 69.18
  HtMsd   <- 2.87
  lnWtMmu <- 5.14
  lnWtMsd <- 0.17
  Mrho    <- 0.42
  Mmean   <- c(HtMmu, lnWtMmu)
  Msigma  <- matrix(c(HtMsd^2, Mrho * HtMsd * lnWtMsd,
                      Mrho * HtMsd * lnWtMsd, lnWtMsd^2), nrow = 2)
  # Women cluster 1:
  HtFmu1   <- 63.11
  HtFsd1   <- 2.76
  lnWtFmu1 <- 5.06
  lnWtFsd1 <- 0.24
  Frho1    <- 0.41
  prop1    <- 0.46
  Fmean1   <- c(HtFmu1, lnWtFmu1)
  Fsigma1  <- matrix(c(HtFsd1^2, Frho1 * HtFsd1 * lnWtFsd1,
                       Frho1 * HtFsd1 * lnWtFsd1, lnWtFsd1^2), nrow = 2)
  # Women cluster 2:
  HtFmu2   <- 64.36
  HtFsd2   <- 2.49
  lnWtFmu2 <- 4.86
  lnWtFsd2 <- 0.14
  Frho2    <- 0.44
  prop2    <- 1 - prop1
  Fmean2   <- c(HtFmu2, lnWtFmu2)
  Fsigma2  <- matrix(c(HtFsd2^2, Frho2 * HtFsd2 * lnWtFsd2,
                       Frho2 * HtFsd2 * lnWtFsd2, lnWtFsd2^2), nrow = 2)
  
  # Randomly generate data values from those MVN distributions.
  if (!is.null(rndsd)) {set.seed(rndsd)}
  datamatrix <- matrix(0, nrow = nSubj, ncol = 3)
  colnames(datamatrix) <- c("male", "height", "weight")
  maleval <- 1; femaleval <- 0 # arbitrary coding values
  for (i in 1:nSubj)  {
    # Flip coin to decide sex
    sex <- sample(c(maleval, femaleval), size = 1, replace = TRUE,
                  prob = c(maleProb, 1 - maleProb))
    if (sex == maleval) {datum = MASS::mvrnorm(n = 1, mu = Mmean, Sigma = Msigma)}
    if (sex == femaleval) {
      Fclust = sample(c(1, 2), size = 1, replace = TRUE, prob = c(prop1, prop2))
      if (Fclust == 1) {datum = MASS::mvrnorm(n = 1, mu = Fmean1, Sigma = Fsigma1)}
      if (Fclust == 2) {datum = MASS::mvrnorm(n = 1, mu = Fmean2, Sigma = Fsigma2)}
    }
    datamatrix[i, ] = c(sex, round(c(datum[1], exp(datum[2])), 1))
  }
  
  return(datamatrix)
} # end function

And this is the code:

d <-
HtWtDataGenerator(nSubj = 57, maleProb = .5) %>%
as_tibble()

fit1 <-
brm(data = d,
family = gaussian,
weight ~ 1 + height,
prior = c(prior(normal(0, 100), class = Intercept),
prior(normal(0, 100), class = b),
prior(cauchy(0, 10), class = sigma)),
chains = 4, cores = 2, iter = 2000, warmup = 1000,
seed = 2)

Sorry that it didn’t work. I fear you’ll have to wait for @bgoodri to have a look - I don’t know what else you could try.

cores = 1?

Stab in the dark: could it be a problem with your Xcode installation, not R 4.0

1 Like

Today is a Day of Days!! The complex model ran without problem.
I did two things differently:
I changed ‘cores = 2’ to ‘cores = 1’ (not sure what the difference is…) and in Xcode (my version, at least) I clicked on brms in the project panel (which was hidden until I looked for it).
Thank you all so much for your help, patience, and guidance - very much appreciated!!

1 Like

The parentheses seem to be wrong. The second ‘)’ after RSTUDIO_TERM") and the ‘)’ after “Darwin” have no matching ‘(’. No?

@gferraz you mean this?

if (Sys.getenv("RSTUDIO") == "1" && !nzchar(Sys.getenv("RSTUDIO_TERM")) && 
    Sys.info()["sysname"] == "Darwin" ) {
  parallel:::setDefaultClusterOptions(setup_strategy = "sequential")
}

No, I meant this:

if (Sys.getenv(“RSTUDIO”) == “1” && RSTUDIO_TERM")) &&
Sys.info()[“sysname”] == “Darwin”) {
parallel:::setDefaultClusterOptions(setup_strategy = “sequential”)
}

I am having the same problem as bh7

Hmm, this is what I have in my ~/.Rprofile

if (Sys.getenv("RSTUDIO") == "1" && !nzchar(Sys.getenv("RSTUDIO_TERM")) && 
    Sys.info()["sysname"] == "Darwin" ) {
  parallel:::setDefaultClusterOptions(setup_strategy = "sequential")
}

Yes, I see. That is the first bit of code you recommended and all the brackets are matching here. But it didn’t work out for me. And I am positive that I put it on .Rprofile in my home folder, and re-started RStudio. I still get the error message:

Error in unserialize(node$con) : error reading from connection
Calls: -> slaveLoop -> makeSOCKmaster
Execution halted
Error in unserialize(node$con) : error reading from connection
Calls: -> slaveLoop -> makeSOCKmaster
Execution halted
Error in makePSOCKcluster(names = spec, …) :
Cluster setup failed. 2 of 2 workers failed to connect.

If you open a terminal on your computer (search for Terminal in Spotlight on OS X) and cut and paste this (press return afterwards):

cat " ## WORKAROUND: https://github.com/rstudio/rstudio/issues/6692
## Revert to 'sequential' setup of PSOCK cluster in RStudio Console on macOS and R 4.0.0
if (Sys.getenv("RSTUDIO") == "1" && RSTUDIO_TERM")) &&
    Sys.info()["sysname"] == "Darwin" ) {
  parallel:::setDefaultClusterOptions(setup_strategy = "sequential")
} " >> ~/.Rprofile

then it should, for sure, put the above text in ~/.Rprofile. Then restart RStudio.