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, q=NULL, w=NULL, base=NULL, settings=list())
Arguments
p |
A numeric vector of positive prices. |
r , n |
A character vector or factor of regional entities |
q , w |
A numeric vector of non-negative quantities |
base |
A character specifying the base region to be used for the calculation of price ratios. If |
settings |
A list of control settings to be used. The following settings are supported:
|
Details
If there are k=1,\ldots,K^{r}_{n}
duplicated prices for product n
in region r
, these are averaged using the quantities q
(or similarly as a weighted arithmetic mean using the weights w
) if provided:
\bar{p}^{r}_n = \sum_{k=1}^{K^r_n} p^r_{nk} q^r_{nk} \Big/ \sum_{k=1}^{K^r_n} q^r_{nk}
Price ratios are then derived for each product n
by p_n^r \Big/ \frac{1}{R} \sum_{s=1}^{R} \bar{p}_{n}^s
if base=NULL
and by p_n^r \big/ \bar{p}^{base}_n
otherwise.
Value
A numeric vector of the same length as p
.
If base
is not NULL
and static=FALSE
, the attribute attr(x, "base")
is added to the output, providing the respective base region for each product.
Author(s)
Sebastian Weinand
Examples
### (1) unique price observations
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 and missing base region
# drop two observations:
dt2 <- dt1[-c(5,10), ]
# now, region 'a' is base for product 2:
dt2[, "pr" := ratios(p=price, r=region, n=product, base="b",
settings=list(static=FALSE))]
# base regions are stored in attributes:
attr(dt2$pr, "base")
# with static base, NAs are produced:
dt2[, "pr_static" := ratios(p=price, r=region, n=product, base="b")]
### (3) duplicated prices
# insert duplicates and missings:
dt3 <- rbind(dt1[c(2,3),], dt1[-c(11),])
dt3[1:2, c("price","quantity") := list(price*1.1, quantity*0.95)]
anyDuplicated(dt3, by=c("region","product"))
# duplicated prices are divided by the weighted average base prices:
dt3[, ratios(p=price, r=region, n=product, q=quantity, base="b",
settings=list(static=FALSE))]