Learn R Programming

distributions3 (version 0.3.0)

random.Empirical: Draw a random sample from an Empirical distribution

Description

Draws n random values from the empirical distribution with replacement. Please see the documentation of Empirical() for some properties of the empircal ensemble distribution.

Usage

# S3 method for Empirical
random(x, n = 1L, drop = TRUE, ...)

Value

In case of a single distribution object or n = 1, either a numeric vector of length n (if drop = TRUE, default) or a matrix with n columns (if drop = FALSE).

Arguments

x

A n Empirical object created by a call to Empirical().

n

The number of samples to draw. Defaults to 1L.

drop

logical. Should the result be simplified to a vector if possible?

...

currently unused.

See Also

Other Empirical distribution: Empirical(), cdf.Empirical(), dempirical(), pdf.Empirical(), quantile.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