| ratios {pricelevels} | R Documentation |
Calculation of price ratios
Description
Calculation of regional price ratios per product with flexible setting of base prices.
Usage
ratios(p, r, n, base=NULL, static=FALSE, settings=list())
Arguments
p |
A numeric vector of prices. |
r, n |
A character vector or factor of regional entities |
base |
A character specifying the base region to be used for the calculation of price ratios, i.e., |
static |
A logical indicating whether the |
settings |
A list of control settings to be used. The following settings are supported:
|
Details
If base is not available for a specific product, n, and static=FALSE, another base region is used instead. This is particularly important in cases of missing prices. Otherwise, for static=TRUE, computation is not possible and gives NA.
If there are duplicated observations, only one of these duplicates is used as the base price. For example, if two prices are available for product n in base region r, ratios() divides both prices by the first one.
Value
A numeric vector of the same length as p. If base has been adjusted for some products, the attribute attr("base") is added to the output, providing the respective base region.
Author(s)
Sebastian Weinand
Examples
### (1) unique price observations; no missings
set.seed(123)
dt1 <- rdata(R=3, B=1, N=4)
levels(dt1$region) <- c("a","b","c")
# calculate price ratios by product:
dt1[, ratios(p=price, r=region, n=product, base="b")]
### (2) unique price observations; missings
# drop two observations:
dt2 <- dt1[-c(5,10), ]
# now, region 'a' is base for product 2:
(pr <- dt2[, ratios(p=price, r=region, n=product, base="b")])
# base region prices are stored in attributes:
attr(pr, "base")
# with static base, NAs are produced:
dt2[, ratios(p=price, r=region, n=product, base="b", static=TRUE)]
### (3) treatment of duplicates and missing prices (not NAs):
# insert duplicates and missings:
dt3 <- rbind(dt1[2,], dt1[-c(1,10),])
dt3[1, "price" := dt1[2,price]+abs(rnorm(1))]
anyDuplicated(dt3, by=c("region","product"))
# duplicated prices are divided by the first base price:
dt3[, ratios(p=price, r=region, n=product, base="b")]