ppd: Prices Production and Development

Description Details Author(s) References See Also Examples

Description

A collection of useful tools to simplify working with price indices in R.

Run news(package = 'ppd') for news.

Details

Here's a quick way to get started.

Author(s)

Maintainer: Steve Martin steve.martin5@canada.ca

Other contributors:

References

Kirby-McGregor, M. and Martin, S. (2019). An R package for calculating repeat-sale price indices. Romanian Statistical Review, 3: 17-33.

See Also

Useful links:

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#### Common price-index manipulations ####

# Turn a monthly index into a quarterly index
aggregate(index ~ quarter + city, 
          transform(sample_index, quarter = year_quarter(date)), 
          mean, 
          subset = complete_quarters(date, city)
)

# Turn a monthly index into an annual index
aggregate(index ~ year + city, 
          transform(sample_index, year = year(date)), 
          mean, 
          subset = complete_years_m(date, city)
)

# Works with dplyr too
## Not run: 
sample_index %>%
  filter(complete_years_m(date, city)) %>%
  group_by(city, year = year(date)) %>%
  summarize(index = mean(index))

## End(Not run)

# Calculate quarter-over-quarter change

sample_index$qoq <- 
  unsplit(
    lapply(
      split(sample_index, sample_index$city), 
      function(x) with(x, (index / index[match(year_month(date, -3), date)] - 1) * 100)
    ), 
    sample_index$city
  )

# Works with dplyr too
## Not run: 
sample_index %>%
  group_by(city) %>%
  mutate(qoq = (index / index[match(year_month(date, -3), date)] - 1) * 100)

## End(Not run)

# Turn a year into a base period
sample_index$index <- 
unsplit(
  lapply(
    split(sample_index, sample_index$city),
    function(x)
      with(x, index / mean(index[year(date) == "2017-01-01" & 
                                   complete_years_m(date)]) * 100)
  ),
  sample_index$city
)

# Works with dplyr too
## Not run: 
sample_index %>%
  group_by(city) %>%
  mutate(index = index / mean(index[year(date) == "2017-01-01" &
                                      complete_years_m(date)]) * 100)

## End(Not run)

#### Calculating a price index ####

# Make some price relatives over two periods for five groups
price_rel <- data.frame(pr = runif(10, 0.5, 1.5),
                        group = letters[1:5],
                        weight = sample(1:10),
                        stringsAsFactors = FALSE
                        )

# Arithmetic index
sapply(split(price_rel, price_rel$group), function(x) weighted.mean(x$pr, x$weight))

# Geometric index
sapply(split(price_rel, price_rel$group), function(x) geomean(x$pr, x$weight))

# Paasche index if weights are current-period expenditure/revenue shares
sapply(split(price_rel, price_rel$group), function(x) harmean(x$pr, x$weight))

# Aggregation structure
agg <- list(ab = c("a", "b"), cd = c("c", "d"), a2e = letters[1:5])
pias <- pias_matrix(agg, tapply(price_rel$weight, price_rel$group, sum))
elemental_index <- sapply(split(price_rel, price_rel$group), function(x) geomean(x$pr, x$weight))
(pias %*% elemental_index)[, 1]

# Make some microdata
price_micro <- data.frame(price = runif(11, 1 ,2),
                          ea = factor(c(rep("a", 5), rep("b", 6))),
                          period = factor(c(1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 2))
                         )
                         
# Unbalanced Jevons index
X <- model.matrix(~ ea + ea:period - 1, price_micro)
X0 <- X[, seq_len(nlevels(price_micro$ea))]
X1 <- X[, -seq_len(nlevels(price_micro$ea))]
y <- log(price_micro$price)
r <- X1 - X0 %*% solve(crossprod(X0), crossprod(X0, X1))
v <- y - X0 %*% solve(crossprod(X0), crossprod(X0, y))
b <- solve(crossprod(r), crossprod(r, v))
exp(b) * 100

# Unbalanced Dutot index
mm <- model.matrix(~ ea:period - 1, price_micro)
Y <- rowSums(mm[, seq_len(nlevels(price_micro$ea))] * price_micro$price)
X <- mm[, -seq_len(nlevels(price_micro$ea))] * price_micro$price
Z <- mm / colSums(mm)[col(mm)]
Z <- Z[, -seq_len(nlevels(price_micro$ea))] + c(Z[, seq_len(nlevels(price_micro$ea))])
b <- solve(crossprod(Z, X), crossprod(Z, Y))
100 / b

marberts/ppd documentation built on March 27, 2020, 7:21 p.m.