Skip to content

Commit

Permalink
Merge pull request #389 from spsanderson/development
Browse files Browse the repository at this point in the history
Update to native pipe
  • Loading branch information
spsanderson authored Dec 28, 2023
2 parents f9e9c55 + 32d1281 commit abda956
Show file tree
Hide file tree
Showing 97 changed files with 312 additions and 310 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
URL: https://github.com/spsanderson/TidyDensity
BugReports: https://github.com/spsanderson/TidyDensity/issues
Depends:
R (>= 4.1.0)
Imports:
magrittr,
rlang (>= 0.4.11),
Expand Down
24 changes: 12 additions & 12 deletions R/augment-bootstrap-density.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
#' @examples
#' x <- mtcars$mpg
#'
#' tidy_bootstrap(x) %>%
#' tidy_bootstrap(x) |>
#' bootstrap_density_augment()
#'
#' tidy_bootstrap(x) %>%
#' bootstrap_unnest_tbl() %>%
#' tidy_bootstrap(x) |>
#' bootstrap_unnest_tbl() |>
#' bootstrap_density_augment()
#'
#' @return
Expand Down Expand Up @@ -56,26 +56,26 @@ bootstrap_density_augment <- function(.data) {

# Add density data
if (atb$tibble_type == "tidy_bootstrap_nested") {
df_tbl <- dplyr::as_tibble(.data) %>%
df_tbl <- dplyr::as_tibble(.data) |>
TidyDensity::bootstrap_unnest_tbl()
}

if (atb$tibble_type == "tidy_bootstrap") {
df_tbl <- dplyr::as_tibble(.data)
}

df_tbl <- df_tbl %>%
dplyr::nest_by(sim_number) %>%
df_tbl <- df_tbl |>
dplyr::nest_by(sim_number) |>
dplyr::mutate(dens_tbl = list(
stats::density(unlist(data),
n = nrow(data)
)[c("x", "y")] %>%
purrr::set_names("dx", "dy") %>%
)[c("x", "y")] |>
purrr::set_names("dx", "dy") |>
dplyr::as_tibble()
)) %>%
tidyr::unnest(cols = c(data, dens_tbl)) %>%
dplyr::mutate(x = dplyr::row_number()) %>%
dplyr::ungroup() %>%
)) |>
tidyr::unnest(cols = c(data, dens_tbl)) |>
dplyr::mutate(x = dplyr::row_number()) |>
dplyr::ungroup() |>
dplyr::select(sim_number, x, y, dx, dy, dplyr::everything())

# Return
Expand Down
4 changes: 2 additions & 2 deletions R/augment-bootstrap-p.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#'
#' @examples
#' x <- mtcars$mpg
#' tidy_bootstrap(x) %>%
#' bootstrap_unnest_tbl() %>%
#' tidy_bootstrap(x) |>
#' bootstrap_unnest_tbl() |>
#' bootstrap_p_augment(y)
#'
#' @return
Expand Down
4 changes: 2 additions & 2 deletions R/augment-bootstrap-q.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#' @examples
#' x <- mtcars$mpg
#'
#' tidy_bootstrap(x) %>%
#' bootstrap_unnest_tbl() %>%
#' tidy_bootstrap(x) |>
#' bootstrap_unnest_tbl() |>
#' bootstrap_q_augment(y)
#'
#' @return
Expand Down
34 changes: 17 additions & 17 deletions R/autoplot-combined-dist.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
#'
#' combined_tbl
#'
#' combined_tbl %>%
#' combined_tbl |>
#' tidy_combined_autoplot()
#'
#' combined_tbl %>%
#' combined_tbl |>
#' tidy_combined_autoplot(.plot_type = "qq")
#'
#' @return
Expand Down Expand Up @@ -126,7 +126,7 @@ tidy_combined_autoplot <- function(.data, .plot_type = "density", .line_size = .
}

