contrib {piar} | R Documentation |
Extract percent-change contributions
Description
Extract a matrix of percent-change contributions from a price index.
Usage
contrib(x, ...)
## S3 method for class 'piar_index'
contrib(x, level = levels(x)[1L], period = time(x), ..., pad = 0)
Arguments
x |
A price index, as made by, e.g., |
... |
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()
,
vcov.aggregate_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)
# Calculate EA contributions for the chained index
library(gpindex)
arithmetic_contributions(
as.matrix(chain(index))[c("a", "b", "c"), 2],
weights(pias)
)