Error caused by missing stan header

Hi all! I’m pretty new to Stan, but the error I’m getting doesn’t seem to be related to my model.

When I tried to do a test run of my model, I got this error output:

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_log.hpp:5:0,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat.hpp:278,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:12,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file399019ac55df.cpp:14:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_lpmf.hpp: In function ‘stan::return_type_t<T1, T2> stan::math::dirichlet_lpmf(const T_prob&, const T_prior_size&)’:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_l
In addition: Warning messages:
1: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/Rtools/mingw_/bin/g++’ not found
2: In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-3.6.3/bin/x64/R CMD SHLIB file399019ac55df.cpp 2> file399019ac55df.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

I’m not really sure what this is referring to. I’ve tried updating all my libraries and I’m currently using R 3.6.3.

Welcome to the community! I don’t have Windows myself but I know someone who I’m sure will be able to help you out @andrjohns

This here seems to definitely be a problem:

‘C:/Rtools/mingw_/bin/g++’ not found?

3 Likes

Thanks @torkar!

ybordag, can you try running:

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

And post any lines that start with error:?

I don’t think I get any error lines, but I do get a few warnings, I still see the following warnings though:

Warning messages:
1: In find.package(package, lib.loc, verbose = verbose) :
package ‘base’ found more than once, using the first from
“C:/PROGRA~1/R/R-3.6.3/library/base”,
“C:/Program Files/R/R-3.6.3/library/base”
2: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/Rtools/mingw_/bin/g++’ not found

and the sampling apparently ran successfully.

This part of the log seems to be related to the issue I had before:

Compilation argument:
C:/PROGRA~1/R/R-3.6.3/bin/x64/R CMD SHLIB filef241dfcb3f.cpp 2> filef241dfcb3f.cpp.err.txt
c:/Rtools/mingw_64/bin/g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-3.6.3/include" -DNDEBUG -I"C:/Users/ybord/Documents/R/win-library/3.6/Rcpp/include/" -I"C:/Users/ybord/Documents/R/win-library/3.6/RcppEigen/include/" -I"C:/Users/ybord/Documents/R/win-library/3.6/RcppEigen/include/unsupported" -I"C:/Users/ybord/Documents/R/win-library/3.6/BH/include" -I"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/src/" -I"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/" -I"C:/Users/ybord/Documents/R/win-library/3.6/RcppParallel/include/" -I"C:/Users/ybord/Documents/R/win-library/3.6/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DBOOST_NO_AUTO_PTR -include “C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp” -std=c++1y -O2 -Wall -mtune=core2 -c filef241dfcb3f.cpp -o filef241dfcb3f.o
c:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o filef241dfcb3f.dll tmp.def filef241dfcb3f.o C:/Users/ybord/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a -LC:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/libs/x64 -lStanHeaders -LC:/Users/ybord/Documents/R/win-library/3.6/RcppParallel/lib/x64 -ltbb -LC:/PROGRA~1/R/R-3.6.3/bin/x64 -lR
In file included from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_log.hpp:5:0,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat.hpp:278,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:12,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/src/stan/model/model_header.hpp:4,
from filef241dfcb3f.cpp:14:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_lpmf.hpp: In function ‘stan::return_type_t<T1, T2> stan::math::dirichlet_lpmf(const T_prob&, const T_prior_size&)’:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_lpmf.hpp:60:74: warning: typedef ‘using T_partials_vec = struct Eigen::Matrix<typename stan::partials_return_type<T_prob, T_prior_size>::type, -1, 1>’ locally defined but not used [-Wunused-local-typedefs]
using T_partials_vec = typename Eigen::Matrix<T_partials_return, -1, 1>;
^
In file included from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/poisson_log_glm_log.hpp:5:0,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat.hpp:328,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:12,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/src/stan/model/model_header.hpp:4,
from filef241dfcb3f.cpp:14:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/poisson_log_glm_lpmf.hpp: In function ‘stan::return_type_t<T_x, T_alpha, T_beta> stan::math::poisson_log_glm_lpmf(const T_y&, const T_x&, const T_alpha&, const T_beta&)’:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/poisson_log_glm_lpmf.hpp:55:34: warning: typedef ‘using T_alpha_val = std::conditional_t<stan::is_vector::value, Eigen::Array<typename stan::partials_return_type<T_alpha>::type, -1, 1>, typename stan::partials_return_type<T_alpha>::type>’ locally defined but not used [-Wunused-local-typedefs]
partials_return_t<T_alpha>>;
^
In file included from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:51:0,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/src/stan/model/model_header.hpp:4,
from filef241dfcb3f.cpp:14:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat/fun/squared_distance.hpp: In static member function ‘static double stan::math::internal::squared_distance_vv_vari::var_squared_distance(const Eigen::Matrix<stan::math::var, R, C>&, const Eigen::Matrix<stan::math::var, R2, C2, (AutoAlign | (((R2 == 1) && (C2 != 1)) ? RowMajor : (((C2 == 1) && (R2 != 1)) ? ColMajor : ColMajor))), R2, C2>&)’:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat/fun/squared_distance.hpp:27:54: warning: typedef ‘using idx_t = using type = Eigen::EigenBase<Eigen::Matrix<stan::math::var, -1, -1> >::Index’ locally defined but not used [-Wunused-local-typedefs]
using idx_t = typename index_type<matrix_v>::type;
^
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat/fun/squared_distance.hpp: In static member function ‘static double stan::math::internal::squared_distance_vd_vari::var_squared_distance(const Eigen::Matrix<stan::math::var, R, C>&, const Eigen::Matrix<double, R2, C2>&)’:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat/fun/squared_distance.hpp:64:54: warning: typedef ‘using idx_t = using type = Eigen::EigenBase<Eigen::Matrix<double, -1, -1> >::Index’ locally defined but not used [-Wunused-local-typedefs]
using idx_t = typename index_type<matrix_d>::type;
^
filef241dfcb3f.cpp: In member function ‘virtual void modelf24739d3ddc_73fc79f8b1915e8208c736914c86d1a1_namespace::modelf24739d3ddc_73fc79f8b1915e8208c736914c86d1a1::transform_inits(const stan::io::var_context&, std::vector&, std::vector&, std::ostream*) const’:
filef241dfcb3f.cpp:108:24: warning: typedef ‘local_scalar_t__’ locally defined but not used [-Wunused-local-typedefs]
typedef double local_scalar_t__;
^
In file included from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/core.hpp:46:0,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:6,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/src/stan/model/model_header.hpp:4,
from filef241dfcb3f.cpp:14:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp: At global scope:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:13: warning: ‘void stan::math::set_zero_all_adjoints()’ defined but not used [-Wunused-function]
static void set_zero_all_adjoints()

