A collection of S4 classes that provide a flexible and structured way to work with probability distributions.
Usage
# S4 method for class 'Distribution,missing'
d(distr, x, ...)
# S4 method for class 'Distribution,missing'
p(distr, q, ...)
# S4 method for class 'Distribution,missing'
qn(distr, p, ...)
# S4 method for class 'Distribution,missing'
r(distr, n, ...)
# S4 method for class 'Distribution,missing'
ll(distr, x, ...)
# S4 method for class 'Distribution,missing'
mle(distr, x, ...)
# S4 method for class 'Distribution,missing'
me(distr, x, ...)
# S4 method for class 'Distribution,missing'
same(distr, x, ...)
Value
When supplied with one argument, the d()
, p()
, q()
, r()
ll()
functions return the density, cumulative probability, quantile, random sample
generator, and log-likelihood functions, respectively.
Details
When x
, q
, p
, or n
are missing, the methods return a function that
takes as input the missing argument, allowing the user to work with the
function object itself. See examples.
Examples
# -----------------------------------------------------
# Beta Distribution Example
# -----------------------------------------------------
# Create the distribution
a <- 3
b <- 5
D <- Beta(a, b)
# ------------------
# dpqr Functions
# ------------------
d(D, c(0.3, 0.8, 0.5)) # density function
#> [1] 2.268945 0.107520 1.640625
p(D, c(0.3, 0.8, 0.5)) # distribution function
#> [1] 0.3529305 0.9953280 0.7734375
qn(D, c(0.4, 0.8)) # inverse distribution function
#> [1] 0.3205858 0.5167578
x <- r(D, 100) # random generator function
# alternative way to use the function
df <- d(D) ; df(x) # df is a function itself
#> [1] 1.832306349 0.664783925 2.197836117 2.218911907 1.969146016 2.100071416
#> [7] 2.002729249 1.567498784 1.006987090 1.220802760 0.839953936 2.081429386
#> [13] 0.357641569 2.186707808 2.064837424 2.168532431 2.271240756 1.414795380
#> [19] 2.301749794 2.016197381 2.158672532 1.281156142 2.243814645 0.001106043
#> [25] 1.616132177 2.271547070 2.065361832 2.304458713 1.900562965 2.211118210
#> [31] 0.518508091 1.138651488 0.618925261 1.992796136 1.509465489 1.416017519
#> [37] 0.900226269 0.719170924 2.262744403 2.100773856 1.050960272 2.303615168
#> [43] 2.281784759 2.139293477 0.737308701 1.994982937 2.291699505 2.235898890
#> [49] 1.429123553 1.767726487 0.790154504 1.974435112 1.715618581 1.782390720
#> [55] 2.015090745 2.201406892 1.807148295 0.123923150 1.986468407 2.249127528
#> [61] 1.982668633 1.025986840 1.519965865 2.033383298 2.297135048 2.299953386
#> [67] 1.704568454 2.277863263 2.267235810 2.082505612 1.447012812 0.790179893
#> [73] 1.985726134 2.195537468 1.007736926 1.720512128 2.293591188 1.789591679
#> [79] 1.408189077 2.279112906 1.551205012 1.306998689 1.655381663 2.103332862
#> [85] 2.221211403 1.652859132 1.240611670 1.558916553 2.091652743 1.695053107
#> [91] 1.609270564 1.250288491 2.283137806 1.372333862 1.512817539 2.303696296
#> [97] 2.218488985 2.237738410 1.035968651 0.653927385
# ------------------
# Moments
# ------------------
mean(D) # Expectation
#> [1] 0.375
var(D) # Variance
#> [1] 0.02604167
sd(D) # Standard Deviation
#> [1] 0.1613743
skew(D) # Skewness
#> [1] 0.3098387
kurt(D) # Excess Kurtosis
#> [1] 0.04
entro(D) # Entropy
#> [1] -0.4301508
finf(D) # Fisher Information Matrix
#> shape1 shape2
#> shape1 0.2617971 -0.13313701
#> shape2 -0.1331370 0.08818594
# List of all available moments
mom <- moments(D)
mom$mean # expectation
#> [1] 0.375
# ------------------
# Point Estimation
# ------------------
ll(D, x)
#> [1] 39.04561
llbeta(x, a, b)
#> [1] 39.04561
ebeta(x, type = "mle")
#> $shape1
#> [1] 2.865864
#>
#> $shape2
#> [1] 4.575531
#>
ebeta(x, type = "me")
#> $shape1
#> [1] 2.886968
#>
#> $shape2
#> [1] 4.680023
#>
ebeta(x, type = "same")
#> $shape1
#> [1] 2.867425
#>
#> $shape2
#> [1] 4.648342
#>
mle(D, x)
#> $shape1
#> [1] 2.865864
#>
#> $shape2
#> [1] 4.575531
#>
me(D, x)
#> $shape1
#> [1] 2.886968
#>
#> $shape2
#> [1] 4.680023
#>
same(D, x)
#> $shape1
#> [1] 2.867425
#>
#> $shape2
#> [1] 4.648342
#>
e(D, x, type = "mle")
#> $shape1
#> [1] 2.865864
#>
#> $shape2
#> [1] 4.575531
#>
mle("beta", x) # the distr argument can be a character
#> $shape1
#> [1] 2.865864
#>
#> $shape2
#> [1] 4.575531
#>
# ------------------
# Estimator Variance
# ------------------
vbeta(a, b, type = "mle")
#> shape1 shape2
#> shape1 16.44844 24.83272
#> shape2 24.83272 48.83039
vbeta(a, b, type = "me")
#> shape1 shape2
#> shape1 17.64848 26.56970
#> shape2 26.56970 51.39394
vbeta(a, b, type = "same")
#> shape1 shape2
#> shape1 16.57719 24.96198
#> shape2 24.96198 49.01071
avar_mle(D)
#> shape1 shape2
#> shape1 16.44844 24.83272
#> shape2 24.83272 48.83039
avar_me(D)
#> shape1 shape2
#> shape1 17.64848 26.56970
#> shape2 26.56970 51.39394
avar_same(D)
#> shape1 shape2
#> shape1 16.57719 24.96198
#> shape2 24.96198 49.01071
v(D, type = "mle")
#> shape1 shape2
#> shape1 16.44844 24.83272
#> shape2 24.83272 48.83039