2-2. Measures of Central Location (Central Tendency)

集中趋势的统计量

1. Mean

1.1 산술평균(Arithmetic Mean)

EXAMPLE 10. 다음 데이터 세트의 Σx\Sigma x , Σx2\Sigma x^2 , Σ(x1)2\Sigma {(x-1)^2} 을 구하시오.

1 3 4

[Solution]

x <- c(1, 3, 4)
sum(x)
sum(x^2)
sum((x-1)^2)

The sample mean of a set of n sample data is the number xˉ\bar{x} defined by the formula

xˉ=Σxn\bar{x}=\frac{Σx}{n}

EXAMPLE 11. 다음 표본 데이터의 평균을 구하라.

2 -1 0  2

[Solution]

x <- c( 2, -1, 0, 2)

# method 1
n <- length(x)            # number of elements of x
mean_x <- sum(x * 1/n)    # or  mean_x <- sum(x) / n

# method 2 : using function mean()
mean(x)                   # function : mean()

EXAMPLE 12. 무작위로 선발한 10명의 학생의 평균 평점은 다음과 같다. 표본의 평균을 구하라.

1.90 3.00 2.53 3.71 2.12 1.76 2.71 1.39 4.00 3.33

[ Solution ]

x <- c(1.90, 3.00, 2.53, 3.71, 2.12, 1.76, 2.71, 1.39, 4.00, 3.33)
mean(x)

EXAMPLE 13. 가임기 년령을 넘긴 무작위로 선발된 19명의 여성의 데이터가 다음과 같다( xx 는 자녀의 수, ff 는 그 값의 빈도). 표본의 평균을 구하라.

x   0   1   2   3   4
f   3   6   6   3   1

[Solution]

x <- c(0, 1, 2, 3, 4)
f <- c(3, 6, 6, 3, 1)
mean <- sum(x * f) / sum(f)        # or   f <- f/sum(f) ; mean <- sum(x*f)
mean

The population mean of a set of N population data is the number μμ defined by the formula

μ=ΣxNμ=\frac{Σx}{N}

1.2 기하평균(Geometric Mean)

산술평균은 '합의 평균'이고, 기하평균은 '곱의 평균'이다. 예를 들어 다음 문제를 보자.

EXAMPLE 14. 첫번째 해에는 5%, 두번째 해에는 10% 증가했다. 연평균 증가율은?

[ Solution ]

연평균 증가율 r로 2년 연속 증가한 값과, 5%, 10% 로 두 번 증가한 값이 같아야 한다. 이것을 식으로 세우면, rr=510=510,r=510r⋅r=5⋅10=5⋅10, r=\sqrt{5⋅10} 이다.

x <- c(5, 10)
n <- length(x)

g_mean <- prod(x)^(1/n); g_mean

이 예제를 일반화 하면, 두 수 a, b의 기하 평균은 ab\sqrt{a⋅b} 이다.

곱의 평균을 기하 평균이라고 부르는 이유는, 기하의 비례식에서 유래하였기 때문인데, 예를 들어, 반원에서 직각 삼각형의 닮음 식은 a:p=p:ba:p=p:b 이고, 여기서 p 가 a, b의 기하 평균이다.

또다른 예로, 변의 길이가 aabb 인 직사각형과 넓이가 같은 정사각형의 한변의 길이는 ab\sqrt{ab} 이다.

1.3 조화평균(Harmonic Mean)

조화평균은 '역수의 산술평균의 역수'이다. 역수의 차원에서 평균을 구하고, 다시 역수를 취해 원래 차원의 값으로 돌아오는 것이다. 다음 예시를 보자.

EXAMPLE 15. 갈 때 10m/s, 올때 20m/s 로 주행하였다. 평균 속력은?

[ Solution ]

10과 20의 산술 평균인 15는 답이 아니다. 갈 때와 올 때 투여한 시간이 다르기 때문이다. 여기서는 시간의 차원에서 평균을 구해야 한다.

거리를 속력으로 나누면(역수), 시간인데, 이 시간의 평균을 구한 후에, 구한 시간 값에 대해, 다시 속력으로 바꾼 것이 평균 속력이다.

x <- c(10, 20)
h_mean <- mean(x^(-1))^(-1)
h_mean

거리를 S라 하고, 시간에 대해 식을 세우면, S10+S20=2Sx\frac{S}{10}+\frac{S}{20}=\frac{2S}{x} 이다.

이 예제를 일반화 하여, 갈 때 속력을 a, 올 때 속력을 b라 하여 정리하면, 조화평균은 x=2aba+bx=\frac{2ab}{a+b} 이다.

