숙제 2번은 답은 다음과 같이 구할 수 있습니다. 여기에 소개하진 않았지만 lapply()를 사용할 수도 있다.
library(tidyverse)
## -- Attaching packages -------------------------------------------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.0.0 √ purrr 0.3.2
## √ tibble 2.1.1 √ dplyr 0.8.3
## √ tidyr 1.0.0 √ stringr 1.4.0
## √ readr 1.3.1 √ forcats 0.4.0
## -- Conflicts ----------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
n <- 1000
s <- 20
df <- matrix(data = 0, nrow = n, ncol = s)
for (i in 1:20) {
set.seed(1)
x <- rchisq(n, df = i) %>%
as.vector()
df[,i] <- x
}
colnames(df) <- 1:s
df %>%
data.frame() %>%
gather(df, value) %>%
ggplot(aes(x = value, color = df)) +
geom_density() +
theme(legend.position = "none") +
xlab("Chi-square random variables")
\(\chi^2\) 분포는 표준정규분포를 따르는 확률변수를 제곱한 이후에 더한 확률변수이기 때문에 기본적으로 합의 구조를 갖게 된다. 따라서 합한 수가 늘어날수록(\(n\): 자유도), 정규분포와 유사해진다.