Skip to content

Commit

Permalink
Add feat_diff_summary() functions to help summarise diff(). Useful …
Browse files Browse the repository at this point in the history
…for exploring the time gaps in the `index`. (#100)
  • Loading branch information
njtierney committed Sep 9, 2020
1 parent cf66d11 commit 141c101
Show file tree
Hide file tree
Showing 18 changed files with 161 additions and 6 deletions.
9 changes: 9 additions & 0 deletions NAMESPACE
Expand Up @@ -10,6 +10,14 @@ export("%>%")
export(add_key_slope)
export(add_n_obs)
export(as_tsibble)
export(b_diff_max)
export(b_diff_mean)
export(b_diff_median)
export(b_diff_min)
export(b_diff_q25)
export(b_diff_q75)
export(b_diff_sd)
export(b_diff_var)
export(b_iqr)
export(b_mad)
export(b_max)
Expand All @@ -26,6 +34,7 @@ export(decreasing)
export(facet_sample)
export(facet_strata)
export(feat_brolgar)
export(feat_diff_summary)
export(feat_five_num)
export(feat_monotonic)
export(feat_ranges)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
@@ -1,5 +1,6 @@
# brolgar 0.0.6.9000

* Add `feat_diff_summary()` functions to help summarise diff(). Useful for exploring the time gaps in the `index`. (#100)
* sample functions now work with multiple keys (#85, #89) (Thanks to @earowang and @deanmarchiori for their help with this.)
* `facet_sample()` now has a default of 3 per plot
* resolve features(data ,.key, n_obs) error (#71)
Expand Down
55 changes: 54 additions & 1 deletion R/b_summaries.R
Expand Up @@ -16,7 +16,12 @@
#' * b_var: The variance
#' * b_mad: The mean absolute deviation
#' * b_iqr: The Inter-quartile range
#'
#' * b_diff_var: The variance diff()
#' * b_diff_sd: The standard deviation of diff()
#' * b_diff_mean: The mean of diff()
#' * b_diff_median: The median of diff()
#' * b_diff_q25: The q25 of diff()
#' * b_diff_q75: The q75 of diff()
#'
#' @param x a vector
#' @param na.rm whether to remove NA values. Default is TRUE
Expand Down Expand Up @@ -105,6 +110,54 @@ b_iqr <- function(x, na.rm = TRUE, ... ){
stats::IQR(x, na.rm = na.rm, type = 8, ...)
}

#' @name b_summaries
#' @export
b_diff_var <- function(x, na.rm = TRUE, ...){
stats::var(diff(x, na.rm = na.rm, ...))
}

#' @name b_summaries
#' @export
b_diff_sd <- function(x, na.rm = TRUE, ...){
stats::sd(diff(x, na.rm = na.rm, ...))
}

#' @name b_summaries
#' @export
b_diff_mean <- function(x, na.rm = TRUE, ...){
mean(diff(x, na.rm = na.rm, ...))
}

#' @name b_summaries
#' @export
b_diff_median <- function(x, na.rm = TRUE, ...){
stats::median(diff(x, na.rm = na.rm, ...))
}

#' @name b_summaries
#' @export
b_diff_q25 <- function(x, na.rm = TRUE, ...){
b_q25(diff(x, na.rm = na.rm, ...))
}

#' @name b_summaries
#' @export
b_diff_q75 <- function(x, na.rm = TRUE, ...){
b_q75(diff(x, na.rm = na.rm, ...))
}

#' @name b_summaries
#' @export
b_diff_max <- function(x, na.rm = TRUE, ...){
b_max(diff(x, na.rm = na.rm, ...))
}

#' @name b_summaries
#' @export
b_diff_min <- function(x, na.rm = TRUE, ...){
b_min(diff(x, na.rm = na.rm, ...))
}

# * `l_n_obs()` Number of observations
# * `l_slope()` Slope and intercept (given some linear model formula)

Expand Down
3 changes: 3 additions & 0 deletions R/data-world-height.R
Expand Up @@ -32,6 +32,8 @@
#' group = country)) +
#' geom_line()
#'
#' \dontrun{
#'
#' # Explore all samples with `facet_strata()`
#' ggplot(heights,
#' aes(x = year,
Expand All @@ -51,5 +53,6 @@
#' # explore the five number summary of height_cm with `features`
#' heights %>%
#' features(height_cm, feat_five_num)
#' }
#'
"heights"
2 changes: 2 additions & 0 deletions R/facet-sample.R
Expand Up @@ -21,13 +21,15 @@
#' geom_line() +
#' facet_sample()
#'
#' \dontrun{
#' ggplot(heights,
#' aes(x = year,
#' y = height_cm,
#' group = country)) +
#' geom_line() +
#' facet_sample(n_per_facet = 1,
#' n_facets = 12)
#' }
facet_sample <- function(n_per_facet = 3,
n_facets = 12,
nrow = NULL,
Expand Down
3 changes: 3 additions & 0 deletions R/facet-strata.R
Expand Up @@ -19,6 +19,8 @@
#' group = country)) +
#' geom_line() +
#' facet_strata()
#'
#' \dontrun{
#'
#' ggplot(heights,
#' aes(x = year,
Expand All @@ -43,6 +45,7 @@
#' geom_line(aes(group = id)) +
#' geom_smooth(method = "lm") +
#' facet_strata(along = .slope_xp)
#' }

facet_strata <- function(n_strata = 12,
along = NULL,
Expand Down
14 changes: 14 additions & 0 deletions R/features.R
Expand Up @@ -98,3 +98,17 @@ feat_brolgar <- function(x, ...){
)
}

#' @rdname brolgar-features
#' @export
feat_diff_summary <- function(x, ...){
c(
diff_var = b_diff_var(x, ...),
diff_sd = b_diff_sd(x, ...),
diff_min = b_diff_min(x, ...),
diff_q25 = b_diff_q25(x, ...),
diff_mean = b_diff_mean(x, ...),
diff_median = b_diff_median(x, ...),
diff_q75 = b_diff_q75(x, ...),
diff_max = b_diff_max(x, ...)
)
}
2 changes: 2 additions & 0 deletions R/monotonics.R
Expand Up @@ -33,6 +33,7 @@
#' unvarying(vec_ran)
#' unvarying(vec_flat)
#'
#' \dontrun{
#' library(ggplot2)
#' library(gghighlight)
#' library(dplyr)
Expand Down Expand Up @@ -68,6 +69,7 @@
#' y = ln_wages,
#' group = id)) +
#' geom_line()
#' }
#'
increasing <- function(x, ...){

Expand Down
3 changes: 2 additions & 1 deletion R/stratify-keys.R
Expand Up @@ -30,7 +30,7 @@
#' facet_wrap(~.strata)
#'
#' # now facet along some feature
#'
#' \dontrun{
#' library(dplyr)
#' wages %>%
#' key_slope(ln_wages ~ xp) %>%
Expand All @@ -53,6 +53,7 @@
#' group = id)) +
#' geom_line() +
#' facet_wrap(~.strata)
#' }
stratify_keys <- function(.data, n_strata, along = NULL, fun = mean, ...){
test_if_tsibble(.data)
test_if_null(.data)
Expand Down
30 changes: 30 additions & 0 deletions man/b_summaries.Rd

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

3 changes: 3 additions & 0 deletions man/brolgar-features.Rd

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

2 changes: 2 additions & 0 deletions man/facet_sample.Rd

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

3 changes: 3 additions & 0 deletions man/facet_strata.Rd

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

3 changes: 3 additions & 0 deletions man/heights.Rd

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

2 changes: 2 additions & 0 deletions man/monotonic.Rd

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

3 changes: 2 additions & 1 deletion man/stratify_keys.Rd

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

26 changes: 26 additions & 0 deletions tests/testthat/test-feature-diff-summary.R
@@ -0,0 +1,26 @@
context("feature diff summary")

heights_diff_summary <- heights %>%
features(year, feat_diff_summary)

test_that("feat_diff_summary returns the right names", {
expect_equal(names(heights_diff_summary),
c("country",
"diff_var",
"diff_sd",
"diff_min",
"diff_q25",
"diff_mean",
"diff_median",
"diff_q75",
"diff_max"))
})

test_that("feat_diff_summary returns the right dimensions", {
expect_equal(dim(heights_diff_summary),
c(144, 9))
})

test_that("feat_diff_summary returns all ids", {
expect_equal(dplyr::n_distinct(heights_diff_summary$country), 144)
})
3 changes: 0 additions & 3 deletions tests/testthat/test-sample-n-frac-multiple-keys.R
Expand Up @@ -7,8 +7,6 @@ context("multiple keys supported")

data("aus_retail")

aus_retail

# will fail
test_that("multiple keys doesn't fail",{
expect_equal(ncol(sample_n_keys(aus_retail, size = 10)), 5)
Expand Down Expand Up @@ -37,7 +35,6 @@ sample_n_keys_retail_nkeys <- sample_n_keys(aus_retail, size = 10)
sample_frac_keys_retail_nkeys <- sample_frac_keys(aus_retail, size = 0.11)
n_keys(sample_frac_keys_retail_nkeys)

aus_retail

test_that("correct number of columns returned",{
expect_equal(ncol(sample_n_keys_retail_nkeys), ncol(aus_retail))
Expand Down

0 comments on commit 141c101

Please sign in to comment.