역수의 평균의 역수를 조화평균(harmonic mean)이라고 부르는 이유는, 음악의 화음(harmony)에서 이 평균을 찾을 수 있기 때문이다.

화음은 주파수가 1:2:3 과 같이 간단한 정수 간격을 이룰 때 발생하는데, 현의 길이는 주파수의 역수이다. 즉 화음을 이루는 현의 길이를 구하기 위해서는, 역수(주파수)의 평균을 구하고, 다시 그 값의 역수(현의 길이)로 되돌아온다.

2. Median

EXAMPLE 16. 기업 직원의 연 평균 수입에 관심이 있다고 가정하자. 무작위로 선발된 직원 7명의 대략적인 연 수입은 다음과 같다(단위 : 천 달러)

24.8 22.8 24.6 192.4 25.2 18.5 23.7
  • 소숫점 1자리에서 반올림한 평균 수입액을 구하라.

[Solution 1]

x <- c(24.8, 22.8, 24.6, 192.4, 25.2, 18.5, 23.7)

round(mean(x), 1)
  • 위의 계산으로 볼 때 192.4 데이터는 이상치(outlier)로 판단이 된다. 따라서 이 데이터의 중심값으로 평균값이 아닌 중앙값(median)을 구하라.

[Solution 2] median without outlier, 이상치 제거 후의 중앙값

x <- c(24.8, 22.8, 24.6, 25.2, 18.5, 23.7)

# 1. sort the data in ascending order
y <- sort(x) ; y

# 2. Numbers of data
n <- length(y)

# 3. Compute the median
if ( n %% 2 == 0 )  {                         # the modulo (%% operator)
     ( y[(n %/% 2)] + y[(n %/% 2) + 1])/2     # the numerical division (%/% operator)
   } else {
            y[(n %/% 2 + 1)]
          }

# R function for median : median()
median(x)

표본 데이터의 중앙값(median)은 다음과 같이 구한다.

  1. 표본 데이터를 오름차순으로 정렬한다. : y <- sort(x)

  2. 데이터의 갯수( nn )가 홀수이면, xmedian=yn2x_{median} = y_{\frac{n}{2}} ( n2\frac{n}{2} 번째 데이터)

  3. 데이터의 갯수가 짝수이면, xmedian=(yn2+y(n2+1))2x_{median} = \frac{ (y_{\frac{n}{2}} + y_{(\frac{n}{2} + 1)})}{2} (즉, n2\frac{n}{2}째 데이터와 (n2+1)(\frac{n}{2} + 1)째 데이터의 평균이 표본 데이터의 중앙값이다.

The sample median x~\tilde{x} of a set of sample data for which there are an odd number of measurements is the middle measurement (n2\frac{n}{2})when the data are arranged in numerical order. The sample median x~\tilde{x} of a set of sample data for which there are an even number of measurements is the mean of the two middle measurements (mean of two numbers, n2\frac{n}{2}th number and n2+1\frac{n}{2} +1th number) when the data are arranged in numerical order.

EXAMPLE 17. 다음 데이터 세트의 중앙값을 구하라.

132 162 133 145 148 139 147 160 150 153

[ Solution ]

x <- c(132, 162, 133, 145, 148, 139, 147, 160, 150, 153)
# 1. sort the data in ascending order
y <- sort(x) ; y

# 2. Numbers of data
n <- length(y)

# 3. Compute the median
if ( n%%2 == 0 ) { (y[(n%/%2)] + y[(n%/%2)+1])/2 }
else { y[(n%/%2) + 1] }

# R function for median : median()
median(x)

[ Median ]

EXAMPLE 18. 다음 데이터 세트의 sample median을 구하라.

-1 0 2 2

[Solution]

x <- c(-1, 0, 2, 2)
median(x)

Note : median of the data set is the mean of 2nd and 3rd data -> (0 + 2) / 2 = 1.

EXAMPLE 19. 다음 데이터 세트의 sample median을 구하라.

1.39 1.76 1.90 2.12 2.53 2.71 3.00 3.33 3.71 4.00

[Solution]

x <- c(1.39, 1.76, 1.90, 2.12, 2.53, 2.71, 3.00, 3.33, 3.71, 4.00)
median(x)

EXAMPLE 20. 다음 데이터 세트의 sample median을 구하라.

0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 4

[Solution]

x <- c(0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4)
median(x)

The relationship between the mean and the median for several common shapes of distributions is shown in Figure "Skewness of Relative Frequency Histograms". The distributions in panels (a) and (b) are said to be symmetric because of the symmetry that they exhibit. The distributions in the remaining two panels are said to be skewed. In each distribution we have drawn a vertical line that divides the area under the curve in half, which in accordance with Figure "The Median" is located at the median. The following facts are true in general:

  1. When the distribution is symmetric, as in panels (a) and (b) of Figure "Skewness of Relative Frequency Histograms", the mean and the median are equal.

  2. When the distribution is as shown in panel (c) of Figure "Skewness of Relative Frequency Histograms", it is said to be skewed right. The mean has been pulled to the right of the median by the long “right tail” of the distribution, the few relatively large data values.

  3. When the distribution is as shown in panel (d) of Figure "Skewness of Relative Frequency Histograms", it is said to be skewed left. The mean has been pulled to the left of the median by the long “left tail” of the distribution, the few relatively small data values.

2.1 Skewness of Relative Frequency Histogram

3. Mode

The sample mode of a set of sample data is the most frequently occurring value.

EXAMPLE 21. 다음 데이터 세트의 mode(최빈값)를 구하라.

-1 0 2 0

[Solution]

x <- c(-1, 0, 2, 0)
y <- table(x)
names(which.max(y))

R does not have a standard in-built function to calculate mode. So we create a user function to calculate mode of a data set in R. This function takes the vector as input and gives the mode value as output.

# Create the user function : getmode().
getmode <- function(v) {
   uniqv <- unique(v)
   uniqv[which.max(tabulate(match(v, uniqv)))]
}

# Create the vector with numbers.
v <- c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)

