library(distr6) set.seed(42) knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
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.
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' N$cdf(1:2) # Distribution function evaluated at points '1' and '2' N$quantile(0.975) # Quantile function evaluated at 0.975 N$rand(5) # 5 samples from the Normal distribution
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()
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() N$variance() N$skewness() N$kurtosis(excess = FALSE) N$cf(2) N$mgf(2)
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!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.