Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
Merge 3e89bef into c6b0af6
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Jun 13, 2019
2 parents c6b0af6 + 3e89bef commit 45b3fdf
Show file tree
Hide file tree
Showing 65 changed files with 3,057 additions and 691 deletions.
20 changes: 11 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
Type: Package
Package: broomExtra
Title: Enhancements for 'broom' Package Family
Version: 0.0.3
Version: 0.0.3.9000
Authors@R:
person(given = "Indrajeet",
family = "Patil",
role = c("aut", "cre"),
role = c("aut", "cre", "ctb"),
email = "patilindrajeet.science@gmail.com",
comment = c(ORCID = "0000-0003-1995-6531"))
Maintainer: Indrajeet Patil <patilindrajeet.science@gmail.com>
Description: Collection of functions to assist 'broom' and
'broom.mixed' package-related data analysis workflows. In particular,
the generic functions tidy(), glance(), and augment() choose
appropriate S3 methods from these two packages depending on which
package exports the needed method. Additionally, 'grouped_' variants
of the generics provides a convenient way to execute functions across
a combination of grouping variable(s) in a dataframe.
package exports the needed method. Additionally, 'grouped_' and
'boot_' variants of the generics provides a convenient way to execute
functions across a combination of grouping variable(s) in a dataframe
or bootstrap them.
License: GPL-3 | file LICENSE
URL: https://indrajeetpatil.github.io/broomExtra/,
https://github.com/IndrajeetPatil/broomExtra
Expand All @@ -27,7 +28,10 @@ Imports:
broom.mixed (>= 0.2.4),
dplyr (>= 0.8.1),
magrittr (>= 1.5),
rlang (>= 0.3.4)
purrr (>= 0.3.2),
rlang (>= 0.3.4),
rsample (>= 0.0.4),
tidyr (>= 0.8.3)
Suggests:
covr,
gapminder,
Expand All @@ -39,9 +43,7 @@ Suggests:
spelling,
stringr,
testthat,
tibble,
tidyr,
utils
tibble
VignetteBuilder:
knitr
Encoding: UTF-8
Expand Down
9 changes: 8 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Generated by roxygen2: do not edit by hand

export("%<>%")
export("%>%")
export(augment)
export(boot_augment)
export(boot_glance)
export(boot_tidy)
export(glance)
export(grouped_augment)
export(grouped_glance)
Expand All @@ -18,10 +22,13 @@ importFrom(dplyr,group_map)
importFrom(dplyr,group_modify)
importFrom(dplyr,mutate)
importFrom(dplyr,ungroup)
importFrom(magrittr,"%<>%")
importFrom(magrittr,"%>%")
importFrom(purrr,map)
importFrom(rlang,"!!!")
importFrom(rlang,"!!")
importFrom(rlang,enquo)
importFrom(rlang,exec)
importFrom(rlang,quo_squash)
importFrom(utils,packageVersion)
importFrom(rsample,bootstraps)
importFrom(tidyr,unnest)
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# broomExtra 0.0.4

* Adds new functions for bootstrapping: `boot_tidy`, `boot_glance`,
`boot_augment`.

# broomExtra 0.0.3

* This is maintenance release to make the package compatible with `R 3.6.0`.
Expand Down
81 changes: 81 additions & 0 deletions R/boot_augment.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#' @title Bootstrapped dataframe with augmented predictions from each sample.
#' @name boot_augment
#' @author Indrajeet Patil
#'
#' @inheritParams grouped_augment
#' @inheritParams rsample::bootstraps
#'
#' @inherit augment return value
#' @inheritSection augment Methods
#'
#' @importFrom rlang !! !!! exec
#' @importFrom dplyr mutate
#' @importFrom purrr map
#' @importFrom tidyr unnest
#' @importFrom rsample bootstraps
#'
#' @examples
#' set.seed(123)
#'
#' # example-1: linear model
#' broomExtra::boot_augment(
#' data = mtcars,
#' times = 10,
#' ..f = stats::lm,
#' formula = mpg ~ wt,
#' na.action = na.omit
#' )
#'
#' # example-2: linear mixed-effects model
#' library(lme4)
#'
#' broomExtra::boot_augment(
#' data = sleepstudy,
#' times = 25,
#' ..f = lme4::lmer,
#' formula = Reaction ~ Days + (Days | Subject)
#' )
#' @export

# function
boot_augment <- function(data,
times = 25,
strata = NULL,
apparent = FALSE,
..f,
...,
augment.args = list()) {

# create a bootstrapped dataframe
boots <-
rsample::bootstraps(
data = data,
times = times,
strata = strata,
apparent = apparent
)

# `.x` and `.y` arguments, where `.x` is the data
augment_group <- function(.x, .y) {

# presumes `..f` will work with these args
model <- ..f(.y = ..., data = .x)

# variation on `do.call` to call function with list of arguments
rlang::exec(.fn = broomExtra::augment, model, !!!augment.args)
}

# execute function for each bootstrapped sample and tidy output
boots_fits <- boots %>%
dplyr::mutate(
.data = .,
augment_df = purrr::map(
.x = splits,
.f = augment_group
)
) %>%
tidyr::unnest(data = ., augment_df)

# return the final dataframe
return(boots_fits)
}
80 changes: 80 additions & 0 deletions R/boot_glance.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#' @title Bootstrapped dataframe with model summaries from each sample.
#' @name boot_glance
#' @author Indrajeet Patil
#'
#' @inheritParams grouped_glance
#' @inheritParams rsample::bootstraps
#'
#' @inherit glance return value
#' @inheritSection glance Methods
#'
#' @importFrom rlang !! !!! exec
#' @importFrom dplyr mutate
#' @importFrom purrr map
#' @importFrom tidyr unnest
#' @importFrom rsample bootstraps
#'
#' @examples
#' set.seed(123)
#'
#' # example-1: linear model
#' broomExtra::boot_glance(
#' data = mtcars,
#' times = 500,
#' ..f = stats::lm,
#' formula = mpg ~ wt,
#' na.action = na.omit
#' )
#'
#' # example-2: linear mixed-effects model
#' library(lme4)
#'
#' broomExtra::boot_glance(
#' data = sleepstudy,
#' times = 25,
#' ..f = lme4::lmer,
#' formula = Reaction ~ Days + (Days | Subject)
#' )
#' @export

