Skip to content

Commit

Permalink
Better argument checking for geks
Browse files Browse the repository at this point in the history
  • Loading branch information
marberts committed Oct 26, 2023
1 parent 2a5deba commit 0529acf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gpindex
Title: Generalized Price and Quantity Indexes
Version: 0.5.0.9005
Version: 0.5.0.9006
Authors@R: c(
person("Steve", "Martin", role = c("aut", "cre", "cph"), email = "[email protected]", comment = c(ORCID = "0000-0003-2544-9480"))
)
Expand Down
28 changes: 14 additions & 14 deletions R/geks.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ geks_matrix <- function(index, p, q, product, n, nper, window, na.rm) {
#' @param product A factor, or something that can be coerced into one, that
#' gives the corresponding product identifier for each element in `p` and
#' `q`.
#' @param window The length of the rolling window. The default is a window that
#' encompasses all periods in `period`. Values that are neither integers
#' nor length 1 are silently truncated to a length 1 integer.
#' @param n A number giving the length of the index series for each window,
#' starting from the end of the window. For example, if there are 13 periods in
#' `window`, setting `n = 1` gives the index for period 13. The
#' default gives an index for each period in `window`. Values that are
#' neither integers nor length 1 are silently truncated to a length 1 integer.
#' @param window A positive integer giving the length of the rolling window.
#' The default is a window that encompasses all periods in `period`.
#' Non-integers are truncated towards zero.
#' @param n A positive integer giving the length of the index series for each
#' window, starting from the end of the window. For example, if there are 13
#' periods in `window`, setting `n = 1` gives the index for period 13. The
#' default gives an index for each period in `window`. Non-integers are
#' truncated towards zero.
#' @param na.rm Passed to `f` to control if missing values are removed.
#'
#' @returns
Expand Down Expand Up @@ -154,9 +154,9 @@ geks <- function(f) {
return(list())
}

window <- as.integer(window[1L])
if (window < 2L) {
stop("'window' must be greater than or equal to 2")
window <- as.integer(window)
if (length(window) > 1L || window < 2L) {
stop("'window' must be a integer greater than or equal to 2")
}
if (window > nper) {
stop(
Expand All @@ -165,9 +165,9 @@ geks <- function(f) {
)
}

n <- as.integer(n[1L])
if (n < 1L) {
stop("'n' must be greater than or equal to 1")
n <- as.integer(n)
if (length(n) > 1L || n < 1L) {
stop("'n' must be an integer greater than or equal to 1")
}
if (n > window - 1L) {
stop("'n' must be less than or equal to 'window' minus 1")
Expand Down
18 changes: 9 additions & 9 deletions man/geks.Rd

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

10 changes: 8 additions & 2 deletions tests/testthat/test-geks.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ test_that("geks agrees with IndexNumR", {
1.28838882562161, 1.34795095779689, 1.40838221049365, 1.46984928172641,
1.53258374680655, 1.59693277586978, 1.66348762735518, 1.7335492978583)
)
(test <- with(
test <- with(
dat,
walsh_geks(price, quantity, period, product, 10, 3)
))
)
expect_equal(
cumprod(
as.numeric(
Expand Down Expand Up @@ -253,10 +253,16 @@ test_that("errors work for geks", {
expect_error(
with(dat, tornqvist_geks(price, quantity, period, product, n = 13))
)
expect_error(
with(dat, tornqvist_geks(price, quantity, period, product, n = 1:2))
)
expect_error(
with(dat, tornqvist_geks(price, quantity, period, product, window = 1))
)
expect_error(
with(dat, tornqvist_geks(price, quantity, period, product, window = 14))
)
expect_error(
with(dat, tornqvist_geks(price, quantity, period, product, window = 1:2))
)
})

0 comments on commit 0529acf

Please sign in to comment.