Unable to install rstan_2.19.2 from CRAN

I attempted to install rstan 2.19.2 on 64 bit Windows 10 from CRAN - the install fails (details below). I don’t see a 2.19.2 version of StanHeaders on CRAN - is this the cause?

install.packages(“rstan”, repos = “https://cloud.r-project.org/”, dependencies = TRUE)

There is a binary version available but the source version is later:
binary source needs_compilation
rstan 2.18.2 2.19.2 TRUE

The install fails as below:

… In file included from C:/Program Files/R/R-3.6.1/library/StanHeaders/include/src/stan/lang/generator/generate_function_instantiation.hpp:6:0,
from C:/Program Files/R/R-3.6.1/library/StanHeaders/include/src/stan/lang/generator/generate_function_instantiations.hpp:5,
from C:/Program Files/R/R-3.6.1/library/StanHeaders/include/src/stan/lang/generator/generate_standalone_functions.hpp:7,
from C:/Program Files/R/R-3.6.1/library/StanHeaders/include/src/stan/lang/compile_functions.hpp:4,
from stanc.cpp:23:
C:/Program Files/R/R-3.6.1/library/StanHeaders/include/src/stan/lang/generator/generate_function_instantiation_body.hpp: In function ‘void stan::lang::generate_function_instantiation_body(const stan::lang::function_decl_def&, const std::vector<std::basic_string >&, bool, bool, bool, const string&, std::ostream&)’:
C:/Program Files/R/R-3.6.1/library/StanHeaders/include/src/stan/lang/generator/generate_function_instantiation_body.hpp:44:45: error: range-based ‘for’ loops are not allowed in C++98 mode
for (const std::string& namespace_i : namespaces) {
^
make: *** [C:/PROGRA~1/R/R-3.6.1/etc/x64/Makeconf:215: stanc.o] Error 1
ERROR: compilation failed for package ‘rstan’

  • removing ‘C:/Program Files/R/R-3.6.1/library/rstan’
    Warning in install.packages :
    installation of package ‘rstan’ had non-zero exit status

Operating System: Windows 10
Interface Version: rstan
Compiler/Toolkit: Rtools version 3.5.0.4

1 Like

I would install the binary version via

install.packages("https://win-builder.r-project.org/rTkumr03G8UM/rstan_2.19.1.zip", repos = NULL)

to install from source, you would have to get the configuration right

What version of StanHeaders is required? The version on CRAN is 2.18.1-10.

The ‘Depends’ for rstan 2.19.1 is given as ‘Depends: R (>= 3.4.0), StanHeaders (> 2.18.1)’

I’m able to load rstan, but running the blocker example generates a new error:

fit <- stan(file = ‘blocker.stan’, data = blocker_dat)
Error in new_CppObject_xp(fields$.module, fields$.pointer, …) :
Exception: variable does not exist; processing stage=data initialization; variable name=N; base type=int (in ‘model404c329a55fe_blocker’ at line 4)

failed to create the sampler; sampling not done

That StanHeaders is fine. What is names(blocker_dat)?

library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())

“rt” <-
c(3, 7, 5, 102, 28, 4, 98, 60, 25, 138, 64, 45, 9, 57, 25, 33,
28, 8, 6, 32, 27, 22)
“nt” <-
c(38, 114, 69, 1533, 355, 59, 945, 632, 278, 1916, 873, 263,
291, 858, 154, 207, 251, 151, 174, 209, 391, 680)
“rc” <-
c(3, 14, 11, 127, 27, 6, 152, 48, 37, 188, 52, 47, 16, 45, 31,
38, 12, 6, 3, 40, 43, 39)
“nc” <-

c(39, 116, 93, 1520, 365, 52, 939, 471, 282, 1921, 583, 266,
293, 883, 147, 213, 122, 154, 134, 218, 364, 674)
“N” <-
22

blocker_dat <- list(“rt”, “nt”, “rc”, “nc”, “N”)
fit <- stan(file = ‘blocker.stan’, data = blocker_dat)
print(fit)

Specifying the data by character vector appears to have gotten broken when we fixed the thing where people were using i as a symbol name in the data block. You can get it to work if you instead do

blocker_dat <- list(rt = rt, nt = nt, rc = rc, nc = nc, N = N)

For me the solution was to modify Makevars.win, adding $(BINPREF) to CXX14= generated by standard rstan installation instruction. My Makevars.win looks like this:

CXX14=$(BINPREF)g++ -O2 -march=native -mtune=native

CXX14FLAGS=-O3 -march=native
CXX14=$(BINPREF)g++ -m$(WIN) -std=c++1y
CXX11FLAGS=-O3 -march=native

Without the proper compiler flags I was getting all kind of compiler errors, like you reported.

Thank you. The rstan source on CRAN (version 2.19.2 ) compiles without error for me using your Makevars.win settings.

version R version 3.6.1 (2019-07-05)

os Windows 10 x64

system x86_64, mingw32

ui RStudio

1 Like

I use the above Makevars.win above and finally got it to make progress toward compiling – just a few warnings like:

note: #pragma message: This header is deprecated. Use <boost/integer/integer_log2.hpp> instead.
BOOST_HEADER_DEPRECATED("<boost/integer/integer_log2.hpp>");

But I still ended up exiting with Error 1:

C:\Users\david\AppData\Local\Temp\ccPAHol3.s: Assembler messages:
C:\Users\david\AppData\Local\Temp\ccPAHol3.s:22629: Error: invalid register for .seh_savexmm
C:\Users\david\AppData\Local\Temp\ccPAHol3.s:22631: Error: invalid register for .seh_savexmm
C:\Users\david\AppData\Local\Temp\ccPAHol3.s:22633: Error: invalid register for .seh_savexmm
C:\Users\david\AppData\Local\Temp\ccPAHol3.s:22635: Error: invalid register for .seh_savexmm
C:\Users\david\AppData\Local\Temp\ccPAHol3.s:22637: Error: invalid register for .seh_savexmm
C:\Users\david\AppData\Local\Temp\ccPAHol3.s:25575: Error: invalid register for .seh_savexmm
make: *** [C:/PROGRA~1/R/R-36~1.1/etc/x64/Makeconf:215: chains.o] Error 1
ERROR: compilation failed for package ‘rstan’

  • removing ‘C:/Users/david/Documents/R/win-library/3.6/rstan’
  • restoring previous ‘C:/Users/david/Documents/R/win-library/3.6/rstan’
    Warning in install.packages :
    installation of package ‘rstan’ had non-zero exit status

Here is my sessionInfo()

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached):
[1] compiler_3.6.1 tools_3.6.1 packrat_0.5.0

Judging from the following link, leaving out -march=native from CXX14FLAGS and CXX11FLAGS may help: Compiler Problem (rstan, Win10, 64)

1 Like

Thank you - jjramsey’s suggestion to leave off -march=native from CXX14FLAGS and CXX11FLAGS did the trick.

Everyone should be able to install the binaries for 2.19.2 now:

install.packages("rstan", repos = "https://cran.rstudio.com")

Even with the new binaries 2.19.2, adding -march=native to the CXX14 and CXX11 flags throws an error when I compile 8schools.stan as a test. Perhaps it’s my processor? i7-7820X Skylake (8 cores).

We don’t have a list of working vs. broken processors, but if yours is not working, then omit the -march=native, -mtune=native.

Thanks - I think it’s a problem with g++ in rtools, which apparently predates skylake. Following information found here:

I learned that g++ thinks my processor is a broadwell. Specifying -march=broadwell -mtune=broadwell works, but -march=native -mtune=native does not. Eventually, I will test whether there is a difference in execution time of the code it compiles when the compiler march and mtune flags are left off (and presumably default to a generic option).