Instructions

These instructions outline a simple way to build the price index for widgets, although feel free to use a different approach if it’s easier. The goal is to understand the process, rather than design the most computationally efficient system. In order to ensure that simply following these instructions isn’t a trivial exercise, parts of the code from the snippets below have been omitted and replaced with tags in angle brackets. Wherever you see <action>, you should insert the name of a function. You can think of functions as “verbs” that perform actions on your data. Wherever you see <variable>, you should insert the name of one of your data structures. You can think of variables as “nouns” that you manipulate with “verbs,” or functions. Finally, wherever you see <parameter>, you should insert additional arguments to be passed to a function. You can think of these arguments or parameters as “adverbs” that modify your functions, or “verbs.” In order to help you out, some lines of code have hints next to them marked with # **HINT**.

Run the following code to grab all the data files and put them in your working environment. Note that these data aren’t perfect. It is worth remembering to check your intermediate calculations to make sure everything is as expected (e.g., with the summary function).

source('https://raw.githubusercontent.com/ppd-dpp/price-index-course/master/scripts/get_data.R')

The gpindex, and piar packages will be used in this module. Install them as follows if they aren’t already installed, and then load them. (Hint: it is worth going through the documentation in the gpindex and piar packages.)

install.packages(c("gpindex", "piar"))

library(gpindex)
library(piar)
  1. Calculate the period-over-period elemental indices for product K

Using the microdata for product K in the micro_prices file, calculate the geometric average of the prices in each province in each month in each reference year. Then, calculate the ratio of the geometric averages computed in step 3, making sure to have a price relative of 1 in January 2018 and January 2019. (January 2019 will be the link month when it is time to chain the index.)

#---- Product 'K' index ----
# Turn period into a year-month and combine product and province into a 
# factor for elemental aggregates
micro_prices <- transform(micro_prices, 
                          period = format(as.Date(period), "%Y%m"), 
                          ea = interaction(province, product, sep = ""))

# Get a single price for each time period by taking the geomean
# **HINT** Take a look at the documentation for the aggregate methods 
# in the stats package
micro_prices <- aggregate(price ~ <variable> + <variable> + <variable>,
                          micro_prices,
                          <action>)

# Calculate elemental indexes for product 'K'
# **HINT** piar has a function for computing ratios of prices in consecutive months
index_k <- lapply(split(micro_prices, micro_prices$basket), 
                  function(df) {
                    with(df, elemental_index(<action>(price, period, ea), period, ea))
                  })
  1. Calculate the period-over-period elemental indices for products A through J, and merge them with them with the product K elemental indices

Using the data from the Globular Prices System in gps_prices, calculate price relatives for each quote in each province in each month in each reference year and use them to compute elemental indices. Then, merge these indices with the product K indices so that they are all in the same tables. This should give you two tables of elemental indices for products A through K, one for each reference year.

#---- Product 'A' to 'J' indices ----
# Melt into a long format
gps_prices <- reshape(gps_prices, 
                      direction = <parameter>, 
                      varying = paste0("price", 1:3),
                      v.names = "price",
                      timevar = "quote",
                      idvar = c("ea", "period", "basket"))

# Combine quote id and ea labels to make unique product labels
gps_prices$product <- paste0(gps_prices$ea, gps_prices$quote)

# Calculate elemental indexes for product 'A' to 'J'
# **HINT** What type of elemental indices have you been asked to compute? 
# What type of mean does that imply? What is the corresponding order of the generalized mean?
index_aj <- lapply(split(gps_prices, gps_prices$basket), 
                  function(df) {
                    with(df, elemental_index(<action>(price, period, product), 
                                             period, ea, na.rm = <parameter>, r = <parameter>))
                  })

#---- Merge elemental indices ----
elementals <- Map(merge, <variable>, <variable>)
  1. Build the aggregation structures

An aggregation structure tells R how to build up your price indices. It indicates how the elemental indices are aggregated to build provincial indices, how the provincial indices are aggregated to build regional indices, and how the regional indices are aggregated to build the national index. It also indicates which weights are assigned to each elemental index in the aggregation. Create one aggregation structure for each year, for a total of two.

#---- Aggregation structure ----
pias <- lapply(split(weights, weights$basket), 
               function(df) {
                 <action>(df[1:4], df$weight)
               })
  1. Aggregate and chain the index

Take the elemental indices you calculated in Steps 1 and 2 and the aggregation structures you created in Step 3, and use them to calculate the aggregate indices for provinces, regions, and the entire country. (There is no need to manually price-update the aggregation weights for each month, as piar’s aggregate function will do this automatically.) You should now have a collection of month-over-month indices. Chain the 2018 and 2019 indices together, using January 2019 as the link month, to get an index from January 2018 to December 2019, with January 2018 as the base period.

#---- Aggregate, chain, and quarter ----
index <- Map(<action>, <variable>, pias)

# **HINT** What do you have to remove from the 2019 index in order to chain it 
# together with the 2018 index?
index <- chain(stack(index[["2018"]][1:16, ], index[["2019"]][1:16, <parameter>]))