1
1
# ' Tidy parameter selection
2
2
# '
3
3
# ' @name tidy-params
4
- # ' @md
5
4
# '
6
- # ' @description As of version `1.7.0`, __bayesplot__ allows the `pars` argument
5
+ # ' @description As of version `1.7.0`, **bayesplot** allows the `pars` argument
7
6
# ' for [MCMC plots][bayesplot::MCMC-overview] to be used for so-called 'tidy'
8
- # ' variable selection (in the style of the __dplyr__ package). The
9
- # ' [`vars()`][dplyr::vars] function is re-exported from __dplyr__ for this
10
- # ' purpose.
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.
11
10
# '
12
11
# ' When using `pars` for tidy parameter selection, the `regex_pars` argument
13
- # ' is ignored because __bayesplot__ supports using [tidyselect helper
14
- # ' functions][tidyselect::select_helpers] (`starts_with()`, `contains()`, etc.)
15
- # ' for the same purpose.
12
+ # ' is ignored because **bayesplot** supports using
13
+ # ' [tidyselect helper functions][tidyselect::select_helpers]
14
+ # ' (`starts_with()`, `contains()`, etc.) for the same purpose.
16
15
# '
17
- # ' **bayesplot** also exports additional helper functions `param_range()` and
18
- # ' `param_glue ()` to help with parameter selection. See the **Examples**
19
- # ' section.
16
+ # ' **bayesplot** also exports additional helper functions
17
+ # ' `param_range ()` and `param_glue()`
18
+ # ' to help with parameter selection. See the **Examples** section.
20
19
# '
21
20
# ' @examples
22
- # '
23
21
# ' x <- example_mcmc_draws(params = 6)
24
22
# ' dimnames(x)
25
23
# ' mcmc_hex(x, pars = vars(alpha, `beta[2]`))
30
28
# ' mcmc_hist(x, pars = vars(param_range("beta", c(1, 3, 4))))
31
29
# '
32
30
# ' \donttest{
31
+ # ' ############################
32
+ # ' ## Example using rstanarm ##
33
+ # ' ############################
33
34
# ' if (requireNamespace("rstanarm", quietly = TRUE)) {
34
- # ' color_scheme_set("brightblue")
35
+ # ' # see ?rstanarm::example_model
35
36
# ' fit <- example("example_model", package = "rstanarm", local=TRUE)$value
36
- # ' posterior <- as.matrix(fit)
37
- # ' print(colnames(posterior))
37
+ # ' print(fit)
38
+ # ' posterior <- as.data.frame(fit)
39
+ # ' str(posterior)
38
40
# '
41
+ # ' color_scheme_set("brightblue")
39
42
# ' mcmc_hist(posterior, pars = vars(size, contains("period")))
43
+ # '
44
+ # ' # same as previous but using dplyr::select() and piping
45
+ # ' library(dplyr)
46
+ # ' posterior %>%
47
+ # ' select(size, contains("period")) %>%
48
+ # ' mcmc_hist()
49
+ # '
40
50
# ' mcmc_intervals(posterior, pars = vars(contains("herd")))
41
51
# ' mcmc_intervals(posterior, pars = vars(contains("herd"), -contains("Sigma")))
42
52
# '
43
53
# ' bayesplot_theme_set(ggplot2::theme_dark())
44
- # ' color_scheme_set("viridisB ")
54
+ # ' color_scheme_set("viridisC ")
45
55
# ' mcmc_areas_ridges(posterior, pars = vars(starts_with("b[")))
46
56
# '
57
+ # ' bayesplot_theme_set()
47
58
# ' color_scheme_set("purple")
48
- # ' mcmc_areas_ridges(posterior, pars = vars(starts_with("b["), -matches("[7-9]")))
49
- # '
50
- # ' # using param_glue() helper
51
- # ' pattern <- "b[(Intercept) herd:{level}]"
52
- # ' mcmc_intervals(posterior, pars = vars(param_glue(pattern, level = c(1, 4, 11))))
53
- # '
54
- # ' # using param_glue() with dplyr::select before passing to bayesplot
55
- # ' library(dplyr)
56
- # ' as.data.frame(fit) %>%
57
- # ' select(param_glue("b[(Intercept) herd:{level}]", level = c(1, 4, 11))) %>%
59
+ # ' not_789 <- vars(starts_with("b["), -matches("[7-9]"))
60
+ # ' mcmc_intervals(posterior, pars = not_789)
61
+ # '
62
+ # ' # using the param_glue() helper
63
+ # ' just_149 <- vars(param_glue("b[(Intercept) herd:{level}]", level = c(1,4,9)))
64
+ # ' mcmc_intervals(posterior, pars = just_149)
65
+ # '
66
+ # ' # same but using param_glue() with dplyr::select()
67
+ # ' # before passing to bayesplot
68
+ # ' posterior %>%
69
+ # ' select(param_glue("b[(Intercept) herd:{level}]",
70
+ # ' level = c(1, 4, 9))) %>%
58
71
# ' mcmc_intervals()
59
72
# ' }
60
- # ' }
61
- # '
73
+ # '}
62
74
# '
63
75
NULL
64
76
65
-
66
77
# re-export vars for tidy parameter selection
67
78
# ' @importFrom dplyr vars
68
79
# ' @export
69
80
dplyr :: vars
70
81
71
-
72
- # internal ----------------------------------------------------------------
73
-
74
- # ' Internal function for tidy parameter selection
75
- # '
76
- # ' This function is called internally by prepare_mcmc_array() if
77
- # ' the user's 'pars' argument is a quosure.
78
- # '
79
- # ' @noRd
80
- # ' @md
81
- # ' @param complete_pars A character vector of all parameter names.
82
- # ' @param pars_list A list of columns generated by `vars()`.
83
- # ' @return Character vector of selected parameter names.
84
- # '
85
- tidyselect_parameters <- function (complete_pars , pars_list ) {
86
- helpers <- tidyselect :: vars_select_helpers
87
- pars_list <- lapply(pars_list , rlang :: env_bury , !!! helpers )
88
- selected <- tidyselect :: vars_select(.vars = complete_pars , !!! pars_list )
89
- if (! length(selected )) {
90
- stop(" No parameters were found matching those names." , call. = FALSE )
91
- }
92
- return (unname(selected ))
93
- }
94
-
95
82
# ' @rdname tidy-params
96
83
# ' @export
97
- # ' @param prefix,range For `param_range()`, `prefix` is a string naming a
98
- # ' parameter and `range` is an integer vector of elements to select. For
99
- # ' example, using
84
+ # ' @param prefix,range For `param_range()` only , `prefix` is a string naming a
85
+ # ' parameter and `range` is an integer vector providing the indices of the
86
+ # ' subset of elements to select. For example, using
100
87
# '
101
88
# ' param_range("beta", c(1,2,8))
102
89
# '
103
90
# ' would select parameters named `beta[1]`, `beta[2]`, and `beta[8]`.
91
+ # ' `param_range()` is only designed for the case that the indices are integers
92
+ # ' surrounded by brackets.
104
93
# '
105
94
param_range <- function (prefix , range ) {
106
95
nms <- paste0(prefix , " [" , range , " ]" )
@@ -110,21 +99,83 @@ param_range <- function(prefix, range) {
110
99
111
100
# ' @rdname tidy-params
112
101
# ' @export
113
- # ' @param pattern,... For `param_glue()`, `pattern` is a string containing
114
- # ' expressions enclosed in braces and `...` should contain one character
115
- # ' vector per expression enclosed in braces. For example,
102
+ # ' @param pattern,... For `param_glue()` only, `pattern` is a string containing
103
+ # ' expressions enclosed in braces and `...` should be named arguments
104
+ # ' providing one character vector per expression in braces in `patten`.
105
+ # ' It easiest to describe how to use these arguments with an example:
116
106
# '
117
107
# ' param_glue("beta_{var}[{level}]",
118
108
# ' var = c("age", "income"),
119
109
# ' level = c(3,8))
120
110
# '
121
- # ' would select parameters with names `"beta_age[3]"`, `"beta_age[8]"`,
122
- # ' `"beta_income[3]"`, and `"beta_income[8]"`. See the end of the **Examples**
123
- # ' section below for demonstrations.
111
+ # ' would select parameters with names
112
+ # ' `"beta_age[3]"`, `"beta_income[3]"`, `"beta_age[8]"`, `"beta_income[8]"`.
113
+ # ' See the **Examples** section below for demonstrations.
114
+ # '
115
+ # ' @examples
116
+ # ' # more examples of param_glue()
117
+ # ' posterior <-
118
+ # ' structure(list(
119
+ # ' b_Intercept = rnorm(1000),
120
+ # ' sd_condition__Intercept = rexp(1000),
121
+ # ' sigma = rexp(1000),
122
+ # ' `r_condition[A,Intercept]` = rnorm(1000),
123
+ # ' `r_condition[B,Intercept]` = rnorm(1000),
124
+ # ' `r_condition[C,Intercept]` = rnorm(1000),
125
+ # ' `r_condition[A,Slope]` = rnorm(1000),
126
+ # ' `r_condition[B,Slope]` = rnorm(1000)
127
+ # ' ),
128
+ # ' class = c("tbl_df", "tbl", "data.frame"),
129
+ # ' row.names = c(NA, -1000L)
130
+ # ' )
131
+ # ' str(posterior)
132
+ # '
133
+ # ' posterior %>%
134
+ # ' select(
135
+ # ' param_glue(
136
+ # ' "r_condition[{level},Intercept]",
137
+ # ' level = c("A", "B"))
138
+ # ' ) %>%
139
+ # ' mcmc_hist()
140
+ # '
141
+ # ' posterior %>%
142
+ # ' select(
143
+ # ' param_glue(
144
+ # ' "r_condition[{level},{type}]",
145
+ # ' level = c("A", "B"),
146
+ # ' type = c("Intercept", "Slope"))
147
+ # ' ) %>%
148
+ # ' mcmc_hist()
149
+ # '
150
+ # '
124
151
# '
125
152
param_glue <- function (pattern , ... ) {
126
153
dots <- as.list(expand.grid(... ))
127
154
nms <- as.character(glue :: glue_data(dots , pattern ))
128
155
param_matches <- match(nms , tidyselect :: peek_vars())
129
156
param_matches [! is.na(param_matches )]
130
157
}
158
+
159
+
160
+ # internal ----------------------------------------------------------------
161
+
162
+ # ' Internal function for tidy parameter selection
163
+ # '
164
+ # ' This function is called internally by `prepare_mcmc_array()` if the user's
165
+ # ' `pars` argument is a quosure.
166
+ # '
167
+ # ' @noRd
168
+ # ' @md
169
+ # ' @param complete_pars A character vector of *all* parameter names.
170
+ # ' @param pars_list A list of columns generated by `vars()`.
171
+ # ' @return Character vector of selected parameter names.
172
+ # '
173
+ tidyselect_parameters <- function (complete_pars , pars_list ) {
174
+ helpers <- tidyselect :: vars_select_helpers
175
+ pars_list <- lapply(pars_list , rlang :: env_bury , !!! helpers )
176
+ selected <- tidyselect :: vars_select(.vars = complete_pars , !!! pars_list )
177
+ if (! length(selected )) {
178
+ stop(" No parameters were found matching those names." , call. = FALSE )
179
+ }
180
+ return (unname(selected ))
181
+ }
0 commit comments