Skip to content

Commit a1c1eec

Browse files
committed
expand documentation
1 parent ea9e854 commit a1c1eec

File tree

3 files changed

+126
-77
lines changed

3 files changed

+126
-77
lines changed

R/tidy-params.R

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,60 @@
11
#' Tidy parameter selection
22
#'
3+
#' Parameter selection in the style of **dplyr** and other tidyverse packages.
4+
#'
35
#' @name tidy-params
46
#'
5-
#' @description As of version `1.7.0`, **bayesplot** allows the `pars` argument
6-
#' for [MCMC plots][bayesplot::MCMC-overview] to be used for so-called 'tidy'
7-
#' variable selection (in the style of the **dplyr** package).
8-
#' The [`vars()`][dplyr::vars] function is re-exported from **dplyr**
9-
#' for this purpose. See the **Examples** section, below.
10-
#'
11-
#' When using `pars` for tidy parameter selection, the `regex_pars` argument
12-
#' is ignored because **bayesplot** supports using
13-
#' [tidyselect helper functions][tidyselect::select_helpers]
14-
#' (`starts_with()`, `contains()`, `num_range()`, etc.) for the same purpose.
15-
#' **bayesplot** also exports some additional helper functions
16-
#' to help with parameter selection:
7+
#' @details
8+
#' As of version `1.7.0`, **bayesplot** allows the `pars` argument for [MCMC
9+
#' plots][bayesplot::MCMC-overview] to use "tidy" variable selection (in the
10+
#' style of the **dplyr** package). The [`vars()`][dplyr::vars] function is
11+
#' re-exported from **dplyr** for this purpose.
12+
#'
13+
#' Features of tidy selection includes direct selection (`vars(alpha, sigma)`),
14+
#' everything-but selection (`vars(-alpha)`), ranged selection
15+
#' (``vars(`beta[1]`:`beta[3]`)``), support for selection functions
16+
#' (`vars(starts_with("beta"))`), and combinations of these features. See the
17+
#' **Examples** section, below.
18+
#'
19+
#' When using `pars` for tidy parameter selection, the `regex_pars` argument is
20+
#' ignored because **bayesplot** supports using [tidyselect helper
21+
#' functions][tidyselect::select_helpers] (`starts_with()`, `contains()`,
22+
#' `num_range()`, etc.) for the same purpose. **bayesplot** also exports some
23+
#' additional helper functions to help with parameter selection:
24+
#'
1725
#' * `param_range()`: like [`num_range()`][tidyselect::num_range] but used
1826
#' when parameter indexes are in brackets (e.g. `beta[2]`).
27+
#'
1928
#' * `param_glue()`: for more complicated parameter names with multiple
2029
#' indexes (including variable names) inside the brackets
2130
#' (e.g., `beta[(Intercept) age_group:3]`).
2231
#'
2332
#' These functions can be used inside of `vars()`, `dplyr::select()`,
2433
#' and similar functions, just like the
2534
#' [tidyselect helper functions][tidyselect::select_helpers].
26-
#' See the **Examples** section.
35+
#'
36+
#' @section Extra Advice:
37+
#'
38+
#' Parameter names in `vars()` are not quoted. When the names contain special
39+
#' characters like brackets, they should be wrapped in backticks, as in
40+
#' ``vars(`beta[1]`)``.
41+
#'
42+
#' To exclude a range of variables, wrap the sequence in parentheses and then
43+
#' negate it. For example, (``vars(-(`beta[1]`:`beta[3]`))``) would exclude
44+
#' `beta[1]`, `beta[2]`, and `beta[3]`.
45+
#'
46+
#' `vars()` is a helper function. It holds onto the names and expressions used
47+
#' to select columns. When selecting variables inside a **bayesplot**
48+
#' function, use `vars(...)`: `mcmc_hist(data, pars = vars(alpha))`. When
49+
#' using `select()` to prepare a dataframe for a **bayesplot** function, do
50+
#' not use `vars()`: `data %>% select(alpha) %>% mcmc_hist()`.
51+
#'
52+
#' Internally, tidy selection works by converting names and expressions
53+
#' into position numbers. As a result, integers will select parameters;
54+
#' `vars(1, 3)` selects the first and third ones. We do not endorse this
55+
#' approach because positions might change as variables are added and
56+
#' removed from models. To select a parameter that happens to be called `1`,
57+
#' use backticks to escape it ``vars(`1`)``.
2758
#'
2859
#' @seealso [glue::glue()]
2960
#'
@@ -81,7 +112,6 @@
81112
#' mcmc_intervals()
82113
#' }
83114
#'}
84-
#'
85115
NULL
86116

87117
# re-export vars for tidy parameter selection
@@ -128,36 +158,29 @@ param_range <- function(prefix, range, vars = NULL) {
128158
#'
129159
#' would select parameters with names
130160
#' `"beta_age[3]"`, `"beta_income[3]"`, `"beta_age[8]"`, `"beta_income[8]"`.
131-
#' See the **Examples** section below for demonstrations.
132161
#'
133162
#' @examples
134163
#' \dontrun{
135164
#' ###################################
136165
#' ## More examples of param_glue() ##
137166
#' ###################################
138167
#' library(dplyr)
139-
#' posterior <-
140-
#' structure(list(
141-
#' b_Intercept = rnorm(1000),
142-
#' sd_condition__Intercept = rexp(1000),
143-
#' sigma = rexp(1000),
144-
#' `r_condition[A,Intercept]` = rnorm(1000),
145-
#' `r_condition[B,Intercept]` = rnorm(1000),
146-
#' `r_condition[C,Intercept]` = rnorm(1000),
147-
#' `r_condition[A,Slope]` = rnorm(1000),
148-
#' `r_condition[B,Slope]` = rnorm(1000)
149-
#' ),
150-
#' class = c("tbl_df", "tbl", "data.frame"),
151-
#' row.names = c(NA, -1000L)
152-
#' )
153-
#' str(posterior)
168+
#' posterior <- tibble(
169+
#' b_Intercept = rnorm(1000),
170+
#' sd_condition__Intercept = rexp(1000),
171+
#' sigma = rexp(1000),
172+
#' `r_condition[A,Intercept]` = rnorm(1000),
173+
#' `r_condition[B,Intercept]` = rnorm(1000),
174+
#' `r_condition[C,Intercept]` = rnorm(1000),
175+
#' `r_condition[A,Slope]` = rnorm(1000),
176+
#' `r_condition[B,Slope]` = rnorm(1000)
177+
#' )
178+
#' posterior
154179
#'
155180
#' # using one expression in braces
156181
#' posterior %>%
157182
#' select(
158-
#' param_glue(
159-
#' "r_condition[{level},Intercept]",
160-
#' level = c("A", "B"))
183+
#' param_glue("r_condition[{level},Intercept]", level = c("A", "B"))
161184
#' ) %>%
162185
#' mcmc_hist()
163186
#'
@@ -171,7 +194,6 @@ param_range <- function(prefix, range, vars = NULL) {
171194
#' ) %>%
172195
#' mcmc_hist()
173196
#'}
174-
#'
175197
param_glue <- function(pattern, ..., vars = NULL) {
176198
if (!is.null(vars) && !is.character(vars)) {
177199
abort("'vars' must be NULL or a character vector.")
@@ -191,17 +213,19 @@ param_glue <- function(pattern, ..., vars = NULL) {
191213
#' `pars` argument is a quosure.
192214
#'
193215
#' @noRd
194-
#' @md
195216
#' @param complete_pars A character vector of *all* parameter names.
196217
#' @param pars_list A list of columns generated by `vars()`.
197218
#' @return Character vector of selected parameter names.
198-
#'
199219
tidyselect_parameters <- function(complete_pars, pars_list) {
220+
# We use the list of helpers so that we don't have to keep track of any
221+
# changes to tidyselect. We use `env_bury()`` so that the definitions of
222+
# selection helpers are available. This pattern is taken from the example code
223+
# in `vars_select_helpers`.
200224
helpers <- tidyselect::vars_select_helpers
201225
pars_list <- lapply(pars_list, rlang::env_bury, !!! helpers)
202226
selected <- tidyselect::vars_select(.vars = complete_pars, !!! pars_list)
203227
if (!length(selected)) {
204228
abort("No parameters were found matching those names.")
205229
}
206-
return(unname(selected))
230+
unname(selected)
207231
}

man/MCMC-scatterplots.Rd

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tidy-params.Rd

Lines changed: 58 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)