Skip to contents

Extract a matrix or data frame 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)

contrib2DF(x, ...)

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

contrib(x, ...) <- value

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

set_contrib(x, ..., 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.

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.)

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

Examples

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)
#>             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

contrib2DF(index)
#>   period level product     value
#> 1      1   top     a.1 0.0000000
#> 2      1   top     a.2 0.2440169
#> 3      1   top     b.1 0.3905243
#> 4      1   top     b.2 0.8284271
#> 5      2   top     a.1 0.5081686
#> 6      2   top     a.2 0.6442213
#> 7      2   top     b.1 2.0513858
#> 8      2   top     b.2 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