Skip to content

Commit

Permalink
Fixed error with str.index
Browse files Browse the repository at this point in the history
  • Loading branch information
marberts committed Aug 9, 2023
1 parent ce79a8a commit 3591149
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
5 changes: 4 additions & 1 deletion R/as_index.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ as_index.matrix <- function(x, chainable = TRUE, ...) {
periods <- as.character(
if (is.null(colnames(x))) seq_len(ncol(x)) else colnames(x)
)

if (any(x <= 0, na.rm = TRUE)) {
warning("some elements of 'x' are less than or equal to 0")
}

index <- index_skeleton(levels, periods)
for (t in seq_along(periods)) {
index[[t]][] <- x[, t]
Expand Down
2 changes: 1 addition & 1 deletion R/summary.index.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ print.index_summary <- function(x, ...) {
}

str.index <- function(object, ...) {
str(as.list(object), ...)
str(unclass(object), ...)
}

print.index <- function(x, ...) {
Expand Down
2 changes: 1 addition & 1 deletion man/aggregation-structure-attributes.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
\alias{levels.aggregation_structure}

\title{
Methods to get the weights and levels for a price index aggregation structure.
Methods to get the weights and levels for a price index aggregation structure
}

\description{
Expand Down
2 changes: 1 addition & 1 deletion man/mean.index.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Aggregate an index over subperiods by taking the (usually arithmetic) mean of in
}

\details{
The \code{mean()} method constructs a set of windows of length \code{window}, starting in the first period of the index, and takes the mean of each index value in these windows for each level of the index. The last window is discarded if it is incomplete, so that index values are always averaged over \code{window} periods. The names for the first time period in each window form the new names for the aggregated time periods. Note that percent-change contributions are discarded when aggregating over subperiods.
The \code{mean()} method constructs a set of non-overlapping windows of length \code{window}, starting in the first period of the index, and takes the mean of each index value in these windows for each level of the index. The last window is discarded if it is incomplete, so that index values are always averaged over \code{window} periods. The names for the first time period in each window form the new names for the aggregated time periods. Note that percent-change contributions are discarded when aggregating over subperiods.

An optional vector of weights can be specified when aggregating index values over subperiods, which is often useful when aggregating a Paasche index; see section 4.3 of Balk (2008) for details.
}
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test-aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,33 @@ test_that("corner cases work", {
expect_equal(as.matrix(aggregate(as_index(1:5), 6)),
matrix(NA_real_, dimnames = list(6, 1)))
})

test_that("partial contributions are correct", {
prices <- data.frame(
rel = 1:8,
period = rep(1:2, each = 4),
ea = rep(letters[1:2], 4)
)

epr <- with(prices, elemental_index(rel, period, ea, contrib = TRUE))

epr <- merge(epr, matrix(9:10, 1, dimnames = list("c", 1:2)))

pias <- aggregation_structure(
list(c("top", "top", "top"), c("a", "b", "c")), 1:3
)

index <- aggregate(epr, pias)
expect_equal(
sum(contrib(index[, 1])),
sum(gpindex::arithmetic_contributions(as.numeric(index[letters[1:3], 1]),
weights(pias, ea_only = TRUE))[1:2])
)

pias2 <- update(pias, index[, 1])
expect_equal(
sum(contrib(index[, 2])),
sum(gpindex::arithmetic_contributions(as.numeric(index[letters[1:3], 2]),
weights(pias2, ea_only = TRUE))[1:2])
)
})
4 changes: 4 additions & 0 deletions tests/testthat/test-extract-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ test_that("replacement methods work", {
contrib(epr),
matrix(0, 0, 2, dimnames = list(NULL, 1:2))
)

# recycling should still happen
epr[1, c(1, 2, 1)] <- 1:3
expect_equal(epr[1, ], as_index(matrix(3:2, 1, dimnames = list("11", 1:2))))
})

0 comments on commit 3591149

Please sign in to comment.