Skip to content

Commit

Permalink
Version 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
marberts committed Aug 10, 2023
1 parent 3591149 commit 297d273
Show file tree
Hide file tree
Showing 9 changed files with 26 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: piar
Title: Price Index Aggregation
Version: 0.4.0.9016
Version: 0.5.0
Authors@R: c(
person("Steve", "Martin", role = c("aut", "cre", "cph"), email = "[email protected]", comment = c(ORCID = "0000-0003-2544-9480"))
)
Expand Down
10 changes: 3 additions & 7 deletions R/aggregation_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ validate_pias <- function(x) {
}

aggregation_structure <- function(x, w = NULL) {
x <- lapply(x, factor)
x <- lapply(x, as.character)
len <- length(x)
ea <- as.character(unlist(x[len], use.names = FALSE))
if (length(ea) == 0L) {
Expand Down Expand Up @@ -82,12 +82,8 @@ aggregation_structure <- function(x, w = NULL) {
child <- parent <- vector("list", len)[-1L]
# produce a list for each level with all the parent and child nodes
for (i in seq_along(upper)) {
child[[i]] <- lapply(
split(as.character(lower[[len - i]]), upper[[len - i]]), unique
)
parent[[i]] <- lapply(
split(as.character(upper[[len - i]]), lower[[len - i]]), unique
)
child[[i]] <- lapply(split(lower[[len - i]], upper[[len - i]]), unique)
parent[[i]] <- lapply(split(upper[[len - i]], lower[[len - i]]), unique)
}
if (any(lengths(unlist(parent, recursive = FALSE)) > 1L)) {
stop("some nodes in the price index aggregation structure have ",
Expand Down
3 changes: 2 additions & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## Test environments

- local Ubuntu 20.04 installation, R 4.2.0, 3.5.0
- local Ubuntu 20.04 installation, R 4.3.1
- win-builder (devel, release, oldrelease)
- mac-builder (release)
- R-hub (fedora-clang devel)

## R CMD check results
Expand Down
2 changes: 1 addition & 1 deletion man/aggregation_structure.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ aggregation_structure(x, w = NULL)
}

\arguments{
\item{x}{A list of factors (or vectors to be coerced to factors) that give the codes/labels for each level of the classification, ordered so that moving down the list goes down the hierarchy. The last vector gives the elemental aggregates, which should have no duplicates. All vectors should be the same length, without \code{NA}s, and there should be no duplicates across different levels of \code{x}.}
\item{x}{A list of character vectors that give the codes/labels for each level of the classification, ordered so that moving down the list goes down the hierarchy. The last vector gives the elemental aggregates, which should have no duplicates. All vectors should be the same length, without \code{NA}s, and there should be no duplicates across different levels of \code{x}.}

\item{w}{A numeric vector of aggregation weights for the elemental aggregates (i.e., the last vector in \code{x}). The default is to give each elemental aggregate the same weight.}
}
Expand Down
2 changes: 1 addition & 1 deletion man/expand_classification.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ expand_classification(x, width = 1)
\arguments{
\item{x}{A character vector, or something that can be coerced into one, of codes/labels for a specific level in a classification (e.g., 5-digit COICOP, 5-digit NAICS, 4-digit SIC).}

\item{width}{An integer vector that gives the width of each digit in \code{x}. A single value is recycled to span the longest element in \code{class}. This cannot contain NAs. The default assumes each digit has a width of 1, as in the NAICS, NAPCS, and SIC classifications.}
\item{width}{An integer vector that gives the width of each digit in \code{x}. A single value is recycled to span the longest element in \code{x}. This cannot contain NAs. The default assumes each digit has a width of 1, as in the NAICS, NAPCS, and SIC classifications.}
}

\value{
Expand Down
2 changes: 1 addition & 1 deletion tests/Examples/piar-Ex.Rout.save
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ top 2.462968 16.4796
>
> ### Name: aggregation-structure-attributes
> ### Title: Methods to get the weights and levels for a price index
> ### aggregation structure.
> ### aggregation structure
> ### Aliases: weights.aggregation_structure weights<-
> ### weights<-.aggregation_structure levels.aggregation_structure
>
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ test_that("a matched-sample index aggregates correctly", {
as.matrix(aggregate(ms_epr, ms_pias, na.rm = TRUE)[levels(ms_index), ]),
as.matrix(ms_index)
)
expect_equal(
as.matrix(aggregate(ms_epr, ms_pias, na.rm = TRUE)[1:3, ]),
as.matrix(ms_index)[1:3, ]
)

# Aggregated contributions should add up
expect_equal(as.matrix(ms_index)[1, ],
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-aggregation_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ x1 <- c("1", "2", "1", "1")
x2 <- c("11", "21", "12", "11")
x3 <- c("111", "211", "121", "112")

test_that("reordering works", {
test_that("reordering does nothing", {
epr <- as_index(matrix(1:12, 4, 3,
dimnames = list(c("111", "112", "121", "211"), 1:3)))
agg1 <- aggregation_structure(list(x1, x2, x3), 1:4)
f <- factor(x2, levels = c("11", "21", "12"))
agg2 <- aggregation_structure(list(x1, f, x3), 1:4)
index1 <- aggregate(epr, agg1)
index2 <- aggregate(epr, agg2)
expect_equal(as.matrix(index1[levels(index2)]), as.matrix(index2))
expect_equal(index1, index2)

f <- factor(x2, levels = c("11", "21", "12", "99"))
expect_equal(agg2, aggregation_structure(list(x1, f, x3), 1:4))
Expand All @@ -27,4 +27,5 @@ test_that("errors work", {
expect_error(aggregation_structure(list(1:2, c(11, NA))))
expect_error(aggregation_structure(list(1:2, c(11, 11, 12))))
expect_error(aggregation_structure(list(1:2, c(3, 3), c(4, 5))))
expect_error(aggregation_structure(list(1:2, c(3, 3), c(4, ""))))
})
10 changes: 10 additions & 0 deletions tests/testthat/test-vcov.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,13 @@ test_that("vcov works for chained index", {
covar[1, 2] # 0.004125957 according to svymean
)
})

test_that("vcov doesn't depend on order", {
covar <- vcov(index, rw * weights$ew, mse = FALSE)
ord <- c(1, 3:4, 8, 2, 5:7)
weights <- weights[ord, ]
pias <- with(weights, aggregation_structure(as.list(weights[1:4]), ew * dw))
index <- aggregate(epr, pias)
rw <- rw[ord, ]
expect_equal(vcov(index, rw * weights$ew, mse = FALSE), covar)
})

0 comments on commit 297d273

Please sign in to comment.