Those are all compiler warnings, rather than actual errors. We need to add compiler flags to turn them off so that only the error is reported.

Can you run:

cat("CXX14FLAGS += -Wno-unused-local-typedefs -Wno-unused-function",file = "~/.R/Makevars.win", sep = "\n", append = TRUE)

And then re-run your model?

I did not have a makevars file to begin with so I created one. After the flag was added, I still got the same error when I ran my code:

test = stan(“C:/Users/ybord/Documents/School/2020-2021/Covid-19 Project/Code/seir-reopening-all-fit/SIER_Reopening_All_fit.stan”,
data = stan_d,
pars = params_monitor,
chains = 1, iter = 10)
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_log.hpp:5:0,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat.hpp:278,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:12,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file389444f83ccd.cpp:14:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_lpmf.hpp: In function ‘stan::return_type_t<T1, T2> stan::math::dirichlet_lpmf(const T_prob&, const T_prior_size&)’:
C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_l
In addition: Warning messages:
1: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/Rtools/mingw_/bin/g++’ not found
2: In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-3.6.3/bin/x64/R CMD SHLIB file389444f83ccd.cpp 2> file389444f83ccd.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

This might be related to issues we’re currently having with the inline package on windows. Can you try installing the following patched version and re-rerunning your model?

install.packages("https://github.com/bgoodri/inline/archive/master.tar.gz", 
                 repos = NULL, type = "source")

Also, can you run:

readLines("~/.R/Makevars.win")

And post the output?

I installed the package and reran everything

