Quantile {lazy.tools}R Documentation

Calculation of sample quantiles by interpolating sample cumulative distribution function.

Description

Calculation of sample quantiles by interpolating sample cumulative distribution function.

Usage

Quantile(
  x,
  freq = NULL,
  probs = seq(0, 1, 0.25),
  method = "constant",
  cdf = NULL
)

Arguments

x

numeric vector

freq

frequency associated with x
(x,freq) is the frequency table

probs

numeric vector of probabilities with values in [0,1]

method

= "constant" | "linear" | "spline", interpolation method

cdf

cdf at x. This has priority over freq.

Details

If freq=1 and method="constant" (default), the result is equivalent to native quantile function with type=1.

Given the frequency table of data, (x, freq), cdf will be calculated as:

 cdf = sumsum(freq)/sum(freq)
 

Value

a vector of quantiles

Examples


# generate data
set.seed(1701)
n <- 100
x <- round(10*rnorm(n),0)

# by native quantile func
probs <- c(0.25, 0.5, 0.75)
res1 <- quantile(x,probs, type=1)

# by Quantile
res2 <- Quantile( x,1, probs=probs )

# frequency table
freq <- table(x)
xc <- as.numeric(names(freq))

# from frequency table
res3 <- Quantile( xc,freq, probs=probs )

# comparison
Print(probs,res1,res2, res3)



#
# Effect of the sample size
#
# When small, Quantile with linear method is equal to quantile with type=4.
#
#

getQ <- function( x, prob ){
 res1 <- NULL;
 for( type in 1:9 ) res1 <- cbind(res1,quantile(x,probs, type=type))
 colnames(res1) <- paste("t",1:9,sep="")
 return( res1 )
}

# generate data
set.seed(1701)
n <- 5
x <- round(10*rnorm(n),0)
probs <- c(0.10, 0.25, 0.5, 0.75, 0.90)

# by native quantile func
res1 <- getQ(x,probs)
# by Quantile
res2 <- Quantile( x,1, probs=probs, method="linear" )
# comparison
Print(res1, fmt="8.4")
Print(probs,res2, fmt="8.4")


# generate data
set.seed(1701)
n <- 50
x <- round(10*rnorm(n),0)
probs <- c(0.10, 0.25, 0.5, 0.75, 0.90)

# by native quantile func
res1 <- getQ(x,probs)
# by Quantile
res2 <- Quantile( x,1, probs=probs, method="linear" )
# comparison
Print(res1, fmt="8.4")
Print(probs,res2, fmt="8.4")

# comparison
Print(res1)
Print(probs,res2, res3)


# generate data
set.seed(1701)
n <- 500
x <- round(10*rnorm(n),0)
probs <- c(0.10, 0.25, 0.5, 0.75, 0.90)

# by native quantile func
res1 <- getQ(x,probs)
# by Quantile
res2 <- Quantile( x,1, probs=probs, method="linear" )
# comparison
Print(res1, fmt="8.4")
Print(probs,res2, fmt="8.4")

# comparison
Print(res1)
Print(probs,res2, res3)



[Package lazy.tools version 0.1.6 ]