Contents

In this tutorial, you will learn about what is geometric mean, when to use geometric mean and how to calculate geometric mean in R.

## What is geometric mean?

Geometric mean is a measure of central tendency. It is useful in averaging rate of changes when ratio changes are more important that the absolute changes.

Let $x_i, i=1,2, \cdots , n$ be $n$ positive observations

then the geometric mean of $X$ is denoted by $GM$ and is is defined as

` $$ \begin{aligned} GM &=\big(\prod_{i=1}^n x_i\big)^{1/n}\\ &=\sqrt[n]{x_1\times x_2\times \cdots \times x_n} \end{aligned} $$ `

Thus, the geometric mean is the $n^{th}$ root of the product of all the observations.

## When to use geometric mean?

Geometric mean is often used in averaging the data about rates and ratios. Geometric mean is commonly used measure of central tendency in finance to calculate average rate of interest, average growth rate or average return on financial portfolio.

## How to calculate geometric mean in R?

As such there is no specific function available in `base`

package. But you can define your own function for geometric mean or use `geometric.mean()`

function from `psych`

package.

### Geometric mean using user-defined function

Let us create a user-defined function to calculate geometric mean as follows:

```
Geometric.Mean <- function(x, na.rm = TRUE) {
if (na.rm == FALSE & any(is.na(x))) {
avg <- NA
} else{
avg <- exp(mean(log(x), na.rm = TRUE))
}
return(avg)
}
```

### Using `geometric.mean()`

function from `psych`

package

One can use the `geometric.mean()`

function from `psych`

package. First you need to install the `psych`

package using `install.packages("psych")`

. Once the package is installed, then load the package using `library(psych)`

.

The syntax of the `geometric.mean()`

function is

`geometric.mean(x,na.rm=TRUE)`

where,

**x :**a vector, matrix or data.frame,**na.rm :**`na.rm=TRUE`

(default) remove`NA`

values before processing.

For more information about `psych`

package check this link psych package.

Note that, geometric mean is not particularly useful if there are elements that are less than or equal to zero ($\leq 0$).

## Numerical example on geometric mean using R

### Example 1: Geometric Mean Using R

The annual percentage growth rate of profits of a company from year 2010 to 2014 are as follows:

Year 2010 2011 2012 2013 2014 Growth rate (in %) 30 40 76 69 95 Calculate the average annual percentage growth rate of profit.

#### Geometric mean using formula

Geometric mean is the $n^{th}$ root of the product of all the observations. That is

`$$GM =\big(\prod_{i=1}^n x_i\big)^{1/n}$$`

```
# create a data vector
x <- c(30, 40,76, 69, 95)
# compute no. of observations in x
n <- length(x)
# calculate geometric mean
GM <- (prod(x)) ** (1 / n)
GM
```

`[1] 56.92637`

The average annual percentage growth rate of profit is 56.9263721.

#### Geometric mean using user-defined function

Earlier we have defined the function for geometric mean as `Geometric.Mean(x)`

. Let us calculate the geometric mean using user-defined function `Geometric.Mean(x)`

.

```
# create a data vector
x <- c(30, 40,76, 69, 95)
Geometric.Mean(x)
```

`[1] 56.92637`

The average annual percentage growth rate of profit is 56.9263721.

#### Geometric mean using `psych`

package

One can use `geometric.mean()`

function from `psych`

package to calculate geometric mean.

```
# load the package
library(psych)
# use the function to compute geometric mean
geometric.mean(x)
```

`[1] 56.92637`

The average annual percentage growth rate of profit is 56.9263721.

### Example 2: Geometric Mean using R with `NA`

's

Calculate the geometric mean of data

`10,25,36,23,NA,17`

.

#### Geometric mean using formula

Geometric mean is the $n^{th}$ root of the product of all the observations. Using the formula

`$$GM =\big(\prod_{i=1}^n x_i\big)^{1/n}$$`

```
# create a data vector
x <- c(10,25,36,23,NA,17)
# compute no. of observations in x
n <- length(x[!is.na(x)])
# calculate geometric mean
GM <- (prod(x,na.rm=TRUE)) ** (1 / n)
GM
```

`[1] 20.38374`

The geometric mean of given data is 20.3837392.

Note that the default value of `na.rm`

is `FALSE`

in `prod()`

function. Therefore, to remove `NA`

values before processing use the argument `na.rm=TRUE`

. Also to get the length of `x`

without `NA`

use a logical NOT with `is.na(x)`

.

#### Geometric mean using user-defined function

Use the user-defined function `Geometric.Mean(x)`

with additional argument `na.rm=TRUE`

.

```
# create a data vector
x <- c(10,25,36,23,NA,17)
Geometric.Mean(x)
```

`[1] 20.38374`

The geometric mean of given data is 20.3837392.

Note that the default value of `na.rm`

is `TRUE`

in our user-defined function `Geometric.Mean()`

function. Therefore, no need to specify the argument `na.rm=TRUE`

.

#### Geometric mean using `psych`

package

Use `geometric.mean()`

function from `psych`

package to calculate geometric mean.

```
# load the package
library(psych)
# use the function to compute harmonic mean
geometric.mean(x)
```

`[1] 20.38374`

The geometric mean of given data is 20.3837392.

Note that the default value of `na.rm`

is `TRUE`

in `geometric.mean()`

function from `psych`

package. Therefore, the `NA`

values are removed before processing.

## Endnote

In this tutorial, you learned about what is geometric mean and how to compute geometric mean for the data using R.

To learn more about descriptive statistics using R, please refer to the following tutorials:

Harmonic mean using R

Descriptive Statistics using R

Hopefully you enjoyed this tutorial on geometric mean using R.