Skip to content

Commit

Permalink
Fixes #189
Browse files Browse the repository at this point in the history
  • Loading branch information
spsanderson committed May 23, 2022
1 parent 01bcdf3 commit 85807f0
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Description:
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
RoxygenNote: 7.2.0
URL: https://github.com/spsanderson/TidyDensity
BugReports: https://github.com/spsanderson/TidyDensity/issues
Imports:
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export(td_scale_fill_colorblind)
export(tidy_autoplot)
export(tidy_beta)
export(tidy_binomial)
export(tidy_bootstrap)
export(tidy_burr)
export(tidy_cauchy)
export(tidy_chisquare)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ None
1. Fix #181 - Add functions `color_blind()` `td_scale_fill_colorblind()` and
`td_scale_color_colorblind()`
2. Fix #187 - Add functions `ci_lo()` and `ci_hi()`
3. Fix #189 - Add function `tidy_bootstrap()`

## Minor Fixes and Improvements
1. Fix #176 - Update `_autoplot` functions to include cumulative mean MCMC chart
Expand Down
95 changes: 95 additions & 0 deletions R/empirical-tidy-bootstrap.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#' Bootstrap Empirical Data
#'
#' @family Bootstrap
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @details This function will take in a numeric input vector and produce a tibble
#' of bootstrapped values in a list. The table that is output will have two columns:
#' `sim_number` and `bootstrap_samples`
#'
#' The `sim_number` corresponds to how many times you want the data to be resampled,
#' and the `bootstrap_samples` column contains a list of the boostrapped resampled
#' data.
#'
#' @description Takes an input vector of numeric data and produces a bootstrapped
#' nested tibble by simulation number.
#'
#' @param .x The vector of data being passed to the function. Must be a numeric
#' vector.
#' @param .num_sims The default is 2000, can be set to anything desired. A warning
#' will pass to the console if the value is less than 2000.
#' @param .proportion How much of the original data do you want to pass through
#' to the sampling function. The default is 0.80 (80%)
#' @param .distribution_type This can either be 'continuous' or 'discrete'
#'
#' @examples
#' x <- mtcars$mpg
#' tidy_bootstrap(x)
#'
#' @return
#' A nested tibble
#'
#' @export
#'

tidy_bootstrap <- function(.x, .num_sims = 2000, .proportion = 0.8,
.distribution_type = "continuous"){

# Tidyeval ----
x_term <- as.numeric(.x)
n <- length(x_term)
dist_type <- tolower(as.character(.distribution_type))
num_sims <- as.integer(.num_sims)
size <- as.numeric(.proportion)

# Checks ----
if (!is.vector(x_term)) {
rlang::abort(
message = "You must pass a vector as the .x argument to this function.",
use_cli_format = TRUE
)
}

if (size < 0 | size > 1){
rlang::abort(
message = "The '.proportion' parameter must be between 0 and 1 inclusive.",
use_cli_format = TRUE
)
}

if (!dist_type %in% c("continuous","discrete")){
rlang::abort(
message = "You must choose either 'continuous' or 'discrete'.",
use_cli_format = TRUE
)
}

if (num_sims < 2000){
rlang::warn(
message = "Setting '.num_sims' to less than 2000 means that results can be
potentially unstable. Consider setting to 2000 or more.",
use_cli_format = TRUE
)
}

# Data ----
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(size * n) ,replace = TRUE)
)
) %>%
dplyr::ungroup()

# Attach descriptive attributes to tibble
attr(df, "distribution_family_type") <- dist_type
attr(df, ".x") <- .x
attr(df, ".num_sims") <- .num_sims
attr(df, "tibble_type") <- "tidy_bootstrap"
attr(df, "dist_with_params") <- "Empirical"

# Return ----
return(df)

}
4 changes: 4 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ reference:
desc: Function for creating mixture model data
contents:
- has_concept("Mixture Data")
- title: Bootstrap
desc: Functions for bootstrapping data
contents:
- has_concept("Bootstrap")
- title: Parameter Estimation Functions
desc: Functions that help to estimate parameters from raw data.
contents:
Expand Down
50 changes: 50 additions & 0 deletions man/tidy_bootstrap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 20 additions & 10 deletions man/tidyeval.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 85807f0

Please sign in to comment.