A set of functions that calculate the theoretical moments (expectation, variance, skewness, excess kurtosis) and other important parametric functions (median, mode, entropy, Fisher information) of a distribution.
Usage
moments(x)
mean(x, ...)
median(x, na.rm = FALSE, ...)
mode(x)
var(x, y = NULL, na.rm = FALSE, use)
sd(x, na.rm = FALSE)
skew(x, ...)
kurt(x, ...)
entro(x, ...)
finf(x, ...)
Value
Numeric, either vector or matrix depending on the moment and the
distribution. The moments()
function returns a list with all the available
methods.
Details
Given a distribution, these functions calculate the theoretical moments and
other parametric quantities of interest. Some distribution-function
combinations are not available; for example, the sd()
function is
available only for univariate distributions.
The moments()
function automatically finds the available methods for a
given distribution and results all of the results in a list.
Technical Note:
The argument of the moment functions does not follow the naming convention of
the package, i.e. the Distribution
object is names x
rather than distr
.
This is due to the fact that most of the generics are already defined in the
stats
package (mean
, median
, mode
, var
, sd
), therefore the first
argument was already named x
and could not change.
Functions
median()
: Medianmode()
: Modevar()
: Variancesd()
: Standard Deviationskew()
: Skewnesskurt()
: Kurtosisentro()
: Entropyfinf()
: Fisher Information (numeric or matrix)
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] 2.1866737 1.8607350 0.4562757 0.9761423 1.7081981 2.2178898 2.2024303
#> [8] 1.3233835 0.7386163 0.8441121 1.1062408 2.0541726 2.2961593 1.1132598
#> [15] 1.8216994 1.9852110 0.9665665 2.1362498 0.8801992 1.2630775 1.8903052
#> [22] 1.4761686 1.6241568 2.0172665 1.8199744 0.5491640 2.1152278 0.6348995
#> [29] 2.3044960 1.7998164 1.6363645 1.4808840 2.2620830 2.2039299 2.1933650
#> [36] 2.2940351 1.9724819 2.2828936 1.5994340 0.8200878 2.2275840 2.2623429
#> [43] 1.1391907 2.3043038 1.7004710 0.7990487 1.6704638 1.4793582 1.9839090
#> [50] 2.3005360 2.2111064 2.2846296 2.2517941 1.9750353 1.9328287 1.8069511
#> [57] 2.0779686 1.6015518 1.2822302 2.1692320 1.9953819 0.7185783 1.6557242
#> [64] 2.1560955 2.3043668 0.1997865 1.8153208 1.3722137 1.5286853 2.2872361
#> [71] 0.5299206 1.2593337 1.2536151 1.4549079 0.2337613 1.2417615 2.2050286
#> [78] 2.1147822 1.7532197 1.6525623 0.9795870 2.2934107 0.5808408 2.2015315
#> [85] 1.4792728 2.2918547 1.6580528 1.8619862 2.1378689 2.0677331 2.2525593
#> [92] 1.2954350 1.8349490 2.3013482 1.4205110 2.2828708 2.1617535 2.2931346
#> [99] 0.7713010 2.0074225
# ------------------
# 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] 43.31318
llbeta(x, a, b)
#> [1] 43.31318
ebeta(x, type = "mle")
#> $shape1
#> [1] 2.89074
#>
#> $shape2
#> [1] 5.044725
#>
ebeta(x, type = "me")
#> $shape1
#> [1] 2.811521
#>
#> $shape2
#> [1] 4.904165
#>
ebeta(x, type = "same")
#> $shape1
#> [1] 2.862696
#>
#> $shape2
#> [1] 4.99343
#>
mle(D, x)
#> $shape1
#> [1] 2.89074
#>
#> $shape2
#> [1] 5.044725
#>
me(D, x)
#> $shape1
#> [1] 2.811521
#>
#> $shape2
#> [1] 4.904165
#>
same(D, x)
#> $shape1
#> [1] 2.862696
#>
#> $shape2
#> [1] 4.99343
#>
e(D, x, type = "mle")
#> $shape1
#> [1] 2.89074
#>
#> $shape2
#> [1] 5.044725
#>
mle("beta", x) # the distr argument can be a character
#> $shape1
#> [1] 2.89074
#>
#> $shape2
#> [1] 5.044725
#>
# ------------------
# 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