### Example output of .libPaths() Source: https://github.com/stan-dev/rstan/wiki/Installing-Older-Versions-of-RStan Example output of .libPaths() showing the library paths. The first path is the default installation location. ```r [1] "/opt/site-library" "/usr/local/lib/R/site-library" "/usr/lib/R/site-library" [4] "/usr/lib/R/library" ``` -------------------------------- ### Test RStan installation with example(stan_model) Source: https://github.com/stan-dev/rstan/wiki/Installing-RStan-from-Source Runs the example for stan_model to verify the installation works. The model should compile and sample. ```R example(stan_model, package = "rstan", run.dontrun = TRUE) ``` -------------------------------- ### Run stan_demo() to select an example model Source: https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started Launches an interactive menu to choose from many BUGS examples and other Stan models. On first call, it prompts to download the examples; choose option 1 to store them in the rstan installation directory for future use. The returned object is a stanfit instance, so print, plot, pairs, extract, etc. can be used on it. ```R model <- stan_demo() ``` -------------------------------- ### Install rstan with make install Source: https://github.com/stan-dev/rstan/wiki/How-to-build-rstan-package? Run `make install` from the `rstan/` directory to directly install the built rstan package. This is an alternative to using `R CMD INSTALL` on the generated tarball. ```bash $ make install ``` -------------------------------- ### Install RStan v1.3.0 to a custom library path Source: https://github.com/stan-dev/rstan/wiki/Installing-Older-Versions-of-RStan Installs RStan v1.3.0 to a specific library path using install_url(). The output shows the download and installation process. Requires write permissions for the target folder. ```r install_url('https://github.com/stan-dev/rstan/releases/download/v1.3.0/rstan_1.3.0.tar.gz', args = paste0("--library=", .libPaths()[2])) Downloading rstan_1.3.0.tar.gz from https://github.com/stan-dev/rstan/releases/download/v1.3.0/rstan_1.3.0.tar.gz Installing package from /tmp/RtmpJLwfA3/rstan_1.3.0.tar.gz Installing rstan ``` -------------------------------- ### Installation error output Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/failures.md Output from the installation check showing that the package failed to install. The error message points to the log file for details. ```text Installation failed. See ‘/Users/andrew/Downloads/StanHeaders/revdep/checks.noindex/beanz/new/beanz.Rcheck/00install.out’ for details. ``` -------------------------------- ### Install V8 with static libv8 Source: https://github.com/stan-dev/rstan/wiki/Configuring-C-Toolchain-for-Linux Install the V8 R package with a static libv8 build when the system library is not available. Setting DOWNLOAD_STATIC_LIBV8 to 1 triggers the static download during installation. ```R Sys.setenv(DOWNLOAD_STATIC_LIBV8 = 1) install.packages("V8") ``` -------------------------------- ### Install jsonlite from source to verify toolchain Source: https://github.com/stan-dev/rstan/wiki/Configuring-C---Toolchain-for-Windows Tests that packages can be installed from source, confirming the toolchain is configured correctly. ```R install.packages("jsonlite", type = "source") ``` -------------------------------- ### CRAN installation success for visit package Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/failures.md Output from installing the 'visit' package against the CRAN version of StanHeaders. The installation succeeds, completing with '* DONE (visit)' after the usual R package installation steps. ```text * installing *source* package ‘visit’ ... ** package ‘visit’ successfully unpacked and MD5 sums checked ** using staged installation ** libs "/Library/Frameworks/R.framework/Resources/bin/Rscript" -e "source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))" stan_files/visit.stan Wrote C++ file "stan_files/visit.cc" clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"../inst/include" -I"/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/visit/BH/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/visit/Rcpp/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/visit/rstan/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/visit/RcppEigen/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include' -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -Wno-unknown-warning-option -Wno-enum-compare -Wno-ignored-attributes -Wno-unused-local-typedef -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -Wno-infinite-recursion -Wno-unknown-pragmas -Wno-unused-lambda-capture -Wno-deprecated-declarations -Wno-deprecated-builtins -Wno-unused-but-set-variables -ftemplate-backtrace-limit=0 -Wno-nonnull -c stan_files/visit.cc -o stan_files/visit.o ... ** byte-compile and prepare package for lazy loading ** help *** installing help indices ** building package indices ** installing vignettes ** testing if installed package can be loaded from temporary location ** checking absolute paths in shared objects and dynamic libraries ** testing if installed package can be loaded from final location ** testing if installed package keeps a record of temporary installation path * DONE (visit) ``` -------------------------------- ### CRAN installation success for dfpk Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/problems.md Output from installing the 'dfpk' package with the CRAN version of StanHeaders. The installation succeeds, compiling Modules.cpp and init.c, then completing all installation steps and ending with '* DONE (dfpk)'. ```text * installing *source* package ‘dfpk’ ... ** package ‘dfpk’ successfully unpacked and MD5 sums checked ** using staged installation ** libs clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include/src" -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/rstan/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/BH/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/Rcpp/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/RcppEigen/include' -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -Wno-unknown-warning-option -Wno-enum-compare -Wno-ignored-attributes -Wno-unused-local-typedef -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -Wno-infinite-recursion -Wno-unknown-pragmas -Wno-unused-lambda-capture -Wno-deprecated-declarations -Wno-deprecated-builtins -Wno-unused-but-set-variables -ftemplate-backtrace-limit=0 -Wno-nonnull -c Modules.cpp -o Modules.o clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include/src" -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/rstan/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/BH/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/Rcpp/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/RcppEigen/include' -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c init.c -o init.o ... ** inst ** byte-compile and prepare package for lazy loading ** help *** installing help indices ** building package indices ** testing if installed package can be loaded from temporary location ** checking absolute paths in shared objects and dynamic libraries ** testing if installed package can be loaded from final location ** testing if installed package keeps a record of temporary installation path * DONE (dfpk) ``` -------------------------------- ### Run a Stan example with stan_demo() Source: https://github.com/stan-dev/rstan/wiki/Example-Models Run the 'dogs' example using stan_demo() after loading rstan and setting multicore options. The auto_write option caches compiled Stan models. Calling stan_demo() without arguments shows a menu of over 500 examples, though some may not work at any given time. ```R library(rstan) options(mc.cores = parallel::detectCores()) rstan_options(auto_write = TRUE) dogs <- stan_demo("dogs") dogs ``` -------------------------------- ### Installed package size NOTE Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/failures.md Output from the installed package size check, reporting the total installed size and the largest sub-directory. ```text installed size is 7.1Mb sub-directories of 1Mb or more: libs 5.6Mb ``` -------------------------------- ### Install main dependencies from source (Mac) Source: https://github.com/stan-dev/rstan/wiki/Installing-RStan-from-Source Installs the main dependencies (Rcpp, RcppEigen, RcppParallel, StanHeaders) from source with the same compiler settings. ```R install.packages(c("Rcpp", "RcppEigen", "RcppParallel", "StanHeaders"), type = "source") ``` -------------------------------- ### Set number of cores for compilation (Windows) Source: https://github.com/stan-dev/rstan/wiki/Installing-RStan-from-Source Set the MAKEFLAGS environment variable to specify the number of cores to use during installation. The example uses four cores. ```R Sys.setenv(MAKEFLAGS = "-j4") # four cores used ``` -------------------------------- ### CRAN installation success for dfpk Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/failures.md Shows the successful installation of the CRAN version of 'dfpk'. The compilation completes without errors, and the package is installed and loaded successfully, ending with '* DONE (dfpk)'. ```text * installing *source* package ‘dfpk’ ... ** package ‘dfpk’ successfully unpacked and MD5 sums checked ** using staged installation ** libs clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include/src" -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/rstan/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/BH/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/Rcpp/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/RcppEigen/include' -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -Wno-unknown-warning-option -Wno-enum-compare -Wno-ignored-attributes -Wno-unused-local-typedef -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -Wno-infinite-recursion -Wno-unknown-pragmas -Wno-unused-lambda-capture -Wno-deprecated-declarations -Wno-deprecated-builtins -Wno-unused-but-set-variables -ftemplate-backtrace-limit=0 -Wno-nonnull -c Modules.cpp -o Modules.o clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include/src" -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/rstan/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/BH/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/Rcpp/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/RcppEigen/include' -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c init.c -o init.o ... ** inst ** byte-compile and prepare package for lazy loading ** help *** installing help indices ** building package indices ** testing if installed package can be loaded from temporary location ** checking absolute paths in shared objects and dynamic libraries ** testing if installed package can be loaded from final location ** testing if installed package keeps a record of temporary installation path * DONE (dfpk) ``` -------------------------------- ### Install Xcode command line tools with xcode-select --install Source: https://github.com/stan-dev/rstan/wiki/RStan-Troubleshooting Install Xcode command line tools from the R console to resolve xcodebuild or xcode errors. ```bash xcode-select --install ``` -------------------------------- ### CRAN installation success log Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/failures.md Installation log for the CRAN version of the package, showing a successful installation. It includes the compilation step and the final 'DONE (idem)' message. ```text * installing *source* package ‘idem’ ... ** package ‘idem’ successfully unpacked and MD5 sums checked ** using staged installation ** libs "/Library/Frameworks/R.framework/Resources/bin/Rscript" -e "source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))" stan_files/idem.stan Wrote C++ file "stan_files/idem.cc" clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"../inst/include" -I"/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/idem/rstan/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/idem/BH/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/idem/Rcpp/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/idem/RcppEigen/include' -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -Wno-unknown-warning-option -Wno-enum-compare -Wno-ignored-attributes -Wno-unused-local-typedef -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -Wno-infinite-recursion -Wno-unknown-pragmas -Wno-unused-lambda-capture -Wno-deprecated-declarations -Wno-deprecated-builtins -Wno-unused-but-set-variables -ftemplate-backtrace-limit=0 -Wno-nonnull -c stan_files/idem.cc -o stan_files/idem.o ... ** byte-compile and prepare package for lazy loading ** help *** installing help indices ** building package indices ** installing vignettes ** testing if installed package can be loaded from temporary location ** checking absolute paths in shared objects and dynamic libraries ** testing if installed package can be loaded from final location ** testing if installed package keeps a record of temporary installation path * DONE (idem) ``` -------------------------------- ### Installation error for package ‘visit’ Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/problems.md Error message shown when the package ‘visit’ cannot be installed during reverse dependency checks. ```text Installation failed. See ‘/Users/andrew/Downloads/StanHeaders/revdep/checks.noindex/visit/new/visit.Rcheck/00install.out’ for details. ``` -------------------------------- ### Install macOS SDK headers for Mojave Source: https://github.com/stan-dev/rstan/wiki/RStan-Mojave-Mac-OS-X-Prerequisite-Installation-Instructions Installs the macOS 10.14 SDK headers package from the Command Line Tools directory. Run this in the command line before configuring RStan on Mojave. ```bash sudo installer -pkg \ /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg \ -target / ``` -------------------------------- ### Example: Using Rhat, ess_bulk, and ess_tail on simulated draws Source: https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/man/Rhat.Rd This example creates a matrix of 500 random normal draws arranged as 100 iterations for 5 chains, then applies the Rhat, ess_bulk, and ess_tail functions to compute the convergence and efficiency diagnostics. It demonstrates the typical usage pattern for these functions. ```R # pretend these draws came from five actual Markov Chins sims <- matrix(rnorm(500), nrow = 100, ncol = 5) Rhat(sims) ess_bulk(sims) ess_tail(sims) ``` -------------------------------- ### Install rstan from source in R Source: https://github.com/stan-dev/rstan/wiki/Configuring-C-Toolchain-for-Linux Install the rstan R package from source on Ubuntu Non-LTS. Setting DOWNLOAD_STATIC_LIBV8 to 1 requests a static build of libv8 during installation. ```R Sys.setenv(DOWNLOAD_STATIC_LIBV8 = 1) install.packages("rstan") ``` -------------------------------- ### get_inits Source: https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/man/stanfit-class.Rd Gets the initial values used for each chain. ```APIDOC ## get_inits ### Description Gets the initial values used for each chain. ### Usage get_inits(object) ### Arguments - **object** (stanfit) - Required - An object of class 'stanfit'. ### Value A list of initial values, one per chain. ``` -------------------------------- ### Compile with custom libc++ using clang++ Source: https://github.com/stan-dev/rstan/wiki/Catalina-problems Command to compile with a custom libc++ installation, avoiding the system C++ libraries. Replace `` with the root of the clang installation directory. ```bash clang++ -std=c++11 -stdlib=libc++ -nostdinc++ \ -I/include/c++/v1 \ -L/lib \ -Wl,-rpath,/lib \ test.cpp ``` -------------------------------- ### Check R library paths with .libPaths() Source: https://github.com/stan-dev/rstan/wiki/Installing-Older-Versions-of-RStan Displays the library paths where R packages are installed. The first directory is the default installation location. ```r .libPaths() ``` -------------------------------- ### Install withr 2.2.0 Source: https://github.com/stan-dev/rstan/wiki/Configuring-C---Toolchain-for-Windows Manually installs withr version 2.2.0 to avoid the cleanup_makevar error. ```R devtools::install_version("withr", version="2.2.0") ``` -------------------------------- ### Install development version of RStan from GitHub (Windows) Source: https://github.com/stan-dev/rstan/wiki/Installing-RStan-from-Source Installs the development version of RStan from GitHub using remotes::install_github, specifying the develop branch and the subdirectory rstan/rstan. ```R remotes::install_github("stan-dev/rstan", ref = "develop", subdir = "rstan/rstan") ``` -------------------------------- ### CRAN installation success for beanz Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/failures.md Output from installing the 'beanz' package against the CRAN version of StanHeaders. The installation succeeds, completing with '* DONE (beanz)' after compiling the Stan files and running the standard installation checks. ```text * installing *source* package ‘beanz’ ... ** package ‘beanz’ successfully unpacked and MD5 sums checked ** using staged installation ** libs "/Library/Frameworks/R.framework/Resources/bin/Rscript" -e "source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))" stan_files/bs.stan Wrote C++ file "stan_files/bs.cc" clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"../inst/include" -I"/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/old/StanHeaders/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/beanz/rstan/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/beanz/BH/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/beanz/Rcpp/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/beanz/RcppEigen/include' -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -Wno-unknown-warning-option -Wno-enum-compare -Wno-ignored-attributes -Wno-unused-local-typedef -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -Wno-infinite-recursion -Wno-unknown-pragmas -Wno-unused-lambda-capture -Wno-deprecated-declarations -Wno-deprecated-builtins -Wno-unused-but-set-variables -ftemplate-backtrace-limit=0 -Wno-nonnull -c stan_files/bs.cc -o stan_files/bs.o "/Library/Frameworks/R.framework/Resources/bin/Rscript" -e "source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))" stan_files/ds.stan ... ** byte-compile and prepare package for lazy loading ** help *** installing help indices ** building package indices ** installing vignettes ** testing if installed package can be loaded from temporary location ** checking absolute paths in shared objects and dynamic libraries ** testing if installed package can be loaded from final location ** testing if installed package keeps a record of temporary installation path * DONE (beanz) ``` -------------------------------- ### Compute summary of stanfit with custom quantiles Source: https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/man/stanfit-method-summary.Rd Fits a Stan model with exponential prior and computes summary statistics for the parameters with quantiles 0.1 and 0.9. The output includes s$summary for all chains merged and s$c_summary for individual chains. This example is wrapped in \dontrun{} and requires a working Stan installation. ```r ecode <- ' parameters { array[2] real y; } model { y ~ exponential(1); } ' fit <- stan(model_code = ecode) s <- summary(fit, probs = c(0.1, 0.9)) s$summary # all chaines merged s$c_summary # individual chains ``` -------------------------------- ### prophet installed size NOTE Source: https://github.com/stan-dev/rstan/blob/develop/revdep/problems.md Note about installed package size for prophet: 53.9Mb total, with libs 53.1Mb. ```text installed size is 53.9Mb sub-directories of 1Mb or more: libs 53.1Mb ``` -------------------------------- ### Installation failure error message Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/failures.md Error message shown when the package installation fails during the reverse dependency check. It points to the installation log file for details. ```text Installation failed. See ‘/Users/andrew/Downloads/StanHeaders/revdep/checks.noindex/idem/new/idem.Rcheck/00install.out’ for details. ``` -------------------------------- ### Install RStan binary on Ubuntu LTS Source: https://github.com/stan-dev/rstan/wiki/Configuring-C-Toolchain-for-Linux Install a pre-built RStan binary on Ubuntu LTS (16.04, 18.04, 20.04) with R>=4.0. Adds Michael Rutter's PPA repositories, updates package lists, and installs the r-cran-rstan package. ```bash # Add Michael Rutter's c2d4u4.0 PPA (and rrutter4.0 for CRAN builds too) sudo add-apt-repository ppa:marutter/rrutter4.0 sudo add-apt-repository ppa:c2d4u.team/c2d4u4.0+ sudo apt update sudo apt install r-cran-rstan ``` -------------------------------- ### Installation failure error message Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/failures.md Error message indicating that the package installation failed. The log file path is provided for details. ```text Installation failed. See ‘/Users/andrew/Downloads/StanHeaders/revdep/checks.noindex/dfpk/new/dfpk.Rcheck/00install.out’ for details. ``` -------------------------------- ### Install RStan binary on Debian Testing Source: https://github.com/stan-dev/rstan/wiki/Configuring-C-Toolchain-for-Linux Install the pre-built r-cran-rstan package on Debian Testing. ```bash apt-get install r-cran-rstan ``` -------------------------------- ### Install RStan packages and verify build tools for R 4.0 prerelease Source: https://github.com/stan-dev/rstan/wiki/Using-RStan-with-the-R-4.0-Prerelease-on-Windows Install required R packages and check for build tools when using the R 4.0 prerelease with RTools40. The pkgbuild::has_build_tools call will offer to install rtools40 if needed. ```R install.packages(c("RcppEigen", "pkgbuild", "BH", "rstan")) pkgbuild::has_build_tools(debug = TRUE) # will offer to install rtools40 ``` -------------------------------- ### Install CRAN version of RStan from source (Windows) Source: https://github.com/stan-dev/rstan/wiki/Installing-RStan-from-Source Installs the CRAN version of RStan from source using install.packages with type = "source". ```R install.packages("rstan", type = "source") ``` -------------------------------- ### Create named lists with nlist() Source: https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/man/nlist.Rd The first example defines variables x and y in the environment and then calls nlist(x, y) to create a named list using the variable names. The second example shows that variables can be defined directly in the call (z = 3) while taking the rest from the environment. ```r x <- 1 y <- 2 nlist(x, y) ``` ```r nlist(x, y, z = 3) ``` -------------------------------- ### Example 1: Fit a simple model with stan() Source: https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/man/stan.Rd Fits a Stan model with two parameters using the stan() function. The model code is defined as a string, and the fit is run with 10 iterations. The example also shows how to continue sampling from an existing fit using the fit argument. ```r #### example 1 library(rstan) scode <- " parameters { array[2] real y; } model { y[1] ~ normal(0, 1); y[2] ~ double_exponential(0, 2); } " fit1 <- stan(model_code = scode, iter = 10, verbose = FALSE) print(fit1) fit2 <- stan(fit = fit1, iter = 10000, verbose = FALSE) ``` -------------------------------- ### Devel installation failure log Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/failures.md Installation log for the development version of the package, showing a compilation failure due to missing 'lgamma_r' in the global namespace. The error occurs in the Stan math header lgamma.hpp. ```text * installing *source* package ‘idem’ ... ** package ‘idem’ successfully unpacked and MD5 sums checked ** using staged installation ** libs "/Library/Frameworks/R.framework/Resources/bin/Rscript" -e "source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))" stan_files/idem.stan Wrote C++ file "stan_files/idem.cc" clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"../inst/include" -I"/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/idem/rstan/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/idem/BH/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/idem/Rcpp/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/idem/RcppEigen/include' -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -Wno-unknown-warning-option -Wno-enum-compare -Wno-ignored-attributes -Wno-unused-local-typedef -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -Wno-infinite-recursion -Wno-unknown-pragmas -Wno-unused-lambda-capture -Wno-deprecated-declarations -Wno-deprecated-builtins -Wno-unused-but-set-variables -ftemplate-backtrace-limit=0 -Wno-nonnull -c stan_files/idem.cc -o stan_files/idem.o In file included from stan_files/idem.cc:3: ... return ::lgamma_r(x, &sign); ~~^ /Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include/stan/math/prim/fun/lgamma.hpp:85:12: error: no member named 'lgamma_r' in the global namespace return ::lgamma_r(x, &sign); ~~^ 1 warning and 2 errors generated. make: *** [stan_files/idem.o] Error 1 rm stan_files/idem.cc ERROR: compilation failed for package ‘idem’ * removing ‘/Users/andrew/Downloads/StanHeaders/revdep/checks.noindex/idem/new/idem.Rcheck/idem’ ``` -------------------------------- ### Devel installation failure for dfpk with lgamma_r error Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/problems.md Output from installing the 'dfpk' package with the development version of StanHeaders. Compilation fails because the Stan math header 'lgamma.hpp' references 'lgamma_r', which is not available in the global namespace on this macOS arm64 system, producing two errors and one warning. ```text * installing *source* package ‘dfpk’ ... ** package ‘dfpk’ successfully unpacked and MD5 sums checked ** using staged installation ** libs clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include/src" -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/rstan/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/BH/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/Rcpp/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/RcppEigen/include' -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -Wno-unknown-warning-option -Wno-enum-compare -Wno-ignored-attributes -Wno-unused-local-typedef -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -Wno-infinite-recursion -Wno-unknown-pragmas -Wno-unused-lambda-capture -Wno-deprecated-declarations -Wno-deprecated-builtins -Wno-unused-but-set-variables -ftemplate-backtrace-limit=0 -Wno-nonnull -c Modules.cpp -o Modules.o In file included from Modules.cpp:3: In file included from ./include/models.hpp:5: In file included from /Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/rstan/include/rstan/rstaninc.hpp:4: ... /Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include/stan/math/prim/fun/lgamma.hpp:66:12: error: no member named 'lgamma_r' in the global namespace return ::lgamma_r(x, &sign); ~~^ /Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include/stan/math/prim/fun/lgamma.hpp:85:12: error: no member named 'lgamma_r' in the global namespace return ::lgamma_r(x, &sign); ~~^ 1 warning and 2 errors generated. make: *** [Modules.o] Error 1 ERROR: compilation failed for package ‘dfpk’ * removing ‘/Users/andrew/Downloads/StanHeaders/revdep/checks.noindex/dfpk/new/dfpk.Rcheck/dfpk’ ``` -------------------------------- ### Install system dependencies for RStan source build Source: https://github.com/stan-dev/rstan/wiki/Configuring-C-Toolchain-for-Linux Install required system packages for building RStan from source on Ubuntu Non-LTS. This includes R base, development headers, and libcurl development files. ```bash sudo apt-get install r-base r-base-dev libcurl4-openssl-dev ``` -------------------------------- ### Devel installation failure for dfpk Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/failures.md Shows the compilation error when installing the development version of 'dfpk'. The error occurs because 'lgamma_r' is not defined in the global namespace, causing the build to fail with 'ERROR: compilation failed for package 'dfpk''. ```text * installing *source* package ‘dfpk’ ... ** package ‘dfpk’ successfully unpacked and MD5 sums checked ** using staged installation ** libs clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include/src" -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/rstan/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/BH/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/Rcpp/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/RcppEigen/include' -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -Wno-unknown-warning-option -Wno-enum-compare -Wno-ignored-attributes -Wno-unused-local-typedef -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -Wno-infinite-recursion -Wno-unknown-pragmas -Wno-unused-lambda-capture -Wno-deprecated-declarations -Wno-deprecated-builtins -Wno-unused-but-set-variables -ftemplate-backtrace-limit=0 -Wno-nonnull -c Modules.cpp -o Modules.o In file included from Modules.cpp:3: In file included from ./include/models.hpp:5: In file included from /Users/andrew/Downloads/StanHeaders/revdep/library.noindex/dfpk/rstan/include/rstan/rstaninc.hpp:4: ... /Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include/stan/math/prim/fun/lgamma.hpp:66:12: error: no member named 'lgamma_r' in the global namespace return ::lgamma_r(x, &sign); ~~^ /Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include/stan/math/prim/fun/lgamma.hpp:85:12: error: no member named 'lgamma_r' in the global namespace return ::lgamma_r(x, &sign); ~~^ 1 warning and 2 errors generated. make: *** [Modules.o] Error 1 ERROR: compilation failed for package ‘dfpk’ * removing ‘/Users/andrew/Downloads/StanHeaders/revdep/checks.noindex/dfpk/new/dfpk.Rcheck/dfpk’ ``` -------------------------------- ### stan_demo Source: https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/man/stan_demo.Rd Function to demonstrate examples included in Stan. It selects a model based on user input and fits it using the specified method. ```APIDOC ## stan_demo ### Description Demonstrates examples included in Stan. One example is chosen from a list created from matching user input and gets fitted in the demonstration. ### Usage ```r stan_demo(model = character(0), method = c("sampling", "optimizing", "meanfield", "fullrank"), ...) ``` ### Arguments - **model** (character string) - Optional - A character string for model name to specify which model will be used for demonstration. The default is an empty string, which prompts the user to select one of the available models. If `model = 0`, a character vector of all models is returned without any user intervention. If `model = i` where `i > 0`, then the ith available model is chosen without user intervention, which is useful for testing. - **method** (character string) - Optional - Whether to call `sampling` (the default), `optimizing`, or one of the variants of `vb` for the demonstration. - **...** - Optional - Further arguments passed to `method`. ### Value An S4 object of `stanfit`, unless `model = 0`, in which case a character vector of paths to available models is returned. ### Examples ```r \dontrun{ dogsfit <- stan_demo("dogs") # run the dogs model fit1 <- stan_demo(1) # run model_names[1] } ``` ``` -------------------------------- ### Get Stan version with stan_version() Source: https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/man/stan_version.Rd Call this function to obtain the version of Stan used in the current RStan installation. The returned character string follows the major.minor.patch format, for example "1.0.0". ```r stan_version() ``` -------------------------------- ### loo Source: https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/man/stanfit-method-loo.Rd Documentation for the loo method applied to stanfit objects, including usage, parameters, and examples. ```APIDOC ## loo(fit, pars = "log_lik", moment_match = FALSE) ### Description Computes leave-one-out cross-validation (LOO) for a fitted Stan model represented by a stanfit object. It can optionally use the moment matching algorithm to improve the accuracy of the LOO estimates. ### Method Function call ### Endpoint loo(fit, pars = "log_lik", moment_match = FALSE) ### Parameters - **fit** (stanfit) - Required - A fitted Stan model object. - **pars** (character) - Optional - The name of the parameter(s) containing the pointwise log-likelihood values. Defaults to "log_lik". - **moment_match** (logical) - Optional - If TRUE, uses the moment matching algorithm to improve the LOO approximation. Defaults to FALSE. ### Request Example ```r loo1 <- loo(fit1, pars = "log_lik") ``` ### Response Returns an object of class 'loo' containing LOO information criteria and related diagnostics. ### Response Example ```r print(loo1) ``` ``` -------------------------------- ### Fit Rats example with stan() Source: https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started Fits the Rats example from the RStan wiki. Reads the rats data from a URL, defines the time points x and its mean, then calls stan() with the model file URL and the data list. Requires the rstan package and internet access to fetch the data and model. ```R y <- as.matrix(read.table('https://raw.github.com/wiki/stan-dev/rstan/rats.txt', header = TRUE)) x <- c(8, 15, 22, 29, 36) xbar <- mean(x) N <- nrow(y) T <- ncol(y) rats_fit <- stan(file='https://raw.githubusercontent.com/stan-dev/example-models/master/bugs_examples/vol1/rats/rats.stan', data = list(N=N, T=T, y=y, x=x, xbar=xbar)) ``` -------------------------------- ### Devel installation failure for visit package Source: https://github.com/stan-dev/rstan/blob/develop/revdep-macos/failures.md Output from installing the 'visit' package against the devel version of StanHeaders. The compilation fails because the C++ code calls ::lgamma_r, which is not available in the global namespace, producing the error 'no member named 'lgamma_r' in the global namespace' and causing the package build to fail. ```text * installing *source* package ‘visit’ ... ** package ‘visit’ successfully unpacked and MD5 sums checked ** using staged installation ** libs "/Library/Frameworks/R.framework/Resources/bin/Rscript" -e "source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))" stan_files/visit.stan Wrote C++ file "stan_files/visit.cc" clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"../inst/include" -I"/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/visit/BH/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/visit/Rcpp/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/visit/rstan/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/visit/RcppEigen/include' -I'/Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include' -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -Wno-unknown-warning-option -Wno-enum-compare -Wno-ignored-attributes -Wno-unused-local-typedef -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -Wno-infinite-recursion -Wno-unknown-pragmas -Wno-unused-lambda-capture -Wno-deprecated-declarations -Wno-deprecated-builtins -Wno-unused-but-set-variables -ftemplate-backtrace-limit=0 -Wno-nonnull -c stan_files/visit.cc -o stan_files/visit.o In file included from stan_files/visit.cc:3: ... return ::lgamma_r(x, &sign); ~~^ /Users/andrew/Downloads/StanHeaders/revdep/library.noindex/StanHeaders/new/StanHeaders/include/stan/math/prim/fun/lgamma.hpp:85:12: error: no member named 'lgamma_r' in the global namespace return ::lgamma_r(x, &sign); ~~^ 1 warning and 2 errors generated. make: *** [stan_files/visit.o] Error 1 rm stan_files/visit.cc ERROR: compilation failed for package ‘visit’ * removing ‘/Users/andrew/Downloads/StanHeaders/revdep/checks.noindex/visit/new/visit.Rcheck/visit’ ``` -------------------------------- ### Fit Eight Schools model with stan_demo Source: https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/vignettes/stanfit-objects.Rmd Fits the Eight Schools example model using stan_demo, which downloads and runs the model. The refresh = 0 argument suppresses progress output. ```r library(rstan) fit <- stan_demo("eight_schools", refresh = 0) ```