Title: | Probability Distributions as S3 Objects |
Version: | 0.2.2 |
Description: | Tools to create and manipulate probability distributions using S3. Generics pdf(), cdf(), quantile(), and random() provide replacements for base R's d/p/q/r style functions. Functions and arguments have been named carefully to minimize confusion for students in intro stats courses. The documentation for each distribution contains detailed mathematical notes. |
License: | MIT + file LICENSE |
URL: | https://github.com/alexpghayes/distributions3, https://alexpghayes.github.io/distributions3/ |
BugReports: | https://github.com/alexpghayes/distributions3/issues |
Imports: | ggplot2, glue, rlang |
Suggests: | cowplot, knitr, PoissonBinomial, revdbayes (≥ 1.3.5), rmarkdown, testthat (≥ 3.0.0), tibble, vctrs |
VignetteBuilder: | knitr |
Encoding: | UTF-8 |
RoxygenNote: | 7.3.2 |
Config/testthat/edition: | 3 |
Config/testthat/parallel: | true |
NeedsCompilation: | no |
Packaged: | 2024-09-16 15:51:35 UTC; alex |
Author: | Alex Hayes |
Maintainer: | Alex Hayes <alexpghayes@gmail.com> |
Repository: | CRAN |
Date/Publication: | 2024-09-16 16:20:02 UTC |
distributions3: Probability Distributions as S3 Objects
Description
Tools to create and manipulate probability distributions using S3. Generics pdf(), cdf(), quantile(), and random() provide replacements for base R's d/p/q/r style functions. Functions and arguments have been named carefully to minimize confusion for students in intro stats courses. The documentation for each distribution contains detailed mathematical notes.
Author(s)
Maintainer: Alex Hayes alexpghayes@gmail.com (ORCID)
Authors:
Ralph Moller-Trane
Daniel Jordan dandermotj@gmail.com
Paul Northrop p.northrop@ucl.ac.uk
Moritz N. Lang moritz.n.lang@gmail.com (ORCID)
Achim Zeileis Achim.Zeileis@R-project.org (ORCID)
Other contributors:
Emil Hvitfeldt emilhhvitfeldt@gmail.com (ORCID) [contributor]
Bruna Wundervald brunadaviesw@gmail.com [contributor]
Alessandro Gasparini alessandro.gasparini@ki.se [contributor]
See Also
Useful links:
Report bugs at https://github.com/alexpghayes/distributions3/issues
Utilities for distributions3
objects
Description
Various utility functions to implement methods for distributions with a
unified workflow, in particular to facilitate working with vectorized
distributions3
objects.
These are particularly useful in the computation of densities, probabilities, quantiles,
and random samples when classical d/p/q/r functions are readily available for
the distribution of interest.
Usage
apply_dpqr(d, FUN, at, elementwise = NULL, drop = TRUE, type = NULL, ...)
make_support(min, max, d, drop = TRUE)
make_positive_integer(n)
Arguments
d |
A |
FUN |
Function to be computed. Function should be of type |
at |
Specification of values at which |
elementwise |
logical. Should each element of |
drop |
logical. Should the result be simplified to a vector if possible (by
dropping the dimension attribute)? If |
type |
Character string used for naming, typically one of |
... |
Arguments to be passed to |
min , max |
Numeric vectors. Minima and maxima of the supports of a |
n |
numeric. Number of observations for computing random draws. If |
Examples
## Implementing a new distribution based on the provided utility functions
## Illustration: Gaussian distribution
## Note: Gaussian() is really just a copy of Normal() with a different class/distribution name
## Generator function for the distribution object.
Gaussian <- function(mu = 0, sigma = 1) {
stopifnot(
"parameter lengths do not match (only scalars are allowed to be recycled)" =
length(mu) == length(sigma) | length(mu) == 1 | length(sigma) == 1
)
d <- data.frame(mu = mu, sigma = sigma)
class(d) <- c("Gaussian", "distribution")
d
}
## Set up a vector Y containing four Gaussian distributions:
Y <- Gaussian(mu = 1:4, sigma = c(1, 1, 2, 2))
Y
## Extract the underlying parameters:
as.matrix(Y)
## Extractor functions for moments of the distribution include
## mean(), variance(), skewness(), kurtosis().
## These can be typically be defined as functions of the list of parameters.
mean.Gaussian <- function(x, ...) {
rlang::check_dots_used()
setNames(x$mu, names(x))
}
## Analogously for other moments, see distributions3:::variance.Normal etc.
mean(Y)
## The support() method should return a matrix of "min" and "max" for the
## distribution. The make_support() function helps to set the right names and
## dimension.
support.Gaussian <- function(d, drop = TRUE, ...) {
min <- rep(-Inf, length(d))
max <- rep(Inf, length(d))
make_support(min, max, d, drop = drop)
}
support(Y)
## Evaluating certain functions associated with the distribution, e.g.,
## pdf(), log_pdf(), cdf() quantile(), random(), etc. The apply_dpqr()
## function helps to call the typical d/p/q/r functions (like dnorm,
## pnorm, etc.) and set suitable names and dimension.
pdf.Gaussian <- function(d, x, elementwise = NULL, drop = TRUE, ...) {
FUN <- function(at, d) dnorm(x = at, mean = d$mu, sd = d$sigma, ...)
apply_dpqr(d = d, FUN = FUN, at = x, type = "density", elementwise = elementwise, drop = drop)
}
## Evaluate all densities at the same argument (returns vector):
pdf(Y, 0)
## Evaluate all densities at several arguments (returns matrix):
pdf(Y, c(0, 5))
## Evaluate each density at a different argument (returns vector):
pdf(Y, 4:1)
## Force evaluation of each density at a different argument (returns vector)
## or at all arguments (returns matrix):
pdf(Y, 4:1, elementwise = TRUE)
pdf(Y, 4:1, elementwise = FALSE)
## Drawing random() samples also uses apply_dpqr() with the argument
## n assured to be a positive integer.
random.Gaussian <- function(x, n = 1L, drop = TRUE, ...) {
n <- make_positive_integer(n)
if (n == 0L) {
return(numeric(0L))
}
FUN <- function(at, d) rnorm(n = at, mean = d$mu, sd = d$sigma)
apply_dpqr(d = x, FUN = FUN, at = n, type = "random", drop = drop)
}
## One random sample for each distribution (returns vector):
random(Y, 1)
## Several random samples for each distribution (returns matrix):
random(Y, 3)
## For further analogous methods see the "Normal" distribution provided
## in distributions3.
methods(class = "Normal")
Create a Bernoulli distribution
Description
Bernoulli distributions are used to represent events like coin flips
when there is single trial that is either successful or unsuccessful.
The Bernoulli distribution is a special case of the Binomial()
distribution with n = 1
.
Usage
Bernoulli(p = 0.5)
Arguments
p |
The success probability for the distribution. |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail.
In the following, let X
be a Bernoulli random variable with parameter
p
= p
. Some textbooks also define q = 1 - p
, or use
\pi
instead of p
.
The Bernoulli probability distribution is widely used to model
binary variables, such as 'failure' and 'success'. The most
typical example is the flip of a coin, when p
is thought as the
probability of flipping a head, and q = 1 - p
is the
probability of flipping a tail.
Support: \{0, 1\}
Mean: p
Variance: p \cdot (1 - p) = p \cdot q
Probability mass function (p.m.f):
P(X = x) = p^x (1 - p)^{1-x} = p^x q^{1-x}
Cumulative distribution function (c.d.f):
P(X \le x) =
\left \{
\begin{array}{ll}
0 & x < 0 \\
1 - p & 0 \leq x < 1 \\
1 & x \geq 1
\end{array}
\right.
Moment generating function (m.g.f):
E(e^{tX}) = (1 - p) + p e^t
Value
A Bernoulli
object.
See Also
Other discrete distributions:
Binomial()
,
Categorical()
,
Geometric()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
HyperGeometric()
,
Multinomial()
,
NegativeBinomial()
,
Poisson()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
set.seed(27)
X <- Bernoulli(0.7)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 1)
log_pdf(X, 1)
cdf(X, 0)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Create a Beta distribution
Description
Create a Beta distribution
Usage
Beta(alpha = 1, beta = 1)
Arguments
alpha |
The alpha parameter. |
beta |
The beta parameter. |
Value
A beta
object.
See Also
Other continuous distributions:
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- Beta(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
mean(X)
variance(X)
skewness(X)
kurtosis(X)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Create a Binomial distribution
Description
Binomial distributions are used to represent situations can that can
be thought as the result of n
Bernoulli experiments (here the
n
is defined as the size
of the experiment). The classical
example is n
independent coin flips, where each coin flip has
probability p
of success. In this case, the individual probability of
flipping heads or tails is given by the Bernoulli(p) distribution,
and the probability of having x
equal results (x
heads,
for example), in n
trials is given by the Binomial(n, p) distribution.
The equation of the Binomial distribution is directly derived from
the equation of the Bernoulli distribution.
Usage
Binomial(size, p = 0.5)
Arguments
size |
The number of trials. Must be an integer greater than or equal
to one. When |
p |
The success probability for a given trial. |
Details
The Binomial distribution comes up when you are interested in the portion
of people who do a thing. The Binomial distribution
also comes up in the sign test, sometimes called the Binomial test
(see stats::binom.test()
), where you may need the Binomial C.D.F. to
compute p-values.
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail.
In the following, let X
be a Binomial random variable with parameter
size
= n
and p
= p
. Some textbooks define q = 1 - p
,
or called \pi
instead of p
.
Support: \{0, 1, 2, ..., n\}
Mean: np
Variance: np \cdot (1 - p) = np \cdot q
Probability mass function (p.m.f):
P(X = k) = {n \choose k} p^k (1 - p)^{n-k}
Cumulative distribution function (c.d.f):
P(X \le k) = \sum_{i=0}^{\lfloor k \rfloor} {n \choose i} p^i (1 - p)^{n-i}
Moment generating function (m.g.f):
E(e^{tX}) = (1 - p + p e^t)^n
Value
A Binomial
object.
See Also
Other discrete distributions:
Bernoulli()
,
Categorical()
,
Geometric()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
HyperGeometric()
,
Multinomial()
,
NegativeBinomial()
,
Poisson()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
set.seed(27)
X <- Binomial(10, 0.2)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2L)
log_pdf(X, 2L)
cdf(X, 4L)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Create a Categorical distribution
Description
Create a Categorical distribution
Usage
Categorical(outcomes, p = NULL)
Arguments
outcomes |
A vector specifying the elements in the sample space. Can be numeric, factor, character, or logical. |
p |
A vector of success probabilities for each outcome.
Each element of |
Value
A Categorical
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Geometric()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
HyperGeometric()
,
Multinomial()
,
NegativeBinomial()
,
Poisson()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
set.seed(27)
X <- Categorical(1:3, p = c(0.4, 0.1, 0.5))
X
Y <- Categorical(LETTERS[1:4])
Y
random(X, 10)
random(Y, 10)
pdf(X, 1)
log_pdf(X, 1)
cdf(X, 1)
quantile(X, 0.5)
# cdfs are only defined for numeric sample spaces. this errors!
# cdf(Y, "a")
# same for quantiles. this also errors!
# quantile(Y, 0.7)
Create a Cauchy distribution
Description
Note that the Cauchy distribution is the student's t distribution with one degree of freedom. The Cauchy distribution does not have a well defined mean or variance. Cauchy distributions often appear as priors in Bayesian contexts due to their heavy tails.
Usage
Cauchy(location = 0, scale = 1)
Arguments
location |
The location parameter. Can be any real number. Defaults
to |
scale |
The scale parameter. Must be greater than zero (?). Defaults
to |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a Cauchy variable with mean
location =
x_0
and scale
= \gamma
.
Support: R
, the set of all real numbers
Mean: Undefined.
Variance: Undefined.
Probability density function (p.d.f):
f(x) = \frac{1}{\pi \gamma \left[1 + \left(\frac{x - x_0}{\gamma} \right)^2 \right]}
Cumulative distribution function (c.d.f):
F(t) = \frac{1}{\pi} \arctan \left( \frac{t - x_0}{\gamma} \right) +
\frac{1}{2}
Moment generating function (m.g.f):
Does not exist.
Value
A Cauchy
object.
See Also
Other continuous distributions:
Beta()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- Cauchy(10, 0.2)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 2)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the cumulative distribution function of a probability distribution
Description
Generic function for computing probabilities from distribution objects based on the cumulative distribution function (CDF).
Usage
cdf(d, x, drop = TRUE, ...)
Arguments
d |
An object. The package provides methods for distribution
objects such as those from |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Arguments passed to methods. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
Probabilities corresponding to the vector x
.
Examples
## distribution object
X <- Normal()
## probabilities from CDF
cdf(X, c(1, 2, 3, 4, 5))
Evaluate the cumulative distribution function of a Bernoulli distribution
Description
Evaluate the cumulative distribution function of a Bernoulli distribution
Usage
## S3 method for class 'Bernoulli'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Bernoulli(0.7)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 1)
log_pdf(X, 1)
cdf(X, 0)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the cumulative distribution function of a Beta distribution
Description
Evaluate the cumulative distribution function of a Beta distribution
Usage
## S3 method for class 'Beta'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Beta(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
mean(X)
variance(X)
skewness(X)
kurtosis(X)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the cumulative distribution function of a Binomial distribution
Description
Evaluate the cumulative distribution function of a Binomial distribution
Usage
## S3 method for class 'Binomial'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Binomial(10, 0.2)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2L)
log_pdf(X, 2L)
cdf(X, 4L)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the cumulative distribution function of a Categorical distribution
Description
Evaluate the cumulative distribution function of a Categorical distribution
Usage
## S3 method for class 'Categorical'
cdf(d, x, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
A vector of probabilities, one for each element of x
.
Examples
set.seed(27)
X <- Categorical(1:3, p = c(0.4, 0.1, 0.5))
X
Y <- Categorical(LETTERS[1:4])
Y
random(X, 10)
random(Y, 10)
pdf(X, 1)
log_pdf(X, 1)
cdf(X, 1)
quantile(X, 0.5)
# cdfs are only defined for numeric sample spaces. this errors!
# cdf(Y, "a")
# same for quantiles. this also errors!
# quantile(Y, 0.7)
Evaluate the cumulative distribution function of a Cauchy distribution
Description
Evaluate the cumulative distribution function of a Cauchy distribution
Usage
## S3 method for class 'Cauchy'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Cauchy(10, 0.2)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 2)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the cumulative distribution function of a chi square distribution
Description
Evaluate the cumulative distribution function of a chi square distribution
Usage
## S3 method for class 'ChiSquare'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- ChiSquare(5)
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)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the cumulative distribution function of an Erlang distribution
Description
Evaluate the cumulative distribution function of an Erlang distribution
Usage
## S3 method for class 'Erlang'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
An |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Erlang(5, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the cumulative distribution function of an Exponential distribution
Description
Evaluate the cumulative distribution function of an Exponential distribution
Usage
## S3 method for class 'Exponential'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
An |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Exponential(5)
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)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the cumulative distribution function of an F distribution
Description
Evaluate the cumulative distribution function of an F distribution
Usage
## S3 method for class 'FisherF'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- FisherF(5, 10, 0.2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the cumulative distribution function of a Frechet distribution
Description
Evaluate the cumulative distribution function of a Frechet distribution
Usage
## S3 method for class 'Frechet'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Frechet(0, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the cumulative distribution function of a Gamma distribution
Description
Evaluate the cumulative distribution function of a Gamma distribution
Usage
## S3 method for class 'Gamma'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Gamma(5, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the cumulative distribution function of a Geometric distribution
Description
Evaluate the cumulative distribution function of a Geometric distribution
Usage
## S3 method for class 'Geometric'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other Geometric distribution:
pdf.Geometric()
,
quantile.Geometric()
,
random.Geometric()
Examples
set.seed(27)
X <- Geometric(0.3)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Evaluate the cumulative distribution function of a GEV distribution
Description
Evaluate the cumulative distribution function of a GEV distribution
Usage
## S3 method for class 'GEV'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- GEV(1, 2, 0.1)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the cumulative distribution function of a GP distribution
Description
Evaluate the cumulative distribution function of a GP distribution
Usage
## S3 method for class 'GP'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- GP(0, 2, 0.1)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the cumulative distribution function of a Gumbel distribution
Description
Evaluate the cumulative distribution function of a Gumbel distribution
Usage
## S3 method for class 'Gumbel'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Gumbel(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the cumulative distribution function of a hurdle negative binomial distribution
Description
Evaluate the cumulative distribution function of a hurdle negative binomial distribution
Usage
## S3 method for class 'HurdleNegativeBinomial'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
## set up a hurdle negative binomial distribution
X <- HurdleNegativeBinomial(mu = 2.5, theta = 1, pi = 0.75)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Evaluate the cumulative distribution function of a hurdle Poisson distribution
Description
Evaluate the cumulative distribution function of a hurdle Poisson distribution
Usage
## S3 method for class 'HurdlePoisson'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
## set up a hurdle Poisson distribution
X <- HurdlePoisson(lambda = 2.5, pi = 0.75)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Evaluate the cumulative distribution function of a HyperGeometric distribution
Description
Evaluate the cumulative distribution function of a HyperGeometric distribution
Usage
## S3 method for class 'HyperGeometric'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other HyperGeometric distribution:
pdf.HyperGeometric()
,
quantile.HyperGeometric()
,
random.HyperGeometric()
Examples
set.seed(27)
X <- HyperGeometric(4, 5, 8)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Evaluate the cumulative distribution function of a Logistic distribution
Description
Evaluate the cumulative distribution function of a Logistic distribution
Usage
## S3 method for class 'Logistic'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other Logistic distribution:
pdf.Logistic()
,
quantile.Logistic()
,
random.Logistic()
Examples
set.seed(27)
X <- Logistic(2, 4)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Evaluate the cumulative distribution function of a LogNormal distribution
Description
Evaluate the cumulative distribution function of a LogNormal distribution
Usage
## S3 method for class 'LogNormal'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other LogNormal distribution:
fit_mle.LogNormal()
,
pdf.LogNormal()
,
quantile.LogNormal()
,
random.LogNormal()
Examples
set.seed(27)
X <- LogNormal(0.3, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Evaluate the cumulative distribution function of a negative binomial distribution
Description
Evaluate the cumulative distribution function of a negative binomial distribution
Usage
## S3 method for class 'NegativeBinomial'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other NegativeBinomial distribution:
pdf.NegativeBinomial()
,
quantile.NegativeBinomial()
,
random.NegativeBinomial()
Examples
set.seed(27)
X <- NegativeBinomial(size = 5, p = 0.1)
X
random(X, 10)
pdf(X, 50)
log_pdf(X, 50)
cdf(X, 50)
quantile(X, 0.7)
## alternative parameterization of X
Y <- NegativeBinomial(mu = 45, size = 5)
Y
cdf(Y, 50)
quantile(Y, 0.7)
Evaluate the cumulative distribution function of a Normal distribution
Description
Evaluate the cumulative distribution function of a Normal distribution
Usage
## S3 method for class 'Normal'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other Normal distribution:
fit_mle.Normal()
,
pdf.Normal()
,
quantile.Normal()
Examples
set.seed(27)
X <- Normal(5, 2)
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: calculating p-values for two-sided Z-test
# here the null hypothesis is H_0: mu = 3
# and we assume sigma = 2
# exactly the same as: Z <- Normal(0, 1)
Z <- Normal()
# data to test
x <- c(3, 7, 11, 0, 7, 0, 4, 5, 6, 2)
nx <- length(x)
# calculate the z-statistic
z_stat <- (mean(x) - 3) / (2 / sqrt(nx))
z_stat
# calculate the two-sided p-value
1 - cdf(Z, abs(z_stat)) + cdf(Z, -abs(z_stat))
# exactly equivalent to the above
2 * cdf(Z, -abs(z_stat))
# p-value for one-sided test
# H_0: mu <= 3 vs H_A: mu > 3
1 - cdf(Z, z_stat)
# p-value for one-sided test
# H_0: mu >= 3 vs H_A: mu < 3
cdf(Z, z_stat)
### example: calculating a 88 percent Z CI for a mean
# same `x` as before, still assume `sigma = 2`
# lower-bound
mean(x) - quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# upper-bound
mean(x) + quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# equivalent to
mean(x) + c(-1, 1) * quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# also equivalent to
mean(x) + quantile(Z, 0.12 / 2) * 2 / sqrt(nx)
mean(x) + quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
### generating random samples and plugging in ks.test()
set.seed(27)
# generate a random sample
ns <- random(Normal(3, 7), 26)
# test if sample is Normal(3, 7)
ks.test(ns, pnorm, mean = 3, sd = 7)
# test if sample is gamma(8, 3) using base R pgamma()
ks.test(ns, pgamma, shape = 8, rate = 3)
### MISC
# note that the cdf() and quantile() functions are inverses
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the cumulative distribution function of a Poisson distribution
Description
Evaluate the cumulative distribution function of a Poisson distribution
Usage
## S3 method for class 'Poisson'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Poisson(2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the cumulative distribution function of a PoissonBinomial distribution
Description
Evaluate the cumulative distribution function of a PoissonBinomial distribution
Usage
## S3 method for class 'PoissonBinomial'
cdf(
d,
x,
drop = TRUE,
elementwise = NULL,
lower.tail = TRUE,
log.p = FALSE,
verbose = TRUE,
...
)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
lower.tail , log.p , ... |
|
verbose |
logical. Should a warning be issued if the normal approximation is applied because the PoissonBinomial package is not installed? |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- PoissonBinomial(0.5, 0.3, 0.8)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 2)
quantile(X, 0.8)
cdf(X, quantile(X, 0.8))
quantile(X, cdf(X, 2))
## equivalent definitions of four Poisson binomial distributions
## each summing up three Bernoulli probabilities
p <- cbind(
p1 = c(0.1, 0.2, 0.1, 0.2),
p2 = c(0.5, 0.5, 0.5, 0.5),
p3 = c(0.8, 0.7, 0.9, 0.8))
PoissonBinomial(p)
PoissonBinomial(p[, 1], p[, 2], p[, 3])
PoissonBinomial(p[, 1:2], p[, 3])
Evaluate the cumulative distribution function of an RevWeibull distribution
Description
Evaluate the cumulative distribution function of an RevWeibull distribution
Usage
## S3 method for class 'RevWeibull'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- RevWeibull(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the cumulative distribution function of a StudentsT distribution
Description
Evaluate the cumulative distribution function of a StudentsT distribution
Usage
## S3 method for class 'StudentsT'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other StudentsT distribution:
pdf.StudentsT()
,
quantile.StudentsT()
,
random.StudentsT()
Examples
set.seed(27)
X <- StudentsT(3)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
### example: calculating p-values for two-sided T-test
# here the null hypothesis is H_0: mu = 3
# data to test
x <- c(3, 7, 11, 0, 7, 0, 4, 5, 6, 2)
nx <- length(x)
# calculate the T-statistic
t_stat <- (mean(x) - 3) / (sd(x) / sqrt(nx))
t_stat
# null distribution of statistic depends on sample size!
T <- StudentsT(df = nx - 1)
# calculate the two-sided p-value
1 - cdf(T, abs(t_stat)) + cdf(T, -abs(t_stat))
# exactly equivalent to the above
2 * cdf(T, -abs(t_stat))
# p-value for one-sided test
# H_0: mu <= 3 vs H_A: mu > 3
1 - cdf(T, t_stat)
# p-value for one-sided test
# H_0: mu >= 3 vs H_A: mu < 3
cdf(T, t_stat)
### example: calculating a 88 percent T CI for a mean
# lower-bound
mean(x) - quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# upper-bound
mean(x) + quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# equivalent to
mean(x) + c(-1, 1) * quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# also equivalent to
mean(x) + quantile(T, 0.12 / 2) * sd(x) / sqrt(nx)
mean(x) + quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
Evaluate the cumulative distribution function of a Tukey distribution
Description
Evaluate the cumulative distribution function of a Tukey distribution
Usage
## S3 method for class 'Tukey'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other Tukey distribution:
quantile.Tukey()
Examples
set.seed(27)
X <- Tukey(4L, 16L, 2L)
X
cdf(X, 4)
quantile(X, 0.7)
Evaluate the cumulative distribution function of a continuous Uniform distribution
Description
Evaluate the cumulative distribution function of a continuous Uniform distribution
Usage
## S3 method for class 'Uniform'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Uniform(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the cumulative distribution function of a Weibull distribution
Description
Evaluate the cumulative distribution function of a Weibull distribution
Usage
## S3 method for class 'Weibull'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other Weibull distribution:
pdf.Weibull()
,
quantile.Weibull()
,
random.Weibull()
Examples
set.seed(27)
X <- Weibull(0.3, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Evaluate the cumulative distribution function of a zero-inflated negative binomial distribution
Description
Evaluate the cumulative distribution function of a zero-inflated negative binomial distribution
Usage
## S3 method for class 'ZINegativeBinomial'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
## set up a zero-inflated negative binomial distribution
X <- ZINegativeBinomial(mu = 2.5, theta = 1, pi = 0.25)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Evaluate the cumulative distribution function of a zero-inflated Poisson distribution
Description
Evaluate the cumulative distribution function of a zero-inflated Poisson distribution
Usage
## S3 method for class 'ZIPoisson'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
## set up a zero-inflated Poisson distribution
X <- ZIPoisson(lambda = 2.5, pi = 0.25)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Evaluate the cumulative distribution function of a zero-truncated negative binomial distribution
Description
Evaluate the cumulative distribution function of a zero-truncated negative binomial distribution
Usage
## S3 method for class 'ZTNegativeBinomial'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
## set up a zero-truncated negative binomial distribution
X <- ZTNegativeBinomial(mu = 2.5, theta = 1)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Evaluate the cumulative distribution function of a zero-truncated Poisson distribution
Description
Evaluate the cumulative distribution function of a zero-truncated Poisson distribution
Usage
## S3 method for class 'ZTPoisson'
cdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
## set up a zero-truncated Poisson distribution
X <- ZTPoisson(lambda = 2.5)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Create a Chi-Square distribution
Description
Chi-square distributions show up often in frequentist settings as the sampling distribution of test statistics, especially in maximum likelihood estimation settings.
Usage
ChiSquare(df)
Arguments
df |
Degrees of freedom. Must be positive. |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a \chi^2
random variable with
df
= k
.
Support: R^+
, the set of positive real numbers
Mean: k
Variance: 2k
Probability density function (p.d.f):
f(x) = \frac{1}{\sqrt{2 \pi \sigma^2}} e^{-(x - \mu)^2 / 2 \sigma^2}
Cumulative distribution function (c.d.f):
The cumulative distribution function has the form
F(t) = \int_{-\infty}^t \frac{1}{\sqrt{2 \pi \sigma^2}} e^{-(x - \mu)^2 / 2 \sigma^2} dx
but this integral does not have a closed form solution and must be
approximated numerically. The c.d.f. of a standard normal is sometimes
called the "error function". The notation \Phi(t)
also stands
for the c.d.f. of a standard normal evaluated at t
. Z-tables
list the value of \Phi(t)
for various t
.
Moment generating function (m.g.f):
E(e^{tX}) = e^{\mu t + \sigma^2 t^2 / 2}
Value
A ChiSquare
object.
Transformations
A squared standard Normal()
distribution is equivalent to a
\chi^2_1
distribution with one degree of freedom. The
\chi^2
distribution is a special case of the Gamma()
distribution with shape (TODO: check this) parameter equal
to a half. Sums of \chi^2
distributions
are also distributed as \chi^2
distributions, where the
degrees of freedom of the contributing distributions get summed.
The ratio of two \chi^2
distributions is a FisherF()
distribution. The ratio of a Normal()
and the square root
of a scaled ChiSquare()
is a StudentsT()
distribution.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- ChiSquare(5)
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)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
The hurdle negative binomial distribution
Description
Density, distribution function, quantile function, and random
generation for the zero-hurdle negative binomial distribution with
parameters mu
, theta
(or size
), and pi
.
Usage
dhnbinom(x, mu, theta, size, pi, log = FALSE)
phnbinom(q, mu, theta, size, pi, lower.tail = TRUE, log.p = FALSE)
qhnbinom(p, mu, theta, size, pi, lower.tail = TRUE, log.p = FALSE)
rhnbinom(n, mu, theta, size, pi)
Arguments
x |
vector of (non-negative integer) quantiles. |
mu |
vector of (non-negative) negative binomial location parameters. |
theta , size |
vector of (non-negative) negative binomial overdispersion parameters.
Only |
pi |
vector of zero-hurdle probabilities in the unit interval. |
log , log.p |
logical indicating whether probabilities p are given as log(p). |
q |
vector of quantiles. |
lower.tail |
logical indicating whether probabilities are |
p |
vector of probabilities. |
n |
number of random values to return. |
Details
All functions follow the usual conventions of d/p/q/r functions
in base R. In particular, all four hnbinom
functions for the
hurdle negative binomial distribution call the corresponding nbinom
functions for the negative binomial distribution from base R internally.
Note, however, that the precision of qhnbinom
for very large
probabilities (close to 1) is limited because the probabilities
are internally handled in levels and not in logs (even if log.p = TRUE
).
See Also
HurdleNegativeBinomial
, dnbinom
Examples
## theoretical probabilities for a hurdle negative binomial distribution
x <- 0:8
p <- dhnbinom(x, mu = 2.5, theta = 1, pi = 0.75)
plot(x, p, type = "h", lwd = 2)
## corresponding empirical frequencies from a simulated sample
set.seed(0)
y <- rhnbinom(500, mu = 2.5, theta = 1, pi = 0.75)
hist(y, breaks = -1:max(y) + 0.5)
The hurdle Poisson distribution
Description
Density, distribution function, quantile function, and random
generation for the zero-hurdle Poisson distribution with
parameters lambda
and pi
.
Usage
dhpois(x, lambda, pi, log = FALSE)
phpois(q, lambda, pi, lower.tail = TRUE, log.p = FALSE)
qhpois(p, lambda, pi, lower.tail = TRUE, log.p = FALSE)
rhpois(n, lambda, pi)
Arguments
x |
vector of (non-negative integer) quantiles. |
lambda |
vector of (non-negative) Poisson parameters. |
pi |
vector of zero-hurdle probabilities in the unit interval. |
log , log.p |
logical indicating whether probabilities p are given as log(p). |
q |
vector of quantiles. |
lower.tail |
logical indicating whether probabilities are |
p |
vector of probabilities. |
n |
number of random values to return. |
Details
All functions follow the usual conventions of d/p/q/r functions
in base R. In particular, all four hpois
functions for the
hurdle Poisson distribution call the corresponding pois
functions for the Poisson distribution from base R internally.
Note, however, that the precision of qhpois
for very large
probabilities (close to 1) is limited because the probabilities
are internally handled in levels and not in logs (even if log.p = TRUE
).
See Also
Examples
## theoretical probabilities for a hurdle Poisson distribution
x <- 0:8
p <- dhpois(x, lambda = 2.5, pi = 0.75)
plot(x, p, type = "h", lwd = 2)
## corresponding empirical frequencies from a simulated sample
set.seed(0)
y <- rhpois(500, lambda = 2.5, pi = 0.75)
hist(y, breaks = -1:max(y) + 0.5)
The zero-inflated negative binomial distribution
Description
Density, distribution function, quantile function, and random
generation for the zero-inflated negative binomial distribution with
parameters mu
, theta
(or size
), and pi
.
Usage
dzinbinom(x, mu, theta, size, pi, log = FALSE)
pzinbinom(q, mu, theta, size, pi, lower.tail = TRUE, log.p = FALSE)
qzinbinom(p, mu, theta, size, pi, lower.tail = TRUE, log.p = FALSE)
rzinbinom(n, mu, theta, size, pi)
Arguments
x |
vector of (non-negative integer) quantiles. |
mu |
vector of (non-negative) negative binomial location parameters. |
theta , size |
vector of (non-negative) negative binomial overdispersion parameters.
Only |
pi |
vector of zero-inflation probabilities in the unit interval. |
log , log.p |
logical indicating whether probabilities p are given as log(p). |
q |
vector of quantiles. |
lower.tail |
logical indicating whether probabilities are |
p |
vector of probabilities. |
n |
number of random values to return. |
Details
All functions follow the usual conventions of d/p/q/r functions
in base R. In particular, all four zinbinom
functions for the
zero-inflated negative binomial distribution call the corresponding nbinom
functions for the negative binomial distribution from base R internally.
Note, however, that the precision of qzinbinom
for very large
probabilities (close to 1) is limited because the probabilities
are internally handled in levels and not in logs (even if log.p = TRUE
).
See Also
Examples
## theoretical probabilities for a zero-inflated negative binomial distribution
x <- 0:8
p <- dzinbinom(x, mu = 2.5, theta = 1, pi = 0.25)
plot(x, p, type = "h", lwd = 2)
## corresponding empirical frequencies from a simulated sample
set.seed(0)
y <- rzinbinom(500, mu = 2.5, theta = 1, pi = 0.25)
hist(y, breaks = -1:max(y) + 0.5)
The zero-inflated Poisson distribution
Description
Density, distribution function, quantile function, and random
generation for the zero-inflated Poisson distribution with
parameters lambda
and pi
.
Usage
dzipois(x, lambda, pi, log = FALSE)
pzipois(q, lambda, pi, lower.tail = TRUE, log.p = FALSE)
qzipois(p, lambda, pi, lower.tail = TRUE, log.p = FALSE)
rzipois(n, lambda, pi)
Arguments
x |
vector of (non-negative integer) quantiles. |
lambda |
vector of (non-negative) Poisson parameters. |
pi |
vector of zero-inflation probabilities in the unit interval. |
log , log.p |
logical indicating whether probabilities p are given as log(p). |
q |
vector of quantiles. |
lower.tail |
logical indicating whether probabilities are |
p |
vector of probabilities. |
n |
number of random values to return. |
Details
All functions follow the usual conventions of d/p/q/r functions
in base R. In particular, all four zipois
functions for the
zero-inflated Poisson distribution call the corresponding pois
functions for the Poisson distribution from base R internally.
Note, however, that the precision of qzipois
for very large
probabilities (close to 1) is limited because the probabilities
are internally handled in levels and not in logs (even if log.p = TRUE
).
See Also
Examples
## theoretical probabilities for a zero-inflated Poisson distribution
x <- 0:8
p <- dzipois(x, lambda = 2.5, pi = 0.25)
plot(x, p, type = "h", lwd = 2)
## corresponding empirical frequencies from a simulated sample
set.seed(0)
y <- rzipois(500, lambda = 2.5, pi = 0.25)
hist(y, breaks = -1:max(y) + 0.5)
The zero-truncated negative binomial distribution
Description
Density, distribution function, quantile function, and random
generation for the zero-truncated negative binomial distribution with
parameters mu
and theta
(or size
).
Usage
dztnbinom(x, mu, theta, size, log = FALSE)
pztnbinom(q, mu, theta, size, lower.tail = TRUE, log.p = FALSE)
qztnbinom(p, mu, theta, size, lower.tail = TRUE, log.p = FALSE)
rztnbinom(n, mu, theta, size)
Arguments
x |
vector of (non-negative integer) quantiles. |
mu |
vector of (non-negative) negative binomial location parameters. |
theta , size |
vector of (non-negative) negative binomial overdispersion parameters.
Only |
log , log.p |
logical indicating whether probabilities p are given as log(p). |
q |
vector of quantiles. |
lower.tail |
logical indicating whether probabilities are |
p |
vector of probabilities. |
n |
number of random values to return. |
Details
The negative binomial distribution left-truncated at zero (or zero-truncated negative binomial for short) is the distribution obtained, when considering a negative binomial variable Y conditional on Y being greater than zero.
All functions follow the usual conventions of d/p/q/r functions
in base R. In particular, all four ztnbinom
functions for the
zero-truncated negative binomial distribution call the corresponding nbinom
functions for the negative binomial distribution from base R internally.
See Also
Examples
## theoretical probabilities for a zero-truncated negative binomial distribution
x <- 0:8
p <- dztnbinom(x, mu = 2.5, theta = 1)
plot(x, p, type = "h", lwd = 2)
## corresponding empirical frequencies from a simulated sample
set.seed(0)
y <- rztnbinom(500, mu = 2.5, theta = 1)
hist(y, breaks = -1:max(y) + 0.5)
The zero-truncated Poisson distribution
Description
Density, distribution function, quantile function, and random
generation for the zero-truncated Poisson distribution with
parameter lambda
.
Usage
dztpois(x, lambda, log = FALSE)
pztpois(q, lambda, lower.tail = TRUE, log.p = FALSE)
qztpois(p, lambda, lower.tail = TRUE, log.p = FALSE)
rztpois(n, lambda)
Arguments
x |
vector of (non-negative integer) quantiles. |
lambda |
vector of (non-negative) Poisson parameters. |
log , log.p |
logical indicating whether probabilities p are given as log(p). |
q |
vector of quantiles. |
lower.tail |
logical indicating whether probabilities are |
p |
vector of probabilities. |
n |
number of random values to return. |
Details
The Poisson distribution left-truncated at zero (or zero-truncated Poisson for short) is the distribution obtained, when considering a Poisson variable Y conditional on Y being greater than zero.
All functions follow the usual conventions of d/p/q/r functions
in base R. In particular, all four ztpois
functions for the
zero-truncated Poisson distribution call the corresponding pois
functions for the Poisson distribution from base R internally.
See Also
Examples
## theoretical probabilities for a zero-truncated Poisson distribution
x <- 0:8
p <- dztpois(x, lambda = 2.5)
plot(x, p, type = "h", lwd = 2)
## corresponding empirical frequencies from a simulated sample
set.seed(0)
y <- rztpois(500, lambda = 2.5)
hist(y, breaks = -1:max(y) + 0.5)
Create an Erlang distribution
Description
The Erlang distribution is a two-parameter family of continuous probability
distributions with support x \in [0,\infty)
.
The two parameters are a positive integer shape parameter k
and a
positive real rate parameter \lambda
.
The Erlang distribution with shape parameter k = 1
simplifies to the
exponential distribution, and it is a special case of the gamma distribution.
It corresponds to a sum of k
independent exponential variables with mean
1 / \lambda
each.
Usage
Erlang(k, lambda)
Arguments
k |
The shape parameter. Can be any positive integer number. |
lambda |
The rate parameter. Can be any positive number. |
Value
An Erlang
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- Erlang(5, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Create an Exponential distribution
Description
Exponential distributions are frequently used for modeling the amount of time that passes until a specific event occurs. For example, exponential distributions could be used to model the time between two earthquakes, the amount of delay between internet packets, or the amount of time a piece of machinery can run before needing repair.
Usage
Exponential(rate = 1)
Arguments
rate |
The rate parameter, written |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be an Exponential random variable with
rate parameter rate
= \lambda
.
Support: x \in (0, \infty)
Mean: \frac{1}{\lambda}
Variance: \frac{1}{\lambda^2}
Probability density function (p.d.f):
f(x) = \lambda e^{-\lambda x}
Cumulative distribution function (c.d.f):
F(x) = 1 - e^{-\lambda x}
Moment generating function (m.g.f):
\frac{\lambda}{\lambda - t}, for t < \lambda
Value
An Exponential
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- Exponential(5)
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)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Goals scored in all 2018 FIFA World Cup matches
Description
Data from all 64 matches in the 2018 FIFA World Cup along with predicted ability differences based on bookmakers odds.
Usage
data("FIFA2018", package = "distributions3")
Format
A data frame with 128 rows and 7 columns.
- goals
integer. Number of goals scored in normal time (90 minutes), \ i.e., excluding potential extra time or penalties in knockout matches.
- team
character. 3-letter FIFA code for the team.
- match
integer. Match ID ranging from 1 (opening match) to 64 (final).
- type
factor. Type of match for groups A to H, round of 16 (R16), quarter final, semi-final, match for 3rd place, and final.
- stage
factor. Group vs. knockout tournament stage.
- logability
numeric. Estimated log-ability for each team based on bookmaker consensus model.
- difference
numeric. Difference in estimated log-abilities between a team and its opponent in each match.
Details
To investigate the number of goals scored per match in the 2018 FIFA World Cup,
FIFA2018
provides two rows, one for each team, for each of the matches
during the tournament. In addition some basic meta-information for the matches
(an ID, team name abbreviations, type of match, group vs. knockout stage),
information on the estimated log-ability for each team is provided. These
have been estimated by Zeileis et al. (2018) prior to the start of the
tournament (2018-05-20) based on quoted odds from 26 online bookmakers using
the bookmaker consensus model of Leitner et al. (2010). The difference in
log-ability between a team and its opponent is a useful predictor for the
number of goals scored.
To model the data a basic Poisson regression model provides a good fit. This treats the number of goals by the two teams as independent given the ability difference which is a reasonable assumption in this data set.
Source
The goals for each match have been obtained from Wikipedia (https://en.wikipedia.org/wiki/2018_FIFA_World_Cup) and the log-abilities from Zeileis et al. (2018) based on quoted odds from Oddschecker.com and Bwin.com.
References
Leitner C, Zeileis A, Hornik K (2010). Forecasting Sports Tournaments by Ratings of (Prob)abilities: A Comparison for the EURO 2008. International Journal of Forecasting, 26(3), 471-481. doi:10.1016/j.ijforecast.2009.10.001
Zeileis A, Leitner C, Hornik K (2018). Probabilistic Forecasts for the 2018 FIFA World Cup Based on the Bookmaker Consensus Model. Working Paper 2018-09, Working Papers in Economics and Statistics, Research Platform Empirical and Experimental Economics, University of Innsbruck. https://EconPapers.RePEc.org/RePEc:inn:wpaper:2018-09
Examples
## load data
data("FIFA2018", package = "distributions3")
## observed relative frequencies of goals in all matches
obsrvd <- prop.table(table(FIFA2018$goals))
## expected probabilities assuming a simple Poisson model,
## using the average number of goals across all teams/matches
## as the point estimate for the mean (lambda) of the distribution
p_const <- Poisson(lambda = mean(FIFA2018$goals))
p_const
expctd <- pdf(p_const, 0:6)
## comparison: observed vs. expected frequencies
## frequencies for 3 and 4 goals are slightly overfitted
## while 5 and 6 goals are slightly underfitted
cbind("observed" = obsrvd, "expected" = expctd)
## instead of fitting the same average Poisson model to all
## teams/matches, take ability differences into account
m <- glm(goals ~ difference, data = FIFA2018, family = poisson)
summary(m)
## when the ratio of abilities increases by 1 percent, the
## expected number of goals increases by around 0.4 percent
## this yields a different predicted Poisson distribution for
## each team/match
p_reg <- Poisson(lambda = fitted(m))
head(p_reg)
## as an illustration, the following goal distributions
## were expected for the final (that France won 4-2 against Croatia)
p_final <- tail(p_reg, 2)
p_final
pdf(p_final, 0:6)
## clearly France was expected to score more goals than Croatia
## but both teams scored more goals than expected, albeit not unlikely many
## assuming independence of the number of goals scored, obtain
## table of possible match results (after normal time), along with
## overall probabilities of win/draw/lose
res <- outer(pdf(p_final[1], 0:6), pdf(p_final[2], 0:6))
sum(res[lower.tri(res)]) ## France wins
sum(diag(res)) ## draw
sum(res[upper.tri(res)]) ## France loses
## update expected frequencies table based on regression model
expctd <- pdf(p_reg, 0:6)
head(expctd)
expctd <- colMeans(expctd)
cbind("observed" = obsrvd, "expected" = expctd)
Create an F distribution
Description
Create an F distribution
Usage
FisherF(df1, df2, lambda = 0)
Arguments
df1 |
Numerator degrees of freedom. Can be any positive number. |
df2 |
Denominator degrees of freedom. Can be any positive number. |
lambda |
Non-centrality parameter. Can be any positive number.
Defaults to |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail.
TODO
Value
A FisherF
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- FisherF(5, 10, 0.2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Fit a distribution to data
Description
Generic function for fitting maximum-likelihood estimates (MLEs) of a distribution based on empirical data.
Usage
fit_mle(d, x, ...)
Arguments
d |
An object. The package provides methods for distribution
objects such as those from |
x |
A vector of data to compute the likelihood. |
... |
Arguments passed to methods. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
A distribution (the same kind as d
) where the parameters
are the MLE estimates based on x
.
Examples
X <- Normal()
fit_mle(X, c(-1, 0, 0, 0, 3))
Fit a Bernoulli distribution to data
Description
Fit a Bernoulli distribution to data
Usage
## S3 method for class 'Bernoulli'
fit_mle(d, x, ...)
Arguments
d |
A |
x |
A vector of zeroes and ones. |
... |
Unused. |
Value
a Bernoulli
object
Fit a Binomial distribution to data
Description
The fit distribution will inherit the same size
parameter as
the Binomial
object passed.
Usage
## S3 method for class 'Binomial'
fit_mle(d, x, ...)
Arguments
d |
A |
x |
A vector of zeroes and ones. |
... |
Unused. |
Value
a Binomial
object
Fit an Exponential distribution to data
Description
Fit an Exponential distribution to data
Usage
## S3 method for class 'Exponential'
fit_mle(d, x, ...)
Arguments
d |
An |
x |
A vector of data. |
... |
Unused. |
Value
An Exponential
object.
Fit a Gamma distribution to data
Description
Fit a Gamma distribution to data
Usage
## S3 method for class 'Gamma'
fit_mle(d, x, ...)
Arguments
d |
A |
x |
A vector to fit the Gamma distribution to. |
... |
Unused. |
Value
a Gamma
object
Fit a Geometric distribution to data
Description
Fit a Geometric distribution to data
Usage
## S3 method for class 'Geometric'
fit_mle(d, x, ...)
Arguments
d |
A |
x |
A vector of zeroes and ones. |
... |
Unused. |
Value
a Geometric
object
Fit a Log Normal distribution to data
Description
Fit a Log Normal distribution to data
Usage
## S3 method for class 'LogNormal'
fit_mle(d, x, ...)
Arguments
d |
A |
x |
A vector of data. |
... |
Unused. |
Value
A LogNormal
object.
See Also
Other LogNormal distribution:
cdf.LogNormal()
,
pdf.LogNormal()
,
quantile.LogNormal()
,
random.LogNormal()
Fit a Normal distribution to data
Description
Fit a Normal distribution to data
Usage
## S3 method for class 'Normal'
fit_mle(d, x, ...)
Arguments
d |
A |
x |
A vector of data. |
... |
Unused. |
Value
A Normal
object.
See Also
Other Normal distribution:
cdf.Normal()
,
pdf.Normal()
,
quantile.Normal()
Fit an Poisson distribution to data
Description
Fit an Poisson distribution to data
Usage
## S3 method for class 'Poisson'
fit_mle(d, x, ...)
Arguments
d |
An |
x |
A vector of data. |
... |
Unused. |
Value
An Poisson
object.
Create a Frechet distribution
Description
The Frechet distribution is a special case of the \link{GEV}
distribution,
obtained when the GEV shape parameter \xi
is positive.
It may be referred to as a type II extreme value distribution.
Usage
Frechet(location = 0, scale = 1, shape = 1)
Arguments
location |
The location (minimum) parameter |
scale |
The scale parameter |
shape |
The shape parameter |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a Frechet random variable with location
parameter location
= m
, scale parameter scale
=
s
, and shape parameter shape
= \alpha
.
A Frechet(m, s, \alpha
) distribution is equivalent to a
\link{GEV}
(m + s, s / \alpha, 1 / \alpha
) distribution.
Support: (m, \infty)
.
Mean: m + s\Gamma(1 - 1/\alpha)
, for \alpha > 1
; undefined
otherwise.
Median: m + s(\ln 2)^{-1/\alpha}
.
Variance:
s^2 [\Gamma(1 - 2 / \alpha) - \Gamma(1 - 1 / \alpha)^2]
for \alpha > 2
; undefined otherwise.
Probability density function (p.d.f):
f(x) = \alpha s ^ {-1} [(x - m) / s] ^ {-(1 + \alpha)}%
\exp\{-[(x - m) / s] ^ {-\alpha} \}
for x > m
. The p.d.f. is 0 for x \leq m
.
Cumulative distribution function (c.d.f):
F(x) = \exp\{-[(x - m) / s] ^ {-\alpha} \}
for x > m
. The c.d.f. is 0 for x \leq m
.
Value
A Frechet
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- Frechet(0, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Create a Gamma distribution
Description
Several important distributions are special cases of the Gamma
distribution. When the shape parameter is 1
, the Gamma is an
exponential distribution with parameter 1/\beta
. When the
shape = n/2
and rate = 1/2
, the Gamma is a equivalent to
a chi squared distribution with n degrees of freedom. Moreover, if
we have X_1
is Gamma(\alpha_1, \beta)
and
X_2
is Gamma(\alpha_2, \beta)
, a function of these two variables
of the form \frac{X_1}{X_1 + X_2}
Beta(\alpha_1, \alpha_2)
.
This last property frequently appears in another distributions, and it
has extensively been used in multivariate methods. More about the Gamma
distribution will be added soon.
Usage
Gamma(shape, rate = 1)
Arguments
shape |
The shape parameter. Can be any positive number. |
rate |
The rate parameter. Can be any positive number. Defaults
to |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail.
In the following, let X
be a Gamma random variable
with parameters
shape
= \alpha
and
rate
= \beta
.
Support: x \in (0, \infty)
Mean: \frac{\alpha}{\beta}
Variance: \frac{\alpha}{\beta^2}
Probability density function (p.m.f):
f(x) = \frac{\beta^{\alpha}}{\Gamma(\alpha)} x^{\alpha - 1} e^{-\beta x}
Cumulative distribution function (c.d.f):
f(x) = \frac{\Gamma(\alpha, \beta x)}{\Gamma{\alpha}}
Moment generating function (m.g.f):
E(e^{tX}) = \Big(\frac{\beta}{ \beta - t}\Big)^{\alpha}, \thinspace t < \beta
Value
A Gamma
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- Gamma(5, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Create a Geometric distribution
Description
The Geometric distribution can be thought of as a generalization
of the Bernoulli()
distribution where we ask: "if I keep flipping a
coin with probability p
of heads, what is the probability I need
k
flips before I get my first heads?" The Geometric
distribution is a special case of Negative Binomial distribution.
Usage
Geometric(p = 0.5)
Arguments
p |
The success probability for the distribution. |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a Geometric random variable with
success probability p
= p
. Note that there are multiple
parameterizations of the Geometric distribution.
Support: 0 < p < 1, x = 0, 1, \dots
Mean: \frac{1-p}{p}
Variance: \frac{1-p}{p^2}
Probability mass function (p.m.f):
P(X = x) = p(1-p)^x,
Cumulative distribution function (c.d.f):
P(X \le x) = 1 - (1-p)^{x+1}
Moment generating function (m.g.f):
E(e^{tX}) = \frac{pe^t}{1 - (1-p)e^t}
Value
A Geometric
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Categorical()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
HyperGeometric()
,
Multinomial()
,
NegativeBinomial()
,
Poisson()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
set.seed(27)
X <- Geometric(0.3)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Create a Generalised Extreme Value (GEV) distribution
Description
The GEV distribution arises from the Extremal Types Theorem, which is rather
like the Central Limit Theorem (see \link{Normal}
) but it relates to
the maximum of n
i.i.d. random variables rather than to the sum.
If, after a suitable linear rescaling, the distribution of this maximum
tends to a non-degenerate limit as n
tends to infinity then this limit
must be a GEV distribution. The requirement that the variables are independent
can be relaxed substantially. Therefore, the GEV distribution is often used
to model the maximum of a large number of random variables.
Usage
GEV(mu = 0, sigma = 1, xi = 0)
Arguments
mu |
The location parameter, written |
sigma |
The scale parameter, written |
xi |
The shape parameter, written |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a GEV random variable with location
parameter mu
= \mu
, scale parameter sigma
= \sigma
and
shape parameter xi
= \xi
.
Support:
(-\infty, \mu - \sigma / \xi)
for \xi < 0
;
(\mu - \sigma / \xi, \infty)
for \xi > 0
;
and R
, the set of all real numbers, for \xi = 0
.
Mean: \mu + \sigma[\Gamma(1 - \xi) - 1]/\xi
for
\xi < 1, \xi \neq 0
;
\mu + \sigma\gamma
for \xi = 0
, where \gamma
is Euler's constant, approximately equal to 0.57722; undefined otherwise.
Median: \mu + \sigma[(\ln 2) ^ {-\xi} - 1]/\xi
for \xi \neq 0
;
\mu - \sigma\ln(\ln 2)
for \xi = 0
.
Variance:
\sigma^2 [\Gamma(1 - 2 \xi) - \Gamma(1 - \xi)^2] / \xi^2
for \xi < 1 / 2, \xi \neq 0
;
\sigma^2 \pi^2 / 6
for \xi = 0
; undefined otherwise.
Probability density function (p.d.f):
If \xi \neq 0
then
f(x) = \sigma ^ {-1} [1 + \xi (x - \mu) / \sigma] ^ {-(1 + 1/\xi)}%
\exp\{-[1 + \xi (x - \mu) / \sigma] ^ {-1/\xi} \}
for 1 + \xi (x - \mu) / \sigma > 0
. The p.d.f. is 0 outside the
support.
In the \xi = 0
(Gumbel) special case
f(x) = \sigma ^ {-1} \exp[-(x - \mu) / \sigma]%
\exp\{-\exp[-(x - \mu) / \sigma] \}
for x
in R
, the set of all real numbers.
Cumulative distribution function (c.d.f):
If \xi \neq 0
then
F(x) = \exp\{-[1 + \xi (x - \mu) / \sigma] ^ {-1/\xi} \}
for 1 + \xi (x - \mu) / \sigma > 0
. The c.d.f. is 0 below the
support and 1 above the support.
In the \xi = 0
(Gumbel) special case
F(x) = \exp\{-\exp[-(x - \mu) / \sigma] \}
for x
in R
, the set of all real numbers.
Value
A GEV
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- GEV(1, 2, 0.1)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Create a Generalised Pareto (GP) distribution
Description
The GP distribution has a link to the \link{GEV}
distribution.
Suppose that the maximum of n
i.i.d. random variables has
approximately a GEV distribution. For a sufficiently large threshold
u
, the conditional distribution of the amount (the threshold
excess) by which a variable exceeds u
given that it exceeds u
has approximately a GP distribution. Therefore, the GP distribution is
often used to model the threshold excesses of a high threshold u
.
The requirement that the variables are independent can be relaxed
substantially, but then exceedances of u
may cluster.
Usage
GP(mu = 0, sigma = 1, xi = 0)
Arguments
mu |
The location parameter, written |
sigma |
The scale parameter, written |
xi |
The shape parameter, written |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a GP random variable with location
parameter mu
= \mu
, scale parameter sigma
= \sigma
and
shape parameter xi
= \xi
.
Support:
[\mu, \mu - \sigma / \xi]
for \xi < 0
;
[\mu, \infty)
for \xi \geq 0
.
Mean: \mu + \sigma/(1 - \xi)
for
\xi < 1
; undefined otherwise.
Median: \mu + \sigma[2 ^ \xi - 1]/\xi
for \xi \neq 0
;
\mu + \sigma\ln 2
for \xi = 0
.
Variance:
\sigma^2 / (1 - \xi)^2 (1 - 2\xi)
for \xi < 1 / 2
; undefined otherwise.
Probability density function (p.d.f):
If \xi \neq 0
then
f(x) = \sigma^{-1} [1 + \xi (x - \mu) / \sigma] ^ {-(1 + 1/\xi)}
for 1 + \xi (x - \mu) / \sigma > 0
. The p.d.f. is 0 outside the
support.
In the \xi = 0
special case
f(x) = \sigma ^ {-1} \exp[-(x - \mu) / \sigma]
for x
in [\mu, \infty
). The p.d.f. is 0 outside the support.
Cumulative distribution function (c.d.f):
If \xi \neq 0
then
F(x) = 1 - \exp\{-[1 + \xi (x - \mu) / \sigma] ^ {-1/\xi} \}
for 1 + \xi (x - \mu) / \sigma > 0
. The c.d.f. is 0 below the
support and 1 above the support.
In the \xi = 0
special case
F(x) = 1 - \exp[-(x - \mu) / \sigma] \}
for x
in R
, the set of all real numbers.
Value
A GP
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- GP(0, 2, 0.1)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Create a Gumbel distribution
Description
The Gumbel distribution is a special case of the \link{GEV}
distribution,
obtained when the GEV shape parameter \xi
is equal to 0.
It may be referred to as a type I extreme value distribution.
Usage
Gumbel(mu = 0, sigma = 1)
Arguments
mu |
The location parameter, written |
sigma |
The scale parameter, written |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a Gumbel random variable with location
parameter mu
= \mu
, scale parameter sigma
= \sigma
.
Support: R
, the set of all real numbers.
Mean: \mu + \sigma\gamma
, where \gamma
is Euler's
constant, approximately equal to 0.57722.
Median: \mu - \sigma\ln(\ln 2)
.
Variance: \sigma^2 \pi^2 / 6
.
Probability density function (p.d.f):
f(x) = \sigma ^ {-1} \exp[-(x - \mu) / \sigma]%
\exp\{-\exp[-(x - \mu) / \sigma] \}
for x
in R
, the set of all real numbers.
Cumulative distribution function (c.d.f):
In the \xi = 0
(Gumbel) special case
F(x) = \exp\{-\exp[-(x - \mu) / \sigma] \}
for x
in R
, the set of all real numbers.
Value
A Gumbel
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- Gumbel(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Create a hurdle negative binomial distribution
Description
Hurdle negative binomial distributions are frequently used to model counts with overdispersion and many zero observations.
Usage
HurdleNegativeBinomial(mu, theta, pi)
Arguments
mu |
Location parameter of the negative binomial component of the distribution. Can be any positive number. |
theta |
Overdispersion parameter of the negative binomial component of the distribution. Can be any positive number. |
pi |
Zero-hurdle probability, can be any value in |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail.
In the following, let X
be a hurdle negative binomial random variable with parameters
mu
= \mu
and theta
= \theta
.
Support: \{0, 1, 2, 3, ...\}
Mean:
\mu \cdot \frac{\pi}{1 - F(0; \mu, \theta)}
where F(k; \mu)
is the c.d.f. of the NegativeBinomial
distribution.
Variance:
m \cdot \left(1 + \frac{\mu}{\theta} + \mu - m \right)
where m
is the mean above.
Probability mass function (p.m.f.): P(X = 0) = 1 - \pi
and for k > 0
P(X = k) = \pi \cdot \frac{f(k; \mu, \theta)}{1 - F(0; \mu, \theta)}
where f(k; \mu, \theta)
is the p.m.f. of the NegativeBinomial
distribution.
Cumulative distribution function (c.d.f.): P(X \le 0) = 1 - \pi
and for k > 0
P(X \le k) = 1 - \pi + \pi \cdot \frac{F(k; \mu, \theta) - F(0; \mu, \theta)}{1 - F(0; \mu, \theta)}
Moment generating function (m.g.f.):
Omitted for now.
Value
A HurdleNegativeBinomial
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Categorical()
,
Geometric()
,
HurdlePoisson()
,
HyperGeometric()
,
Multinomial()
,
NegativeBinomial()
,
Poisson()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
## set up a hurdle negative binomial distribution
X <- HurdleNegativeBinomial(mu = 2.5, theta = 1, pi = 0.75)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Create a hurdle Poisson distribution
Description
Hurdle Poisson distributions are frequently used to model counts with many zero observations.
Usage
HurdlePoisson(lambda, pi)
Arguments
lambda |
Parameter of the Poisson component of the distribution. Can be any positive number. |
pi |
Zero-hurdle probability, can be any value in |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail.
In the following, let X
be a hurdle Poisson random variable with parameter
lambda
= \lambda
.
Support: \{0, 1, 2, 3, ...\}
Mean:
\lambda \cdot \frac{\pi}{1 - e^{-\lambda}}
Variance: m \cdot (\lambda + 1 - m)
, where m
is the mean above.
Probability mass function (p.m.f.): P(X = 0) = 1 - \pi
and for k > 0
P(X = k) = \pi \cdot \frac{f(k; \lambda)}{1 - f(0; \lambda)}
where f(k; \lambda)
is the p.m.f. of the Poisson
distribution.
Cumulative distribution function (c.d.f.): P(X \le 0) = 1 - \pi
and for k > 0
P(X \le k) = 1 - \pi + \pi \cdot \frac{F(k; \lambda) - F(0; \lambda)}{1 - F(0; \lambda)}
where F(k; \lambda)
is the c.d.f. of the Poisson
distribution.
Moment generating function (m.g.f.):
Omitted for now.
Value
A HurdlePoisson
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Categorical()
,
Geometric()
,
HurdleNegativeBinomial()
,
HyperGeometric()
,
Multinomial()
,
NegativeBinomial()
,
Poisson()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
## set up a hurdle Poisson distribution
X <- HurdlePoisson(lambda = 2.5, pi = 0.75)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Create a HyperGeometric distribution
Description
To understand the HyperGeometric distribution, consider a set of
r
objects, of which m
are of the type I and
n
are of the type II. A sample with size k
(k<r
)
with no replacement is randomly chosen. The number of observed
type I elements observed in this sample is set to be our random
variable X
. For example, consider that in a set of 20
car parts, there are 4 that are defective (type I).
If we take a sample of size 5 from those car parts, the
probability of finding 2 that are defective will be given by
the HyperGeometric distribution (needs double checking).
Usage
HyperGeometric(m, n, k)
Arguments
m |
The number of type I elements available. |
n |
The number of type II elements available. |
k |
The size of the sample taken. |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a HyperGeometric random variable with
success probability p
= p = m/(m+n)
.
Support: x \in { \{\max{(0, k-n)}, \dots, \min{(k,m)}}\}
Mean: \frac{km}{n+m} = kp
Variance: \frac{km(n)(n+m-k)}{(n+m)^2 (n+m-1)} =
kp(1-p)(1 - \frac{k-1}{m+n-1})
Probability mass function (p.m.f):
P(X = x) = \frac{{m \choose x}{n \choose k-x}}{{m+n \choose k}}
Cumulative distribution function (c.d.f):
P(X \le k) \approx \Phi\Big(\frac{x - kp}{\sqrt{kp(1-p)}}\Big)
Moment generating function (m.g.f):
Not useful.
Value
A HyperGeometric
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Categorical()
,
Geometric()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
Multinomial()
,
NegativeBinomial()
,
Poisson()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
set.seed(27)
X <- HyperGeometric(4, 5, 8)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Determine whether a distribution is discrete or continuous
Description
Generic functions for determining whether a certain probability distribution is discrete or continuous, respectively.
Usage
is_discrete(d, ...)
is_continuous(d, ...)
Arguments
d |
An object. The package provides methods for distribution
objects such as those from |
... |
Arguments passed to methods. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Details
The generic function is_discrete
is intended to return TRUE
for every distribution whose entire support is discrete and FALSE
otherwise. Analogously, is_continuous
is intended to return TRUE
for every distribution whose entire support is continuous and FALSE
otherwise. For mixed discrete-continuous distributions both methods should
return FALSE
.
Methods for both generics are provided for all distribution
classes
set up in this package.
Value
A logical vector indicating whether the distribution(s) in d
is/are discrete or continuous, respectively.
Examples
X <- Normal()
is_discrete(X)
is_continuous(X)
Y <- Binomial(size = 10, p = c(0.2, 0.5, 0.8))
is_discrete(Y)
is_continuous(Y)
Is an object a distribution?
Description
is_distribution
tests if x
inherits from "distribution"
.
Usage
is_distribution(x)
Arguments
x |
An object to test. |
Examples
Z <- Normal()
is_distribution(Z)
is_distribution(1L)
Compute the (log-)likelihood of a probability distribution given data
Description
Functions for computing the (log-)likelihood based on a distribution object and observed data. The log-likelihood is computed as the sum of log-density contributions and the likelihood by taking the exponential thereof.
Usage
log_likelihood(d, x, ...)
likelihood(d, x, ...)
Arguments
d |
An object. The package provides methods for distribution
objects such as those from |
x |
A vector of data to compute the likelihood. |
... |
Arguments passed to methods. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
Numeric value of the (log-)likelihood.
Examples
## distribution object
X <- Normal()
## sum of log_pdf() contributions
log_likelihood(X, c(-1, 0, 0, 0, 3))
## exp of log_likelihood()
likelihood(X, c(-1, 0, 0, 0, 3))
Create a Logistic distribution
Description
A continuous distribution on the real line. For binary outcomes
the model given by P(Y = 1 | X) = F(X \beta)
where
F
is the Logistic cdf()
is called logistic regression.
Usage
Logistic(location = 0, scale = 1)
Arguments
location |
The location parameter for the distribution. For Logistic distributions, the location parameter is the mean, median and also mode. Defaults to zero. |
scale |
The scale parameter for the distribution. Defaults to one. |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a Logistic random variable with
location
= \mu
and scale
= s
.
Support: R
, the set of all real numbers
Mean: \mu
Variance: s^2 \pi^2 / 3
Probability density function (p.d.f):
f(x) = \frac{e^{-(\frac{x - \mu}{s})}}{s [1 + \exp(-(\frac{x - \mu}{s})) ]^2}
Cumulative distribution function (c.d.f):
F(t) = \frac{1}{1 + e^{-(\frac{t - \mu}{s})}}
Moment generating function (m.g.f):
E(e^{tX}) = e^{\mu t} \beta(1 - st, 1 + st)
where \beta(x, y)
is the Beta function.
Value
A Logistic
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- Logistic(2, 4)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Create a LogNormal distribution
Description
A random variable created by exponentiating a Normal()
distribution. Taking the log of LogNormal data returns in
Normal()
data.
Usage
LogNormal(log_mu = 0, log_sigma = 1)
Arguments
log_mu |
The location parameter, written |
log_sigma |
The scale parameter, written |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a LogNormal random variable with
success probability p
= p
.
Support: R^+
Mean: \exp(\mu + \sigma^2/2)
Variance: [\exp(\sigma^2)-1]\exp(2\mu+\sigma^2)
Probability density function (p.d.f):
f(x) = \frac{1}{x \sigma \sqrt{2 \pi}} \exp \left(-\frac{(\log x - \mu)^2}{2 \sigma^2} \right)
Cumulative distribution function (c.d.f):
F(x) = \frac{1}{2} + \frac{1}{2\sqrt{pi}}\int_{-x}^x e^{-t^2} dt
Moment generating function (m.g.f): Undefined.
Value
A LogNormal
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- LogNormal(0.3, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Create a Multinomial distribution
Description
The multinomial distribution is a generalization of the binomial
distribution to multiple categories. It is perhaps easiest to think
that we first extend a Bernoulli()
distribution to include more
than two categories, resulting in a Categorical()
distribution.
We then extend repeat the Categorical experiment several (n
)
times.
Usage
Multinomial(size, p)
Arguments
size |
The number of trials. Must be an integer greater than or equal
to one. When |
p |
A vector of success probabilities for each trial. |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X = (X_1, ..., X_k)
be a Multinomial
random variable with success probability p
= p
. Note that
p
is vector with k
elements that sum to one. Assume
that we repeat the Categorical experiment size
= n
times.
Support: Each X_i
is in {0, 1, 2, ..., n}
.
Mean: The mean of X_i
is n p_i
.
Variance: The variance of X_i
is n p_i (1 - p_i)
.
For i \neq j
, the covariance of X_i
and X_j
is -n p_i p_j
.
Probability mass function (p.m.f):
P(X_1 = x_1, ..., X_k = x_k) = \frac{n!}{x_1! x_2! ... x_k!} p_1^{x_1} \cdot p_2^{x_2} \cdot ... \cdot p_k^{x_k}
Cumulative distribution function (c.d.f):
Omitted for multivariate random variables for the time being.
Moment generating function (m.g.f):
E(e^{tX}) = \left(\sum_{i=1}^k p_i e^{t_i}\right)^n
Value
A Multinomial
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Categorical()
,
Geometric()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
HyperGeometric()
,
NegativeBinomial()
,
Poisson()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
set.seed(27)
X <- Multinomial(size = 5, p = c(0.3, 0.4, 0.2, 0.1))
X
random(X, 10)
# pdf(X, 2)
# log_pdf(X, 2)
Create a negative binomial distribution
Description
A generalization of the geometric distribution. It is the number
of failures in a sequence of i.i.d. Bernoulli trials before
a specified target number (r
) of successes occurs.
Usage
NegativeBinomial(size, p = 0.5, mu = size)
Arguments
size |
The target number of successes (greater than |
p |
The success probability for a given trial. |
mu |
Alternative parameterization via the non-negative mean
of the distribution (instead of the probability |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a negative binomial random variable with
success probability p
= p
.
Support: \{0, 1, 2, 3, ...\}
Mean: \frac{(1 - p) r}{p} = \mu
Variance: \frac{(1 - p) r}{p^2}
Probability mass function (p.m.f.):
f(k) = {k + r - 1 \choose k} \cdot p^r (1-p)^k
Cumulative distribution function (c.d.f.):
Omitted for now.
Moment generating function (m.g.f.):
\left(\frac{p}{1 - (1 -p) e^t}\right)^r, t < -\log (1-p)
Alternative parameterization: Sometimes, especially when used in
regression models, the negative binomial distribution is parameterized
by its mean \mu
(as listed above) plus the size parameter r
.
This implies a success probability of p = r/(r + \mu)
. This can
also be seen as a generalization of the Poisson distribution where the
assumption of equidispersion (i.e., variance equal to mean) is relaxed.
The negative binomial distribution is overdispersed (i.e., variance greater than mean)
and its variance can also be written as \mu + 1/r \mu^2
. The Poisson
distribution is then obtained as r
goes to infinity. Note that in this
view it is natural to also allow for non-integer r
parameters.
The factorials in the equations above are then expressed in terms of the
gamma function.
Value
A NegativeBinomial
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Categorical()
,
Geometric()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
HyperGeometric()
,
Multinomial()
,
Poisson()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
set.seed(27)
X <- NegativeBinomial(size = 5, p = 0.1)
X
random(X, 10)
pdf(X, 50)
log_pdf(X, 50)
cdf(X, 50)
quantile(X, 0.7)
## alternative parameterization of X
Y <- NegativeBinomial(mu = 45, size = 5)
Y
cdf(Y, 50)
quantile(Y, 0.7)
Create a Normal distribution
Description
The Normal distribution is ubiquitous in statistics, partially because of the central limit theorem, which states that sums of i.i.d. random variables eventually become Normal. Linear transformations of Normal random variables result in new random variables that are also Normal. If you are taking an intro stats course, you'll likely use the Normal distribution for Z-tests and in simple linear regression. Under regularity conditions, maximum likelihood estimators are asymptotically Normal. The Normal distribution is also called the gaussian distribution.
Usage
Normal(mu = 0, sigma = 1)
Arguments
mu |
The location parameter, written |
sigma |
The scale parameter, written |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a Normal random variable with mean
mu
= \mu
and standard deviation sigma
= \sigma
.
Support: R
, the set of all real numbers
Mean: \mu
Variance: \sigma^2
Probability density function (p.d.f):
f(x) = \frac{1}{\sqrt{2 \pi \sigma^2}} e^{-(x - \mu)^2 / 2 \sigma^2}
Cumulative distribution function (c.d.f):
The cumulative distribution function has the form
F(t) = \int_{-\infty}^t \frac{1}{\sqrt{2 \pi \sigma^2}} e^{-(x - \mu)^2 / 2 \sigma^2} dx
but this integral does not have a closed form solution and must be
approximated numerically. The c.d.f. of a standard Normal is sometimes
called the "error function". The notation \Phi(t)
also stands
for the c.d.f. of a standard Normal evaluated at t
. Z-tables
list the value of \Phi(t)
for various t
.
Moment generating function (m.g.f):
E(e^{tX}) = e^{\mu t + \sigma^2 t^2 / 2}
Value
A Normal
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- Normal(5, 2)
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: calculating p-values for two-sided Z-test
# here the null hypothesis is H_0: mu = 3
# and we assume sigma = 2
# exactly the same as: Z <- Normal(0, 1)
Z <- Normal()
# data to test
x <- c(3, 7, 11, 0, 7, 0, 4, 5, 6, 2)
nx <- length(x)
# calculate the z-statistic
z_stat <- (mean(x) - 3) / (2 / sqrt(nx))
z_stat
# calculate the two-sided p-value
1 - cdf(Z, abs(z_stat)) + cdf(Z, -abs(z_stat))
# exactly equivalent to the above
2 * cdf(Z, -abs(z_stat))
# p-value for one-sided test
# H_0: mu <= 3 vs H_A: mu > 3
1 - cdf(Z, z_stat)
# p-value for one-sided test
# H_0: mu >= 3 vs H_A: mu < 3
cdf(Z, z_stat)
### example: calculating a 88 percent Z CI for a mean
# same `x` as before, still assume `sigma = 2`
# lower-bound
mean(x) - quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# upper-bound
mean(x) + quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# equivalent to
mean(x) + c(-1, 1) * quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# also equivalent to
mean(x) + quantile(Z, 0.12 / 2) * 2 / sqrt(nx)
mean(x) + quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
### generating random samples and plugging in ks.test()
set.seed(27)
# generate a random sample
ns <- random(Normal(3, 7), 26)
# test if sample is Normal(3, 7)
ks.test(ns, pnorm, mean = 3, sd = 7)
# test if sample is gamma(8, 3) using base R pgamma()
ks.test(ns, pgamma, shape = 8, rate = 3)
### MISC
# note that the cdf() and quantile() functions are inverses
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the probability density of a probability distribution
Description
Generic function for computing probability density function (PDF) contributions based on a distribution object and observed data.
Usage
pdf(d, x, drop = TRUE, ...)
log_pdf(d, x, ...)
pmf(d, x, ...)
Arguments
d |
An object. The package provides methods for distribution
objects such as those from |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Arguments passed to methods. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Details
The generic function pdf()
computes the probability density,
both for continuous and discrete distributions. pmf()
(for the
probability mass function) is an alias that just calls pdf()
internally.
For computing log-density contributions (e.g., to a log-likelihood)
either pdf(..., log = TRUE)
can be used or the generic function
log_pdf()
.
Value
Probabilities corresponding to the vector x
.
Examples
## distribution object
X <- Normal()
## probability density
pdf(X, c(1, 2, 3, 4, 5))
pmf(X, c(1, 2, 3, 4, 5))
## log-density
pdf(X, c(1, 2, 3, 4, 5), log = TRUE)
log_pdf(X, c(1, 2, 3, 4, 5))
Evaluate the probability mass function of a Bernoulli distribution
Description
Evaluate the probability mass function of a Bernoulli distribution
Usage
## S3 method for class 'Bernoulli'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Bernoulli'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Bernoulli(0.7)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 1)
log_pdf(X, 1)
cdf(X, 0)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the probability mass function of a Beta distribution
Description
Evaluate the probability mass function of a Beta distribution
Usage
## S3 method for class 'Beta'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Beta'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Beta(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
mean(X)
variance(X)
skewness(X)
kurtosis(X)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the probability mass function of a Binomial distribution
Description
Evaluate the probability mass function of a Binomial distribution
Usage
## S3 method for class 'Binomial'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Binomial'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Binomial(10, 0.2)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2L)
log_pdf(X, 2L)
cdf(X, 4L)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the probability mass function of a Categorical discrete distribution
Description
Evaluate the probability mass function of a Categorical discrete distribution
Usage
## S3 method for class 'Categorical'
pdf(d, x, ...)
## S3 method for class 'Categorical'
log_pdf(d, x, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
A vector of probabilities, one for each element of x
.
Examples
set.seed(27)
X <- Categorical(1:3, p = c(0.4, 0.1, 0.5))
X
Y <- Categorical(LETTERS[1:4])
Y
random(X, 10)
random(Y, 10)
pdf(X, 1)
log_pdf(X, 1)
cdf(X, 1)
quantile(X, 0.5)
# cdfs are only defined for numeric sample spaces. this errors!
# cdf(Y, "a")
# same for quantiles. this also errors!
# quantile(Y, 0.7)
Evaluate the probability mass function of a Cauchy distribution
Description
Evaluate the probability mass function of a Cauchy distribution
Usage
## S3 method for class 'Cauchy'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Cauchy'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Cauchy(10, 0.2)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 2)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the probability mass function of a chi square distribution
Description
Evaluate the probability mass function of a chi square distribution
Usage
## S3 method for class 'ChiSquare'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'ChiSquare'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- ChiSquare(5)
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)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the probability mass function of an Erlang distribution
Description
Evaluate the probability mass function of an Erlang distribution
Usage
## S3 method for class 'Erlang'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Erlang'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
An |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Erlang(5, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the probability density function of an Exponential distribution
Description
Evaluate the probability density function of an Exponential distribution
Usage
## S3 method for class 'Exponential'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Exponential'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
An |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Exponential(5)
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)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the probability mass function of an F distribution
Description
Evaluate the probability mass function of an F distribution
Usage
## S3 method for class 'FisherF'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'FisherF'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- FisherF(5, 10, 0.2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the probability mass function of a Frechet distribution
Description
Evaluate the probability mass function of a Frechet distribution
Usage
## S3 method for class 'Frechet'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Frechet'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Frechet(0, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the probability mass function of a Gamma distribution
Description
Evaluate the probability mass function of a Gamma distribution
Usage
## S3 method for class 'Gamma'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Gamma'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Gamma(5, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the probability mass function of a Geometric distribution
Description
Please see the documentation of Geometric()
for some properties
of the Geometric distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Usage
## S3 method for class 'Geometric'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Geometric'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other Geometric distribution:
cdf.Geometric()
,
quantile.Geometric()
,
random.Geometric()
Examples
set.seed(27)
X <- Geometric(0.3)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Evaluate the probability mass function of a GEV distribution
Description
Evaluate the probability mass function of a GEV distribution
Usage
## S3 method for class 'GEV'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'GEV'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- GEV(1, 2, 0.1)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the probability mass function of a GP distribution
Description
Evaluate the probability mass function of a GP distribution
Usage
## S3 method for class 'GP'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'GP'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- GP(0, 2, 0.1)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the probability mass function of a Gumbel distribution
Description
Evaluate the probability mass function of a Gumbel distribution
Usage
## S3 method for class 'Gumbel'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Gumbel'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Gumbel(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the probability mass function of a hurdle negative binomial distribution
Description
Evaluate the probability mass function of a hurdle negative binomial distribution
Usage
## S3 method for class 'HurdleNegativeBinomial'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'HurdleNegativeBinomial'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
## set up a hurdle negative binomial distribution
X <- HurdleNegativeBinomial(mu = 2.5, theta = 1, pi = 0.75)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Evaluate the probability mass function of a hurdle Poisson distribution
Description
Evaluate the probability mass function of a hurdle Poisson distribution
Usage
## S3 method for class 'HurdlePoisson'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'HurdlePoisson'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
## set up a hurdle Poisson distribution
X <- HurdlePoisson(lambda = 2.5, pi = 0.75)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Evaluate the probability mass function of a HyperGeometric distribution
Description
Please see the documentation of HyperGeometric()
for some properties
of the HyperGeometric distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Usage
## S3 method for class 'HyperGeometric'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'HyperGeometric'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other HyperGeometric distribution:
cdf.HyperGeometric()
,
quantile.HyperGeometric()
,
random.HyperGeometric()
Examples
set.seed(27)
X <- HyperGeometric(4, 5, 8)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Evaluate the probability mass function of a Logistic distribution
Description
Please see the documentation of Logistic()
for some properties
of the Logistic distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Usage
## S3 method for class 'Logistic'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Logistic'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other Logistic distribution:
cdf.Logistic()
,
quantile.Logistic()
,
random.Logistic()
Examples
set.seed(27)
X <- Logistic(2, 4)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Evaluate the probability mass function of a LogNormal distribution
Description
Please see the documentation of LogNormal()
for some properties
of the LogNormal distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Usage
## S3 method for class 'LogNormal'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'LogNormal'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other LogNormal distribution:
cdf.LogNormal()
,
fit_mle.LogNormal()
,
quantile.LogNormal()
,
random.LogNormal()
Examples
set.seed(27)
X <- LogNormal(0.3, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Evaluate the probability mass function of a Multinomial distribution
Description
Please see the documentation of Multinomial()
for some properties
of the Multinomial distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Usage
## S3 method for class 'Multinomial'
pdf(d, x, ...)
## S3 method for class 'Multinomial'
log_pdf(d, x, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
A vector of probabilities, one for each element of x
.
See Also
Other Multinomial distribution:
random.Multinomial()
Examples
set.seed(27)
X <- Multinomial(size = 5, p = c(0.3, 0.4, 0.2, 0.1))
X
random(X, 10)
# pdf(X, 2)
# log_pdf(X, 2)
Evaluate the probability mass function of a NegativeBinomial distribution
Description
Evaluate the probability mass function of a NegativeBinomial distribution
Usage
## S3 method for class 'NegativeBinomial'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'NegativeBinomial'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other NegativeBinomial distribution:
cdf.NegativeBinomial()
,
quantile.NegativeBinomial()
,
random.NegativeBinomial()
Examples
set.seed(27)
X <- NegativeBinomial(size = 5, p = 0.1)
X
random(X, 10)
pdf(X, 50)
log_pdf(X, 50)
cdf(X, 50)
quantile(X, 0.7)
## alternative parameterization of X
Y <- NegativeBinomial(mu = 45, size = 5)
Y
cdf(Y, 50)
quantile(Y, 0.7)
Evaluate the probability mass function of a Normal distribution
Description
Please see the documentation of Normal()
for some properties
of the Normal distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Usage
## S3 method for class 'Normal'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Normal'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other Normal distribution:
cdf.Normal()
,
fit_mle.Normal()
,
quantile.Normal()
Examples
set.seed(27)
X <- Normal(5, 2)
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: calculating p-values for two-sided Z-test
# here the null hypothesis is H_0: mu = 3
# and we assume sigma = 2
# exactly the same as: Z <- Normal(0, 1)
Z <- Normal()
# data to test
x <- c(3, 7, 11, 0, 7, 0, 4, 5, 6, 2)
nx <- length(x)
# calculate the z-statistic
z_stat <- (mean(x) - 3) / (2 / sqrt(nx))
z_stat
# calculate the two-sided p-value
1 - cdf(Z, abs(z_stat)) + cdf(Z, -abs(z_stat))
# exactly equivalent to the above
2 * cdf(Z, -abs(z_stat))
# p-value for one-sided test
# H_0: mu <= 3 vs H_A: mu > 3
1 - cdf(Z, z_stat)
# p-value for one-sided test
# H_0: mu >= 3 vs H_A: mu < 3
cdf(Z, z_stat)
### example: calculating a 88 percent Z CI for a mean
# same `x` as before, still assume `sigma = 2`
# lower-bound
mean(x) - quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# upper-bound
mean(x) + quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# equivalent to
mean(x) + c(-1, 1) * quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# also equivalent to
mean(x) + quantile(Z, 0.12 / 2) * 2 / sqrt(nx)
mean(x) + quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
### generating random samples and plugging in ks.test()
set.seed(27)
# generate a random sample
ns <- random(Normal(3, 7), 26)
# test if sample is Normal(3, 7)
ks.test(ns, pnorm, mean = 3, sd = 7)
# test if sample is gamma(8, 3) using base R pgamma()
ks.test(ns, pgamma, shape = 8, rate = 3)
### MISC
# note that the cdf() and quantile() functions are inverses
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the probability mass function of a Poisson distribution
Description
Evaluate the probability mass function of a Poisson distribution
Usage
## S3 method for class 'Poisson'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Poisson'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Poisson(2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Evaluate the probability mass function of a PoissonBinomial distribution
Description
Evaluate the probability mass function of a PoissonBinomial distribution
Usage
## S3 method for class 'PoissonBinomial'
pdf(d, x, drop = TRUE, elementwise = NULL, log = FALSE, verbose = TRUE, ...)
## S3 method for class 'PoissonBinomial'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
log , ... |
|
verbose |
logical. Should a warning be issued if the normal approximation is applied because the PoissonBinomial package is not installed? |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- PoissonBinomial(0.5, 0.3, 0.8)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 2)
quantile(X, 0.8)
cdf(X, quantile(X, 0.8))
quantile(X, cdf(X, 2))
## equivalent definitions of four Poisson binomial distributions
## each summing up three Bernoulli probabilities
p <- cbind(
p1 = c(0.1, 0.2, 0.1, 0.2),
p2 = c(0.5, 0.5, 0.5, 0.5),
p3 = c(0.8, 0.7, 0.9, 0.8))
PoissonBinomial(p)
PoissonBinomial(p[, 1], p[, 2], p[, 3])
PoissonBinomial(p[, 1:2], p[, 3])
Evaluate the probability mass function of an RevWeibull distribution
Description
Evaluate the probability mass function of an RevWeibull distribution
Usage
## S3 method for class 'RevWeibull'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'RevWeibull'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- RevWeibull(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the probability mass function of a StudentsT distribution
Description
Please see the documentation of StudentsT()
for some properties
of the StudentsT distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Usage
## S3 method for class 'StudentsT'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'StudentsT'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other StudentsT distribution:
cdf.StudentsT()
,
quantile.StudentsT()
,
random.StudentsT()
Examples
set.seed(27)
X <- StudentsT(3)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
### example: calculating p-values for two-sided T-test
# here the null hypothesis is H_0: mu = 3
# data to test
x <- c(3, 7, 11, 0, 7, 0, 4, 5, 6, 2)
nx <- length(x)
# calculate the T-statistic
t_stat <- (mean(x) - 3) / (sd(x) / sqrt(nx))
t_stat
# null distribution of statistic depends on sample size!
T <- StudentsT(df = nx - 1)
# calculate the two-sided p-value
1 - cdf(T, abs(t_stat)) + cdf(T, -abs(t_stat))
# exactly equivalent to the above
2 * cdf(T, -abs(t_stat))
# p-value for one-sided test
# H_0: mu <= 3 vs H_A: mu > 3
1 - cdf(T, t_stat)
# p-value for one-sided test
# H_0: mu >= 3 vs H_A: mu < 3
cdf(T, t_stat)
### example: calculating a 88 percent T CI for a mean
# lower-bound
mean(x) - quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# upper-bound
mean(x) + quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# equivalent to
mean(x) + c(-1, 1) * quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# also equivalent to
mean(x) + quantile(T, 0.12 / 2) * sd(x) / sqrt(nx)
mean(x) + quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
Evaluate the probability mass function of a continuous Uniform distribution
Description
Evaluate the probability mass function of a continuous Uniform distribution
Usage
## S3 method for class 'Uniform'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Uniform'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
set.seed(27)
X <- Uniform(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Evaluate the probability mass function of a Weibull distribution
Description
Please see the documentation of Weibull()
for some properties
of the Weibull distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Usage
## S3 method for class 'Weibull'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'Weibull'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
See Also
Other Weibull distribution:
cdf.Weibull()
,
quantile.Weibull()
,
random.Weibull()
Examples
set.seed(27)
X <- Weibull(0.3, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Evaluate the probability mass function of a zero-inflated negative binomial distribution
Description
Evaluate the probability mass function of a zero-inflated negative binomial distribution
Usage
## S3 method for class 'ZINegativeBinomial'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'ZINegativeBinomial'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
## set up a zero-inflated negative binomial distribution
X <- ZINegativeBinomial(mu = 2.5, theta = 1, pi = 0.25)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Evaluate the probability mass function of a zero-inflated Poisson distribution
Description
Evaluate the probability mass function of a zero-inflated Poisson distribution
Usage
## S3 method for class 'ZIPoisson'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'ZIPoisson'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
## set up a zero-inflated Poisson distribution
X <- ZIPoisson(lambda = 2.5, pi = 0.25)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Evaluate the probability mass function of a zero-truncated negative binomial distribution
Description
Evaluate the probability mass function of a zero-truncated negative binomial distribution
Usage
## S3 method for class 'ZTNegativeBinomial'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'ZTNegativeBinomial'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
## set up a zero-truncated negative binomial distribution
X <- ZTNegativeBinomial(mu = 2.5, theta = 1)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Evaluate the probability mass function of a zero-truncated Poisson distribution
Description
Evaluate the probability mass function of a zero-truncated Poisson distribution
Usage
## S3 method for class 'ZTPoisson'
pdf(d, x, drop = TRUE, elementwise = NULL, ...)
## S3 method for class 'ZTPoisson'
log_pdf(d, x, drop = TRUE, elementwise = NULL, ...)
Arguments
d |
A |
x |
A vector of elements whose probabilities you would like to
determine given the distribution |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(x)
columns (if drop = FALSE
). In case of a vectorized distribution
object, a matrix with length(x)
columns containing all possible combinations.
Examples
## set up a zero-truncated Poisson distribution
X <- ZTPoisson(lambda = 2.5)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Plot the CDF of a distribution
Description
A function to easily plot the CDF of a distribution using ggplot2
. Requires ggplot2
to be loaded.
Usage
plot_cdf(d, limits = NULL, p = 0.001, plot_theme = NULL)
Arguments
d |
A |
limits |
either |
p |
If |
plot_theme |
specify theme of resulting plot using |
Examples
N1 <- Normal()
plot_cdf(N1)
N2 <- Normal(0, c(1, 2))
plot_cdf(N2)
B1 <- Binomial(10, 0.2)
plot_cdf(B1)
B2 <- Binomial(10, c(0.2, 0.5))
plot_cdf(B2)
Plot the PDF of a distribution
Description
A function to easily plot the PDF of a distribution using ggplot2
. Requires ggplot2
to be loaded.
Usage
plot_pdf(d, limits = NULL, p = 0.001, plot_theme = NULL)
Arguments
d |
A |
limits |
either |
p |
If |
plot_theme |
specify theme of resulting plot using |
Examples
N1 <- Normal()
plot_pdf(N1)
N2 <- Normal(0, c(1, 2))
plot_pdf(N2)
B1 <- Binomial(10, 0.2)
plot_pdf(B1)
B2 <- Binomial(10, c(0.2, 0.5))
plot_pdf(B2)
Plot the p.m.f, p.d.f or c.d.f. of a univariate distribution
Description
Plot method for an object inheriting from class "distribution"
.
By default the probability density function (p.d.f.), for a continuous
variable, or the probability mass function (p.m.f.), for a discrete
variable, is plotted. The cumulative distribution function (c.d.f.)
will be plotted if cdf = TRUE
. Multiple functions are included
in the plot if any of the parameter vectors in x
has length greater
than 1. See the argument all
.
Usage
## S3 method for class 'distribution'
plot(
x,
cdf = FALSE,
p = c(0.1, 99.9),
len = 1000,
all = FALSE,
legend_args = list(),
...
)
Arguments
x |
an object of class |
cdf |
A logical scalar. If |
p |
A numeric vector. If |
len |
An integer scalar. If |
all |
A logical scalar. If |
legend_args |
A list of arguments to be passed to
|
... |
Further arguments to be passed to |
Details
If xlim
is passed in ...
then this determines the
range of values of the variable to be plotted on the horizontal axis.
If x
is a discrete distribution object then the values for which
the p.m.f. or c.d.f. is plotted is the smallest set of consecutive
integers that contains both components of xlim
. Otherwise,
xlim
is used directly.
If xlim
is not passed in ...
then the range of values spans
the support of the distribution, with the following proviso: if the
lower (upper) endpoint of the distribution is -Inf
(Inf
)
then the lower (upper) limit of the plotting range is set to the
p[1]
\
If the name of x
is a single upper case letter then that name is
used to labels the axes of the plot. Otherwise, x
and
P(X = x)
or f(x)
are used.
A legend is included only if at least one of the parameter vectors in
x
has length greater than 1.
Plots of c.d.f.s are produced using calls to
approxfun
and plot.ecdf
.
Value
An object with the same class as x
, in which the parameter
vectors have been expanded to contain a parameter combination for each
function plotted.
Examples
B <- Binomial(20, 0.7)
plot(B)
plot(B, cdf = TRUE)
B2 <- Binomial(20, c(0.1, 0.5, 0.9))
plot(B2, legend_args = list(x = "top"))
x <- plot(B2, cdf = TRUE)
x$size
x$p
X <- Poisson(2)
plot(X)
plot(X, cdf = TRUE)
G <- Gamma(c(1, 3), 1:2)
plot(G)
plot(G, all = TRUE)
plot(G, cdf = TRUE)
C <- Cauchy()
plot(C, p = c(1, 99), len = 10000)
plot(C, cdf = TRUE, p = c(1, 99))
Create a Poisson distribution
Description
Poisson distributions are frequently used to model counts.
Usage
Poisson(lambda)
Arguments
lambda |
The shape parameter, which is also the mean and the variance of the distribution. Can be any positive number. |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail.
In the following, let X
be a Poisson random variable with parameter
lambda
= \lambda
.
Support: \{0, 1, 2, 3, ...\}
Mean: \lambda
Variance: \lambda
Probability mass function (p.m.f):
P(X = k) = \frac{\lambda^k e^{-\lambda}}{k!}
Cumulative distribution function (c.d.f):
P(X \le k) = e^{-\lambda}
\sum_{i = 0}^{\lfloor k \rfloor} \frac{\lambda^i}{i!}
Moment generating function (m.g.f):
E(e^{tX}) = e^{\lambda (e^t - 1)}
Value
A Poisson
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Categorical()
,
Geometric()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
HyperGeometric()
,
Multinomial()
,
NegativeBinomial()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
set.seed(27)
X <- Poisson(2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Create a Poisson binomial distribution
Description
The Poisson binomial distribution is a generalization of the
Binomial
distribution. It is also a sum
of n
independent Bernoulli experiments. However, the success
probabilities can vary between the experiments so that they are
not identically distributed.
Usage
PoissonBinomial(...)
Arguments
... |
An arbitrary number of numeric vectors or matrices
of success probabilities in |
Details
The Poisson binomial distribution comes up when you consider the number of successes in independent binomial experiments (coin flips) with potentially varying success probabilities.
The PoissonBinomial
distribution class in distributions3
is mostly based on the PoissonBinomial package, providing fast
Rcpp implementations of efficient algorithms. Hence, it is
recommended to install the PoissonBinomial package when working
with this distribution. However, as a fallback for when the PoissonBinomial
package is not installed the methods for the PoissonBinomial
distribution employ a normal approximation.
We recommend reading the following documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail.
In the following, let X
be a Poisson binomial random variable with
success probabilities p_1
to p_n
.
Support: \{0, 1, 2, ..., n\}
Mean: p_1 + \dots + p_n
Variance: p_1 \cdot (1 - p_1) + \dots + p_1 \cdot (1 - p_1)
Probability mass function (p.m.f):
P(X = k) = \sum_A \prod_{i \in A} p_i \prod_{j \in A^C} (1 - p_j)
where the sum is taken over all sets A
with k
elements from
\{0, 1, 2, ..., n\}
. A^C
is the complement
of A
.
Cumulative distribution function (c.d.f):
P(X \le k) = \sum_{i=0}^{\lfloor k \rfloor} P(X = i)
Moment generating function (m.g.f):
E(e^{tX}) = \prod_{i = 1}^n (1 - p_i + p_i e^t)
Value
A PoissonBinomial
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Categorical()
,
Geometric()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
HyperGeometric()
,
Multinomial()
,
NegativeBinomial()
,
Poisson()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
set.seed(27)
X <- PoissonBinomial(0.5, 0.3, 0.8)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 2)
quantile(X, 0.8)
cdf(X, quantile(X, 0.8))
quantile(X, cdf(X, 2))
## equivalent definitions of four Poisson binomial distributions
## each summing up three Bernoulli probabilities
p <- cbind(
p1 = c(0.1, 0.2, 0.1, 0.2),
p2 = c(0.5, 0.5, 0.5, 0.5),
p3 = c(0.8, 0.7, 0.9, 0.8))
PoissonBinomial(p)
PoissonBinomial(p[, 1], p[, 2], p[, 3])
PoissonBinomial(p[, 1:2], p[, 3])
Extracting fitted or predicted probability distributions from models
Description
Generic function with methods for various model classes for extracting
fitted (in-sample) or predicted (out-of-sample) probability distributions3
objects.
Usage
prodist(object, ...)
## S3 method for class 'lm'
prodist(object, ..., sigma = "ML")
## S3 method for class 'glm'
prodist(object, ..., dispersion = NULL)
## S3 method for class 'distribution'
prodist(object, ...)
Arguments
object |
A model object. |
... |
Arguments passed on to methods, typically for calling the
underlying |
sigma |
character or numeric or |
dispersion |
character or numeric or |
Details
To facilitate making probabilistic forecasts based on regression and time
series model objects, the function prodist
extracts fitted or
predicted probability distribution
objects. Currently, methods are
provided for objects fitted by lm
,
glm
, and arima
in base R as
well as glm.nb
from the MASS package and
hurdle
/zeroinfl
/zerotrunc
from the pscl or
countreg packages.
All methods essentially
proceed in two steps: First, the standard predict
method for these model objects is used to compute fitted (in-sample, default)
or predicted (out-of-sample) distribution parameters. Typically, this includes
the mean plus further parameters describing scale, dispersion, shape, etc.).
Second, the distributions
objects are set up using the generator
functions from distributions3.
Note that these probability distributions only reflect the random variation in the dependent variable based on the model employed (and its associated distributional assumpation for the dependent variable). This does not capture the uncertainty in the parameter estimates.
For both linear regression models and generalized linear models, estimated
by lm
and glm
respectively, there is some ambiguity as to which
estimate for the dispersion parameter of the model is to be used. While the
logLik
methods use the maximum-likelihood (ML) estimate
implicitly, the summary
methods report an estimate that is standardized
with the residual degrees of freedom, n - k (rather than the number of
observations, n). The prodist
methods for these objects follow
the logLik
method by default but the summary
behavior can be
mimicked by setting the sigma
or dispersion
arguments
accordingly.
Value
An object inheriting from distribution
.
See Also
Examples
## Model: Linear regression
## Fit: lm
## Data: 1920s cars data
data("cars", package = "datasets")
## Stopping distance (ft) explained by speed (mph)
reg <- lm(dist ~ speed, data = cars)
## Extract fitted normal distributions (in-sample, with constant variance)
pd <- prodist(reg)
head(pd)
## Extract log-likelihood from model object
logLik(reg)
## Replicate log-likelihood via distributions object
sum(log_pdf(pd, cars$dist))
log_likelihood(pd, cars$dist)
## Compute corresponding medians and 90% interval
qd <- quantile(pd, c(0.05, 0.5, 0.95))
head(qd)
## Visualize observations with predicted quantiles
plot(dist ~ speed, data = cars)
matplot(cars$speed, qd, add = TRUE, type = "l", col = 2, lty = 1)
## Sigma estimated by maximum-likelihood estimate (default, used in logLik)
## vs. least-squares estimate (used in summary)
nd <- data.frame(speed = 50)
prodist(reg, newdata = nd, sigma = "ML")
prodist(reg, newdata = nd, sigma = "OLS")
summary(reg)$sigma
## Model: Poisson generalized linear model
## Fit: glm
## Data: FIFA 2018 World Cup data
data("FIFA2018", package = "distributions3")
## Number of goals per team explained by ability differences
poisreg <- glm(goals ~ difference, data = FIFA2018, family = poisson)
summary(poisreg)
## Interpretation: When the ratio of abilities increases by 1 percent,
## the expected number of goals increases by around 0.4 percent
## Predict fitted Poisson distributions for teams with equal ability (out-of-sample)
nd <- data.frame(difference = 0)
prodist(poisreg, newdata = nd)
## Extract fitted Poisson distributions (in-sample)
pd <- prodist(poisreg)
head(pd)
## Extract log-likelihood from model object
logLik(poisreg)
## Replicate log-likelihood via distributions object
sum(log_pdf(pd, FIFA2018$goals))
log_likelihood(pd, FIFA2018$goals)
## Model: Autoregressive integrated moving average model
## Fit: arima
## Data: Quarterly approval ratings of U.S. presidents (1945-1974)
data("presidents", package = "datasets")
## ARMA(2,1) model
arma21 <- arima(presidents, order = c(2, 0, 1))
## Extract predicted normal distributions for next two years
p <- prodist(arma21, n.ahead = 8)
p
## Compute median (= mean) forecast along with 80% and 95% interval
quantile(p, c(0.5, 0.1, 0.9, 0.025, 0.975))
Determine quantiles of a Bernoulli distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'Bernoulli'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- Bernoulli(0.7)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 1)
log_pdf(X, 1)
cdf(X, 0)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Determine quantiles of a Beta distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'Beta'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- Beta(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
mean(X)
variance(X)
skewness(X)
kurtosis(X)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Determine quantiles of a Binomial distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'Binomial'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Shoul the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- Binomial(10, 0.2)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2L)
log_pdf(X, 2L)
cdf(X, 4L)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Determine quantiles of a Categorical discrete distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'Categorical'
quantile(x, probs, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
A vector of quantiles, one for each element of probs
.
Examples
set.seed(27)
X <- Categorical(1:3, p = c(0.4, 0.1, 0.5))
X
Y <- Categorical(LETTERS[1:4])
Y
random(X, 10)
random(Y, 10)
pdf(X, 1)
log_pdf(X, 1)
cdf(X, 1)
quantile(X, 0.5)
# cdfs are only defined for numeric sample spaces. this errors!
# cdf(Y, "a")
# same for quantiles. this also errors!
# quantile(Y, 0.7)
Determine quantiles of a Cauchy distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'Cauchy'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- Cauchy(10, 0.2)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 2)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Determine quantiles of a chi square distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'ChiSquare'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- ChiSquare(5)
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)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Determine quantiles of an Erlang distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'Erlang'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
An |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- Erlang(5, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Determine quantiles of an Exponential distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'Exponential'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
An |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- Exponential(5)
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)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Determine quantiles of an F distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'FisherF'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- FisherF(5, 10, 0.2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Determine quantiles of a Frechet distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'Frechet'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- Frechet(0, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Determine quantiles of a Gamma distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'Gamma'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- Gamma(5, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Determine quantiles of a Geometric distribution
Description
Determine quantiles of a Geometric distribution
Usage
## S3 method for class 'Geometric'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
See Also
Other Geometric distribution:
cdf.Geometric()
,
pdf.Geometric()
,
random.Geometric()
Examples
set.seed(27)
X <- Geometric(0.3)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Determine quantiles of a GEV distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'GEV'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- GEV(1, 2, 0.1)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Determine quantiles of a GP distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'GP'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- GP(0, 2, 0.1)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Determine quantiles of a Gumbel distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'Gumbel'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- Gumbel(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Determine quantiles of a hurdle negative binomial distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'HurdleNegativeBinomial'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
## set up a hurdle negative binomial distribution
X <- HurdleNegativeBinomial(mu = 2.5, theta = 1, pi = 0.75)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Determine quantiles of a hurdle Poisson distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'HurdlePoisson'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
## set up a hurdle Poisson distribution
X <- HurdlePoisson(lambda = 2.5, pi = 0.75)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Determine quantiles of a HyperGeometric distribution
Description
Determine quantiles of a HyperGeometric distribution
Usage
## S3 method for class 'HyperGeometric'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
See Also
Other HyperGeometric distribution:
cdf.HyperGeometric()
,
pdf.HyperGeometric()
,
random.HyperGeometric()
Examples
set.seed(27)
X <- HyperGeometric(4, 5, 8)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Determine quantiles of a Logistic distribution
Description
Determine quantiles of a Logistic distribution
Usage
## S3 method for class 'Logistic'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
See Also
Other Logistic distribution:
cdf.Logistic()
,
pdf.Logistic()
,
random.Logistic()
Examples
set.seed(27)
X <- Logistic(2, 4)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Determine quantiles of a LogNormal distribution
Description
Determine quantiles of a LogNormal distribution
Usage
## S3 method for class 'LogNormal'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
See Also
Other LogNormal distribution:
cdf.LogNormal()
,
fit_mle.LogNormal()
,
pdf.LogNormal()
,
random.LogNormal()
Examples
set.seed(27)
X <- LogNormal(0.3, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Determine quantiles of a NegativeBinomial distribution
Description
Determine quantiles of a NegativeBinomial distribution
Usage
## S3 method for class 'NegativeBinomial'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
See Also
Other NegativeBinomial distribution:
cdf.NegativeBinomial()
,
pdf.NegativeBinomial()
,
random.NegativeBinomial()
Examples
set.seed(27)
X <- NegativeBinomial(size = 5, p = 0.1)
X
random(X, 10)
pdf(X, 50)
log_pdf(X, 50)
cdf(X, 50)
quantile(X, 0.7)
## alternative parameterization of X
Y <- NegativeBinomial(mu = 45, size = 5)
Y
cdf(Y, 50)
quantile(Y, 0.7)
Determine quantiles of a Normal distribution
Description
Please see the documentation of Normal()
for some properties
of the Normal distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
quantile()
Usage
## S3 method for class 'Normal'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Details
This function returns the same values that you get from a Z-table. Note
quantile()
is the inverse of cdf()
. Please see the documentation of Normal()
for some properties
of the Normal distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
See Also
Other Normal distribution:
cdf.Normal()
,
fit_mle.Normal()
,
pdf.Normal()
Examples
set.seed(27)
X <- Normal(5, 2)
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: calculating p-values for two-sided Z-test
# here the null hypothesis is H_0: mu = 3
# and we assume sigma = 2
# exactly the same as: Z <- Normal(0, 1)
Z <- Normal()
# data to test
x <- c(3, 7, 11, 0, 7, 0, 4, 5, 6, 2)
nx <- length(x)
# calculate the z-statistic
z_stat <- (mean(x) - 3) / (2 / sqrt(nx))
z_stat
# calculate the two-sided p-value
1 - cdf(Z, abs(z_stat)) + cdf(Z, -abs(z_stat))
# exactly equivalent to the above
2 * cdf(Z, -abs(z_stat))
# p-value for one-sided test
# H_0: mu <= 3 vs H_A: mu > 3
1 - cdf(Z, z_stat)
# p-value for one-sided test
# H_0: mu >= 3 vs H_A: mu < 3
cdf(Z, z_stat)
### example: calculating a 88 percent Z CI for a mean
# same `x` as before, still assume `sigma = 2`
# lower-bound
mean(x) - quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# upper-bound
mean(x) + quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# equivalent to
mean(x) + c(-1, 1) * quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# also equivalent to
mean(x) + quantile(Z, 0.12 / 2) * 2 / sqrt(nx)
mean(x) + quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
### generating random samples and plugging in ks.test()
set.seed(27)
# generate a random sample
ns <- random(Normal(3, 7), 26)
# test if sample is Normal(3, 7)
ks.test(ns, pnorm, mean = 3, sd = 7)
# test if sample is gamma(8, 3) using base R pgamma()
ks.test(ns, pgamma, shape = 8, rate = 3)
### MISC
# note that the cdf() and quantile() functions are inverses
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Determine quantiles of a Poisson distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'Poisson'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- Poisson(2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Determine quantiles of a PoissonBinomial distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'PoissonBinomial'
quantile(
x,
probs,
drop = TRUE,
elementwise = NULL,
lower.tail = TRUE,
log.p = FALSE,
verbose = TRUE,
...
)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Shoul the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
lower.tail , log.p , ... |
|
verbose |
logical. Should a warning be issued if the normal approximation is applied because the PoissonBinomial package is not installed? |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- PoissonBinomial(0.5, 0.3, 0.8)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 2)
quantile(X, 0.8)
cdf(X, quantile(X, 0.8))
quantile(X, cdf(X, 2))
## equivalent definitions of four Poisson binomial distributions
## each summing up three Bernoulli probabilities
p <- cbind(
p1 = c(0.1, 0.2, 0.1, 0.2),
p2 = c(0.5, 0.5, 0.5, 0.5),
p3 = c(0.8, 0.7, 0.9, 0.8))
PoissonBinomial(p)
PoissonBinomial(p[, 1], p[, 2], p[, 3])
PoissonBinomial(p[, 1:2], p[, 3])
Determine quantiles of a RevWeibull distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'RevWeibull'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- RevWeibull(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Determine quantiles of a StudentsT distribution
Description
Please see the documentation of StudentsT()
for some properties
of the StudentsT distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
quantile()
Usage
## S3 method for class 'StudentsT'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Details
This function returns the same values that you get from a Z-table. Note
quantile()
is the inverse of cdf()
. Please see the documentation of
StudentsT()
for some properties
of the StudentsT distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
See Also
Other StudentsT distribution:
cdf.StudentsT()
,
pdf.StudentsT()
,
random.StudentsT()
Examples
set.seed(27)
X <- StudentsT(3)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
### example: calculating p-values for two-sided T-test
# here the null hypothesis is H_0: mu = 3
# data to test
x <- c(3, 7, 11, 0, 7, 0, 4, 5, 6, 2)
nx <- length(x)
# calculate the T-statistic
t_stat <- (mean(x) - 3) / (sd(x) / sqrt(nx))
t_stat
# null distribution of statistic depends on sample size!
T <- StudentsT(df = nx - 1)
# calculate the two-sided p-value
1 - cdf(T, abs(t_stat)) + cdf(T, -abs(t_stat))
# exactly equivalent to the above
2 * cdf(T, -abs(t_stat))
# p-value for one-sided test
# H_0: mu <= 3 vs H_A: mu > 3
1 - cdf(T, t_stat)
# p-value for one-sided test
# H_0: mu >= 3 vs H_A: mu < 3
cdf(T, t_stat)
### example: calculating a 88 percent T CI for a mean
# lower-bound
mean(x) - quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# upper-bound
mean(x) + quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# equivalent to
mean(x) + c(-1, 1) * quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# also equivalent to
mean(x) + quantile(T, 0.12 / 2) * sd(x) / sqrt(nx)
mean(x) + quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
Determine quantiles of a Tukey distribution
Description
Determine quantiles of a Tukey distribution
Usage
## S3 method for class 'Tukey'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A vector of elements whose cumulative probabilities you would
like to determine given the distribution |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
See Also
Other Tukey distribution:
cdf.Tukey()
Examples
set.seed(27)
X <- Tukey(4L, 16L, 2L)
X
cdf(X, 4)
quantile(X, 0.7)
Determine quantiles of a continuous Uniform distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'Uniform'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
set.seed(27)
X <- Uniform(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Determine quantiles of a Weibull distribution
Description
Determine quantiles of a Weibull distribution
Usage
## S3 method for class 'Weibull'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
See Also
Other Weibull distribution:
cdf.Weibull()
,
pdf.Weibull()
,
random.Weibull()
Examples
set.seed(27)
X <- Weibull(0.3, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Determine quantiles of a zero-inflated negative binomial distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'ZINegativeBinomial'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
## set up a zero-inflated negative binomial distribution
X <- ZINegativeBinomial(mu = 2.5, theta = 1, pi = 0.25)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Determine quantiles of a zero-inflated Poisson distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'ZIPoisson'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
## set up a zero-inflated Poisson distribution
X <- ZIPoisson(lambda = 2.5, pi = 0.25)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Determine quantiles of a zero-truncated negative binomial distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'ZTNegativeBinomial'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
## set up a zero-truncated negative binomial distribution
X <- ZTNegativeBinomial(mu = 2.5, theta = 1)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Determine quantiles of a zero-truncated Poisson distribution
Description
quantile()
is the inverse of cdf()
.
Usage
## S3 method for class 'ZTPoisson'
quantile(x, probs, drop = TRUE, elementwise = NULL, ...)
Arguments
x |
A |
probs |
A vector of probabilities. |
drop |
logical. Should the result be simplified to a vector if possible? |
elementwise |
logical. Should each distribution in |
... |
Arguments to be passed to |
Value
In case of a single distribution object, either a numeric
vector of length probs
(if drop = TRUE
, default) or a matrix
with
length(probs)
columns (if drop = FALSE
). In case of a vectorized
distribution object, a matrix with length(probs)
columns containing all
possible combinations.
Examples
## set up a zero-truncated Poisson distribution
X <- ZTPoisson(lambda = 2.5)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Draw a random sample from a probability distribution
Description
Generic function for drawing random samples from distribution objects.
Usage
random(x, n = 1L, drop = TRUE, ...)
## S3 method for class 'distribution'
simulate(object, nsim = 1L, seed = NULL, ...)
Arguments
x , object |
An object. The package provides methods for distribution
objects such as those from |
n , nsim |
The number of samples to draw. Should be a positive
integer. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Arguments passed to methods. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
seed |
An optional random seed that is to be set using |
Details
random
is a new generic for drawing random samples from
the S3 distribution
objects provided in this package, such as
Normal
or Binomial
etc. The respective
methods typically call the "r" function for the corresponding
distribution functions provided in base R such as rnorm
,
rbinom
etc.
In addition to the new random
generic there is also a
simulate
method for distribution objects which simply
calls the random
method internally.
Value
Random samples drawn from the distriubtion x
. The random
methods typically return either a matrix or, if possible, a vector. The
simulate
method always returns a data frame (with an attribute
"seed"
containing the .Random.seed
from before the simulation).
Examples
## distribution object
X <- Normal()
## 10 random samples
random(X, 10)
Draw a random sample from a Bernoulli distribution
Description
Draw a random sample from a Bernoulli distribution
Usage
## S3 method for class 'Bernoulli'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- Bernoulli(0.7)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 1)
log_pdf(X, 1)
cdf(X, 0)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Draw a random sample from a Beta distribution
Description
Draw a random sample from a Beta distribution
Usage
## S3 method for class 'Beta'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
Values in [0, 1]
. 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
).
Examples
set.seed(27)
X <- Beta(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
mean(X)
variance(X)
skewness(X)
kurtosis(X)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Draw a random sample from a Binomial distribution
Description
Draw a random sample from a Binomial distribution
Usage
## S3 method for class 'Binomial'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
Integers containing values between 0
and x$size
.
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
).
Examples
set.seed(27)
X <- Binomial(10, 0.2)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2L)
log_pdf(X, 2L)
cdf(X, 4L)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Draw a random sample from a Categorical distribution
Description
Draw a random sample from a Categorical distribution
Usage
## S3 method for class 'Categorical'
random(x, n = 1L, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
A vector containing values from outcomes
of length n
.
Examples
set.seed(27)
X <- Categorical(1:3, p = c(0.4, 0.1, 0.5))
X
Y <- Categorical(LETTERS[1:4])
Y
random(X, 10)
random(Y, 10)
pdf(X, 1)
log_pdf(X, 1)
cdf(X, 1)
quantile(X, 0.5)
# cdfs are only defined for numeric sample spaces. this errors!
# cdf(Y, "a")
# same for quantiles. this also errors!
# quantile(Y, 0.7)
Draw a random sample from a Cauchy distribution
Description
Draw a random sample from a Cauchy distribution
Usage
## S3 method for class 'Cauchy'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- Cauchy(10, 0.2)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 2)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Draw a random sample from a chi square distribution
Description
Draw a random sample from a chi square distribution
Usage
## S3 method for class 'ChiSquare'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- ChiSquare(5)
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)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Draw a random sample from an Erlang distribution
Description
Draw a random sample from an Erlang distribution
Usage
## S3 method for class 'Erlang'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
An |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- Erlang(5, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Draw a random sample from an Exponential distribution
Description
Draw a random sample from an Exponential distribution
Usage
## S3 method for class 'Exponential'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
An |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- Exponential(5)
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)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Draw a random sample from an F distribution
Description
Draw a random sample from an F distribution
Usage
## S3 method for class 'FisherF'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- FisherF(5, 10, 0.2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Draw a random sample from a Frechet distribution
Description
Draw a random sample from a Frechet distribution
Usage
## S3 method for class 'Frechet'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- Frechet(0, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Draw a random sample from a Gamma distribution
Description
Draw a random sample from a Gamma distribution
Usage
## S3 method for class 'Gamma'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- Gamma(5, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Draw a random sample from a Geometric distribution
Description
Please see the documentation of Geometric()
for some properties
of the Geometric distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Usage
## S3 method for class 'Geometric'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
See Also
Other Geometric distribution:
cdf.Geometric()
,
pdf.Geometric()
,
quantile.Geometric()
Examples
set.seed(27)
X <- Geometric(0.3)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Draw a random sample from a GEV distribution
Description
Draw a random sample from a GEV distribution
Usage
## S3 method for class 'GEV'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- GEV(1, 2, 0.1)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Draw a random sample from a GP distribution
Description
Draw a random sample from a GP distribution
Usage
## S3 method for class 'GP'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- GP(0, 2, 0.1)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Draw a random sample from a Gumbel distribution
Description
Draw a random sample from a Gumbel distribution
Usage
## S3 method for class 'Gumbel'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- Gumbel(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Draw a random sample from a hurdle negative binomial distribution
Description
Draw a random sample from a hurdle negative binomial distribution
Usage
## S3 method for class 'HurdleNegativeBinomial'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
## set up a hurdle negative binomial distribution
X <- HurdleNegativeBinomial(mu = 2.5, theta = 1, pi = 0.75)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Draw a random sample from a hurdle Poisson distribution
Description
Draw a random sample from a hurdle Poisson distribution
Usage
## S3 method for class 'HurdlePoisson'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
## set up a hurdle Poisson distribution
X <- HurdlePoisson(lambda = 2.5, pi = 0.75)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Draw a random sample from a HyperGeometric distribution
Description
Please see the documentation of HyperGeometric()
for some properties
of the HyperGeometric distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Usage
## S3 method for class 'HyperGeometric'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
See Also
Other HyperGeometric distribution:
cdf.HyperGeometric()
,
pdf.HyperGeometric()
,
quantile.HyperGeometric()
Examples
set.seed(27)
X <- HyperGeometric(4, 5, 8)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Draw a random sample from a Logistic distribution
Description
Draw a random sample from a Logistic distribution
Usage
## S3 method for class 'Logistic'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
See Also
Other Logistic distribution:
cdf.Logistic()
,
pdf.Logistic()
,
quantile.Logistic()
Examples
set.seed(27)
X <- Logistic(2, 4)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Draw a random sample from a LogNormal distribution
Description
Draw a random sample from a LogNormal distribution
Usage
## S3 method for class 'LogNormal'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
See Also
Other LogNormal distribution:
cdf.LogNormal()
,
fit_mle.LogNormal()
,
pdf.LogNormal()
,
quantile.LogNormal()
Examples
set.seed(27)
X <- LogNormal(0.3, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Draw a random sample from a Multinomial distribution
Description
Draw a random sample from a Multinomial distribution
Usage
## S3 method for class 'Multinomial'
random(x, n = 1L, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
An integer vector of length n
.
See Also
Other Multinomial distribution:
pdf.Multinomial()
Examples
set.seed(27)
X <- Multinomial(size = 5, p = c(0.3, 0.4, 0.2, 0.1))
X
random(X, 10)
# pdf(X, 2)
# log_pdf(X, 2)
Draw a random sample from a negative binomial distribution
Description
Draw a random sample from a negative binomial distribution
Usage
## S3 method for class 'NegativeBinomial'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
See Also
Other NegativeBinomial distribution:
cdf.NegativeBinomial()
,
pdf.NegativeBinomial()
,
quantile.NegativeBinomial()
Examples
set.seed(27)
X <- NegativeBinomial(size = 5, p = 0.1)
X
random(X, 10)
pdf(X, 50)
log_pdf(X, 50)
cdf(X, 50)
quantile(X, 0.7)
## alternative parameterization of X
Y <- NegativeBinomial(mu = 45, size = 5)
Y
cdf(Y, 50)
quantile(Y, 0.7)
Draw a random sample from a Normal distribution
Description
Please see the documentation of Normal()
for some properties
of the Normal distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Usage
## S3 method for class 'Normal'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- Normal(5, 2)
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: calculating p-values for two-sided Z-test
# here the null hypothesis is H_0: mu = 3
# and we assume sigma = 2
# exactly the same as: Z <- Normal(0, 1)
Z <- Normal()
# data to test
x <- c(3, 7, 11, 0, 7, 0, 4, 5, 6, 2)
nx <- length(x)
# calculate the z-statistic
z_stat <- (mean(x) - 3) / (2 / sqrt(nx))
z_stat
# calculate the two-sided p-value
1 - cdf(Z, abs(z_stat)) + cdf(Z, -abs(z_stat))
# exactly equivalent to the above
2 * cdf(Z, -abs(z_stat))
# p-value for one-sided test
# H_0: mu <= 3 vs H_A: mu > 3
1 - cdf(Z, z_stat)
# p-value for one-sided test
# H_0: mu >= 3 vs H_A: mu < 3
cdf(Z, z_stat)
### example: calculating a 88 percent Z CI for a mean
# same `x` as before, still assume `sigma = 2`
# lower-bound
mean(x) - quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# upper-bound
mean(x) + quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# equivalent to
mean(x) + c(-1, 1) * quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
# also equivalent to
mean(x) + quantile(Z, 0.12 / 2) * 2 / sqrt(nx)
mean(x) + quantile(Z, 1 - 0.12 / 2) * 2 / sqrt(nx)
### generating random samples and plugging in ks.test()
set.seed(27)
# generate a random sample
ns <- random(Normal(3, 7), 26)
# test if sample is Normal(3, 7)
ks.test(ns, pnorm, mean = 3, sd = 7)
# test if sample is gamma(8, 3) using base R pgamma()
ks.test(ns, pgamma, shape = 8, rate = 3)
### MISC
# note that the cdf() and quantile() functions are inverses
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Draw a random sample from a Poisson distribution
Description
Draw a random sample from a Poisson distribution
Usage
## S3 method for class 'Poisson'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- Poisson(2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 7))
Draw a random sample from a PoissonBinomial distribution
Description
Draw a random sample from a PoissonBinomial distribution
Usage
## S3 method for class 'PoissonBinomial'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
Integers containing values between 0
and x$size
.
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
).
Examples
set.seed(27)
X <- PoissonBinomial(0.5, 0.3, 0.8)
X
mean(X)
variance(X)
skewness(X)
kurtosis(X)
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 2)
quantile(X, 0.8)
cdf(X, quantile(X, 0.8))
quantile(X, cdf(X, 2))
## equivalent definitions of four Poisson binomial distributions
## each summing up three Bernoulli probabilities
p <- cbind(
p1 = c(0.1, 0.2, 0.1, 0.2),
p2 = c(0.5, 0.5, 0.5, 0.5),
p3 = c(0.8, 0.7, 0.9, 0.8))
PoissonBinomial(p)
PoissonBinomial(p[, 1], p[, 2], p[, 3])
PoissonBinomial(p[, 1:2], p[, 3])
Draw a random sample from an RevWeibull distribution
Description
Draw a random sample from an RevWeibull distribution
Usage
## S3 method for class 'RevWeibull'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- RevWeibull(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Draw a random sample from a StudentsT distribution
Description
Please see the documentation of StudentsT()
for some properties
of the T distribution, as well as extensive examples
showing to how calculate p-values and confidence intervals.
Usage
## S3 method for class 'StudentsT'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
See Also
Other StudentsT distribution:
cdf.StudentsT()
,
pdf.StudentsT()
,
quantile.StudentsT()
Examples
set.seed(27)
X <- StudentsT(3)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
### example: calculating p-values for two-sided T-test
# here the null hypothesis is H_0: mu = 3
# data to test
x <- c(3, 7, 11, 0, 7, 0, 4, 5, 6, 2)
nx <- length(x)
# calculate the T-statistic
t_stat <- (mean(x) - 3) / (sd(x) / sqrt(nx))
t_stat
# null distribution of statistic depends on sample size!
T <- StudentsT(df = nx - 1)
# calculate the two-sided p-value
1 - cdf(T, abs(t_stat)) + cdf(T, -abs(t_stat))
# exactly equivalent to the above
2 * cdf(T, -abs(t_stat))
# p-value for one-sided test
# H_0: mu <= 3 vs H_A: mu > 3
1 - cdf(T, t_stat)
# p-value for one-sided test
# H_0: mu >= 3 vs H_A: mu < 3
cdf(T, t_stat)
### example: calculating a 88 percent T CI for a mean
# lower-bound
mean(x) - quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# upper-bound
mean(x) + quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# equivalent to
mean(x) + c(-1, 1) * quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# also equivalent to
mean(x) + quantile(T, 0.12 / 2) * sd(x) / sqrt(nx)
mean(x) + quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
Draw a random sample from a Tukey distribution
Description
Draw a random sample from a Tukey distribution
Usage
## S3 method for class 'Tukey'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
set.seed(27)
X <- Tukey(4L, 16L, 2L)
X
cdf(X, 4)
quantile(X, 0.7)
Draw a random sample from a continuous Uniform distribution
Description
Draw a random sample from a continuous Uniform distribution
Usage
## S3 method for class 'Uniform'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
Values in [a, b]
. 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
).
Examples
set.seed(27)
X <- Uniform(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Draw a random sample from a Weibull distribution
Description
Draw a random sample from a Weibull distribution
Usage
## S3 method for class 'Weibull'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
See Also
Other Weibull distribution:
cdf.Weibull()
,
pdf.Weibull()
,
quantile.Weibull()
Examples
set.seed(27)
X <- Weibull(0.3, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Draw a random sample from a zero-inflated negative binomial distribution
Description
Draw a random sample from a zero-inflated negative binomial distribution
Usage
## S3 method for class 'ZINegativeBinomial'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
## set up a zero-inflated negative binomial distribution
X <- ZINegativeBinomial(mu = 2.5, theta = 1, pi = 0.25)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Draw a random sample from a zero-inflated Poisson distribution
Description
Draw a random sample from a zero-inflated Poisson distribution
Usage
## S3 method for class 'ZIPoisson'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
## set up a zero-inflated Poisson distribution
X <- ZIPoisson(lambda = 2.5, pi = 0.25)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Draw a random sample from a zero-truncated negative binomial distribution
Description
Draw a random sample from a zero-truncated negative binomial distribution
Usage
## S3 method for class 'ZTNegativeBinomial'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
## set up a zero-truncated negative binomial distribution
X <- ZTNegativeBinomial(mu = 2.5, theta = 1)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Draw a random sample from a zero-truncated Poisson distribution
Description
Draw a random sample from a zero-truncated Poisson distribution
Usage
## S3 method for class 'ZTPoisson'
random(x, n = 1L, drop = TRUE, ...)
Arguments
x |
A |
n |
The number of samples to draw. Defaults to |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Unused. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
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
).
Examples
## set up a zero-truncated Poisson distribution
X <- ZTPoisson(lambda = 2.5)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Create a reversed Weibull distribution
Description
The reversed (or negated) Weibull distribution is a special case of the
\link{GEV}
distribution, obtained when the GEV shape parameter \xi
is negative. It may be referred to as a type III extreme value
distribution.
Usage
RevWeibull(location = 0, scale = 1, shape = 1)
Arguments
location |
The location (maximum) parameter |
scale |
The scale parameter |
shape |
The scale parameter |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a reversed Weibull random variable with
location parameter location
= m
, scale parameter scale
=
s
, and shape parameter shape
= \alpha
.
An RevWeibull(m, s, \alpha
) distribution is equivalent to a
\link{GEV}
(m - s, s / \alpha, -1 / \alpha
) distribution.
If X
has an RevWeibull(m, \lambda, k
) distribution then
m - X
has a \link{Weibull}
(k, \lambda
) distribution,
that is, a Weibull distribution with shape parameter k
and scale
parameter \lambda
.
Support: (-\infty, m)
.
Mean: m + s\Gamma(1 + 1/\alpha)
.
Median: m + s(\ln 2)^{1/\alpha}
.
Variance:
s^2 [\Gamma(1 + 2 / \alpha) - \Gamma(1 + 1 / \alpha)^2]
.
Probability density function (p.d.f):
f(x) = \alpha s ^ {-1} [-(x - m) / s] ^ {\alpha - 1}%
\exp\{-[-(x - m) / s] ^ {\alpha} \}
for x < m
. The p.d.f. is 0 for x \geq m
.
Cumulative distribution function (c.d.f):
F(x) = \exp\{-[-(x - m) / s] ^ {\alpha} \}
for x < m
. The c.d.f. is 1 for x \geq m
.
Value
A RevWeibull
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
StudentsT()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- RevWeibull(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Simulate responses from fitted model objects
Description
Default method for simulating new responses from any model object
with a prodist
method (for extracting a
probability distribution object).
Usage
## Default S3 method:
simulate(object, nsim = 1, seed = NULL, ...)
Arguments
object |
An object for which a |
nsim |
The number of response vectors to simulate. Should be a positive integer. Defaults to 1. |
seed |
An optional random seed that is to be set using |
... |
Arguments passed to |
Details
This default method simply combines two building blocks provided in this
package: (1) prodist
for extracting the probability
distribution from a fitted model object, (2) simulate.distribution
for simulating new observations from this distribution (internally calling
random
).
Thus, this enables simulation from any fitted model object that provides a
prodist
method. It waives the need to implement a dedicated
simulate
method for this model class.
Value
A data frame with an attribute "seed"
containing the
.Random.seed
from before the simulation.
Examples
## Poisson GLM for FIFA 2018 goals
data("FIFA2018", package = "distributions3")
m <- glm(goals ~ difference, data = FIFA2018, family = poisson)
## simulate new goals via glm method
set.seed(0)
g_glm <- simulate(m, n = 3)
## alternatively use the new default method
set.seed(0)
g_default <- simulate.default(m, n = 3)
## same results
all.equal(g_glm, g_default, check.attributes = FALSE)
Fill out area under the curve for a plotted PDF
Description
Fill out area under the curve for a plotted PDF
Usage
stat_auc(
mapping = NULL,
data = NULL,
geom = "auc",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
from = -Inf,
to = Inf,
annotate = FALSE,
digits = 3,
...
)
geom_auc(
mapping = NULL,
data = NULL,
stat = "auc",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
from = -Inf,
to = Inf,
annotate = FALSE,
digits = 3,
...
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
The geometric object to use to display the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
from |
Left end-point of interval |
to |
Right end-point of interval |
annotate |
Should P() be added in the upper left corner as an annotation? Works also with a colour character, e.g., "red". |
digits |
Number of digits shown in annotation |
... |
Other arguments passed on to
|
stat |
The statistical transformation to use on the data for this layer.
When using a
|
Examples
N1 <- Normal()
plot_pdf(N1) + geom_auc(to = -0.645)
plot_pdf(N1) + geom_auc(from = -0.645, to = 0.1, annotate = TRUE)
N2 <- Normal(0, c(1, 2))
plot_pdf(N2) + geom_auc(to = 0)
plot_pdf(N2) + geom_auc(from = -2, to = 2, annotate = TRUE)
Create a Student's T distribution
Description
The Student's T distribution is closely related to the Normal()
distribution, but has heavier tails. As \nu
increases to \infty
,
the Student's T converges to a Normal. The T distribution appears
repeatedly throughout classic frequentist hypothesis testing when
comparing group means.
Usage
StudentsT(df)
Arguments
df |
Degrees of freedom. Can be any positive number. Often
called |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a Students T random variable with
df
= \nu
.
Support: R
, the set of all real numbers
Mean: Undefined unless \nu \ge 2
, in which case the mean is
zero.
Variance:
\frac{\nu}{\nu - 2}
Undefined if \nu < 1
, infinite when 1 < \nu \le 2
.
Probability density function (p.d.f):
f(x) = \frac{\Gamma(\frac{\nu + 1}{2})}{\sqrt{\nu \pi} \Gamma(\frac{\nu}{2})} (1 + \frac{x^2}{\nu} )^{- \frac{\nu + 1}{2}}
Cumulative distribution function (c.d.f):
Nasty, omitted.
Moment generating function (m.g.f):
Undefined.
Value
A StudentsT
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
Tukey()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- StudentsT(3)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
### example: calculating p-values for two-sided T-test
# here the null hypothesis is H_0: mu = 3
# data to test
x <- c(3, 7, 11, 0, 7, 0, 4, 5, 6, 2)
nx <- length(x)
# calculate the T-statistic
t_stat <- (mean(x) - 3) / (sd(x) / sqrt(nx))
t_stat
# null distribution of statistic depends on sample size!
T <- StudentsT(df = nx - 1)
# calculate the two-sided p-value
1 - cdf(T, abs(t_stat)) + cdf(T, -abs(t_stat))
# exactly equivalent to the above
2 * cdf(T, -abs(t_stat))
# p-value for one-sided test
# H_0: mu <= 3 vs H_A: mu > 3
1 - cdf(T, t_stat)
# p-value for one-sided test
# H_0: mu >= 3 vs H_A: mu < 3
cdf(T, t_stat)
### example: calculating a 88 percent T CI for a mean
# lower-bound
mean(x) - quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# upper-bound
mean(x) + quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# equivalent to
mean(x) + c(-1, 1) * quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
# also equivalent to
mean(x) + quantile(T, 0.12 / 2) * sd(x) / sqrt(nx)
mean(x) + quantile(T, 1 - 0.12 / 2) * sd(x) / sqrt(nx)
Compute the sufficient statistics of a distribution from data
Description
Generic function for computing the sufficient statistics of a distribution based on empirical data.
Usage
suff_stat(d, x, ...)
Arguments
d |
An object. The package provides methods for distribution
objects such as those from |
x |
A vector of data to compute the likelihood. |
... |
Arguments passed to methods. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
a named list of sufficient statistics
Examples
X <- Normal()
suff_stat(X, c(-1, 0, 0, 0, 3))
Compute the sufficient statistics for a Bernoulli distribution from data
Description
Compute the sufficient statistics for a Bernoulli distribution from data
Usage
## S3 method for class 'Bernoulli'
suff_stat(d, x, ...)
Arguments
d |
A |
x |
A vector of zeroes and ones. |
... |
Unused. |
Value
A named list of the sufficient statistics of the Bernoulli distribution:
-
successes
: The number of successful trials (sum(x == 1)
) -
failures
: The number of failed trials (sum(x == 0)
).
Compute the sufficient statistics for the Binomial distribution from data
Description
Compute the sufficient statistics for the Binomial distribution from data
Usage
## S3 method for class 'Binomial'
suff_stat(d, x, ...)
Arguments
d |
A |
x |
A vector of zeroes and ones. |
... |
Unused. |
Value
A named list of the sufficient statistics of the Binomial distribution:
-
successes
: The total number of successful trials. -
experiments
: The number of experiments run. -
trials
: The number of trials run per experiment.
Compute the sufficient statistics of an Exponential distribution from data
Description
Compute the sufficient statistics of an Exponential distribution from data
Usage
## S3 method for class 'Exponential'
suff_stat(d, x, ...)
Arguments
d |
An |
x |
A vector of data. |
... |
Unused. |
Value
A named list of the sufficient statistics of the exponential distribution:
-
sum
: The sum of the observations. -
samples
: The number of observations.
Compute the sufficient statistics for a Gamma distribution from data
Description
-
sum
: The sum of the data. -
log_sum
: The log of the sum of the data. -
samples
: The number of samples in the data.
Usage
## S3 method for class 'Gamma'
suff_stat(d, x, ...)
Arguments
d |
A |
x |
A vector to fit the Gamma distribution to. |
... |
Unused. |
Value
a Gamma
object
Compute the sufficient statistics for the Geometric distribution from data
Description
Compute the sufficient statistics for the Geometric distribution from data
Usage
## S3 method for class 'Geometric'
suff_stat(d, x, ...)
Arguments
d |
A |
x |
A vector of zeroes and ones. |
... |
Unused. |
Value
A named list of the sufficient statistics of the Geometric distribution:
-
trials
: The total number of trials ran until the first success. -
experiments
: The number of experiments run.
Compute the sufficient statistics for a Log-normal distribution from data
Description
Compute the sufficient statistics for a Log-normal distribution from data
Usage
## S3 method for class 'LogNormal'
suff_stat(d, x, ...)
Arguments
d |
A |
x |
A vector of data. |
... |
Unused. |
Value
A named list of the sufficient statistics of the normal distribution:
-
mu
: The sample mean of the log of the data. -
sigma
: The sample standard deviation of the log of the data. -
samples
: The number of samples in the data.
Compute the sufficient statistics for a Normal distribution from data
Description
Compute the sufficient statistics for a Normal distribution from data
Usage
## S3 method for class 'Normal'
suff_stat(d, x, ...)
Arguments
d |
A |
x |
A vector of data. |
... |
Unused. |
Value
A named list of the sufficient statistics of the normal distribution:
-
mu
: The sample mean of the data. -
sigma
: The sample standard deviation of the data. -
samples
: The number of samples in the data.
Compute the sufficient statistics of an Poisson distribution from data
Description
Compute the sufficient statistics of an Poisson distribution from data
Usage
## S3 method for class 'Poisson'
suff_stat(d, x, ...)
Arguments
d |
An |
x |
A vector of data. |
... |
Unused. |
Value
A named list of the sufficient statistics of the Poisson distribution:
-
sum
: The sum of the data. -
samples
: The number of samples in the data.
Return the support of a distribution
Description
Generic function for computing the support interval (minimum and maximum) for a given probability distribution object.
Usage
support(d, drop = TRUE, ...)
Arguments
d |
An object. The package provides methods for distribution
objects such as those from |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Arguments passed to methods. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Value
A vector (or matrix) with two elements (or columns) indicating the range (minimum and maximum) of the support.
Examples
X <- Normal()
support(X)
Y <- Uniform(-1, 1:3)
support(Y)
Return the support of the Bernoulli distribution
Description
Return the support of the Bernoulli distribution
Usage
## S3 method for class 'Bernoulli'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the Beta distribution
Description
Return the support of the Beta distribution
Usage
## S3 method for class 'Beta'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the Binomial distribution
Description
Return the support of the Binomial distribution
Usage
## S3 method for class 'Binomial'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Shoul the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the Cauchy distribution
Description
Return the support of the Cauchy distribution
Usage
## S3 method for class 'Cauchy'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the ChiSquare distribution
Description
Return the support of the ChiSquare distribution
Usage
## S3 method for class 'ChiSquare'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the Erlang distribution
Description
Return the support of the Erlang distribution
Usage
## S3 method for class 'Erlang'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the Exponential distribution
Description
Return the support of the Exponential distribution
Usage
## S3 method for class 'Exponential'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the FisherF distribution
Description
Return the support of the FisherF distribution
Usage
## S3 method for class 'FisherF'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the Frechet distribution
Description
Return the support of the Frechet distribution
Usage
## S3 method for class 'Frechet'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
In case of a single distribution object, a numeric vector of length 2
with the minimum and maximum value of the support (if drop = TRUE
, default)
or a matrix
with 2 columns. In case of a vectorized distribution object, a
matrix with 2 columns containing all minima and maxima.
Return the support of the Gamma distribution
Description
Return the support of the Gamma distribution
Usage
## S3 method for class 'Gamma'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the Geometric distribution
Description
Return the support of the Geometric distribution
Usage
## S3 method for class 'Geometric'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of a GEV distribution
Description
Return the support of a GEV distribution
Usage
## S3 method for class 'GEV'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
In case of a single distribution object, a numeric vector of length 2
with the minimum and maximum value of the support (if drop = TRUE
, default)
or a matrix
with 2 columns. In case of a vectorized distribution object, a
matrix with 2 columns containing all minima and maxima.
Return the support of the GP distribution
Description
Return the support of the GP distribution
Usage
## S3 method for class 'GP'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
In case of a single distribution object, a numeric vector of length 2
with the minimum and maximum value of the support (if drop = TRUE
, default)
or a matrix
with 2 columns. In case of a vectorized distribution object, a
matrix with 2 columns containing all minima and maxima.
Return the support of the Gumbel distribution
Description
Return the support of the Gumbel distribution
Usage
## S3 method for class 'Gumbel'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
In case of a single distribution object, a numeric vector of length 2
with the minimum and maximum value of the support (if drop = TRUE
, default)
or a matrix
with 2 columns. In case of a vectorized distribution object, a
matrix with 2 columns containing all minima and maxima.
Return the support of the hurdle negative binomial distribution
Description
Return the support of the hurdle negative binomial distribution
Usage
## S3 method for class 'HurdleNegativeBinomial'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the hurdle Poisson distribution
Description
Return the support of the hurdle Poisson distribution
Usage
## S3 method for class 'HurdlePoisson'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the HyperGeometric distribution
Description
Return the support of the HyperGeometric distribution
Usage
## S3 method for class 'HyperGeometric'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the Logistic distribution
Description
Return the support of the Logistic distribution
Usage
## S3 method for class 'Logistic'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the LogNormal distribution
Description
Return the support of the LogNormal distribution
Usage
## S3 method for class 'LogNormal'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the NegativeBinomial distribution
Description
Return the support of the NegativeBinomial distribution
Usage
## S3 method for class 'NegativeBinomial'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the Normal distribution
Description
Return the support of the Normal distribution
Usage
## S3 method for class 'Normal'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
In case of a single distribution object, a numeric vector of length 2
with the minimum and maximum value of the support (if drop = TRUE
, default)
or a matrix
with 2 columns. In case of a vectorized distribution object, a
matrix with 2 columns containing all minima and maxima.
Return the support of the Poisson distribution
Description
Return the support of the Poisson distribution
Usage
## S3 method for class 'Poisson'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the PoissonBinomial distribution
Description
Return the support of the PoissonBinomial distribution
Usage
## S3 method for class 'PoissonBinomial'
support(d, drop = TRUE, ...)
Arguments
d |
A |
drop |
logical. Shoul the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the RevWeibull distribution
Description
Return the support of the RevWeibull distribution
Usage
## S3 method for class 'RevWeibull'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the StudentsT distribution
Description
Return the support of the StudentsT distribution
Usage
## S3 method for class 'StudentsT'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the Tukey distribution
Description
Return the support of the Tukey distribution
Usage
## S3 method for class 'Tukey'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the Uniform distribution
Description
Return the support of the Uniform distribution
Usage
## S3 method for class 'Uniform'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the Weibull distribution
Description
Return the support of the Weibull distribution
Usage
## S3 method for class 'Weibull'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the zero-inflated negative binomial distribution
Description
Return the support of the zero-inflated negative binomial distribution
Usage
## S3 method for class 'ZINegativeBinomial'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the zero-inflated Poisson distribution
Description
Return the support of the zero-inflated Poisson distribution
Usage
## S3 method for class 'ZIPoisson'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the zero-truncated negative binomial distribution
Description
Return the support of the zero-truncated negative binomial distribution
Usage
## S3 method for class 'ZTNegativeBinomial'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Return the support of the zero-truncated Poisson distribution
Description
Return the support of the zero-truncated Poisson distribution
Usage
## S3 method for class 'ZTPoisson'
support(d, drop = TRUE, ...)
Arguments
d |
An |
drop |
logical. Should the result be simplified to a vector if possible? |
... |
Currently not used. |
Value
A vector of length 2 with the minimum and maximum value of the support.
Create a Tukey distribution
Description
Tukey's studentized range distribution, used for Tukey's honestly significant differences test in ANOVA.
Usage
Tukey(nmeans, df, nranges)
Arguments
nmeans |
Sample size for each range. |
df |
Degrees of freedom. |
nranges |
Number of groups being compared. |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
Support: R^+
, the set of positive real numbers.
Other properties of Tukey's Studentized Range Distribution are omitted, largely because the distribution is not fun to work with.
Value
A Tukey
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Uniform()
,
Weibull()
Examples
set.seed(27)
X <- Tukey(4L, 16L, 2L)
X
cdf(X, 4)
quantile(X, 0.7)
Create a Continuous Uniform distribution
Description
A distribution with constant density on an interval. The
continuous analogue to the Categorical()
distribution.
Usage
Uniform(a = 0, b = 1)
Arguments
a |
The a parameter. |
b |
The a parameter. |
Value
A Uniform
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Weibull()
Examples
set.seed(27)
X <- Uniform(1, 2)
X
random(X, 10)
pdf(X, 0.7)
log_pdf(X, 0.7)
cdf(X, 0.7)
quantile(X, 0.7)
cdf(X, quantile(X, 0.7))
quantile(X, cdf(X, 0.7))
Compute the moments of a probability distribution
Description
Generic functions for computing moments (variance, skewness, excess kurtosis) from probability distributions.
Usage
variance(x, ...)
skewness(x, ...)
kurtosis(x, ...)
Arguments
x |
An object. The package provides methods for distribution
objects such as those from |
... |
Arguments passed to methods. Unevaluated arguments will generate a warning to catch mispellings or other possible errors. |
Details
The functions variance
, skewness
, and kurtosis
are new
generic functions for computing moments of probability distributions such as
those provided in this package. Additionally, the probability distributions
from distributions3 all have methods for the mean
generic. Moreover, quantiles can be computed with methods for
quantile
. For examples illustrating the usage with
probability distribution objects, see the manual pages of the respective
distributions, e.g., Normal
or Binomial
etc.
Value
Numeric vector with the values of the moments.
See Also
Methods for including distributions as vctrs in tibbles
Description
Methods for vec_proxy
and vec_restore
from vctrs in order to include distribution
objects in
tibble
objects.
Usage
vec_proxy.distribution(x, ...)
vec_restore.distribution(x, to, ...)
Arguments
x , to |
Objects inheriting from |
... |
Currently not used. |
Details
The methods for vec_proxy
and
vec_restore
from vctrs are needed so that
distribution
objects can be included as a vector column in
(and extracted from) tibble
data frames.
vec_proxy
simply adds the class data.frame
which is the
actual underlying data structure used by distribution
objects.
This way the number of rows etc. can be correctly determined. Conversely,
vec_restore
strips off the additional data.frame
class and
restores the original distribution
classes. Users typically do not
need to call vec_proxy
and vec_restore
directly.
Value
The vec_proxy
method returns a distribution
object which
additionally inherits of data.frame
while the vec_restore
method
restores the original distribution
classes.
Examples
## Poisson GLM for FIFA 2018 goals data
data("FIFA2018", package = "distributions3")
m <- glm(goals ~ difference, data = FIFA2018, family = poisson)
## Predict fitted Poisson distributions for teams with ability differences
## of -1, 0, 1 (out-of-sample) using the new data as a data.frame
nd <- data.frame(difference = -1:1)
nd$dist <- prodist(m, newdata = nd)
nd
## Do the same using the new data as a tibble
library("tibble")
nt <- tibble(difference = -1:1)
nt$dist <- prodist(m, newdata = nt)
nt
Create a Weibull distribution
Description
Generalization of the gamma distribution. Often used in survival and time-to-event analyses.
Usage
Weibull(shape, scale)
Arguments
shape |
The shape parameter |
scale |
The scale parameter |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail and much greater clarity.
In the following, let X
be a Weibull random variable with
success probability p
= p
.
Support: R^+
and zero.
Mean: \lambda \Gamma(1+1/k)
, where \Gamma
is
the gamma function.
Variance: \lambda [ \Gamma (1 + \frac{2}{k} ) - (\Gamma(1+ \frac{1}{k}))^2 ]
Probability density function (p.d.f):
f(x) = \frac{k}{\lambda}(\frac{x}{\lambda})^{k-1}e^{-(x/\lambda)^k}, x \ge 0
Cumulative distribution function (c.d.f):
F(x) = 1 - e^{-(x/\lambda)^k}, x \ge 0
Moment generating function (m.g.f):
\sum_{n=0}^\infty \frac{t^n\lambda^n}{n!} \Gamma(1+n/k), k \ge 1
Value
A Weibull
object.
See Also
Other continuous distributions:
Beta()
,
Cauchy()
,
ChiSquare()
,
Erlang()
,
Exponential()
,
FisherF()
,
Frechet()
,
GEV()
,
GP()
,
Gamma()
,
Gumbel()
,
LogNormal()
,
Logistic()
,
Normal()
,
RevWeibull()
,
StudentsT()
,
Tukey()
,
Uniform()
Examples
set.seed(27)
X <- Weibull(0.3, 2)
X
random(X, 10)
pdf(X, 2)
log_pdf(X, 2)
cdf(X, 4)
quantile(X, 0.7)
Create a zero-inflated negative binomial distribution
Description
Zero-inflated negative binomial distributions are frequently used to model counts with overdispersion and many zero observations.
Usage
ZINegativeBinomial(mu, theta, pi)
Arguments
mu |
Location parameter of the negative binomial component of the distribution. Can be any positive number. |
theta |
Overdispersion parameter of the negative binomial component of the distribution. Can be any positive number. |
pi |
Zero-inflation probability, can be any value in |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail.
In the following, let X
be a zero-inflated negative binomial random variable with parameters
mu
= \mu
and theta
= \theta
.
Support: \{0, 1, 2, 3, ...\}
Mean: (1 - \pi) \cdot \mu
Variance: (1 - \pi) \cdot \mu \cdot (1 + (\pi + 1/\theta) \cdot \mu)
Probability mass function (p.m.f.):
P(X = k) = \pi \cdot I_{0}(k) + (1 - \pi) \cdot f(k; \mu, \theta)
where I_{0}(k)
is the indicator function for zero and
f(k; \mu, \theta)
is the p.m.f. of the NegativeBinomial
distribution.
Cumulative distribution function (c.d.f.):
P(X \le k) = \pi + (1 - \pi) \cdot F(k; \mu, \theta)
where F(k; \mu, \theta)
is the c.d.f. of the NegativeBinomial
distribution.
Moment generating function (m.g.f.):
Omitted for now.
Value
A ZINegativeBinomial
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Categorical()
,
Geometric()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
HyperGeometric()
,
Multinomial()
,
NegativeBinomial()
,
Poisson()
,
PoissonBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
## set up a zero-inflated negative binomial distribution
X <- ZINegativeBinomial(mu = 2.5, theta = 1, pi = 0.25)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Create a zero-inflated Poisson distribution
Description
Zero-inflated Poisson distributions are frequently used to model counts with many zero observations.
Usage
ZIPoisson(lambda, pi)
Arguments
lambda |
Parameter of the Poisson component of the distribution. Can be any positive number. |
pi |
Zero-inflation probability, can be any value in |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail.
In the following, let X
be a zero-inflated Poisson random variable with parameter
lambda
= \lambda
.
Support: \{0, 1, 2, 3, ...\}
Mean: (1 - \pi) \cdot \lambda
Variance: (1 - \pi) \cdot \lambda \cdot (1 + \pi \cdot \lambda)
Probability mass function (p.m.f.):
P(X = k) = \pi \cdot I_{0}(k) + (1 - \pi) \cdot f(k; \lambda)
where I_{0}(k)
is the indicator function for zero and
f(k; \lambda)
is the p.m.f. of the Poisson
distribution.
Cumulative distribution function (c.d.f.):
P(X \le k) = \pi + (1 - \pi) \cdot F(k; \lambda)
where F(k; \lambda)
is the c.d.f. of the Poisson
distribution.
Moment generating function (m.g.f.):
E(e^{tX}) = \pi + (1 - \pi) \cdot e^{\lambda (e^t - 1)}
Value
A ZIPoisson
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Categorical()
,
Geometric()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
HyperGeometric()
,
Multinomial()
,
NegativeBinomial()
,
Poisson()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZTNegativeBinomial()
,
ZTPoisson()
Examples
## set up a zero-inflated Poisson distribution
X <- ZIPoisson(lambda = 2.5, pi = 0.25)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Create a zero-truncated negative binomial distribution
Description
Zero-truncated negative binomial distributions are frequently used to model counts where zero observations cannot occur or have been excluded.
Usage
ZTNegativeBinomial(mu, theta)
Arguments
mu |
Location parameter of the negative binomial component of the distribution. Can be any positive number. |
theta |
Overdispersion parameter of the negative binomial component of the distribution. Can be any positive number. |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail.
In the following, let X
be a zero-truncated negative binomial random variable with parameter
mu
= \mu
.
Support: \{1, 2, 3, ...\}
Mean:
\mu \cdot \frac{1}{1 - F(0; \mu, \theta)}
where F(k; \mu, \theta)
is the c.d.f. of the NegativeBinomial
distribution.
Variance: m \cdot (\mu + 1 - m)
, where m
is the mean above.
Probability mass function (p.m.f.):
P(X = k) = \frac{f(k; \mu, \theta)}{1 - F(0; \mu, \theta)}
where f(k; \mu, \theta)
is the p.m.f. of the NegativeBinomial
distribution.
Cumulative distribution function (c.d.f.):
P(X = k) = \frac{F(k; \mu, \theta)}{1 - F(0; \mu, \theta)}
Moment generating function (m.g.f.):
Omitted for now.
Value
A ZTNegativeBinomial
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Categorical()
,
Geometric()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
HyperGeometric()
,
Multinomial()
,
NegativeBinomial()
,
Poisson()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTPoisson()
Examples
## set up a zero-truncated negative binomial distribution
X <- ZTNegativeBinomial(mu = 2.5, theta = 1)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)
Create a zero-truncated Poisson distribution
Description
Zero-truncated Poisson distributions are frequently used to model counts where zero observations cannot occur or have been excluded.
Usage
ZTPoisson(lambda)
Arguments
lambda |
Parameter of the underlying untruncated Poisson distribution. Can be any positive number. |
Details
We recommend reading this documentation on https://alexpghayes.github.io/distributions3/, where the math will render with additional detail.
In the following, let X
be a zero-truncated Poisson random variable with parameter
lambda
= \lambda
.
Support: \{1, 2, 3, ...\}
Mean:
\lambda \cdot \frac{1}{1 - e^{-\lambda}}
Variance: m \cdot (\lambda + 1 - m)
, where m
is the mean above.
Probability mass function (p.m.f.):
P(X = k) = \frac{f(k; \lambda)}{1 - f(0; \lambda)}
where f(k; \lambda)
is the p.m.f. of the Poisson
distribution.
Cumulative distribution function (c.d.f.):
P(X = k) = \frac{F(k; \lambda)}{1 - F(0; \lambda)}
where F(k; \lambda)
is the c.d.f. of the Poisson
distribution.
Moment generating function (m.g.f.):
E(e^{tX}) = \frac{1}{1 - e^{-\lambda}} \cdot e^{\lambda (e^t - 1)}
Value
A ZTPoisson
object.
See Also
Other discrete distributions:
Bernoulli()
,
Binomial()
,
Categorical()
,
Geometric()
,
HurdleNegativeBinomial()
,
HurdlePoisson()
,
HyperGeometric()
,
Multinomial()
,
NegativeBinomial()
,
Poisson()
,
PoissonBinomial()
,
ZINegativeBinomial()
,
ZIPoisson()
,
ZTNegativeBinomial()
Examples
## set up a zero-truncated Poisson distribution
X <- ZTPoisson(lambda = 2.5)
X
## standard functions
pdf(X, 0:8)
cdf(X, 0:8)
quantile(X, seq(0, 1, by = 0.25))
## cdf() and quantile() are inverses for each other
quantile(X, cdf(X, 3))
## density visualization
plot(0:8, pdf(X, 0:8), type = "h", lwd = 2)
## corresponding sample with histogram of empirical frequencies
set.seed(0)
x <- random(X, 500)
hist(x, breaks = -1:max(x) + 0.5)