Learn R Programming

piar (version 0.8.2)

contrib: Extract percent-change contributions

Description

Extract a matrix or data frame of percent-change contributions from a price index.

Usage

contrib(x, ...)

# S3 method for piar_index contrib(x, level = levels(x)[1L], period = time(x), ..., pad = 0)

contrib2DF(x, ...)

# S3 method for piar_index contrib2DF(x, level = levels(x)[1L], period = time(x), ...)

contrib(x, ...) <- value

# S3 method for piar_index contrib(x, level = levels(x)[1L], period = time(x), ...) <- value

set_contrib(x, ..., value)

set_contrib_from_index(x)

Value

contrib() returns 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. The replacement methods returns a copy of x with contributions given by the matrix value. (set_contrib() is an alias that's easier to use with pipes.) set_contrib_from_index() is a helper to return a copy of x with all contributions set to the corresponding index value minus 1.

contrib2DF() returns a data frame of contributions with four columns: period, level, product, and value.

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). contrib2DF() can accept multiple levels.

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 numeric matrix of replacement contributions with a row for each product and a column for each time period. Recycling occurs along time periods.

See Also

Other index methods: [.piar_index(), aggregate.piar_index, as.data.frame.piar_index(), as.ts.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

Run this code
prices <- data.frame(
  rel = 1:8,
  period = rep(1:2, each = 4),
  ea = rep(letters[1:2], 4)
)

index <- elemental_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

library(gpindex)

arithmetic_contributions(
  as.matrix(chain(index))[c("a", "b", "c"), 2],
  weights(pias)
)

Run the code above in your browser using DataLab