How is skewness of a distribution calculated in R


  R Interview Questions

Skew measures the asymmetry of a distribution. There is a right skew ( long tail to the right ) and a left skew ( vice-versa ). Let’s overlay a left and right skewed distribution and see how they look.

# A right skewed distribution 
> right_skew = data.frame(r=rbeta(1000,5,1))
# A left skewed distribution
> left_skew = data.frame(l=rbeta(1000,1,5))

And let’s plot this to get a better picture.

ggplot() + 
    geom_density(data = left_skew,aes(left_skew,fill="red",alpha=0.1)) + 
    geom_density(data = right_skew, aes(right_skew,fill="green",alpha=0.1)) +
    geom_vline(aes(xintercept = mean(left_skew$l)),color="red",linetype="dashed") +
    geom_vline(aes(xintercept = median(left_skew$l)),color="red") +  
    geom_vline(aes(xintercept = mean(right_skew$r)),color="green",linetype="dashed") +
    geom_vline(aes(xintercept = median(right_skew$r)),color="green")

Don’t let the syntax get you. Focus on the picture below.

The red distribution is right skewed and the green distribution is left skewed. Let’s find out the skew ness now.

> library(e1071)
> mean(left_skew$l)   # Mean is > Median in a left skewed distribution
[1] 0.1641156
> median(left_skew$l)
[1] 0.1298181
> skewness(left_skew$l)   # Skewness of a left skewed distribution is positive.
[1] 1.1802

Similarly, a right skewed distribution

> mean(right_skew$r)
[1] 0.8281012
> median(right_skew$r)
[1] 0.8659932
> skewness(right_skew$r)
[1] -1.16096

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.