Читать книгу Probability with R - Jane M. Horgan - Страница 47
Example 2.2 A program to calculate skewness
ОглавлениеThe following syntax calculates the skewness coefficient of a set of data and assigns it to a function called that has one argument .
skew <- function(x) { xbar <- mean(x) sum2 <- sum((x-xbar)^2, na.rm = T) sum3 <- sum((x-xbar)^3, na.rm = T) skew <- (sqrt(length(x))* sum3)/(sum2^(1.5)) skew}
You will agree that the conventions of vector calculations make it very easy to calculate statistical functions.
When skew has been defined, you can calculate the skewness on any data set. For example,
skew(downtime)
gives
[1] -0.04818095
which indicates that the data is slightly negatively skewed.
Looking again at the data given Example 2.1, let us calculate the skewness coefficient
skew(usage) [1] 1.322147
which illustrates that the data is highly skewed. Recall that the first two values are outliers in the sense that they are very much larger than the other values in the data set. If we calculate the skewness with those values removed, we get
skew(usage[3:9]) [1] 0.4651059
which is very much smaller than that obtained with the full set.