From 4efdb655d8d52f1392f1ef10d17298dbcb7db491 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98topepo=E2=80=99?= <‘mxkuhn@gmail.com’> Date: Mon, 22 Jan 2024 10:36:15 -0500 Subject: [PATCH] add an example --- DESCRIPTION | 2 +- R/bootci.R | 28 +++++++++++++++++++++++++++- man/int_pctl.Rd | 28 +++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 484f3281..b64c623a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -59,5 +59,5 @@ Config/Needs/website: tidyverse/tidytemplate Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.0 Config/testthat/edition: 3 diff --git a/R/bootci.R b/R/bootci.R index 89a378c3..d69bb97c 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -215,12 +215,13 @@ pctl_single <- function(stats, alpha = 0.05) { #' Application_. Cambridge: Cambridge University Press. #' doi:10.1017/CBO9780511802843 #' -#' @examplesIf rlang::is_installed("broom") +#' @examplesIf rlang::is_installed("broom") & rlang::is_installed("modeldata") #' \donttest{ #' library(broom) #' library(dplyr) #' library(purrr) #' library(tibble) +#' library(tidyr) #' #' # ------------------------------------------------------------------------------ #' @@ -256,6 +257,31 @@ pctl_single <- function(stats, alpha = 0.05) { #' bootstraps(Sacramento, 1000, apparent = TRUE) %>% #' mutate(correlations = map(splits, rank_corr)) %>% #' int_pctl(correlations) +#' +#' # ------------------------------------------------------------------------------ +#' # An example of computing the interval for each value of a custom grouping +#' # factor (type of house in this example) +#' +#' # Get regression estimates for each house type +#' lm_est <- function(split, ...) { +#' analysis(split) %>% +#' tidyr::nest(.by = c(type)) %>% +#' # Compute regression estimates for each house type +#' mutate( +#' betas = purrr::map(data, ~ lm(log10(price) ~ sqft, data = .x) %>% tidy()) +#' ) %>% +#' # Convert the column name to begin with a period +#' rename(.type = type) %>% +#' select(.type, betas) %>% +#' unnest(cols = betas) +#' } +#' +#' set.seed(52156) +#' house_rs <- +#' bootstraps(Sacramento, 1000, apparent = TRUE) %>% +#' mutate(results = map(splits, lm_est)) +#' +#' int_pctl(house_rs, results) #' } #' @export int_pctl <- function(.data, ...) { diff --git a/man/int_pctl.Rd b/man/int_pctl.Rd index 528f43a4..7200a6b0 100644 --- a/man/int_pctl.Rd +++ b/man/int_pctl.Rd @@ -66,12 +66,13 @@ that was used to create the statistics of interest and are computationally taxing. } \examples{ -\dontshow{if (rlang::is_installed("broom")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (rlang::is_installed("broom") & rlang::is_installed("modeldata")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \donttest{ library(broom) library(dplyr) library(purrr) library(tibble) +library(tidyr) # ------------------------------------------------------------------------------ @@ -107,6 +108,31 @@ data(Sacramento, package = "modeldata") bootstraps(Sacramento, 1000, apparent = TRUE) \%>\% mutate(correlations = map(splits, rank_corr)) \%>\% int_pctl(correlations) + +# ------------------------------------------------------------------------------ +# An example of computing the interval for each value of a custom grouping +# factor (type of house in this example) + +# Get regression estimates for each house type +lm_est <- function(split, ...) { + analysis(split) \%>\% + tidyr::nest(.by = c(type)) \%>\% + # Compute regression estimates for each house type + mutate( + betas = purrr::map(data, ~ lm(log10(price) ~ sqft, data = .x) \%>\% tidy()) + ) \%>\% + # Convert the column name to begin with a period + rename(.type = type) \%>\% + select(.type, betas) \%>\% + unnest(cols = betas) +} + +set.seed(52156) +house_rs <- + bootstraps(Sacramento, 1000, apparent = TRUE) \%>\% + mutate(results = map(splits, lm_est)) + +int_pctl(house_rs, results) } \dontshow{\}) # examplesIf} }