Cannot run any Stan models in MRO 4.02 using latest RStan

I cannot seem to make Stan (the latest build) work. I fought for a while and RTools seems to work (I have installed RTools 4.0 and can install jsonlite from source a la here).

The forums have been very helpful and I cannot rule out missing something obvious. But I’ve tried the solutions provided here, but the closest problem (#3) doesn’t apply, because my RTools path is “C:\rtools40\usr\bin\make.exe” (from Sys.which("make")).

Here is the code I cannot run (I cannot run even a simple toy model, e.g. y~ rnorm(N, mu, sigma)):

library(rstan)
example(stan_model, run.dontrun = TRUE)

Here is the error message:

Error in compileCode(f, code, language = language, verbose = verbose) : 
  C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file17e050ee2870.o:file17e050ee2870.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D1Ev[_ZN3tbb8internal26task_scheduler_observer_v3D1Ev]+0x14): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file17e050ee2870.o:file17e050ee2870.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1c): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file17e050ee2870.o:file17e050ee2870.cpp:(.text$_ZN4stan4math16ad_tape_observerD1Ev[_ZN4stan4math16ad_tape_observerD1Ev]+0x15): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:/rt
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found
Error in sink(type = "output") : invalid connection

Here is my Makevars.win file:

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

I am hoping I missed the answer somewhere. Somehow, I fixed the problem two nights ago but it has returned and I do not know why.

sapply(c("StanHeaders", "rstan", "inline"), packageVersion)
$StanHeaders
[1]  2 21  0  6
$rstan
[1]  2 21  2
$inline
[1]  0  3 16

I’d appreciate any and all help.

Sincerely,

Josh Malnight

Get rid of the -mtune and -march stuff and if there is still a problem (after installing the latest version of the inline package) show the part of the output after “make would run …”.

The problem remains, sadly.

I seem to have the latest version of inline but just to be safe I reinstalled the latest rstan, StanHeaders, and inline packages. inline is 0.3.16.

I edited the Makevars.win file to be:

9CXX14FLAGS=-O3 
CXX11FLAGS=-O3 

If I understood correctly, the full error message was cut off. Here is all the error output. If I provided the wrong output re: “Make would run…” I apologize.

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) : 
  C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: 
  file3a8c58163908.o:file3a8c58163908.cpp:
  (.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1c): 
 undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
 file3a8c58163908.o:file3a8c58163908.cpp:
(.text$_ZN4stan4math16ad_tape_observerD1Ev[_ZN4stan4math16ad_tape_observerD1Ev]+0x15): 
 undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: 
file3a8c58163908.o:file3a8c58163908.cpp:
(.text$_ZN4stan4math16ad_tape_observerD1Ev[_ZN4stan4math16ad_tape_observerD1Ev]+0x47): 
undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:/rtools40/mingw64/bin/../lib/

In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

Some additional weirdness: rstanarm works but neither rstan nor the rethinking package does. Here is the code I ran:

#
# stan_problems_reprex: Why isn't rstan working?
# 


# Setup rstan parameters --------------------------------------------------

n_chains <- 1
n_iters  <- 1000
n_cores  <- 1


# Fake data parameters ----------------------------------------------------

N_i <- 30
mu_A <- 10
mu_B <- 20
mu_C <- 30

sd_all <- 5

d <- data.frame(
	group = rep(c("A", "B", "C"), each = N_i)
)

set.seed(1991)

d$y[d$group == "A"] <- rnorm(N_i, mu_A, sd_all)
d$y[d$group == "B"] <- rnorm(N_i, mu_B, sd_all)
d$y[d$group == "C"] <- rnorm(N_i, mu_C, sd_all)

plot(density(d$y[d$group == "A"]), col = "blue", 
		 xlim = range(d$y) + c(-10, 10), ylim = c(0, .1),
		 main = "Three normally distributed groups.")
lines(density(d$y[d$group == "B"]), col = "red")
lines(density(d$y[d$group == "C"]), col = "green")
legend("topleft", c("Group A", "Group B", "Group C"), lty = rep(1, 3), 
			 col = c("blue", "red", "green"))

# Basic rstan -------------------------------------------------------------
#
# This is broken with the same error as previously posted.
#
#
library(rstan)

mod_code <- "data {
  int<lower=0> N;
  vector[N] y;
}

// The parameters accepted by the model. Our model
// accepts two parameters 'mu' and 'sigma'.
parameters {
  real mu;
  real<lower=0> sigma;
}

// The model to be estimated. We model the output
// 'y' to be normally distributed with mean 'mu'
// and standard deviation 'sigma'.
model {
  y ~ normal(mu, sigma);
}
"

fit <- stan(model_code = mod_code, data = list(y = d$y, N = nrow(d)),
						chains = n_chains, iter = n_iters, cores = n_cores)

# the Rethinking package --------------------------------------------------
#
# This too returns the following error:
# Error in sink(type = "output") : invalid connection
# Something went wrong, when calling Stan. Check any debug messages for clues, detective.
# nvalid connection
#
library(rethinking)

fit <- map2stan(
	alist(
		y ~ dnorm(mu_0 + mu[group], sigma),
		mu[group] ~ dnorm(0, sigma_group),
		mu_0 ~ dnorm(0, 10),
		sigma_group ~ dcauchy(0, 1),
		sigma ~ dcauchy(0, 1)
	),
	data = list(
		y = d$y,
		group = as.integer(as.factor(d$group))
	),
	iter = n_iters,
	warmup = n_iters/2,
	chains = n_chains, 
	cores = n_cores
)


# The rstanarm package ----------------------------------------------------
# 
# This works somehow!!

library(rstanarm)

fit <- stan_lmer(y ~ -1 + (1|group), data = d,
								 iter = n_iters,
								 warmup = n_iters/2,
								 chains = n_chains, 
								 cores = n_cores)

Do you have the RcppParallel package installed?

I did not, but just installed it. Running the code above produced the same errors for rstan and rethinking but not for rstanarm (in other words, no change).

@bgoodri This is the same R Open problem we saw over in this thread: Collect2.exe: error: ld returned 1 exit status on Windows 10, R 4.0.2

@JMalnight Unfortunately the best workaround quickly available is to use the cmdstanR package for your models. You can also specify that rethinking use the cmdstanR package to estimate models

Fantastic! Thank you both very much. I’m installing CmdStan right now. Unfortunately, your good advice keeps crashing against the rocks of my inexperience/ignorance/computer setup. I’m trying the fixes here but none of them seem to work either and frankly I’m befuddled as to why (I have Rtools40 installed but the basic commands like “mingw32-make --version” error out).

This is driving me crazy (not your fault! I am deeply grateful for all your help this far). As more experienced users, can you recommend a reasonably robust (i.e. not fragile) way for me to use Stan + fast R[^fn-1]? I would rather not try to manually build CmdStan from source if I can help it.

[^fn-1]: I run lots of time-series models and have definitely noticed benefits from using MRO. I’d prefer using CRAN but can’t seem to link the MKL libraries to R 4.0 using even the simple instructions (e.g. here).

I have to use a Windows machine for work and want it to be fast. I’d love to use Stan but right now it’s eating up too much of my time and energy.

OK, anyone who has this problem, follow these steps, stopping before the end if the problem resolves itself:

  1. Cmdstanr with Rtools 4.0 on Windows 10: mingw32-make.exe missing?
  2. Error while installing cmdstanr version 2.24.0

It works!

1 Like