require(data.table)
# example data:
set.seed(123)
dt1 <- rdata(R=3, B=1, N=5)
# Gerardi price index:
dt1[, gerardi(p=price, q=quantity, r=region, n=product)]
# 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 expenditure share weights:
dt[, "share" := price*quantity/sum(price*quantity), by="region"]
# Gerardi index with quantites or expenditure share weights:
dt[, gerardi(p=price, q=quantity, r=region, n=product)]
dt[, gerardi(p=price, w=share, r=region, n=product)]
Run the code above in your browser using DataLab