# example data:
set.seed(123)
dt1 <- rdata(R=3, B=1, N=5)
### Index pairs
# matrix of bilateral index numbers:
Pje <- dt1[, index.pairs(p=price, r=region, n=product, settings=list(type="jevons"))]
# if the underlying index satisfies the country-reversal
# test (like the Jevons index), the price index numbers of
# the upper-right triangle are the same as the inverse of
# the price index numbers of the lower-left triangle.
all.equal(Pje$jevons[3], 1/Pje$jevons[7]) # true
# hence, one could set all.pairs=FALSE without loosing any
# information. however, this is no longer true for indices
# that do not satisfy this test (like the Carli index):
Pca <- dt1[, index.pairs(p=price, r=region, n=product, settings=list(type="carli"))]
all.equal(Pca$carli[3], 1/Pca$carli[7]) # false
### GEKS method
# for complete price data (no gaps), the jevons index is transitive.
# hence, no adjustment is needed by the geks approach, which is
# why the index numbers are the same:
all.equal(
dt1[, geks(p=price, r=region, n=product, base="1", settings=list(type="jevons"))],
dt1[, jevons(p=price, r=region, n=product, base="1")]
) # true
# this is no longer true when there are gaps in the data:
dt1.gaps <- dt1[!rgaps(region, product, amount=0.25), ]
all.equal(
dt1.gaps[, geks(p=price, r=region, n=product, base="1", settings=list(type="jevons"))],
dt1.gaps[, jevons(p=price, r=region, n=product, base="1")]
) # now, differences
# weighting at the second step of GEKS can be done with respect
# to the intersection of products for each pair of region:
dt1.gaps[, geks(p=price, r=region, n=product, base="1",
settings=list(type="jevons", wmethod="obs"))]
# add price data:
dt2 <- rdata(R=4, B=1, N=4)
dt2[, "region":=factor(region, labels=4:7)]
dt2[, "product":=factor(product, labels=6:9)]
dt <- rbind(dt1, dt2)
dt[, is.connected(r=region, n=product)] # non-connected now
# compute all index pairs and geks:
require(data.table)
as.matrix(dcast(
data=dt[, index.pairs(p=price, r=region, n=product)],
formula=base~region,
value.var="jevons"), rownames="base")
dt[, geks(p=price, r=region, n=product, base="1", settings=list(type="jevons"))]
Run the code above in your browser using DataLab