Moments using R
In this tutorial, you will learn about how to compute raw and central moments of a data set using R.
Before we discuss R functions to compute moments, let us see why we need moments and how moments are defined.
Why we need to find moments of a data set?
Moments of a data set are very useful to describe the nature of a data set.
Moments are useful in describing
- center of a data set,
- variation in a data set,
- skewness of a data set, and
- kurtosis of a data set.
Raw moments of a data set
Let $x_1, x_2,\cdots, x_n$
be $n$ observations. Then the $r^{th}$ raw moment is denoted by $\mu_r^\prime$ and is given by
$$ \begin{aligned} \mu_r^\prime & =\frac{1}{n}\sum_{i=1}^{n}x_i^r,\\ & \quad r = 0,1,2,\cdots. \end{aligned} $$
- $\mu_0^\prime=0$ (always).
- $\mu_1^\prime=\overline{x}$ mean of a data set.
Central moments of a data set
Let $x_1, x_2,\cdots, x_n$
be $n$ observations.The sample mean of $X$ is denoted by $\overline{x}$ and is given by
$$ \begin{eqnarray*} \overline{x}& =\frac{1}{n}\sum_{i=1}^{n}x_i \end{eqnarray*} $$
Then the $r^{th}$ central moment is denoted by $\mu_r$ and is given by
$$ \begin{eqnarray*} \mu_r & =\frac{1}{n}\sum_{i=1}^{n}(x_i-\overline{x})^r,\\ & \quad r = 0,1,2,\cdots. \end{eqnarray*} $$
- $\mu_0=0$ (always).
- $\mu_1=1$ (always).
- $\mu_2=Var(X)$ (variance of $X$).
How to find raw and moments of a data set using R?
The base
package in R doesn't have any function to compute the moments, skewness and kurtosis of a data set. The moment
package contains functions to compute raw moments, central moments, skewness and kurtosis.
To get moments of a data set, we need to install the moments
package if not installed. Once it is installed we can load the moments
package using library()
function.
## load the package
library(moments)
The raw moments and central moments can be calculated using all.moments()
function from moments
package.
The syntax to compute raw and central moments of a data set using R is
all.moments(x,order.max=2,central=FALSE)
where
x
: a numeric vector of dataorder.max
: the maximum order of the moments to be computed (default value is 2).central
: a logical value (FALSE
for raw moments orTRUE
for central moments)
Numerical Problem for moments calculations
To understand the use of all.moments()
function, let us take the following numerical problem.
Raw and Central Moments Example
The hourly earning (in dollars) of sample of 7 workers are :
$26, 21, 24, 22, 25, 24, 23$
.(a) Compute raw moments.
(b) Compute central moments.
(c) Convert raw moments to central moments.
(d) Convert central moments to raw moments.
How to use all.moments()
function to compute raw moments in R?
Let us define a data set as
## define a vector of earnings
hourly.earning <-c(26, 21, 24, 22, 25, 24, 23)
# display the vector
hourly.earning
[1] 26 21 24 22 25 24 23
(a) Raw moments of hourly earnings:
## Raw moments
raw.mom <- all.moments(hourly.earning,order.max = 4)
## display the result
raw.mom
[1] 1.00000 23.57143 558.14286 13275.00000 317104.42857
It returns the value as vector. The first value is the $0^{th}$ order raw moment $\mu_0^\prime$ (which is always 1), the second value is the $1^{st}$ order raw moment $\mu_1^\prime$ (which is the mean), the third value is the $2^{nd}$ order raw moment $\mu_2^\prime$, the fourth value is the $3^{rd}$ order raw moment $\mu_3^\prime$ and the fifth value is the $4^{th}$ order raw moment $\mu_4^\prime$.
The Raw moments of hourly earnings are as follows:
$$ \begin{aligned} \mu_0^\prime &= 1\\ \mu_1^\prime & = 23.5714286\\ \mu_2^\prime & = 558.1428571\\ \mu_3^\prime & = 1.3275\times 10^{4}\\ \mu_4^\prime & = 3.1710443\times 10^{5} \end{aligned} $$
How to use all.moments()
function to compute central moments in R?
(b) Central moments of hourly earnings:
## Central moments
cent.mom <- all.moments(hourly.earning,order.max = 4,central = TRUE)
## display the result
cent.mom
[1] 1.000000e+00 -1.522592e-15 2.530612e+00 -5.247813e-01 1.270637e+01
It returns the value as vector. The first value is the $0^{th}$ order central moment $\mu_0^\prime$ (which is always 1), the second value is the $1^{st}$ order raw moment $\mu_1^\prime$ (which is the mean), the third value is the $2^{nd}$ order raw moment $\mu_2^\prime$, the fourth value is the $3^{rd}$ order raw moment $\mu_3^\prime$ and the fifth value is the $4^{th}$ order raw moment $\mu_4^\prime$.
The Central moments of hourly earnings are as follows:
$$ \begin{aligned} \mu_0 &= 1\\ \mu_1 & = -1.5225916\times 10^{-15}\\ \mu_2 & = 2.5306122\\ \mu_3 & = -0.5247813\\ \mu_4 & = 12.7063723 \end{aligned} $$
How to convert raw moments to central moments?
The first four central moments in terms of raw moments are
$$ \begin{aligned} \mu_1 &= 0\\ \mu_2 &= \mu_2^\prime - {\mu_1^\prime}^2\\ \mu_3 &= \mu_3^\prime - 3\mu_2^\prime \mu_1^\prime + 2 {\mu_1^\prime}^3\\ \mu_4 &= \mu_4^\prime - 4\mu_3^\prime \mu_1^\prime + 6 \mu_2^\prime{\mu_1^\prime}^2-3{\mu_1^\prime}^4. \end{aligned} $$
(c) Raw moments to central moments of hourly earnings:
Given the raw moments of a data set, the function raw2central()
converts raw moments to central moments of a data set.
## Raw Moments to Central Moments
R2C<-raw2central(raw.mom)
## display the result
R2C
[1] 1.0000000 0.0000000 2.5306122 -0.5247813 12.7063723
The Central moments of hourly earnings from raw moments are as follows:
$$ \begin{aligned} \mu_0 &= 1\\ \mu_1 & = 0\\ \mu_2 & = 2.5306122\\ \mu_3 & = -0.5247813\\ \mu_4 & = 12.7063723 \end{aligned} $$
How to convert central moments to raw moments?
The first four raw in terms of central moments are
$$ \begin{aligned} \mu_1^\prime &= \overline{x}\\ \mu_2^\prime &= \mu_2 + {\mu_1^\prime}^2\\ \mu_3^\prime &= \mu_3 + 3\mu_2 \mu_1^\prime + {\mu_1^\prime}^3\\ \mu_4^\prime &= \mu_4 + 4\mu_3 \mu_1^\prime + 6 \mu_2{\mu_1^\prime}^2+{\mu_1^\prime}^4. \end{aligned} $$
(d) Central moments to raw moments of hourly earnings:
Given the central moments of a data set, the function central2raw()
converts central moments to raw moments of a data set.
## Compute mean of data set
eta <- mean(hourly.earning)
## display the result
eta
[1] 23.57143
## Central Moments to Raw Moments
C2R<-central2raw(cent.mom,eta=eta)
## display the result
C2R
[1] 1.00000 23.57143 558.14286 13275.00000 317104.42857
The Raw moments of hourly earnings from central moments are as follows:
$$ \begin{aligned} \mu_0^\prime &= 1\\ \mu_1^\prime & = 23.5714286\\ \mu_2^\prime & = 558.1428571\\ \mu_3^\prime & = 1.3275\times 10^{4}\\ \mu_4^\prime & = 3.1710443\times 10^{5} \end{aligned} $$
Endnote
In this tutorial, you learned about how to compute the raw moments, central moments of a data set using R. You also learned about how to convert raw moments to central moments and central moments to raw moments using R programming.
To learn more about descriptive statistics using R, please refer to the following tutorials:
- Statistical functions in R
- Karl Pearson's Coefficient of Skewness using R
- Bowley's Coefficient of Skewness using R
- Moments Coefficient of Skewness using R
- Moments Coefficient of Kurtosis using R
- Descriptive Statistics Using R
Let me know in the comments below, if you have any questions on moments using R and your thought on this article.