-
-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature tidyselect #188
Feature tidyselect #188
Conversation
Codecov Report
@@ Coverage Diff @@
## master #188 +/- ##
==========================================
+ Coverage 99.33% 99.35% +0.02%
==========================================
Files 30 31 +1
Lines 4212 4502 +290
==========================================
+ Hits 4184 4473 +289
- Misses 28 29 +1
Continue to review full report at Codecov.
|
Given that this PR makes it possible to use the tidyselect helpers (e.g., For example, if parameters are named mcmc_hist(..., pars = vars(num_range("beta", 2:7))) but param_range <- function(prefix, range) {
nms <- paste0(prefix, "[", range, "]")
param_matches <- match(nms, table = tidyselect::peek_vars())
param_matches[!is.na(param_matches)]
} which would enable mcmc_hist(..., pars = vars(param_range("beta", 2:7))) |
This is great. |
Ok cool I’ll add that to the PR. |
I was worried about mixed models where you get a lot going on the brackets, so I tried to make a general one. This would require glue, but glue is dependency-less and it is required by most of the tidyverse packages that we already depend on. library(tidyverse)
#> Registered S3 methods overwritten by 'ggplot2':
#> method from
#> [.quosures rlang
#> c.quosures rlang
#> print.quosures rlang
d <- structure(list(b_Intercept = c(0.25, 0.13, 0.77, 0.67, 0.44,
0.33), sd_condition__Intercept = c(1.01, 1.01, 0.78, 0.77, 0.89,
0.76), sigma = c(0.63, 0.54, 0.62, 0.59, 0.47, 0.44), `r_condition[A,Intercept]` = c(-0.2,
0.16, -0.82, -0.28, -0.53, -0.25), `r_condition[B,Intercept]` = c(0.98,
0.53, 0.44, 0.15, 0.74, 0.41), `r_condition[C,Intercept]` = c(1.33,
1.79, 0.83, 1.5, 1.15, 1.54), `r_condition[A,Slope]` = c(0.86,
0.78, 0.19, -0.14, 0.87, 0.49), `r_condition[B,Slope]` = c(-1.18,
-0.96, -1.66, -1.62, -1.53, -1.1), lp__ = c(-51.66, -51.15, -53.32,
-56.79, -56.48, -54.96)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L))
param_glue <- function(pattern, ...) {
dots <- as.list(expand.grid(...))
nms <- as.character(glue::glue_data(dots, pattern))
param_matches <- match(nms, tidyselect::peek_vars())
param_matches[!is.na(param_matches)]
}
d
#> # A tibble: 6 x 9
#> b_Intercept sd_condition__I~ sigma `r_condition[A,~ `r_condition[B,~
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.25 1.01 0.63 -0.2 0.98
#> 2 0.13 1.01 0.54 0.16 0.53
#> 3 0.77 0.78 0.62 -0.82 0.44
#> 4 0.67 0.77 0.59 -0.28 0.15
#> 5 0.44 0.89 0.47 -0.53 0.74
#> 6 0.33 0.76 0.44 -0.25 0.41
#> # ... with 4 more variables: `r_condition[C,Intercept]` <dbl>,
#> # `r_condition[A,Slope]` <dbl>, `r_condition[B,Slope]` <dbl>, lp__ <dbl>
d %>%
select(
param_glue(
"r_condition[{level},Intercept]",
level = c("A", "B"))
)
#> # A tibble: 6 x 2
#> `r_condition[A,Intercept]` `r_condition[B,Intercept]`
#> <dbl> <dbl>
#> 1 -0.2 0.98
#> 2 0.16 0.53
#> 3 -0.82 0.44
#> 4 -0.28 0.15
#> 5 -0.53 0.74
#> 6 -0.25 0.41
d %>%
select(
param_glue(
"r_condition[{level},{type}]",
level = c("A", "B"),
type = c("Intercept", "Slope"))
)
#> # A tibble: 6 x 4
#> `r_condition[A,In~ `r_condition[B,In~ `r_condition[A,S~ `r_condition[B,S~
#> <dbl> <dbl> <dbl> <dbl>
#> 1 -0.2 0.98 0.86 -1.18
#> 2 0.16 0.53 0.78 -0.96
#> 3 -0.82 0.44 0.19 -1.66
#> 4 -0.28 0.15 -0.14 -1.62
#> 5 -0.53 0.74 0.87 -1.53
#> 6 -0.25 0.41 0.49 -1.1 Created on 2019-05-20 by the reprex package (v0.3.0) |
Good idea! |
I’ll incorporate that. |
this matches tidyselect
I think this is ready to go. |
I expanded the documentation to |
Awesome, thanks! |
Closes #161
Closes #183
This PR allows the
pars
argument to MCMC plots be a list of quosures, as returned bydplyr::vars()
. Internally bayesplot will calltidyselect::vars_select()
ifpars
is a list of quosures. Ifpars
is just a character vector then the behavior is unchanged from previous versions of bayesplot, which preserves backwards compatibility.Example usage
Important changes for review
The important changes for review are in
helpers-mcmc.R
bayesplot/R/helpers-mcmc.R
Lines 31 to 38 in eef7bc8
and the new file
tidy-params.R
, which is mostly for examples of tidy parameter selection but also contains this new internal function:bayesplot/R/tidy-params.R
Lines 73 to 81 in eef7bc8