How to Calculate Skewness and Kurtosis in R
Skewness and Kurtosis are two important statistics that tell us about the shape of a distribution. While skewness tells us how skewed a distribution is to the left or right, kurtosis measures the distribution's 'peakedness,' or whether it is heavy-tailed or light-tailed. Learn more about the concepts of skewness and kurtosis.
In this article, we will learn how to calculate the skewness and kurtosis of a dataset in R.
Dataset
Let’s take the example of the daily stock returns of a stock over the past 30 days.
stock_returns <- c(4, 3, -1, 2, 4, 5, 3, -2, 1, 5, -3, -1, 4, 5, 3, 3, 2, 4, 2, 1, -2, 4, 3, 2, 2, 1, -1, -1, 2, 3)
Visualize Data
Before we calculate the skewness and kurtosis, let's visualise the data in the form of a histogram.
hist(stock_returns, col='lightgreen')
By looking at the histogram, we can say that the distribution is slightly left skewed, that is, more values are concentrated on the right hand side of the distribution. Also, the distribution seems fat-tailed.
Calculate Skewness and Kurtosis in R
Let’s verify our observations by calculating the skewness and kurtosis in R. R comes with a library called moments, which contains the functions for skewness and kurtosis. In the following code, we load this library and then calculate the skewness and kurtosis of our dataset.
install.packages('moments')
library(moments)
#calculate skewness
skewness(stock_returns)
[1] -0.5810439
#calculate kurtosis
kurtosis(stock_returns)
[1] 2.316147
For our stock return data, the skewness is -0.58 and the kurtosis is 2.31. A negative skewness confirms that the data is left-skewed. The kurtosis value is 2.31. The dataset has lighter tails than a normal distribution if the kurtosis is less than 3 (less in the tails).
Jarque-Bera Test
Using the moments library, we can also perform the Jarque-Bera test using the jarque.test function. This function performs the Jarque-Bera test on the given data sample to determine if the data is drawn from a normal population. Under the hypothesis of normality, data should be symmetrical. Its skewness should be equal to zero and its kurtosis should be close to three.
Let’s perform this test on our data and see what we get.
jarque.test(stock_returns)
Jarque-Bera Normality Test
data: stock_returns
JB = 2.2726, p-value = 0.321
alternative hypothesis: greater
The p-value is 0.321, which is greater than 0.05. With a p-value >0.05, one would usually say that the data is consistent with having skewness and excess kurtosis of zero. Therefore, we fail to reject the null hypothesis that the dataset has a skewness and kurtosis similar to a normal distribution.
Data Science in Finance: 9-Book Bundle
Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.
What's Included:
- Getting Started with R
- R Programming for Data Science
- Data Visualization with R
- Financial Time Series Analysis with R
- Quantitative Trading Strategies with R
- Derivatives with R
- Credit Risk Modelling With R
- Python for Data Science
- Machine Learning in Finance using Python
Each book includes PDFs, explanations, instructions, data files, and R code for all examples.
Get the Bundle for $39 (Regular $57)Free Guides - Getting Started with R and Python
Enter your name and email address below and we will email you the guides for R programming and Python.