prices <- data.frame(
rel = 1:8,
period = rep(1:2, each = 4),
ea = rep(letters[1:2], 4)
)
index <- elementary_index(prices, rel ~ period + ea, contrib = TRUE)
pias <- aggregation_structure(
list(c("top", "top", "top"), c("a", "b", "c")),
weights = 1:3
)
index <- aggregate(index, pias, na.rm = TRUE)
# Percent-change contributions for the top-level index.
contrib(index)
contrib2DF(index)
# Calculate EA contributions for the chained index.
arithmetic_contributions <- function(x, w, order = 1) {
(x - 1) * transmute_weights(x, w, order, to = 1)
}
arithmetic_contributions(
as.matrix(chain(index))[c("a", "b", "c"), 2],
weights(pias)
)
Run the code above in your browser using DataLab