test = stan(“C:/Users/ybord/Documents/School/2020-2021/Covid-19 Project/Code/seir-reopening-all-fit/SIER_Reopening_All_fit.stan”,
data = stan_d,
pars = params_monitor,
chains = 1, iter = 10)

make cmd is
make -f “C:/PROGRA~1/R/R-3.6.3/etc/x64/Makeconf” -f “C:/PROGRA~1/R/R-3.6.3/share/make/winshlib.mk” -f “C:\Users\ybord\AppData\Local\Temp\RtmpILLCgg\Makevars41ec18d23d79” CXX=‘(CXX11) (CXX11STD)’ CXXFLAGS=‘(CXX11FLAGS)' CXXPICFLAGS='(CXX11PICFLAGS)’ SHLIB_LDFLAGS=‘(SHLIB_CXX11LDFLAGS)' SHLIB_LD='(SHLIB_CXX11LD)’ SHLIB=“file41ec4ce47ee.dll” WIN=64 TCLBIN=64 OBJECTS=“file41ec4ce47ee.o”

make would use
c:/Rtools/mingw_64/bin/g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-3.6.3/include" -DNDEBUG -I"C:/Users/ybord/Documents/R/win-library/3.6/Rcpp/include/" -I"C:/Users/ybord/Documents/R/win-library/3.6/RcppEigen/include/" -I"C:/Users/ybord/Documents/R/win-library/3.6/RcppEigen/include/unsupported" -I"C:/Users/ybord/Documents/R/win-library/3.6/BH/include" -I"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/src/" -I"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/" -I"C:/Users/ybord/Documents/R/win-library/3.6/RcppParallel/include/" -I"C:/Users/ybord/Documents/R/win-library/3.6/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DBOOST_NO_AUTO_PTR -include “C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp” -std=c++1y -O2 -Wall -mtune=core2 -c file41ec4ce47ee.cpp -o file41ec4ce47ee.o
if test “zfile41ec4ce47ee.o” != “z”; then
if test -e “file41ec4ce47ee-win.def”; then
echo c:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o file41ec4ce47ee.dll file41ec4ce47ee-win.def file41ec4ce47ee.o “C:/Users/ybord/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a” -L"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/ybord/Documents/R/win-library/3.6/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-3.6.3/bin/x64" -lR ;
c:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o file41ec4ce47ee.dll file41ec4ce47ee-win.def file41ec4ce47ee.o “C:/Users/ybord/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a” -L"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/ybord/Documents/R/win-library/3.6/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-3.6.3/bin/x64" -lR ;
else
echo EXPORTS > tmp.def;
c:/Rtools/mingw_64/bin/nm file41ec4ce47ee.o | sed -n ‘s/^.* [BCDRT] / /p’ | sed -e ‘/[.]refptr[.]/d’ -e ‘/[.]weak[.]/d’ | sed ‘s/[^ ][^ ]*/“&”/g’ >> tmp.def;
echo c:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o file41ec4ce47ee.dll tmp.def file41ec4ce47ee.o “C:/Users/ybord/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a” -L"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/ybord/Documents/R/win-library/3.6/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-3.6.3/bin/x64" -lR ;
c:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o file41ec4ce47ee.dll tmp.def file41ec4ce47ee.o “C:/Users/ybord/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a” -L"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/ybord/Documents/R/win-library/3.6/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-3.6.3/bin/x64" -lR ;
rm -f tmp.def;
fi
fi
Error in compileCode(f, code, language = language, verbose = verbose) :
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/src/stan/model/model_header.hpp:4, from file41ec4ce47ee.cpp:14:C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:13: warning: ‘void stan::math::set_zero_all_adjoints()’ defined but not used [-Wunused-function] static void set_zero_all_adjoints() { ^make: *** [C:/PROGRA~1/R/R-3.6.3/etc/x64/Makeconf:215: file41ec4ce47ee.o] Error 1
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/Rtools/mingw_/bin/g++’ not found
Error in sink(type = “output”) : invalid connection

And this is my output for the Makevars.win

readLines(“~/.R/Makevars.win”)
[1] “CXX14FLAGS += -Wno-unused-local-typedefs -Wno-unused-function”

Can you change the Makevars file to:

CXX14FLAGS = -Wno-unused-local-typedefs -Wno-unused-function

(i.e. remove the + operator) and run again?

If that makes no change, can you try running:

fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , 'return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;' )

