Learn R Programming

distributions3 (version 0.3.0)

Empirical: Create an Empirical distribution

Description

An empirical distribution based on a random sample.

Usage

Empirical(sample = numeric())

# S3 method for Empirical mean(x, ...)

# S3 method for Empirical variance(x, ...)

# S3 method for Empirical skewness(x, type = 3L, ...)

# S3 method for Empirical kurtosis(x, type = 3L, ...)

Value

An Empirical object.

Arguments

sample

A numeric vector, list of numeric vectors, matrix, or data.frame (see section 'Details' for more information).

x

an object of class Empirical (see Empirical()).

...

currently unused.

type

integer between 1L and 3L (default) selecting one of three algorithms used for calculating sample skewness/kurtosis. See section Details for more information.

Details

The constructor function Empirical() allows for a variety of different objects as main input sample.

  • Vector: Assumes that the vector contains a series of observations from one empirical distribution.

  • List (named or unnamed) of vectors: Each element in the list describes one empirical distribution defined by the numeric values in each of the vectors.

  • Matrix/data frame: Each row corresponds to one empirical distribution, whilst the columns contain the individual observations.

Missing values are allowed, however, each distribution requires at least two finite observations (-Inf/Inf is replaced by NA). Certain types of moments (see skewness.Empirical(), kurtosis.Empirical()) require at least three or four finite observations.

Support: Set of unique observations in the sample, denoted \(y\) below.

Probability mass function (p.m.f.): $$f(x) = \frac{1}{n} \sum_{i=1}^{N} (y_i = x)$$

Cummulative distribution function (c.d.f.): $$F(x) = \frac{1}{N} \sum_{i=1}^N \mathbf{I}(y_i \leq x)$$

Moment generating functions:

  • Mean/expectation: $$\bar{y} = \frac{1}{N} \sum_{i=1}^{N} y_i$$

  • Variance: $$\frac{1}{N - 1} \sum_{i=1}^{N} (y_i - \bar{y})$$

Third and fourth central moments are also available via skewness() and kurtosis(). For both different types are available as defined below. For details see Joanes and Gill (1998).

  • Skewness:

    • Type 1: $$S_1 = \sqrt{N} \frac{\sum_{i=1}^N (y_i - \bar{y})^3}{\sqrt{\big(\sum_{i=1}^N (y_i - \bar{y})^2\big)^3}}$$

    • Type 2 (only defined for three or more finite values): $$S_2 = \frac{\sqrt{N \cdot (N - 1)}}{(N - 2)} S_1$$

    • Type 3 (default): $$S_3 = \sqrt{(1 - \frac{1}{N})^3} \cdot S_1$$

  • Kurtosis:

    • Type 1: $$K_1 = N \cdot \frac{\sum_{i=1}^N (y_i - \bar{y})^4}{\big(\sum_{i=1}^N (y_i - \bar{y})^2\big)^2} - 3$$

    • Type 2 (only defined for four or more finite values): $$K_2 = \frac{((N + 1) \cdot K_1 + 6) \cdot (N - 1)}{(N - 2) \cdot (N - 3)}$$

    • Type 3 (default): $$K_3 = \big(1 - \frac{1}{N}\big)^2 \cdot (K_1 + 3) - 3$$

References

Joanes DN, Gill CA (1998). “Comparing Measures of Sample Skewness and Kurtosis.” Journal of the Royal Statistical Society D, 47(1), 183--189. tools:::Rd_expr_doi("10.1111/1467-9884.00122")

See Also

Other Empirical distribution: cdf.Empirical(), dempirical(), pdf.Empirical(), quantile.Empirical(), random.Empirical(), support.Empirical()

Examples

Run this code

set.seed(28)

X <- Empirical(rnorm(50))
X

mean(X)
variance(X)
skewness(X)
kurtosis(X)

random(X, 10)

pdf(X, 2)
log_pdf(X, 2)

cdf(X, 4)
quantile(X, 0.7)

### example: allowed types/classes of input arguments

## Single vector (will be coerced to numeric)
Y1 <- rnorm(3, mean = -10)
d1 <- Empirical(Y1)
d1
mean(d1)

## Unnamed list of vectors
Y2 <- list(as.character(rnorm(3, mean = -10)),
           runif(6),
           rpois(4, lambda = 15))
d2 <- Empirical(Y2)
d2
mean(d2)

## Named list of vectors
Y3 <- list("Normal"  = as.character(rnorm(3, mean = -10)),
           "Uniform" = runif(6),
           "Poisson" = rpois(4, lambda = 15))
d3 <- Empirical(Y3)
d3
mean(d3)

## Matrix
Y4 <- matrix(rnorm(20), ncol = 5,
             dimnames = list(paste0("D_", 1:4), paste0("obs_", 1:5)))
d4 <- Empirical(Y4)
d4

## Data frame
d5 <- Empirical(as.data.frame(Y4))
d5

identical(d4, d5)

mean(d5)
variance(d5)
skewness(d5)
kurtosis(d5)

pdf(d5, c(-0.5, 0, 0.5, 1)) # Defaults to elementwise = TRUE
pdf(d5, c(-0.5, 0, 0.5, 1), elementwise = FALSE)

cdf(d5, c(-0.5, 0, 0.5, 1)) # Defaults to elementwise = TRUE
cdf(d5, c(-0.5, 0, 0.5, 1), elementwise = FALSE)

quantile(d5, c(0.2, 0.4, 0.6, 0.8)) # Defaults to elementwise = TRUE
quantile(d5, c(0.2, 0.4, 0.6, 0.8), elementwise = FALSE)

## The quantile function is the inverse of the distribution
## function (cdf) if x in Y
set.seed(6020)
Y <- round(rlnorm(20, log(3), log(2)), 1)
d <- Empirical(Y)

cdf(d, 4.0)
quantile(d, cdf(d, 4.0))

Run the code above in your browser using DataLab