diff --git a/DESCRIPTION b/DESCRIPTION index 8b7ce20..e7a979c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 = "stevemartin041@gmail.com", comment = c(ORCID = "0000-0003-2544-9480")) ) diff --git a/R/geks.R b/R/geks.R index 62abccd..99a4493 100644 --- a/R/geks.R +++ b/R/geks.R @@ -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 @@ -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( @@ -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") diff --git a/man/geks.Rd b/man/geks.Rd index 7da5be6..43d3601 100644 --- a/man/geks.Rd +++ b/man/geks.Rd @@ -57,15 +57,15 @@ to agree with \code{\link[=cut.Date]{cut()}}.} gives the corresponding product identifier for each element in \code{p} and \code{q}.} -\item{window}{The length of the rolling window. The default is a window that -encompasses all periods in \code{period}. Values that are neither integers -nor length 1 are silently truncated to a length 1 integer.} - -\item{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 -\code{window}, setting \code{n = 1} gives the index for period 13. The -default gives an index for each period in \code{window}. Values that are -neither integers nor length 1 are silently truncated to a length 1 integer.} +\item{window}{A positive integer giving the length of the rolling window. +The default is a window that encompasses all periods in \code{period}. +Non-integers are truncated towards zero.} + +\item{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 \code{window}, setting \code{n = 1} gives the index for period 13. The +default gives an index for each period in \code{window}. Non-integers are +truncated towards zero.} \item{na.rm}{Passed to \code{f} to control if missing values are removed.} } diff --git a/tests/testthat/test-geks.R b/tests/testthat/test-geks.R index 546bfb3..e5eb382 100644 --- a/tests/testthat/test-geks.R +++ b/tests/testthat/test-geks.R @@ -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( @@ -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)) + ) })