# function
boot_glance <- function(data,
times = 25,
strata = NULL,
apparent = FALSE,
..f,
...) {

# create a bootstrapped dataframe
boots <-
rsample::bootstraps(
data = data,
times = times,
strata = strata,
apparent = apparent
)

# `.x` and `.y` arguments, where `.x` is the data
glance_group <- function(.x, .y) {

# presumes `..f` will work with these args
model <- ..f(.y = ..., data = .x)

# variation on `do.call` to call function with list of arguments
rlang::exec(.fn = broomExtra::glance, model)
}

# execute function for each bootstrapped sample and tidy output
boots_fits <- boots %>%
dplyr::mutate(
.data = .,
glance_df = purrr::map(
.x = splits,
.f = glance_group
)
) %>%
tidyr::unnest(data = ., glance_df)

# return the final dataframe
return(boots_fits)
}
83 changes: 83 additions & 0 deletions R/boot_tidy.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#' @title Bootstrapped dataframe with estimates from each sample.
#' @name boot_tidy
#' @author Indrajeet Patil
#'
#' @inheritParams grouped_tidy
#' @inheritParams rsample::bootstraps
#'
#' @inherit tidy return value
#' @inheritSection tidy Methods
#'
#' @importFrom rlang !! !!! exec
#' @importFrom dplyr mutate
#' @importFrom purrr map
#' @importFrom tidyr unnest
#' @importFrom rsample bootstraps
#'
#' @examples
#' set.seed(123)
#'
#' # example-1: linear model
#' broomExtra::boot_tidy(
#' data = mtcars,
#' times = 500,
#' ..f = stats::lm,
#' formula = mpg ~ wt,
#' na.action = na.omit,
#' tidy.args = list(conf.int = TRUE, conf.level = 0.50)
#' )
#'
#' # example-2: linear mixed-effects model
#' library(lme4)
#'
#' broomExtra::boot_tidy(
#' data = sleepstudy,
#' times = 25,
#' ..f = lme4::lmer,
#' formula = Reaction ~ Days + (Days | Subject),
#' tidy.args = list(effects = "fixed")
#' )
#' @export

# function
boot_tidy <- function(data,
times = 25,
strata = NULL,
apparent = FALSE,
..f,
...,
tidy.args = list()) {

# create a bootstrapped dataframe
boots <-
rsample::bootstraps(
data = data,
times = times,
strata = strata,
apparent = apparent
)

# `.x` and `.y` arguments, where `.x` is the data
tidy_group <- function(.x, .y) {

# presumes `..f` will work with these args
model <- ..f(.y = ..., data = .x)

# variation on `do.call` to call function with list of arguments
rlang::exec(.fn = broomExtra::tidy, model, !!!tidy.args)
}

# execute function for each bootstrapped sample and tidy output
boots_fits <- boots %>%
dplyr::mutate(
.data = .,
tidy_df = purrr::map(
.x = splits,
.f = tidy_group
)
) %>%
tidyr::unnest(data = ., tidy_df)

# return the final dataframe
return(boots_fits)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 5 additions & 1 deletion R/global_vars.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ utils::globalVariables(
"Q",
"tau",
"tau2",
"id"
"id",
"splits",
"augment_df",
"glance_df",
"tidy_df"
),
package = "broomExtra",
add = FALSE
Expand Down
3 changes: 2 additions & 1 deletion R/grouped_augment.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#' grouping.vars = c(cut, color),
#' formula = price ~ carat - 1,
#' ..f = stats::lm,
#' na.action = na.omit
#' na.action = na.omit,
#' augment.args = list(se_fit = TRUE)
#' )
#'
#' # linear mixed effects model
Expand Down
1 change: 0 additions & 1 deletion R/grouped_glance.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#'
#' @importFrom rlang !! !!! exec quo_squash enquo
#' @importFrom dplyr group_by ungroup mutate group_map group_modify
#' @importFrom utils packageVersion
#'
#' @inherit glance return value
#' @inheritSection glance Methods
Expand Down
7 changes: 7 additions & 0 deletions R/reexports.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' @export
#' @importFrom magrittr "%>%"
magrittr::`%>%`

#' @export
#' @importFrom magrittr "%<>%"
magrittr::`%<>%`
11 changes: 0 additions & 11 deletions R/utils-pipe.R

This file was deleted.

0 comments on commit 45b3fdf

Please sign in to comment.