And post the result?

I first tried to change the makevars file, but I got the same output

I then ran your next suggestion and still got the following output

make cmd is
make -f “C:/PROGRA~1/R/R-3.6.3/etc/x64/Makeconf” -f “C:/PROGRA~1/R/R-3.6.3/share/make/winshlib.mk” -f “C:\Users\ybord\AppData\Local\Temp\RtmpILLCgg\Makevars41ec19276a25” CXX=’(CXX11) (CXX11STD)’ CXXFLAGS=’(CXX11FLAGS)' CXXPICFLAGS='(CXX11PICFLAGS)’ SHLIB_LDFLAGS=’(SHLIB_CXX11LDFLAGS)' SHLIB_LD='(SHLIB_CXX11LD)’ SHLIB=“file41ec584834e8.dll” WIN=64 TCLBIN=64 OBJECTS=“file41ec584834e8.o”

make would use
c:/Rtools/mingw_64/bin/g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-3.6.3/include" -DNDEBUG -I"C:/Users/ybord/Documents/R/win-library/3.6/Rcpp/include/" -I"C:/Users/ybord/Documents/R/win-library/3.6/RcppEigen/include/" -I"C:/Users/ybord/Documents/R/win-library/3.6/RcppEigen/include/unsupported" -I"C:/Users/ybord/Documents/R/win-library/3.6/BH/include" -I"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/src/" -I"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/" -I"C:/Users/ybord/Documents/R/win-library/3.6/RcppParallel/include/" -I"C:/Users/ybord/Documents/R/win-library/3.6/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DBOOST_NO_AUTO_PTR -include “C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp” -std=c++1y -O2 -Wall -mtune=core2 -c file41ec584834e8.cpp -o file41ec584834e8.o
if test “zfile41ec584834e8.o” != “z”; then
if test -e “file41ec584834e8-win.def”; then
echo c:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o file41ec584834e8.dll file41ec584834e8-win.def file41ec584834e8.o “C:/Users/ybord/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a” -L"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/ybord/Documents/R/win-library/3.6/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-3.6.3/bin/x64" -lR ;
c:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o file41ec584834e8.dll file41ec584834e8-win.def file41ec584834e8.o “C:/Users/ybord/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a” -L"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/ybord/Documents/R/win-library/3.6/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-3.6.3/bin/x64" -lR ;
else
echo EXPORTS > tmp.def;
c:/Rtools/mingw_64/bin/nm file41ec584834e8.o | sed -n ‘s/^.* [BCDRT] / /p’ | sed -e ‘/[.]refptr[.]/d’ -e ‘/[.]weak[.]/d’ | sed ‘s/[^ ][^ ]*/"&"/g’ >> tmp.def;
echo c:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o file41ec584834e8.dll tmp.def file41ec584834e8.o “C:/Users/ybord/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a” -L"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/ybord/Documents/R/win-library/3.6/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-3.6.3/bin/x64" -lR ;
c:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o file41ec584834e8.dll tmp.def file41ec584834e8.o “C:/Users/ybord/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a” -L"C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/ybord/Documents/R/win-library/3.6/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-3.6.3/bin/x64" -lR ;
rm -f tmp.def;
fi
fi
Error in compileCode(f, code, language = language, verbose = verbose) :
from C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/src/stan/model/model_header.hpp:4, from file41ec584834e8.cpp:14:C:/Users/ybord/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:13: warning: ‘void stan::math::set_zero_all_adjoints()’ defined but not used [-Wunused-function] static void set_zero_all_adjoints() { ^make: *** [C:/PROGRA~1/R/R-3.6.3/etc/x64/Makeconf:215: file41ec584834e8.o] Error 1
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/Rtools/mingw_/bin/g++’ not found
Error in sink(type = “output”) : invalid connection

Can you restart R and run:

fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , 'return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;' )

I don’t get an output after this. Is this error a result of my model after all?

Yes, it looks like your toolchain/compiler is fine but something with your model is causing an error. Are you able to post your model?

I am trying to fit multiple sets of parameters at once to a compartmental model. I was able to get a piece of code running that only fit one set of data to the model:

