Skip to content

Commit

Permalink
splice_index() keeps names (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
marberts committed Aug 1, 2024
1 parent 9068631 commit 6553c19
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 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.6.1.9001
Version: 0.6.1.9002
Authors@R: c(
person("Steve", "Martin", role = c("aut", "cre", "cph"),
email = "[email protected]",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 0.6.2

- `splice_index()` now keeps names.

## Version 0.6.1

- Updated maintainer email.
Expand Down
4 changes: 2 additions & 2 deletions R/splice.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ splice_index <- function(x, periods = NULL, initial = NULL, published = FALSE) {
}

y <- lapply(x, \(z) rev(cumprod(rev(z)))[periods])
res <- numeric(offset + length(x))
res[seq_along(initial)] <- initial
# Use recursive = TRUE to keep names.
res <- c(initial, lapply(x, \(x) x[length(x)]), recursive = TRUE)

iw <- seq.int(to = offset - 1L, length.out = n)[periods]
if (published) {
Expand Down
6 changes: 4 additions & 2 deletions tests/Examples/gpindex-Ex.Rout.save
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,14 @@ Warning in back_period(period) :
> # Use a movement splice to combine the indexes in each window
>
> splice_index(tg, 2)
[1] 1.391443 1.801142 2.230521 2.689833
2 3 4 5
1.391443 1.801142 2.230521 2.689833
>
> # ... or use a mean splice
>
> splice_index(tg)
[1] 1.391443 1.801142 2.228836 2.687826
2 3 4 5
1.391443 1.801142 2.228836 2.687826
>
> #---- Missing data ----
>
Expand Down
27 changes: 18 additions & 9 deletions tests/testthat/test-geks.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,27 +177,36 @@ test_that("geks works with different splices", {
splice_index(
with(dat, tornqvist_geks(price, quantity, period, product, window = 7))
),
c(1.06202143784605, 1.1203648908828, 1.17858384039388, 1.23760852899173,
1.29782138422918, 1.35944797067126, 1.4190362067647, 1.48010601630221,
1.5422495225642, 1.60541380403309, 1.66962623911985, 1.73494499443271)
setNames(
c(1.06202143784605, 1.1203648908828, 1.17858384039388, 1.23760852899173,
1.29782138422918, 1.35944797067126, 1.4190362067647, 1.48010601630221,
1.5422495225642, 1.60541380403309, 1.66962623911985, 1.73494499443271),
2:13
)
)
expect_equal(
splice_index(
with(dat, fisher_geks(price, quantity, period, product, window = 3)),
periods = 1
),
c(1.05490954999787, 1.11049864213464, 1.16678403299157, 1.22378841881023,
1.28153702754999, 1.34005894105164, 1.3993875342448, 1.45956101762071,
1.52062310501187, 1.58262383588933, 1.64562059096184, 1.70967935288461)
setNames(
c(1.05490954999787, 1.11049864213464, 1.16678403299157, 1.22378841881023,
1.28153702754999, 1.34005894105164, 1.3993875342448, 1.45956101762071,
1.52062310501187, 1.58262383588933, 1.64562059096184, 1.70967935288461),
2:13
)
)
expect_equal(
splice_index(
with(dat, jevons_geks(price, quantity, period, product, window = 6)),
periods = 3
),
c(1.18338442313092, 1.32033674805411, 1.43711591151299, 1.54248452241533,
1.64048980305452, 1.73334427991861, 1.82239254133738, 1.90851390842145,
1.99231538702512, 2.07423380909548, 2.15459410924166, 2.23364458259815)
setNames(
c(1.18338442313092, 1.32033674805411, 1.43711591151299, 1.54248452241533,
1.64048980305452, 1.73334427991861, 1.82239254133738, 1.90851390842145,
1.99231538702512, 2.07423380909548, 2.15459410924166, 2.23364458259815),
2:13
)
)
})

Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-splice.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,8 @@ test_that("splicing is the same as chaining", {
expect_equal(splice_index(x, initial = 1:3), cumprod(c(1:3, 1:5)))
expect_equal(splice_index(x, published = TRUE), cumprod(1:5))
})

test_that("splicing keeps names", {
x <- list(1:3, c(a = 1, b = 2, c = 3), z = c(1:2, c = 3))
expect_identical(names(splice_index(x)), c("", "", "", "c", "z.c"))
})

0 comments on commit 6553c19

Please sign in to comment.