Extract a matrix of percent-change contributions from a price index.
Arguments
- x
A price index, as made by, e.g.,
elemental_index()
.- ...
Further arguments passed to or used by methods.
- level
The level of an index for which percent-change contributions are desired, defaulting to the first level (usually the top-level for an aggregate index).
- period
The time periods for which percent-change contributions are desired, defaulting to all time periods.
- pad
A numeric value to pad contributions so that they fit into a rectangular array when products differ over time. The default is 0.
Value
A matrix of percent-change contributions with a column for each
period
and a row for each product (sorted) for which there are
contributions in level
. Contributions are padded with pad
to fit into a
rectangular array when products differ over time.
See also
Other index methods:
[.piar_index()
,
aggregate.piar_index()
,
as.data.frame.piar_index()
,
chain()
,
head.piar_index()
,
is.na.piar_index()
,
levels.piar_index()
,
mean.piar_index()
,
merge.piar_index()
,
split.piar_index()
,
stack.piar_index()
,
time.piar_index()
,
window.piar_index()
Examples
prices <- data.frame(
rel = 1:8,
period = rep(1:2, each = 4),
ea = rep(letters[1:2], 4)
)
index <- with(
prices,
elemental_index(rel, period, ea, contrib = TRUE)
)
pias <- aggregation_structure(
list(c("top", "top", "top"), c("a", "b", "c")), 1:3
)
index <- aggregate(index, pias, na.rm = TRUE)
# Percent-change contributions for the top-level index
contrib(index)
#> 1 2
#> a.1 0.0000000 0.5081686
#> a.2 0.2440169 0.6442213
#> b.1 0.3905243 2.0513858
#> b.2 0.8284271 2.4871732
# Calculate EA contributions for the chained index
library(gpindex)
arithmetic_contributions(
as.matrix(chain(index))[c("a", "b", "c"), 2],
weights(pias)
)
#> a b c
#> 1.541158 6.198639 7.739798