1
- # Prepare 3-D array for MCMC plots
2
- #
3
- # @param x,pars,regex_pars,transformations Users's arguments to one of the
4
- # mcmc_* functions.
5
- # @return A 3-D (Iterations x Chains x Parameters) array.
6
- #
1
+ # ' Prepare 3-D array for MCMC plots
2
+ # '
3
+ # ' @noRd
4
+ # ' @param x,pars,regex_pars,transformations Users's arguments to one of the
5
+ # ' mcmc_* functions.
6
+ # ' @return A 3-D (Iterations x Chains x Parameters) array.
7
+ # '
7
8
prepare_mcmc_array <- function (x ,
8
9
pars = character (),
9
10
regex_pars = character (),
@@ -29,12 +30,14 @@ prepare_mcmc_array <- function(x,
29
30
abort(" NAs not allowed in 'x'." )
30
31
}
31
32
32
- parnames <- parameter_names(x )
33
- pars <- select_parameters(
34
- explicit = pars ,
35
- patterns = regex_pars ,
36
- complete = parnames
37
- )
33
+ if (rlang :: is_quosures(pars )) {
34
+ pars <- tidyselect_parameters(complete_pars = parameter_names(x ),
35
+ pars_list = pars )
36
+ } else {
37
+ pars <- select_parameters(complete_pars = parameter_names(x ),
38
+ explicit = pars ,
39
+ patterns = regex_pars )
40
+ }
38
41
39
42
# possibly recycle transformations (apply same to all pars)
40
43
if (is.function(transformations ) ||
@@ -61,12 +64,63 @@ prepare_mcmc_array <- function(x,
61
64
}
62
65
63
66
64
- # Melt a 3-D array or matrix of MCMC draws
65
- #
66
- # @param x An mcmc_array (from prepare_mcmc_array).
67
- # @param varnames,value.name,... Passed to reshape2::melt (array method).
68
- # @return A molten data frame.
69
- #
67
+ # ' Explicit and/or regex parameter selection
68
+ # '
69
+ # ' @noRd
70
+ # ' @param explicit Character vector of selected parameter names.
71
+ # ' @param patterns Character vector of regular expressions.
72
+ # ' @param complete_pars Character vector of all possible parameter names.
73
+ # ' @return Character vector of combined explicit and matched (via regex)
74
+ # ' parameter names, unless an error is thrown.
75
+ # '
76
+ select_parameters <-
77
+ function (explicit = character (),
78
+ patterns = character (),
79
+ complete_pars = character ()) {
80
+
81
+ stopifnot(is.character(explicit ),
82
+ is.character(patterns ),
83
+ is.character(complete_pars ))
84
+
85
+ if (! length(explicit ) && ! length(patterns )) {
86
+ return (complete_pars )
87
+ }
88
+
89
+ if (length(explicit )) {
90
+ if (! all(explicit %in% complete_pars )) {
91
+ not_found <- which(! explicit %in% complete_pars )
92
+ abort(paste(
93
+ " Some 'pars' don't match parameter names:" ,
94
+ paste(explicit [not_found ], collapse = " , " ),
95
+ call. = FALSE
96
+ ))
97
+ }
98
+ }
99
+
100
+ if (! length(patterns )) {
101
+ return (unique(explicit ))
102
+ } else {
103
+ regex_pars <-
104
+ unlist(lapply(seq_along(patterns ), function (j ) {
105
+ grep(patterns [j ], complete_pars , value = TRUE )
106
+ }))
107
+
108
+ if (! length(regex_pars )) {
109
+ abort(" No matches for 'regex_pars'." )
110
+ }
111
+ }
112
+
113
+ unique(c(explicit , regex_pars ))
114
+ }
115
+
116
+
117
+ # ' Melt a 3-D array or matrix of MCMC draws
118
+ # '
119
+ # ' @noRd
120
+ # ' @param x An mcmc_array (from prepare_mcmc_array).
121
+ # ' @param varnames,value.name,... Passed to reshape2::melt (array method).
122
+ # ' @return A molten data frame.
123
+ # '
70
124
melt_mcmc <- function (x , ... ) UseMethod(" melt_mcmc" )
71
125
melt_mcmc.mcmc_array <- function (x ,
72
126
varnames =
@@ -103,9 +157,11 @@ melt_mcmc.matrix <- function(x,
103
157
long
104
158
}
105
159
106
- # Set dimnames of 3-D array
107
- # @param x 3-D array
108
- # @param parnames Character vector of parameter names
160
+ # ' Set dimnames of 3-D array
161
+ # ' @noRd
162
+ # ' @param x 3-D array
163
+ # ' @param parnames Character vector of parameter names
164
+ # ' @return x with a modified dimnames.
109
165
set_mcmc_dimnames <- function (x , parnames ) {
110
166
stopifnot(is_3d_array(x ))
111
167
dimnames(x ) <- list (
@@ -116,11 +172,12 @@ set_mcmc_dimnames <- function(x, parnames) {
116
172
structure(x , class = c(class(x ), " mcmc_array" ))
117
173
}
118
174
119
- # Convert 3-D array to matrix with chains merged
120
- #
121
- # @param x A 3-D array (iter x chain x param)
122
- # @return A matrix with one column per parameter
123
- #
175
+ # ' Convert 3-D array to matrix with chains merged
176
+ # '
177
+ # ' @noRd
178
+ # ' @param x A 3-D array (iter x chain x param)
179
+ # ' @return A matrix with one column per parameter
180
+ # '
124
181
merge_chains <- function (x ) {
125
182
xdim <- dim(x )
126
183
mat <- array (x , dim = c(prod(xdim [1 : 2 ]), xdim [3 ]))
@@ -129,10 +186,11 @@ merge_chains <- function(x) {
129
186
}
130
187
131
188
132
- # Check if an object is a data.frame with a chain index column
133
- #
134
- # @param x object to check
135
- # @return TRUE or FALSE
189
+ # ' Check if an object is a data.frame with a chain index column
190
+ # '
191
+ # ' @noRd
192
+ # ' @param x object to check
193
+ # ' @return TRUE or FALSE
136
194
is_df_with_chain <- function (x ) {
137
195
is.data.frame(x ) && any(tolower(colnames(x )) %in% " chain" )
138
196
}
@@ -167,11 +225,11 @@ df_with_chain2array <- function(x) {
167
225
}
168
226
169
227
170
- # Check if an object is a list (but not a data.frame) that contains
171
- # all 2-D objects
172
- #
173
- # @param x object to check
174
- # @return TRUE or FALSE
228
+ # ' Check if an object is a list (but not a data.frame) that contains
229
+ # ' all 2-D objects
230
+ # ' @noRd
231
+ # ' @param x object to check
232
+ # ' @return TRUE or FALSE
175
233
is_chain_list <- function (x ) {
176
234
check1 <- ! is.data.frame(x ) && is.list(x )
177
235
dims <- try(sapply(x , function (chain ) length(dim(chain ))), silent = TRUE )
@@ -316,13 +374,14 @@ validate_transformations <-
316
374
}
317
375
318
376
319
- # Apply transformations to matrix or 3-D array of parameter draws
320
- #
321
- # @param x A matrix or 3-D array of draws
322
- # @param transformation User's 'transformations' argument to one of the mcmc_*
323
- # functions.
324
- # @return x, with tranformations having been applied to some parameters.
325
- #
377
+ # ' Apply transformations to matrix or 3-D array of parameter draws
378
+ # '
379
+ # ' @noRd
380
+ # ' @param x A matrix or 3-D array of draws
381
+ # ' @param transformation User's 'transformations' argument to one of the mcmc_*
382
+ # ' functions.
383
+ # ' @return x, with tranformations having been applied to some parameters.
384
+ # '
326
385
apply_transformations <- function (x , transformations = list (), ... ) {
327
386
UseMethod(" apply_transformations" )
328
387
}
@@ -395,4 +454,3 @@ num_iters.data.frame <- function(x, ...) {
395
454
396
455
n
397
456
}
398
-
0 commit comments