variance
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| variance [2026/03/10 23:26] – [Variance] hkimscil | variance [2026/03/10 23:56] (current) – hkimscil | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Variance ====== | ====== Variance ====== | ||
| [[Mean]], | [[Mean]], | ||
| + | |||
| + | <tabbox rs1a> | ||
| + | < | ||
| + | rnorm2 <- function(n, | ||
| + | n.x <- 100 | ||
| + | m.x <- 50 | ||
| + | sd.x <- 4 | ||
| + | |||
| + | set.seed(101) | ||
| + | x <- rnorm2(n.x, m.x, sd.x) | ||
| + | x[1] | ||
| + | # x[1] = m.x + e.x | ||
| + | e.x <- x - m.x | ||
| + | head(e.x) | ||
| + | sum(e.x) | ||
| + | |||
| + | x2 <- m.x + e.x | ||
| + | x2 == x | ||
| + | |||
| + | se.x <- e.x^2 # square error | ||
| + | head(se) | ||
| + | sse.x <- sum(se) | ||
| + | ############### | ||
| + | sse.x # sum of square error | ||
| + | # (residual, deviation score, total) | ||
| + | ############### | ||
| + | df.x <- n.x - 1 | ||
| + | mse.x <- sse.x / df.x | ||
| + | mse.x | ||
| + | var(x) | ||
| + | mse.x == var(x) | ||
| + | |||
| + | ############################ | ||
| + | </ | ||
| + | |||
| + | <tabbox ro1a> | ||
| + | < | ||
| + | > rnorm2 <- function(n, | ||
| + | > n.x <- 100 | ||
| + | > m.x <- 50 | ||
| + | > sd.x <- 4 | ||
| + | > | ||
| + | > set.seed(101) | ||
| + | > x <- rnorm2(n.x, m.x, sd.x) | ||
| + | > x[1] | ||
| + | [1] 48.76307 | ||
| + | > # x[1] = m.x + e.x | ||
| + | > e.x <- x - m.x | ||
| + | > head(e.x) | ||
| + | [,1] | ||
| + | [1,] -1.236931 | ||
| + | [2,] 2.525088 | ||
| + | [3,] -2.731068 | ||
| + | [4,] 1.077222 | ||
| + | [5,] 1.490081 | ||
| + | [6,] 5.186575 | ||
| + | > sum(e.x) | ||
| + | [1] 7.105427e-15 | ||
| + | > | ||
| + | > x2 <- m.x + e.x | ||
| + | > x2 == x | ||
| + | [,1] | ||
| + | [1,] TRUE | ||
| + | [2,] TRUE | ||
| + | [3,] TRUE | ||
| + | [4,] TRUE | ||
| + | [5,] TRUE | ||
| + | [6,] TRUE | ||
| + | [10,] TRUE | ||
| + | . . . . . . | ||
| + | [99,] TRUE | ||
| + | [100,] TRUE | ||
| + | > | ||
| + | > se.x <- e.x^2 # square error | ||
| + | > head(se) | ||
| + | [,1] | ||
| + | [1,] 2.003177 | ||
| + | [2,] 40.421681 | ||
| + | [3,] 3.024598 | ||
| + | [4,] 15.246176 | ||
| + | [5,] 1.763444 | ||
| + | [6,] 33.997606 | ||
| + | > sse.x <- sum(se) | ||
| + | > ############### | ||
| + | > sse.x # sum of square error (residual, deviation score, total) | ||
| + | [1] 1584 | ||
| + | > ############### | ||
| + | > | ||
| + | > df.x <- n.x - 1 | ||
| + | > mse.x <- sse.x / df.x | ||
| + | > mse.x | ||
| + | [1] 16 | ||
| + | > var(x) | ||
| + | [,1] | ||
| + | [1,] 16 | ||
| + | > mse.x == var(x) | ||
| + | [,1] | ||
| + | [1,] TRUE | ||
| + | > | ||
| + | > ############################ | ||
| + | > | ||
| + | </ | ||
| + | </ | ||
| * 숫자로 측정된 한 변인이 (variable Y) 있다. | * 숫자로 측정된 한 변인이 (variable Y) 있다. | ||
| Line 155: | Line 258: | ||
| ====== Read more ====== | ====== Read more ====== | ||
| - | |||
| 샘플의 분산으로 모집단의 분산값을 추정할 때에는, 샘플의 숫자인 $n$ 대신에 $n-1$ 을 사용한다 (참조. [[: | 샘플의 분산으로 모집단의 분산값을 추정할 때에는, 샘플의 숫자인 $n$ 대신에 $n-1$ 을 사용한다 (참조. [[: | ||
| - | + | \begin{eqnarray*} | |
| - | $ s^2 = Var[X] = \displaystyle \frac{\displaystyle \sum_{i=1}^n (X_i - \overline{X})^2}{n-1}$ | + | s^2 & = & Var[X] |
| + | & = & \displaystyle \frac{\displaystyle \sum_{i=1}^n (X_i - \overline{X})^2}{n-1} | ||
| + | \end{eqnarray*} | ||
| 위에서 언급한 것처럼, 분산 공식의 분자부분을 **Sum of Squares**라고 부르고 줄여서 $SS$라고 쓰고, n-1을 [[:degrees of freedom]] 혹은 이를 줄여서 $df$라고 쓴다. 따라서 위의 분산을 구하는 식은 아래와 같이 표현될 수 있다. | 위에서 언급한 것처럼, 분산 공식의 분자부분을 **Sum of Squares**라고 부르고 줄여서 $SS$라고 쓰고, n-1을 [[:degrees of freedom]] 혹은 이를 줄여서 $df$라고 쓴다. 따라서 위의 분산을 구하는 식은 아래와 같이 표현될 수 있다. | ||
| - | + | \begin{eqnarray*} | |
| - | $$s^2 = \displaystyle \frac{SS}{df}$$ | + | s^2 & = & \displaystyle \frac{\text{SS}} {\text{df}} |
| + | \end{eqnarray*} | ||
| 위에서 샘플의 분산으로 모집단의 분산을 추정할 때 n-1을 쓴다고 하였지만, | 위에서 샘플의 분산으로 모집단의 분산을 추정할 때 n-1을 쓴다고 하였지만, | ||
| - | $$\sigma^2 = \displaystyle \frac{SS}{N-1} = \displaystyle \frac{SS}{df}$$ | + | \begin{eqnarray*} |
| + | \sigma^2 | ||
| + | & = & \displaystyle \frac{\text{SS}} {\text{df}} | ||
| + | |||
| + | \end{eqnarray*} | ||
| + | |||
| 아래는 R에서 보는 간단한 예이다. | 아래는 R에서 보는 간단한 예이다. | ||
| - | < | ||
| - | * variance: | ||
| - | * *variance: | ||
| - | </ | ||
| + | <tabbox rs2> | ||
| + | < | ||
| + | y <- c(3, 4, 3, 4, 6) | ||
| + | s.y <- sum(y) | ||
| + | s.y | ||
| + | n.y <- length(y) | ||
| + | n.y | ||
| + | m.y <- mean(y) | ||
| + | m.y | ||
| + | error <- y - m.y | ||
| + | se <- error^2 | ||
| + | sse <- sum(se) | ||
| + | tmp <- data.frame(error, | ||
| + | print(tmp) | ||
| + | print(sse) | ||
| + | |||
| + | sse2 <- sum(error^2) | ||
| + | sse2 | ||
| + | |||
| + | df.y <- n.y - 1 | ||
| + | |||
| + | mse <- sse / df.y | ||
| + | mse | ||
| + | var(y) | ||
| + | </ | ||
| + | |||
| + | <tabbox ro2> | ||
| + | < | ||
| + | > rnorm2 <- function(n, | ||
| + | > set.seed(1) | ||
| + | > a <- rnorm2(100000000, | ||
| + | > head(a) | ||
| + | [,1] | ||
| + | [1,] 93.73555 | ||
| + | [2,] 101.83562 | ||
| + | [3,] 91.64403 | ||
| + | [4,] 115.95044 | ||
| + | [5,] 103.29411 | ||
| + | [6,] 91.79562 | ||
| + | > tail(a,20) | ||
| + | [,1] | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | [100000000, | ||
| + | > m.a <- mean(a) | ||
| + | > sse.a <- sum((a-m.a)^2) | ||
| + | > n.a <- length(a) | ||
| + | > df.a <- n.a - 1 | ||
| + | > | ||
| + | > sse.a / n.a | ||
| + | [1] 100 | ||
| + | > sse.a/ df.a | ||
| + | [1] 100 | ||
| + | > | ||
| + | </ | ||
| + | </ | ||
| 더 자세한 것은 [[:why n-1]] 참조. \\ | 더 자세한 것은 [[:why n-1]] 참조. \\ | ||
variance.1773185173.txt.gz · Last modified: by hkimscil
