Created
December 4, 2016 20:31
-
-
Save ericnovik/ff7daab5c3eb0c8396887110a9dbd6ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Before running this script make sure you have | |
### RTools installed: | |
### https://github.com/stan-dev/rstan/wiki/Install-Rtools-for-Windows | |
# your version of R | |
sessionInfo()$R.version | |
# your version of RTools; If your Rtools version is | |
# at another location, specify the correct location | |
# in the file.path() | |
rtools_path <- file.path("C:/Rtools/VERSION.txt") | |
(rtools <- readLines(rtools_path)) | |
# Pull out major and minor version of RTools and R | |
rtools_v <- as.integer(unlist(strsplit(gsub("[^0-9]", "", rtools), "")))[1:2] | |
r_minor <- as.integer(unlist(strsplit(gsub("[^0-9]", "", sessionInfo()$R.version$minor), ""))) | |
r_v <- c(as.integer(sessionInfo()$R.version$major), | |
r_minor[1], r_minor[2]) | |
# R must be version 3.0.2 or later | |
r_less_than_302 <- as.integer(paste0(r_v, collapse = "")) < 302 | |
if (r_less_than_302) | |
stop("Please, install the latest R version") | |
# If you have installed R version 3.2.x or lower, your | |
# R tools versions must match your R version | |
r_less_than_330 <- as.integer(paste0(r_v, collapse = "")) <= 329 | |
if (r_less_than_330) | |
if (rtools_v[1] != r_v[1] || rtools_v[2] != r_v[2]) | |
stop("There is a mismatch between your R version and your RTools version. Please, upgrade.") | |
# Check PATH for presence of RTools and gcc | |
sys_path <- Sys.getenv("PATH") | |
if (!grepl("Rtools", sys_path)) | |
stop("Rtools is missing from your PATH") | |
if (!grepl("gcc", sys_path)) | |
stop("gcc is missing from your PATH") | |
# check gcc version | |
system('g++ -v') | |
# Set C++ compiler options | |
dotR <- file.path(Sys.getenv("HOME"), ".R") | |
if (!file.exists(dotR)) dir.create(dotR) | |
(M <- file.path(dotR, "Makevars")) | |
if (!file.exists(M)) file.create(M) | |
cat("\nCXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function", | |
file = M, sep = "\n", append = TRUE) | |
cat('Sys.setenv(BINPREF = "C:/Rtools/mingw_$(WIN)/bin/")', | |
file = file.path(Sys.getenv("HOME"), ".Rprofile"), | |
sep = "\n", append = TRUE) | |
# to get rid of deprecation warnings on new compilers | |
cat("\nCXXFLAGS += -Wno-ignored-attributes -Wno-deprecated-declarations", | |
file = M, sep = "\n", append = TRUE) | |
cat(readLines(M), sep = "\n") | |
# change to number of processors on your system | |
# here we have 2, thus: "-j2" | |
Sys.setenv(MAKEFLAGS = "-j2") | |
# install RStan from CRAN | |
install.packages('rstan', repos = 'https://cloud.r-project.org/', dependencies = TRUE) | |
# test the install with a small model | |
library(rstan) | |
rstan_options(auto_write = TRUE) | |
options(mc.cores = parallel::detectCores()) | |
scode <- " | |
parameters { | |
real y[2]; | |
} | |
model { | |
y[1] ~ normal(0, 1); | |
y[2] ~ double_exponential(0, 2); | |
} | |
" | |
fit1 <- stan(model_code = scode, iter = 10, verbose = FALSE) | |
print(fit1) | |
### the print(fit1) line should produce something like: | |
# Inference for Stan model: 5687625e16aea608e5f18bd8a8d65730. | |
# 4 chains, each with iter=10; warmup=5; thin=1; | |
# post-warmup draws per chain=5, total post-warmup draws=20. | |
# | |
# mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat | |
# y[1] -0.04 0.30 1.34 -2.69 -0.87 0.05 1.10 1.68 20 0.82 | |
# y[2] 0.28 0.67 3.01 -5.52 -0.90 -0.25 2.36 5.87 20 1.36 | |
# lp__ -1.91 0.31 1.29 -4.58 -2.29 -1.73 -0.95 -0.36 18 1.10 | |
# | |
# Samples were drawn using NUTS(diag_e) at Tue Aug 16 14:07:56 2016. | |
# For each parameter, n_eff is a crude measure of effective sample size, | |
# and Rhat is the potential scale reduction factor on split chains (at | |
# convergence, Rhat=1). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment