1
1
# ' Tidy parameter selection
2
2
# '
3
+ # ' Parameter selection in the style of **dplyr** and other tidyverse packages.
4
+ # '
3
5
# ' @name tidy-params
4
6
# '
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
+ # '
17
25
# ' * `param_range()`: like [`num_range()`][tidyselect::num_range] but used
18
26
# ' when parameter indexes are in brackets (e.g. `beta[2]`).
27
+ # '
19
28
# ' * `param_glue()`: for more complicated parameter names with multiple
20
29
# ' indexes (including variable names) inside the brackets
21
30
# ' (e.g., `beta[(Intercept) age_group:3]`).
22
31
# '
23
32
# ' These functions can be used inside of `vars()`, `dplyr::select()`,
24
33
# ' and similar functions, just like the
25
34
# ' [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`)``.
27
58
# '
28
59
# ' @seealso [glue::glue()]
29
60
# '
81
112
# ' mcmc_intervals()
82
113
# ' }
83
114
# '}
84
- # '
85
115
NULL
86
116
87
117
# re-export vars for tidy parameter selection
@@ -128,36 +158,29 @@ param_range <- function(prefix, range, vars = NULL) {
128
158
# '
129
159
# ' would select parameters with names
130
160
# ' `"beta_age[3]"`, `"beta_income[3]"`, `"beta_age[8]"`, `"beta_income[8]"`.
131
- # ' See the **Examples** section below for demonstrations.
132
161
# '
133
162
# ' @examples
134
163
# ' \dontrun{
135
164
# ' ###################################
136
165
# ' ## More examples of param_glue() ##
137
166
# ' ###################################
138
167
# ' 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
154
179
# '
155
180
# ' # using one expression in braces
156
181
# ' posterior %>%
157
182
# ' 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"))
161
184
# ' ) %>%
162
185
# ' mcmc_hist()
163
186
# '
@@ -171,7 +194,6 @@ param_range <- function(prefix, range, vars = NULL) {
171
194
# ' ) %>%
172
195
# ' mcmc_hist()
173
196
# '}
174
- # '
175
197
param_glue <- function (pattern , ... , vars = NULL ) {
176
198
if (! is.null(vars ) && ! is.character(vars )) {
177
199
abort(" 'vars' must be NULL or a character vector." )
@@ -191,17 +213,19 @@ param_glue <- function(pattern, ..., vars = NULL) {
191
213
# ' `pars` argument is a quosure.
192
214
# '
193
215
# ' @noRd
194
- # ' @md
195
216
# ' @param complete_pars A character vector of *all* parameter names.
196
217
# ' @param pars_list A list of columns generated by `vars()`.
197
218
# ' @return Character vector of selected parameter names.
198
- # '
199
219
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`.
200
224
helpers <- tidyselect :: vars_select_helpers
201
225
pars_list <- lapply(pars_list , rlang :: env_bury , !!! helpers )
202
226
selected <- tidyselect :: vars_select(.vars = complete_pars , !!! pars_list )
203
227
if (! length(selected )) {
204
228
abort(" No parameters were found matching those names." )
205
229
}
206
- return ( unname(selected ) )
230
+ unname(selected )
207
231
}
0 commit comments