These are external functions to calculate skewness and kurtosis for a vector in R. Anyone using R is likely able to write similar functions themselves, but you never know.

[code]

### sample_skew.R
### Enda Hargaden, January 2013

sample_skew = function(data_vec_dirty)
{

# Remove missing values
data_vec = data_vec_dirty[!is.na(data_vec_dirty)]

n = length(data_vec)
xbar = mean(data_vec)

second_moment = c()
third_moment = c()

for(i in 1:n)
{
second_moment[i] = (data_vec[i] – xbar) ^ 2
third_moment[i] = (data_vec[i] – xbar) ^ 3
}

s_numerator = sum(third_moment) / n
s_denominator = sum(second_moment /n) ^ 1.5
skew = s_numerator / s_denominator

skew
}

[/code]

 

[code]

### sample_kurtosis.R
### Enda Hargaden, January 2013

sample_kurtosis = function(data_vec_dirty)
{

#Remove missing values
data_vec = data_vec_dirty[!is.na(data_vec_dirty)]

n = length(data_vec)
xbar = mean(data_vec)

second_moment = c()
forth_moment = c()

for(i in 1:n)
{
second_moment[i] = (data_vec[i] – xbar) ^ 2
forth_moment[i] = (data_vec[i] – xbar) ^ 4
}

k_numerator = sum(forth_moment) / n
k_denominator = ( sum(second_moment) /n )^2
kurtosis = k_numerator / k_denominator

kurtosis

}

[/code]

//
Scroll to Top