functions {
// This largely follows the deSolve package, but also includes the x_r and x_i variables.
// These variables are used in the background.
// params = [beta, sigma, gamma, delta]
// y = [S, E, I, R]

real[] SIER(real t,
real[] y,
real[] params,
real[] x_r,
int[] x_i) {

  // Our compartments
  real S = y[1];
  real E = y[2];
  real I = y[3];
  real R = y[4];

  // Our model parameters
  real beta = params[1];
  real sigma = params[2];
  real gamma = params[3];
  real delta = params[4];
  real dydt[4];
  dydt[1] = - beta * S * I;
  dydt[2] = beta * S * I - sigma * E;
  dydt[3] = sigma * E - (gamma + delta) * I;
  dydt[4] = gamma * I;
  return dydt;
}

}

data {
int<lower = 1> n_obs; // Number of days sampled
int<lower = 1> n_params; // Number of model parameters
int<lower = 1> n_difeq; // Number of differential equations in the system
int<lower = 100> n_pop; // This is to estimate the susceptible population
real<lower = 0> deaths[n_obs]; // The death data
real t0; // Initial time point (zero)
real ts[n_obs]; // Time index
}

transformed data {
real x_r[0];
int x_i[0];
}

parameters {
real<lower = 0, upper = 1> params[n_params]; // Model parameters
//int<lower = 0> S0; // Initial estimate of hosts susceptible
real<lower = 0> E0; // Initial estimate of infected hosts
real<lower = 0> I0; // Initial estimate of exposed hosts
real<lower = 0, upper = 1> prop_sus; // Estimated inverse of proportion of susceptible individuals
real<lower = 0> phi; // Variance of model
}

transformed parameters{
real y_hat[n_obs, n_difeq]; // Output from the ODE solver
real y0[n_difeq]; // Initial conditions for S, E, I, R
vector[n_obs] approx_deaths;
y0[1] = n_pop * prop_sus;
y0[2] = E0;
y0[3] = I0;
y0[4] = 0;
y_hat = integrate_ode_bdf(SIER, y0, t0, ts, params, x_r, x_i, 1e-9, 1e-5, 1e6);
approx_deaths = to_vector(y_hat[,3]) * params[4];
}

model {
params ~ beta(1, 20); //constrained to be 0-1
prop_sus ~ beta(10, 6); //constrained to be 0-1
E0 ~ normal(10, 2); //constrained to be positive.
I0 ~ normal(10, 2); //constrained to be positive.
phi ~ cauchy(15, 5); //constrained to be positive
deaths ~ normal(approx_deaths, phi);
}

generated quantities {
// Generate predicted data over the whole time series:
vector[n_obs] pred_deaths;
pred_deaths = to_vector(integrate_ode_bdf(SIER, y0, t0, ts, params, x_r, x_i, 1e-9, 1e-5, 1e6)[,3]) * params[4];
}

which works reasonably well. This is the code I am currently trying to run:

functions {
// This largely follows the deSolve package, but also includes the x_r and x_i variables.
// These variables are used in the background.
// params = [beta, sigma, gamma, delta]
// y = [S, E, I, R]

real[] SEIR(real t,
real[] y,
real[] params,
real[] x_r,
int[] x_i) {
// Our compartments
real S = y[1];
real E = y[2];
real I = y[3];
real R = y[4];

  // Our model parameters
  real beta = params[1];
  real sigma = params[2];
  real gamma = params[3];
  real delta = params[4];
  real dydt[4];

  dydt[1] = - beta * S * I;
  dydt[2] = beta * S * I - sigma * E;
  dydt[3] = sigma * E - (gamma + delta) * I;
  dydt[4] = gamma * I;
  return dydt;
}

}

data {

int<lower = 1> n_obs; // Number of days sampled
int<lower = 1> n_states; // Number of state data provided
int<lower = 1> n_params; // Number of model parameters
int<lower = 1> n_difeq; // Number of differential equations in the system
int<lower = 100> n_pop[n_states]; // This is to estimate the susceptible population
real<lower = 0> deaths[n_states, n_obs]; // The death data
real t0; // Initial time point (zero)
real ts[n_obs]; // Time index

}

