wquantile {lazy.tools}R Documentation

Weighted Quantile

Description

This program calculates the cdf of x from the frequency distribution (x, freq) and reverse interpolates it to calculate quantiles.

Usage

wquantile(
  x,
  freq = 1,
  probs = c(0, 0.25, 0.5, 0.75, 1),
  interpol = approx,
  ...
)

Arguments

x

A vector of the random variable

freq

A vector of probability or frequency associated with x

probs

A vector of percents/100

interpol

Name of interpolation function: approx or spline

...

additional parameters to interpol function.

Details

When length(unique(x)) <= 10 , quantile(x,probs=probs) will be returned.

Quantile and wquantile differs in the way the interpolate (cdf,x).
wquantile modifies (cdf,x) to (cdf, xb ) where xb is the mid point of neighboring unique values of x.

Value

A vector containing quantile values with name.

Examples

seed <- 1701
set.seed(seed)

# original real random variable
n <- 100
x0 <- 100+10*rnorm(n)
# round to integers
x <- round(x0)
# (xc, f) summarizes the dist of x
f <- table(x)
xc <- as.numeric(names(f))
# expand (xc, f) to (xxf, 1) : These two contains the same info.
xxf <- unlist(mapply( rep,xc,f ))

# quantiles
qtx0 <- quantile(x0)
qtx <- quantile(x)
qtxxf <- quantile(xxf)
qtxc <- quantile(xc)
wqtxc <- wquantile( xc, f )

# comparison
qt <- cbind(qtx0,qtx,qtxxf,qtxc,wqtxc)
Print(qt)
# difference from the real quantiles
dif <- abs(qt-qt[,1])
mad <- matrix(colMeans(dif),,1); rownames(mad)=colnames(dif)
Print(dif,mad)


[Package lazy.tools version 0.1.4 ]