Skip to content

Commit

Permalink
Version 0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
marberts committed Nov 15, 2023
1 parent 7cdb54e commit 134f898
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
^_pkgdown\.yml$
^docs$
^pkgdown$
^inst/logo\.R$
^man/figures$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*.Rproj
inst/doc
docs
pkgdown
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Description: Tools to build and work with bilateral generalized-mean
Balk (2008, ISBN:978-1-107-40496-0),
von der Lippe (2001, ISBN:3-8246-0638-0), and the
CPI manual (2020, ISBN:978-1-51354-298-0).
Depends: R (>= 3.5)
Depends: R (>= 4.0)
Imports: stats
Suggests:
knitr,
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Version 0.6.0

- Bumped minimum version of R to at least 4.0.

- The use of `...` in `grouped()` and `balanced()` is deprecated, and will be
removed in a future version. The same behavior can be had by using an
anonymous function.
Expand Down
32 changes: 26 additions & 6 deletions R/outliers.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
#'
#' @param x A strictly positive numeric vector of price relatives. These can be
#' made with, e.g., [back_period()].
#' @param cu,cl A numeric vector giving the upper and lower cutoffs for each
#' element of `x`. The usual recycling rules apply.
#' @param a A numeric vector between 0 and 1 giving the scale factor for the
#' median to establish the minimum dispersion between quartiles for each
#' element of `x`. The default does not set a minimum dispersion. The
#' usual recycling rules apply.
#' @param cu,cl A numeric vector, or something that can be coerced into one,
#' giving the upper and lower cutoffs for each element of `x`. Recycled to the
#' same length as `x`.
#' @param a A numeric vector, or something that can be coerced into one,
#' between 0 and 1 giving the scale factor for the median to establish the
#' minimum dispersion between quartiles for each element of `x`. The default
#' does not set a minimum dispersion. Recycled to the same length as `x`.
#' @param type See [quantile()].
#'
#' @returns
Expand Down Expand Up @@ -90,7 +91,13 @@
quartile_method <- function(x, cu = 2.5, cl = cu, a = 0, type = 7) {
x <- as.numeric(x)
cu <- as.numeric(cu)
# it's faster to not recycle cu, cl, or a when they're length 1
if (length(cu) != 1L) cu <- rep_len(cu, length(x))
cl <- as.numeric(cl)
if (length(cl) != 1L) cl <- rep_len(cl, length(x))
a <- as.numeric(a)
if (length(a) != 1L) a <- rep_len(a, length(x))

q <- stats::quantile(
x, c(0.25, 0.5, 0.75),
names = FALSE, na.rm = TRUE, type = type
Expand All @@ -107,7 +114,12 @@ quartile_method <- function(x, cu = 2.5, cl = cu, a = 0, type = 7) {
resistant_fences <- function(x, cu = 2.5, cl = cu, a = 0, type = 7) {
x <- as.numeric(x)
cu <- as.numeric(cu)
if (length(cu) != 1L) cu <- rep_len(cu, length(x))
cl <- as.numeric(cl)
if (length(cl) != 1L) cl <- rep_len(cl, length(x))
a <- as.numeric(a)
if (length(a) != 1L) a <- rep_len(a, length(x))

q <- stats::quantile(
x, c(0.25, 0.5, 0.75),
names = FALSE, na.rm = TRUE, type = type
Expand All @@ -124,7 +136,10 @@ resistant_fences <- function(x, cu = 2.5, cl = cu, a = 0, type = 7) {
robust_z <- function(x, cu = 2.5, cl = cu) {
x <- as.numeric(x)
cu <- as.numeric(cu)
if (length(cu) != 1L) cu <- rep_len(cu, length(x))
cl <- as.numeric(cl)
if (length(cl) != 1L) cl <- rep_len(cl, length(x))

med <- stats::median(x, na.rm = TRUE)
s <- stats::mad(x, na.rm = TRUE)
x <- x - med
Expand All @@ -139,7 +154,9 @@ robust_z <- function(x, cu = 2.5, cl = cu) {
fixed_cutoff <- function(x, cu = 2.5, cl = 1 / cu) {
x <- as.numeric(x)
cu <- as.numeric(cu)
if (length(cu) != 1L) cu <- rep_len(cu, length(x))
cl <- as.numeric(cl)
if (length(cl) != 1L) cl <- rep_len(cl, length(x))
x > cu | x < cl
}

Expand All @@ -149,7 +166,10 @@ fixed_cutoff <- function(x, cu = 2.5, cl = 1 / cu) {
tukey_algorithm <- function(x, cu = 2.5, cl = cu, type = 7) {
x <- as.numeric(x)
cu <- as.numeric(cu)
if (length(cu) != 1L) cu <- rep_len(cu, length(x))
cl <- as.numeric(cl)
if (length(cl) != 1L) cl <- rep_len(cl, length(x))

q <- stats::quantile(
x, c(0.05, 0.95),
names = FALSE, na.rm = TRUE, type = type
Expand Down
2 changes: 2 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ knitr::opts_chunk$set(
)
```

<a href="https://marberts.github.io/gpindex/"><img src="man/figures/logo.png" align="right" height="139" alt="gpindex website" /></a>

# Generalized Price and Quantity Indexes

<!-- Badges -->
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

<!-- README.md is generated from README.Rmd. Please edit that file. -->

<a href="https://marberts.github.io/gpindex/"><img src="man/figures/logo.png" align="right" height="139" alt="gpindex website" /></a>

# Generalized Price and Quantity Indexes

<!-- Badges -->
Expand Down
12 changes: 12 additions & 0 deletions inst/logo.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
library(ggplot2)
library(hexSticker)

sticker(ggplot() + theme_void(),
package = "gpindex",
filename = "man/figures/logo.png",
p_y = 1.2,
p_size = 18,
p_family = "mono",
p_color = "#97b3b3",
h_fill = "#f0f99f",
h_color = "#97b3b3")
Binary file added man/figures/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions man/gpindex-package.Rd

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

15 changes: 8 additions & 7 deletions man/outliers.Rd

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

22 changes: 22 additions & 0 deletions tests/testthat/test-outliers.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,25 @@ test_that("hb transform works", {
expect_equal(hb_transform(x),
ifelse(x < median(x), 1 - median(x) / x, x / median(x) - 1))
})

test_that("recycling works", {
expect_identical(fixed_cutoff(x, cl = rep(1 / 2.5, 11)), fixed_cutoff(x))
expect_identical(fixed_cutoff(x, cu = rep(2.5, 11)), fixed_cutoff(x))
expect_identical(fixed_cutoff(x, numeric(0)), rep(NA, 10))

expect_identical(robust_z(x, cl = rep(2.5, 11)), robust_z(x))
expect_identical(robust_z(x, cu = rep(2.5, 11)), robust_z(x))

expect_identical(quartile_method(x, cl = rep(2.5, 11)), quartile_method(x))
expect_identical(quartile_method(x, cu = rep(2.5, 11)), quartile_method(x))
expect_identical(quartile_method(x, a = rep(0, 11)), quartile_method(x))

expect_identical(resistant_fences(x, cl = rep(2.5, 11)), resistant_fences(x))
expect_identical(resistant_fences(x, cu = rep(2.5, 11)), resistant_fences(x))
expect_identical(resistant_fences(x, a = rep(0, 11)), resistant_fences(x))

expect_identical(tukey_algorithm(x, cl = rep(2.5, 11)), tukey_algorithm(x))
expect_identical(tukey_algorithm(x, cu = rep(2.5, 11)), tukey_algorithm(x))
expect_identical(tukey_algorithm(x, integer(0)),
replace(rep(NA, 10), c(6, 10), TRUE))
})

0 comments on commit 134f898

Please sign in to comment.