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 r and products n, respectively.

base

A character specifying the base region to be used for the calculation of price ratios, i.e., p_n^r / p_n^{base}. If NULL, price ratios are calculated with reference to the average price of a product, i.e., p_n^r / \bar{p}_n, with \bar{p}_n = (1/R) \sum_{s=1}^{R} p_n^s.

static

A logical indicating whether the base region is static (TRUE), i.e. always the same as base, or if another region than base is allowed to be used when prices for base are not available or missing (=NA). Only relevant if base is not NULL.

settings

A list of control settings to be used. The following settings are supported:

  • chatty : A logical specifying if warnings and info messages should be printed or not. The default is getOption("pricelevels.chatty").

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")]

[Package pricelevels version 1.3.0 Index]