# Trouble replicating a journal article on firearm mortality in rstan

**URL:** <https://discourse.mc-stan.org/t/trouble-replicating-a-journal-article-on-firearm-mortality-in-rstan/24862>\
**Category:** General\
**Created:** [October 15, 2021, 11:01pm UTC](https://discourse.mc-stan.org/t/trouble-replicating-a-journal-article-on-firearm-mortality-in-rstan/24862 "2021-10-15T23:01:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvbondy](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mvbondy](https://discourse.mc-stan.org/u/mvbondy)\
**Post date:** [October 15, 2021, 11:01pm UTC](https://discourse.mc-stan.org/t/trouble-replicating-a-journal-article-on-firearm-mortality-in-rstan/24862/1 "2021-10-15T23:01:26Z")

</div>

I’m new to rstan and been trying to replicate the results from a recently published [journal article](https://www.pnas.org/content/117/26/14906) that used rstan to do some Bayesian modeling. (Replication code available [here](https://accounts.osf.io/login?service=https://osf.io/5yqx3/).) I have followed the installation instructions posted [here](https://github.com/stan-dev/rstan/wiki/Installing-RStan-from-Source#windows), but am running into the following error when I try to run the model.

```
# total iterations per chain.
iter = 1000

# fit the model via MCMC
M <- stan(file = "C:/Users/mvbondy/Dropbox (Stanford Law School)/Team_Donohue/RTC (New)/misc requests/PNAS_Paper/NegBinFinal.stan",
                            data = stan.data ,
                            pars = c("alpha","betachI","betach6","betaI","beta6","delta", "gamma","zeta","invphi"),
                            iter=iter)

```

That results in the following output

```
make cmd is
  make -f "C:/PROGRA~1/R/R-41~1.1/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-41~1.1/share/make/winshlib.mk" -f "C:/Users/mvbondy/Documents/.R/Makevars.win" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file28c07b815915.dll" WIN=64 TCLBIN=64 OBJECTS="file28c07b815915.o"

make would use
"C:/rtools40/mingw64/bin/"g++ -std=gnu++14 -I"C:/PROGRA~1/R/R-41~1.1/include" -DNDEBUG -I"C:/Users/mvbondy/Documents/R/win-library/4.1/Rcpp/include/" -I"C:/Users/mvbondy/Documents/R/win-library/4.1/RcppEigen/include/" -I"C:/Users/mvbondy/Documents/R/win-library/4.1/RcppEigen/include/unsupported" -I"C:/Users/mvbondy/Documents/R/win-library/4.1/BH/include" -I"C:/Users/mvbondy/Documents/R/win-library/4.1/StanHeaders/include/src/" -I"C:/Users/mvbondy/Documents/R/win-library/4.1/StanHeaders/include/" -I"C:/Users/mvbondy/Documents/R/win-library/4.1/RcppParallel/include/" -I"C:/Users/mvbondy/Documents/R/win-library/4.1/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DBOOST_NO_AUTO_PTR -include "C:/Users/mvbondy/Documents/R/win-library/4.1/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp" -std=c++1y -O2 -Wall -mfpmath=sse -msse2 -mstackrealign -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -c file28c07b815915.cpp -o file28c07b815915.o
if test "zfile28c07b815915.o" != "z"; then \
  if test -e "file28c07b815915-win.def"; then \
    echo "C:/rtools40/mingw64/bin/"g++ -shared -s -static-libgcc -o file28c07b815915.dll file28c07b815915-win.def file28c07b815915.o "C:/Users/mvbondy/Documents/R/win-library/4.1/rstan/lib/x64/libStanServices.a" -L"C:/Users/mvbondy/Documents/R/win-library/4.1/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/mvbondy/Documents/R/win-library/4.1/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-41~1.1/bin/x64" -lR ; \
    "C:/rtools40/mingw64/bin/"g++ -shared -s -static-libgcc -o file28c07b815915.dll file28c07b815915-win.def file28c07b815915.o "C:/Users/mvbondy/Documents/R/win-library/4.1/rstan/lib/x64/libStanServices.a" -L"C:/Users/mvbondy/Documents/R/win-library/4.1/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/mvbondy/Documents/R/win-library/4.1/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-41~1.1/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    "C:/rtools40/mingw64/bin/"nm file28c07b815915.o | sed -n 's/^.* [BCDRT] / /p' | sed -e '/[.]refptr[.]/d' -e '/[.]weak[.]/d' | sed 's/[^][^]*/"&"/g' >> tmp.def; \
    echo "C:/rtools40/mingw64/bin/"g++ -shared -s -static-libgcc -o file28c07b815915.dll tmp.def file28c07b815915.o "C:/Users/mvbondy/Documents/R/win-library/4.1/rstan/lib/x64/libStanServices.a" -L"C:/Users/mvbondy/Documents/R/win-library/4.1/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/mvbondy/Documents/R/win-library/4.1/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-41~1.1/bin/x64" -lR ; \
    "C:/rtools40/mingw64/bin/"g++ -shared -s -static-libgcc -o file28c07b815915.dll tmp.def file28c07b815915.o "C:/Users/mvbondy/Documents/R/win-library/4.1/rstan/lib/x64/libStanServices.a" -L"C:/Users/mvbondy/Documents/R/win-library/4.1/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/mvbondy/Documents/R/win-library/4.1/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-41~1.1/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi
Error in compileCode(f, code, language = language, verbose = verbose) : 
                   from file28c07b815915.cpp:14:C:/Users/mvbondy/Documents/R/win-library/4.1/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp: At global scope:C:/Users/mvbondy/Documents/R/win-library/4.1/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-41~1.1/etc/x64/Makeconf:245: file28c07b815915.o] Error 1
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

```

I was hoping this is a simple software installation problem and someone could help point me in the right direction. Much appreciated.

---

<div class="post-metadata">

**Author:** ![rfc](https://avatars.discourse-cdn.com/v4/letter/r/eb9ed0/32.png) [@rfc](https://discourse.mc-stan.org/u/rfc)\
**Post date:** [October 15, 2021, 11:15pm UTC](https://discourse.mc-stan.org/t/trouble-replicating-a-journal-article-on-firearm-mortality-in-rstan/24862/2 "2021-10-15T23:15:05Z")

</div>

Searching the internet for the symptom

```
Error in sink(type = "output") : invalid connection

```

finds a few earlier discussions of similar windows build issues with some suggested fixes:

- (stan forum) [Error in sink(type = "output") : invalid connection](https://discourse.mc-stan.org/t/error-in-sink-type-output-invalid-connection/8911)
- (stan forum) [Error in sink(type = "output") : invalid connection](https://discourse.mc-stan.org/t/error-in-sink-type-output-invalid-connection/20672)
- (stackoverflow) [rstan - Error on the stan file compilation using R 3.6.0. and Win 10 - Stack Overflow](https://stackoverflow.com/questions/56256884/error-on-the-stan-file-compilation-using-r-3-6-0-and-win-10)

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [October 15, 2021, 11:42pm UTC](https://discourse.mc-stan.org/t/trouble-replicating-a-journal-article-on-firearm-mortality-in-rstan/24862/3 "2021-10-15T23:42:40Z")

</div>

Can you try installing the preview of the next rstan version and running again:

```
remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

```