transformed data {

real x_r[0];
int x_i[0];

}

parameters {

real<lower = 0, upper = 1> params[n_states, n_params]; // Model parameters
//int<lower = 0> S0; // Initial estimate of hosts susceptible
real<lower = 0> I0[n_states]; // Initial estimate of infected hosts
real<lower = 0> E0[n_states]; // Initial estimate of exposed hosts
real<lower = 0, upper = 1> prop_sus[n_states]; // Estimated inverse of proportion of susceptible individuals
real<lower = 0> phi[n_states];

}

transformed parameters{

real y_hat[n_states, n_obs, n_difeq]; // Output from the ODE solver
real y0[n_states, n_difeq]; // Initial conditions for S, E, I, R
vector[n_obs] approx_deaths[n_states];

for (i in 1:n_states) {
y0[i][1] = n_pop[i] * prop_sus[i];
y0[i][2] = E0[i];
y0[i][3] = I0[i];
y0[i][4] = 0;

y_hat[i] = integrate_ode_bdf(SEIR, y0[i], t0, ts, params[i], x_r, x_i, 1e-9, 1e-5, 1e6);
approx_deaths[i] = to_vector(y_hat[i][,3]) * params[i][4];

}
}

model {

prop_sus ~ beta(10, 6); //constrained to be 0-1
E0 ~ normal(10, 2); //constrained to be positive.
I0 ~ normal(10, 2); //constrained to be positive.
phi ~ cauchy(15, 5); //constrained to be positive

for (i in 1:n_states) {
params[i] ~ beta(1, 20); //constrained to be 0-1
deaths[i] ~ normal(approx_deaths[i], phi[i]);
}
}

generated quantities {
// Generate predicted data over the whole time series:
vector[n_obs] pred_deaths[n_states];

for (i in 1:n_states) {
pred_deaths[i] = to_vector(integrate_ode_bdf(SEIR, y0[i], t0, ts, params[i], x_r, x_i, 1e-9, 1e-5, 1e6)[,3]) * params[i][4];
}
}

Thanks!

Thanks! I can reproduce the error on my system, which means there’s definitely something to be fixed!

Thanks for finding this, I’ll have a closer look at where the issue is coming from and keep you updated

1 Like

I’m able to compile your model in cmdstanR, which uses a more recent version of Stan, so I’d recommend that you run your models through that in the mean time.

The Getting Started guide is available here: https://mc-stan.org/cmdstanr/articles/cmdstanr.html

But many people are running into issues with getting it installed and up-and-running.

Here’s what worked for me:

Install the mingw32-make package and update the PATH:

processx::run(
    "pacman",
    args = c("-Syu", "mingw-w64-x86_64-make","--noconfirm"),
    wd = file.path(Sys.getenv("RTOOLS40_HOME"), "usr", "bin"),
    echo = TRUE
)
write('PATH="${RTOOLS40_HOME}\\usr\\bin;${RTOOLS40_HOME}\\mingw64\\bin;${PATH}"', file = "~/.Renviron", append = FALSE)

Restart R and run:

install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

library(cmdstanr)
install_cmdstan()

If the install_cmdstan() finishes without an error, then you’re good to start running models. If it does error, try running:

setwd(".cmdstanr/cmdstan-2.24.1/")
processx::run(
    "mingw32-make",args="clean"
)
processx::run(
    "mingw32-make",args=c("-j4","build"),
    echo=T
)

If that runs without an error, then restart R and you’re ready to run your Stan model!

1 Like

I was able to compile it successfully, thank you for the help!

I still don’t quite understand why the code wouldn’t compile. Is there anything I should try to keep in mind for future projects to avoid these compilation issues?

This was just a bug specific to this version of Stan, it’s not really something predictable that you have to worry about

1 Like

I am getting similar errors (in the original post - dirichlet_log.hpp:5:0, mat.hpp:278, mat.hpp:12, math.hpp:4, model_header.hpp:4, dirchlet_lpmf.hpp:6 ) using the 8schools model. Installing CmdStan is not an option for me. Is there another workaround?

— UPDATE —
Although the errors were the same, this was an unrelated problem. Dealing with a Citrix server on a non-internet system. Simplified my package installation structure and this fixed the problem.