This article explains exactly how to compute the geometric mean in the R programming language.
You are watching: Geometric mean in r
This short article contains two examples consisting of reproducible R codes.
So without additional ado, let’s relocate on come the examples!
In the examples of this tutorial, I’ll usage the adhering to numeric vector together exemplifying data:
exp(mean(log(x))) # Compute geometric mean manually# 4.209156
As you deserve to see, the geometric typical of our instance data is 4.209156.
This seems to be too complicated to carry out it on a constant basis? No problem! The following instance makes the computation of the geometric mean also easier…
Example 2: geometric.mean duty of psych R Package
The psych package is an R add-on parcel which, besides plenty of other very useful functions, consists of the geometric.mean command. Let’s see how we have the right to use the geometric.mean function in practice.
First, we need to install and load the psych package to R…
install.packages(\"psych\") # download psych packagelibrary(\"psych\") # load psych package
…and then we can apply the geometric.mean duty to compute the geometric median of our example data:
geometric.mean(x) # use geometric.mean function# 4.209156
The result is 4.209156 – the exact same value we gained with our hand-operated solution of example 1.
By the way: The geometric.mean function also provides an option to eliminate NA worths from your computation that the geometric mean. Definitely an additional advantage the the psych package compared to the hand-operated solution.
See more: When Actual Inflation Is Less Than Expected Inflation (Article)
August 2, 2021 12:48 am