# Calculate the mode using the user function.
result <- getmode(v)
print(result)

# Create the vector with characters.
charv <- c("o","it","the","it","it")

# Calculate the mode using the user function.
result <- getmode(charv)
print(result)

  • 최빈값은 지지하는 정당이나 좋아하는 숫자 등 수가 없거나 수가 있더라도 대소관계가 의미 없는 질적 자료에서 많이 쓰인다.

  • 중앙값은 소득이나 성적처럼 우열을 가릴 수 있는 등 순위가 중요한 중요한 자료에서 많이 쓰인다.

  • 평균은 알다시피 가장 즐겨쓰는 대표값이지만 이상치에 민감하다는 단점이 있어 의외로 주의를 요한다. 평균이 데이터를 잘 설명하지 못하는 경우가 비단 소표본에서만 일어나는 것이 아니다. 실제로 각종 경제 지표, 특히 국가 규모의 데이터를 다룰때는 소득 상위 10%, 저소득층과 같이 분위수를 쓰는 경우가 많다. 소득 불균형이 커질수록 평균은 의미를 잃어가며, 중앙값과 평균을 구분할 수 있는 분별력이 필요해진다.

4. Skewness and Kurtosis in R

  • Skewness : β3=n(n1)(n2)Σ(xixˉs)3\beta_3 = \frac {n} {(n-1)(n-2)} \Sigma (\frac{x_i - \bar{x}}{s})^3

  • kurtosis : β4={n(n+1)(n1)(n2)(n3)Σ(xixˉs)4}3(n1)2(n2)(n3)\beta_4 = \{\frac {n(n+1)} {(n-1)(n-2)(n-3)} \Sigma (\frac{x_i - \bar{x}}{s})^4\} - \frac{3(n-1)^2}{(n-2)(n-3)}

EXAMPLE 22. MASS 라이브러리의 Cars93 데이터 세트를 이용하여 차종별 가격(Price by Type)의 분포를 히스토그램으로 그리고, 차종별 가격의 skewness(왜도, 좌우 대칭 정도)와 kurtosis(첨도, 정규분포 대비 봉우리 높이 정도)를 구하라.

library(MASS)
str(Cars93)
 
# 1. Histogram, Price by Car Type
library(ggplot2)
ggplot(Cars93, aes(x=Price)) + 
   geom_histogram(binwidth=3, fill = "blue", colour = "black") + 
   ggtitle("Histogram, Price by Type") + 
   facet_grid(Type ~ .)

library(fBasics)

# 2. skewness : skewness()
skewness(Cars93$Price)

with(Cars93, tapply(Price, Type, skewness))

# 3. kurtosis : kurtosis()
kurtosis(Cars93$Price)

with(Cars93, tapply(Price, Type, kurtosis))

  1. 集中趋势的统计量

    1)平均数(mean)。

    2)中位数(median)。

    3)众数(mode)。

    4)几何平均数(geometric mean)。

    5)调和平均数(harmonic mean)。

  2. 偏度(skewness) / 峰度(kurtosis)。

Last updated