Calculate Kurtosis in R
You need to calculate the kurtosis of a numerical dataset in R.
We'll use the
moments package for this. Install it as follows:
moments package supports a couple of different kurtosis calculations, both involving
estimating kurtosis from samples. One is an estimator for Pearson's (non-excess) kurtosis by way of the
kurtosis function. The other is an estimator for Geary's kurtosis using the
function. Here we'll use
For data, let's use the Titanic dataset. You
can use readr's read_csv function to read the file. We'll
calculate the skewness of the age column. Note that in the original dataset this variable has some
? values, so it reads as character data. We need to remove those and convert the column to
> library(moments) > library(readr) > titanic <- read_csv("titanic-full.csv") > age <- as.numeric(titanic$age) > kurtosis(age, na.rm=TRUE)
na.rm=TRUE to remove NAs from the data. Alternatively, we could have used
age <- na.omit(as.numeric(titanic$age)) to remove them.
The result is 3.144524.
For visualization, we can generate a histogram using
hist as well: