set.seed(12345) # 이 명령어는 난수생성기의 초기값을 설정하며, 아무 양수를 넣고 실행 후 아래 명령어를 실행하면, 매번 같은 수가 나옴
sample(x=1:10, size=5, replace=T) # 1부터 10의 수에서 크기가 5인 표본을 복원추출로 뽑음
[1] 3 10 8 10 8
Kim Jae Sook
January 30, 2024
[1] 1.2240818 0.3598138 0.4007715 0.1106827 -0.5558411 1.7869131
[7] 0.4978505 -1.9666172 0.7013559 -0.4727914
# 난수발생에 따라 평균값이 xlim=c(0.3, 1.7)을 벗어나거나,
# 도수가 40보다 많아 ylim=c(0, 40)의 범위를 벗어나는 경우가 있을 수 있다.
par(mfrow=c(2,2))
column_1 <- c()
for (i in 1:100) column_1[i] <- mean(runif(5,0,2)) # 균등분포에서 난수발생하기
hist(column_1, border="white", col="blue", main="mean of 5 samples",
xlim=c(0.3,1.7), ylim=c(0,40), xlab="Class", ylab="Frequency")
column_2 <- c()
for (i in 1:100) column_2[i] <- mean(runif(10,0,2))
hist(column_2, border="white", col="blue", main="mean of 10 samples",
xlim=c(0.3,1.7), ylim=c(0,40), xlab="Class", ylab="Frequency")
column_3 <- c()
for (i in 1:100) column_3[i] <- mean(runif(30,0,2))
hist(column_3, border="white", col="blue", main="mean of 30 samples",
xlim=c(0.3,1.7), ylim=c(0,40), xlab="Class", ylab="Frequency")
column_4 <- c()
for (i in 1:100) column_4[i] <- mean(runif(100,0,2))
hist(column_4, border="white", col="blue", main="mean of 100 samples",
xlim=c(0.3,1.7), ylim=c(0,40), xlab="Class", ylab="Frequency")
중심극한정리에 의하면 표본평균
stat_1 <- c(mean(column_1), var(column_1), median(column_1),
min(column_1), max(column_1))
stat_2 <- c(mean(column_2), var(column_2), median(column_2),
min(column_2), max(column_2))
stat_3 <- c(mean(column_3), var(column_3), median(column_3),
min(column_3), max(column_3))
stat_4 <- c(mean(column_4), var(column_4), median(column_4),
min(column_4), max(column_4))
SimStat <- cbind(stat_1, stat_2, stat_3, stat_4)
rownames(SimStat) <- c("mean", "var", "median", "min", "max")
colnames(SimStat) <- c("n=5", "n=10", "n=30", "n=100")
SimStat
n=5 n=10 n=30 n=100
mean 1.04403650 0.99194277 0.99585782 0.993736371
var 0.04754242 0.02959242 0.01132904 0.002992766
median 1.04478925 0.98403686 0.98856144 0.990584588
min 0.43296607 0.50810527 0.71512453 0.877360070
max 1.51308187 1.39665089 1.28565056 1.141150215
표준정규분포
모분산
즉,
[1] 0.3956322
[1] 0.3989423
[1] 0.5
[1] 0.8373457
[1] 0.1626543
[1] 1
[1] -1.697261
[1] -2.042272
[1] 1.697261
[1] 2.042272
Warning: package 'TeachingDemos' was built under R version 4.3.3
One Sample z-test
data: x
z = -37.5, n = 9.00000, Std. Dev. = 0.40000, Std. Dev. of the sample
mean = 0.13333, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 9
95 percent confidence interval:
3.738671 4.261329
sample estimates:
mean of x
4
One Sample t-test
data: x
t = 25.298, df = 8, p-value = 6.384e-09
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
3.635389 4.364611
sample estimates:
mean of x
4
[1] 15.36584
[1] 18.17231
[1] 17.80529