if (plot_type == "density") {
plt <- data_tbl %>%
plt <- data_tbl |>
ggplot2::ggplot(
ggplot2::aes(
x = dx, y = dy,
Expand All @@ -144,16 +144,16 @@ tidy_combined_autoplot <- function(.data, .plot_type = "density", .line_size = .
ggplot2::theme(legend.position = leg_pos)
} else if (plot_type == "quantile") {
## EDIT
data_tbl <- data_tbl %>%
dplyr::select(sim_number, dist_type, q) %>%
dplyr::group_by(sim_number, dist_type) %>%
dplyr::arrange(q) %>%
dplyr::mutate(x = 1:dplyr::n() %>%
tidy_scale_zero_one_vec()) %>%
data_tbl <- data_tbl |>
dplyr::select(sim_number, dist_type, q) |>
dplyr::group_by(sim_number, dist_type) |>
dplyr::arrange(q) |>
dplyr::mutate(x = 1:dplyr::n() |>
tidy_scale_zero_one_vec()) |>
dplyr::ungroup()
## End EDIT
plt <- data_tbl %>%
dplyr::filter(q > -Inf, q < Inf) %>%
plt <- data_tbl |>
dplyr::filter(q > -Inf, q < Inf) |>
ggplot2::ggplot(
ggplot2::aes(
# x = tidy_scale_zero_one_vec(dx),
Expand All @@ -174,7 +174,7 @@ tidy_combined_autoplot <- function(.data, .plot_type = "density", .line_size = .
) +
ggplot2::theme(legend.position = leg_pos)
} else if (plot_type == "probability") {
plt <- data_tbl %>%
plt <- data_tbl |>
ggplot2::ggplot(
ggplot2::aes(
x = y,
Expand All @@ -192,7 +192,7 @@ tidy_combined_autoplot <- function(.data, .plot_type = "density", .line_size = .
) +
ggplot2::theme(legend.position = leg_pos)
} else if (plot_type == "qq") {
plt <- data_tbl %>%
plt <- data_tbl |>
ggplot2::ggplot(
ggplot2::aes(
sample = y,
Expand All @@ -210,10 +210,10 @@ tidy_combined_autoplot <- function(.data, .plot_type = "density", .line_size = .
) +
ggplot2::theme(legend.position = leg_pos)
} else if (plot_type == "mcmc") {
plt <- data_tbl %>%
dplyr::group_by(sim_number, dist_type) %>%
dplyr::mutate(cmy = dplyr::cummean(y)) %>%
dplyr::ungroup() %>%
plt <- data_tbl |>
dplyr::group_by(sim_number, dist_type) |>
dplyr::mutate(cmy = dplyr::cummean(y)) |>
dplyr::ungroup() |>
ggplot2::ggplot(ggplot2::aes(
x = x, y = cmy, group = interaction(dist_type, sim_number), color = sim_number
)) +
Expand Down
2 changes: 1 addition & 1 deletion R/autoplot-four-graphs.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#' will return an interactive `plotly` plot.
#'
#' @examples
#' tidy_normal(.num_sims = 5) %>%
#' tidy_normal(.num_sims = 5) |>
#' tidy_four_autoplot()
#'
#' @return
Expand Down
32 changes: 16 additions & 16 deletions R/autoplot-multi-dist.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
#' )
#' )
#'
#' tn %>%
#' tn |>
#' tidy_multi_dist_autoplot()
#'
#' tn %>%
#' tn |>
#' tidy_multi_dist_autoplot(.plot_type = "qq")
#'
#' @return
Expand Down Expand Up @@ -223,7 +223,7 @@ tidy_multi_dist_autoplot <- function(.data, .plot_type = "density", .line_size =
}

if (plot_type == "density" & atb$all$distribution_family_type == "continuous") {
plt <- data_tbl %>%
plt <- data_tbl |>
ggplot2::ggplot(
ggplot2::aes(x = dx, y = dy, group = interaction(dist_name, sim_number), color = dist_name)
) +
Expand All @@ -236,7 +236,7 @@ tidy_multi_dist_autoplot <- function(.data, .plot_type = "density", .line_size =
) +
ggplot2::theme(legend.position = leg_pos)
} else if (plot_type == "density" & atb$all$distribution_family_type == "discrete") {
plt <- data_tbl %>%
plt <- data_tbl |>
ggplot2::ggplot(
ggplot2::aes(x = y, group = interaction(dist_name, sim_number), fill = dist_name)
) +
Expand All @@ -253,14 +253,14 @@ tidy_multi_dist_autoplot <- function(.data, .plot_type = "density", .line_size =
ggplot2::theme(legend.position = leg_pos)
} else if (plot_type == "quantile") {
## EDIT
data_tbl <- data_tbl %>%
dplyr::select(sim_number, dist_name, q) %>%
dplyr::group_by(sim_number, dist_name) %>%
dplyr::arrange(q) %>%
dplyr::mutate(x = 1:dplyr::n()) %>%
data_tbl <- data_tbl |>
dplyr::select(sim_number, dist_name, q) |>
dplyr::group_by(sim_number, dist_name) |>
dplyr::arrange(q) |>
dplyr::mutate(x = 1:dplyr::n()) |>
dplyr::ungroup()
## End EDIT
plt <- data_tbl %>%
plt <- data_tbl |>
ggplot2::ggplot(
ggplot2::aes(
x = x, y = q, group = interaction(dist_name, sim_number), color = dist_name
Expand All @@ -275,7 +275,7 @@ tidy_multi_dist_autoplot <- function(.data, .plot_type = "density", .line_size =
) +
ggplot2::theme(legend.position = leg_pos)
} else if (plot_type == "probability") {
plt <- data_tbl %>%
plt <- data_tbl |>
ggplot2::ggplot(
ggplot2::aes(
x = y, group = interaction(dist_name, sim_number), color = dist_name
Expand All @@ -290,7 +290,7 @@ tidy_multi_dist_autoplot <- function(.data, .plot_type = "density", .line_size =
) +
ggplot2::theme(legend.position = leg_pos)
} else if (plot_type == "qq") {
plt <- data_tbl %>%
plt <- data_tbl |>
ggplot2::ggplot(
ggplot2::aes(
sample = y, group = interaction(dist_name, sim_number), color = dist_name
Expand All @@ -306,10 +306,10 @@ tidy_multi_dist_autoplot <- function(.data, .plot_type = "density", .line_size =
) +
ggplot2::theme(legend.position = leg_pos)
} else if (plot_type == "mcmc") {
plt <- data_tbl %>%
dplyr::group_by(sim_number, dist_name) %>%
dplyr::mutate(cmy = dplyr::cummean(y)) %>%
dplyr::ungroup() %>%
plt <- data_tbl |>
dplyr::group_by(sim_number, dist_name) |>
dplyr::mutate(cmy = dplyr::cummean(y)) |>
dplyr::ungroup() |>
ggplot2::ggplot(ggplot2::aes(
x = x, y = cmy, group = interaction(dist_name, sim_number), color = sim_number
)) +
Expand Down
14 changes: 7 additions & 7 deletions R/autoplot-randomwalk.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
#' will return an interactive `plotly` plot.
#'
#' @examples
#' tidy_normal(.sd = .1, .num_sims = 5) %>%
#' tidy_random_walk(.value_type = "cum_sum") %>%
#' tidy_normal(.sd = .1, .num_sims = 5) |>
#' tidy_random_walk(.value_type = "cum_sum") |>
#' tidy_random_walk_autoplot()
#'
#' tidy_normal(.sd = .1, .num_sims = 20) %>%
#' tidy_random_walk(.value_type = "cum_sum", .sample = TRUE, .replace = TRUE) %>%
#' tidy_normal(.sd = .1, .num_sims = 20) |>
#' tidy_random_walk(.value_type = "cum_sum", .sample = TRUE, .replace = TRUE) |>
#' tidy_random_walk_autoplot()
#'
#' @return
Expand All @@ -51,8 +51,8 @@ tidy_random_walk_autoplot <- function(.data, .line_size = .5, .geom_rug = FALSE,
atb <- attributes(.data)
n <- atb$all$.n
sims <- atb$all$.num_sims
# dist_type <- stringr::str_remove(atb$all$tibble_type, "tidy_") %>%
# stringr::str_replace_all(pattern = "_", " ") %>%
# dist_type <- stringr::str_remove(atb$all$tibble_type, "tidy_") |>
# stringr::str_replace_all(pattern = "_", " ") |>
# stringr::str_to_title()
dist_type <- dist_type_extractor(atb$all$tibble_type)

Expand Down Expand Up @@ -160,7 +160,7 @@ tidy_random_walk_autoplot <- function(.data, .line_size = .5, .geom_rug = FALSE,
data_tbl <- dplyr::as_tibble(.data)

# Plot ----
plt <- data_tbl %>%
plt <- data_tbl |>
ggplot2::ggplot(ggplot2::aes(
x = x, y = random_walk_value,
group = sim_number, color = sim_number
Expand Down
4 changes: 2 additions & 2 deletions R/bootstrap-unnest-tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' tb <- tidy_bootstrap(.x = mtcars$mpg)
#' bootstrap_unnest_tbl(tb)
#'
#' bootstrap_unnest_tbl(tb) %>%
#' bootstrap_unnest_tbl(tb) |>
#' tidy_distribution_summary_tbl(sim_number)
#'
#' @return
Expand All @@ -41,7 +41,7 @@ bootstrap_unnest_tbl <- function(.data) {
}

# Data ----
df <- tidyr::unnest(.data, bootstrap_samples) %>%
df <- tidyr::unnest(.data, bootstrap_samples) |>
purrr::set_names("sim_number", "y")

# Return ----
Expand Down
20 changes: 10 additions & 10 deletions R/combine-multi-single-dist-tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,22 @@ tidy_multi_single_dist <- function(.tidy_dist = NULL,
names(param_grid) <- methods::formalArgs(td)

# Run call on the grouped df ----
dff <- param_grid %>%
dff <- param_grid |>
dplyr::mutate(results = purrr::pmap(dplyr::pick(dplyr::everything()), match.fun(td)))
#dplyr::mutate(results = purrr::pmap(dplyr::cur_data(), match.fun(td)))

# Get the attributes to be used later on ----
atb <- dff$results[[1]] %>% attributes()
atb <- dff$results[[1]] |> attributes()

# Make Dist Type for column ----
# dist_type <- stringr::str_remove(atb$tibble_type, "tidy_") %>%
# stringr::str_replace_all(pattern = "_", " ") %>%
# dist_type <- stringr::str_remove(atb$tibble_type, "tidy_") |>
# stringr::str_replace_all(pattern = "_", " ") |>
# stringr::str_to_title()

dist_type <- dist_type_extractor(atb$tibble_type)

# Get column names from the param_grid in order to make teh dist_type column ----
cols <- names(param_grid %>% dplyr::select(-c(.n, .num_sims)))
cols <- names(param_grid |> dplyr::select(-c(.n, .num_sims)))

if (length(cols) == 1) {
dff$dist_name <- paste0(dist_type, " c(", dff[, cols], ")")
Expand All @@ -101,11 +101,11 @@ tidy_multi_single_dist <- function(.tidy_dist = NULL,
# ")"
# )

df_unnested_tbl <- dff %>%
tidyr::unnest(results) %>%
dplyr::ungroup() %>%
dplyr::select(sim_number, dist_name, x:q) %>%
dplyr::mutate(dist_name = as.factor(dist_name)) %>%
df_unnested_tbl <- dff |>
tidyr::unnest(results) |>
dplyr::ungroup() |>
dplyr::select(sim_number, dist_name, x:q) |>
dplyr::mutate(dist_name = as.factor(dist_name)) |>
dplyr::arrange(sim_number, dist_name)

# Attach attributes ----
Expand Down
6 changes: 3 additions & 3 deletions R/combine-tidy-distributions-tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ tidy_combine_distributions <- function(...) {
# Get the distribution type
dist_final_tbl <- purrr::map(
.x = dist_list,
.f = ~ .x %>%
dplyr::mutate(dist_type = attributes(.x)[["dist_with_params"]]) %>%
.f = ~ .x |>
dplyr::mutate(dist_type = attributes(.x)[["dist_with_params"]]) |>
dplyr::mutate(dist_type = as.factor(dist_type))
) %>%
) |>
purrr::map_dfr(dplyr::as_tibble)

attr(dist_final_tbl, "tibble_type") <- "tidy_multi_dist_combine"
Expand Down
6 changes: 3 additions & 3 deletions R/empirical-tidy-bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ tidy_bootstrap <- function(.x, .num_sims = 2000, .proportion = 0.8,
}

# Data ----
df <- dplyr::tibble(sim_number = as.factor(1:num_sims)) %>%
dplyr::group_by(sim_number) %>%
df <- dplyr::tibble(sim_number = as.factor(1:num_sims)) |>
dplyr::group_by(sim_number) |>
dplyr::mutate(bootstrap_samples = list(
sample(x = x_term, size = floor(prop * n), replace = TRUE)
)) %>%
)) |>
dplyr::ungroup()

# Attach descriptive attributes to tibble
Expand Down
Loading

0 comments on commit abda956

Please sign in to comment.