Example with R: Stratified index

Calculating a stratified index is straightforward in practice. One neat way to do it is with a simple linear regression.

Consider the following data with transactions over two periods for two groups of goods.

# Make some data
df <- data.frame(period = c(0, 0, 0, 1, 1, 1, 1, 1), 
                 group = letters[1:2],
                 price = 1:8)
df
##   period group price
## 1      0     a     1
## 2      0     b     2
## 3      0     a     3
## 4      1     b     4
## 5      1     a     5
## 6      1     b     6
## 7      1     a     7
## 8      1     b     8

The indices for each group can be calculated with a single linear regression, and then aggregated with an average.

# Bring in gpindex library
library(gpindex)

# Calculate strata-level indices with a linear regression
mdl <- lm(log(price) ~ group + group:period - 1, df)

# Turn regression coefficients into indices
index <- exp(coef(mdl)[-seq_len(length(unique(mdl$model$group)))])

# Aggregate, assuming both strata have equal weight
geometric_mean(index) * 100
## [1] 313.886