Skip to content

Commit

Permalink
Merge branch 'master' into loo-moment-match
Browse files Browse the repository at this point in the history
  • Loading branch information
andrjohns committed Jun 27, 2023
2 parents d428b27 + 83bbc35 commit 42b2e2c
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 34 deletions.
78 changes: 44 additions & 34 deletions R/install.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ install_cmdstan <- function(dir = NULL,
stanc_makefile)
writeLines(stanc_makefile, con = file.path(dir_cmdstan, "make", "stanc"))

if ((is_rtools42_toolchain() || is_rtools43_toolchain()) && !wsl) {
if (is_ucrt_toolchain() && !wsl) {
cmdstan_make_local(
dir = dir_cmdstan,
cpp_options = list(
"CXXFLAGS += -Wno-nonnull",
"TBB_CXXFLAGS= -U__MSVCRT_VERSION__ -D__MSVCRT_VERSION__=0x0E00"
"CXXFLAGS += -Wno-nonnull -D_UCRT",
"TBB_CXXFLAGS= -D_UCRT"
),
append = TRUE
)
Expand Down Expand Up @@ -534,13 +534,13 @@ build_status_ok <- function(process_log, quiet = FALSE) {

install_toolchain <- function(quiet = FALSE) {
rtools_usr_bin <- file.path(rtools4x_home_path(), "usr", "bin")
if (R.version$minor < "2.0") {
install_pkgs <- "mingw-w64-x86_64-make"
if (!quiet) message("Installing mingw32-make with Rtools40.")
} else {
ver <- ifelse(is_rtools43_toolchain(), "Rtools43", "Rtools42")
rtools_version <- paste0("Rtools", rtools4x_version())
if (is_ucrt_toolchain()) {
install_pkgs <- c("mingw-w64-ucrt-x86_64-make", "mingw-w64-ucrt-x86_64-gcc")
if (!quiet) message("Installing mingw32-make and g++ with ", ver, ".")
if (!quiet) message(paste0("Installing mingw32-make and g++ with ", rtools_version))
} else {
install_pkgs <- "mingw-w64-x86_64-make"
if (!quiet) message(paste0("Installing mingw32-make with ", rtools_version))
}
if (!checkmate::test_directory(rtools_usr_bin, access = "w")) {
warning("No write permissions in the RTools folder. This might prevent installing the toolchain.",
Expand Down Expand Up @@ -595,8 +595,8 @@ check_wsl_toolchain <- function() {
}

check_rtools4x_windows_toolchain <- function(fix = FALSE, quiet = FALSE) {
rtools_path <- rtools4x_home_path()
rtools_version <- if (is_rtools43_toolchain()) "Rtools43" else if (is_rtools42_toolchain()) "Rtools42" else "Rtools40"
rtools_path <- rtools_home_path()
rtools_version <- paste0("Rtools", rtools4x_version())
toolchain_path <- rtools4x_toolchain_path()
# If RTOOLS4X_HOME is not set (the env. variable gets set on install)
# we assume that RTools 40 is not installed.
Expand Down Expand Up @@ -794,7 +794,7 @@ is_toolchain_installed <- function(app, path) {

toolchain_PATH_env_var <- function() {
path <- NULL
if (is_rtools43_toolchain() || is_rtools42_toolchain() || is_rtools40_toolchain()) {
if (R.version$major == "4") {
rtools_home <- rtools4x_home_path()
path <- paste0(
repair_path(file.path(rtools_home, "usr", "bin")), ";",
Expand All @@ -805,34 +805,44 @@ toolchain_PATH_env_var <- function() {
}

rtools4x_toolchain_path <- function() {
if (is_rtools43_toolchain() || is_rtools42_toolchain()) {
path <- repair_path(file.path(rtools4x_home_path(), "ucrt64", "bin"))
c_runtime <- ifelse(is_ucrt_toolchain(), "ucrt64", "mingw64")
repair_path(file.path(rtools4x_home_path(), c_runtime, "bin"))
}

rtools4x_version <- function() {
rtools_ver <- NULL

if (R.version$minor < "2.0") {
rtools_ver <- "40"
} else if (R.version$minor < "3.0") {
rtools_ver <- "42"
} else {
path <- repair_path(file.path(rtools4x_home_path(), "mingw64", "bin"))
rtools_ver <- "43"
}
path
rtools_ver
}

rtools4x_home_path <- function() {
path <- NULL
if (is_rtools43_toolchain()) {
path <- Sys.getenv("RTOOLS43_HOME")
if (!nzchar(path)) {
default_path <- repair_path(file.path("C:/rtools43"))
if (dir.exists(default_path)) {
path <- default_path
}
}
} else if (is_rtools42_toolchain()) {
path <- Sys.getenv("RTOOLS42_HOME")
if (!nzchar(path)) {
default_path <- repair_path(file.path("C:/rtools42"))
if (dir.exists(default_path)) {
path <- default_path
}
rtools_ver <- rtools4x_version()
path <- Sys.getenv(paste0("RTOOLS", rtools_ver, "_HOME"))

if (!nzchar(path)) {
default_path <- repair_path(file.path(paste0("C:/rtools", rtools_ver)))
if (dir.exists(default_path)) {
path <- default_path
}
} else {
path <- Sys.getenv("RTOOLS40_HOME")
}

path
}

rtools_home_path <- function() {
path <- NULL
if (R.version$major == "3") {
path <- Sys.getenv("RTOOLS_HOME")
}
if (R.version$major == "4") {
path <- rtools4x_home_path()
}
path
}
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ is_rtools40_toolchain <- function() {
os_is_windows() && R.version$major == "4" && R.version$minor < "2.0"
}

is_ucrt_toolchain <- function() {
os_is_windows() && R.version$major == "4" && R.version$minor >= "2.0"
}

# Check if running R in Rosetta 2 translation environment, which is an
# Intel-to-ARM translation layer.
is_rosetta2 <- function() {
Expand Down
84 changes: 84 additions & 0 deletions vignettes/posterior.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
title: "Working with Posteriors"
output:
rmarkdown::html_vignette:
toc: true
toc_depth: 3
params:
EVAL: !r identical(Sys.getenv("NOT_CRAN"), "true")
vignette: >
%\VignetteIndexEntry{Working with Posteriors}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r child="children/settings-knitr.Rmd"}
```

## Summary

We can easily customise the summary statistics reported by `$summary()` and `$print()`.

```{r}
fit <- cmdstanr::cmdstanr_example("schools", method = "sample")
fit$summary()
```

By default all variables are summaries with the follow functions:
```{r}
posterior::default_summary_measures()
```

To change the variables summarised, we use the variables argument
```{r}
fit$summary(variables = c("mu", "tau"))
```

We can additionally change which functions are used
```{r}
fit$summary(variables = c("mu", "tau"), mean, sd)
```

To summarise all variables with non-default functions, it is necessary to set explicitly set the variables argument, either to `NULL` or the full vector of variable names.
```{r}
fit$metadata()$model_params
fit$summary(variables = NULL, "mean", "median")
```

Summary functions can be specified by character string, function, or using a formula (or anything else supported by [rlang::as_function]). If these arguments are named, those names will be used in the tibble output. If the summary results are named they will take precedence.
```{r}
my_sd <- function(x) c(My_SD = sd(x))
fit$summary(
c("mu", "tau"),
MEAN = mean,
"median",
my_sd,
~quantile(.x, probs = c(0.1, 0.9)),
Minimum = \(x) min(x)
)
```

Arguments to all summary functions can also be specified with `.args`.
```{r}
fit$summary(c("mu", "tau"), quantile, .args = list(probs = c(0.025, .05, .95, .975)))
```

The summary functions are applied to the array of sample values, with dimension `iter_sampling`x`chains`.
```{r}
fit$summary(variables = NULL, dim, colMeans)
```

For this reason users may have unexpected results if they use [stats::var()] directly, as it will return a covariance matrix. An alternative is the [distributional::variance] function.
```{r}
fit$summary(c("mu", "tau"), distributional::variance, ~var(as.vector(.x)))
```

Summary functions need not be numeric, but these won't work with `$print()`.

```{r}
strict_pos <- function(x) if (all(x > 0)) "yes" else "no"
fit$summary(variables = NULL, "Strictly Positive" = strict_pos)
# fit$print(variables = NULL, "Strictly Positive" = strict_pos)
```

For more information, see [posterior::summarise_draws()], which is is called by `$summary()`.

0 comments on commit 42b2e2c

Please sign in to comment.