library(truncnorm)
normal <- rtruncnorm (100, a=0,b=20,10,1)
Poisson <- rpois(100,10)
exponential <- rexp(100,1)
hist(normal, col="dodgerblue")hist(Poisson, col="cornflowerblue")hist(exponential, col="steelblue")There are three methods of calculating the mean. There is the usual arithmetic mean calculated by summing the values and dividing by the number of measurements. There are also two more variants, the geometric and the harmonic means which need to be used in some specific circumstances.
Firstly I am going to create some data which is normally, Poisson and exponentially distributed. I am going to create data sets with 100 samples in each. The normal is actually the truncated normal as I need positive only values for the geometric mean. Plotting the histograms show that they have different shapes.
library(truncnorm)
normal <- rtruncnorm (100, a=0,b=20,10,1)
Poisson <- rpois(100,10)
exponential <- rexp(100,1)
hist(normal, col="dodgerblue")hist(Poisson, col="cornflowerblue")hist(exponential, col="steelblue")These can also be summarised.
summary(normal) Min. 1st Qu. Median Mean 3rd Qu. Max.
7.691 9.357 9.831 9.933 10.433 12.656
summary(Poisson) Min. 1st Qu. Median Mean 3rd Qu. Max.
2.00 7.75 10.00 10.13 12.00 18.00
summary(exponential) Min. 1st Qu. Median Mean 3rd Qu. Max.
0.005562 0.222324 0.814185 1.084043 1.547682 4.356194
I can also calculate the means for each of the sets of data.
mean(normal)[1] 9.932609
mean(Poisson)[1] 10.13
mean(exponential)[1] 1.084043
As well as the usual form of the mean we can also calculate the geometric and harmonic means.
The geometric mean multiples all of the values together and then takes the nth root of the product.
\(\huge{G}= \left( \huge\prod_{i}^{n}{x_{i}} \right)^{\left( \dfrac{1}{n} \right)}\)
This formula should be used when you are working with growth of a population or an organism.
library(DescTools)
Gmean(normal)[1] 9.888754
Gmean(Poisson)[1] 9.56872
Gmean(exponential)[1] 0.5832726
The harmonic mean is n divided by the sum of the reciprocal of each of the values.
\(\huge{H}=\dfrac{n}{\huge\sum_{i}^{n}\dfrac{1}{x_{i}}}\)
This formula should be used when you are finding the mean of ratios such as speed which is distance/time or price-earnings ratios. It is also used in population genetics to average census population sizes in order to deal with the effects of bottlenecks when there will be a limited gene pool.
Hmean(normal)[1] 9.845253
Hmean(Poisson)[1] 8.875932
Hmean(exponential)[1] 0.1737709