In the previous tutorial we constructed a Normal distribution and accessed and updated its parameters for a variety of parameterisations. In this tutorial we cover how to access mathematical and statistical methods of the Normal distribution including the dnorm/pnorm/qnorm/rnorm equivalents in distr6.

d/p/q/r

The advantage of distr6 over R stats is that once a distribution is constructed, it’s very easy to find properties and results from the distribution by changing very little. For simple distributions like the Normal distribution, this may not seem like a big difference, but for more complicated ones with multiple parameters, you’ll find yourself saving a lot of time!

Once again we start with constructing the Standard Normal distribution

N <- Normal$new()

For simplicity, we refer to both the probability density functions of continuous distributions and probability mass functions of discrete distributions, as the “pdf” function. This is in line with R stats using “d” for “density”. The other statistical methods from R stats are referred to as “cdf”, “quantile” and “rand”, the same as in R stats:

N$pdf(1:2) # Density evaluated at points '1' and '2'
#> [1] 0.24197072 0.05399097
N$cdf(1:2) # Distribution function evaluated at points '1' and '2'
#> [1] 0.8413447 0.9772499
N$quantile(0.975) # Quantile function evaluated at 0.975
#> [1] 1.959964
N$rand(5) # 5 samples from the Normal distribution
#> [1]  1.3709584 -0.5646982  0.3631284  0.6328626  0.4042683

Other Mathematical and Statistical Methods

We have seen in the first tutorial how the summary method can be used to view quick statistics about a probability distribution, i.e.

N$summary()
#> Normal Probability Distribution. 
#> Parameterised with:
#> 
#>        Id Support  Value            Tags
#>    <char>  <char> <list>          <list>
#> 1:   mean       ℝ      0        required
#> 2:   prec      ℝ+        linked,required
#> 3:     sd      ℝ+        linked,required
#> 4:    var      ℝ+      1 linked,required
#> 
#> 
#> Quick Statistics
#>  Mean:       0
#>  Variance:   1
#>  Skewness:   0
#>  Ex. Kurtosis:   0
#> 
#> Support: ℝ   Scientific Type: ℝ 
#> 
#> Traits:      continuous; univariate
#> Properties:  symmetric; mesokurtic; no skew

But all these statistics can be accessed individually as well. To see the full list of available methods view the ‘Statistical Methods’ section of the distribution help page, ?Normal. All probability distributions have the same methods available if possible, i.e. If there is an analytic expression for a statistical result, then we provide it! Below are just a few examples

N$mean()
#> [1] 0
N$variance()
#> [1] 1
N$skewness()
#> [1] 0
N$kurtosis(excess = FALSE)
#> [1] 3
N$cf(2)
#> [1] 0.1353353+0i
N$mgf(2)
#> [1] 7.389056

Summary

In this tutorial we looked at using the d/p/q/r functions in distr6 and accessing other statistical results. In the next tutorial we take a quick look at distribution properties and traits, whilst trying not to get into too big a discussion about object-oriented programming!