신뢰구간은 통계학에서 모수 추정에 사용되는 중요한 개념입니다. 신뢰구간은 표본에서 계산된 추정량이 실제 모수를 포함할 확률을 나타내는 구간을 의미합니다. 신뢰구간은 모수 추정의 불확실성을 고려하여 모수 추정에 대한 더 정확한 정보를 제공합니다. 대표적으로 모집단 평균과 모집단 비율에 대한 신뢰구간을 계산하는 방법을 시각적으로 확인할 수 있습니다.
1 Shiny 앱
2 코딩
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
library(ggplot2)
# 설정값 지정
n_samples <- 100
sample_size <- 50
conf_level <- 0.95
# 모집단 비율에 대한 신뢰구간 계산 및 시각화
pop_prop <- 0.4
n_population <- 100000
population <- rbernoulli(n_population, p = pop_prop)
true_prop <- mean(population)
sample_props <- vector('numeric', n_samples)
upper_cis_prop <- vector('numeric', n_samples)
lower_cis_prop <- vector('numeric', n_samples)
contains_true_prop <- vector('numeric', n_samples)
for (i in 1:n_samples) {
sample <- sample(population, sample_size, replace = FALSE)
sample_prop <- mean(sample)
sample_std <- sqrt(sample_prop * (1 - sample_prop) / sample_size)
z_value <- qnorm((1 - conf_level) / 2, lower.tail = FALSE)
margin_of_error <- z_value * sample_std
sample_props[i] <- sample_prop
upper_cis_prop[i] <- sample_prop + margin_of_error
lower_cis_prop[i] <- sample_prop - margin_of_error
contains_true_prop[i] <- ifelse(true_prop >= lower_cis_prop[i] & true_prop <= upper_cis_prop[i], 1, 0)
}
prop_df <- data.frame(
sample_prop = sample_props,
upper_ci = upper_cis_prop,
lower_ci = lower_cis_prop,
contains_true_prop = contains_true_prop
)
prop_plot <- ggplot(prop_df, aes(x = 1:n_samples, y = sample_prop)) +
geom_point() +
geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci, color = factor(contains_true_prop))) +
geom_hline(yintercept = true_prop, linetype = "dashed", color = "red") +
labs(title = "모집단 비율에 대한 신뢰구간",
x = "표본 번호",
y = "표본 비율") +
scale_color_manual(values = c("0" = "gray", "1" = "blue"),
labels = c("포함 안 함", "포함"),
name = "참값 포함 여부") +
theme_minimal()
print(prop_plot)
cat("모집단 비율에 대한 신뢰구간 포함률:", mean(contains_true_prop), "\n")
# 모집단 평균에 대한 신뢰구간 계산 및 시각화
pop_mean <- 10
pop_sd <- 2
n_population <- 100000
population <- rnorm(n_population, mean = pop_mean, sd = pop_sd)
true_mean <- mean(population)
sample_means <- vector('numeric', n_samples)
upper_cis_mean <- vector('numeric', n_samples)
lower_cis_mean <- vector('numeric', n_samples)
contains_true_mean <- vector('numeric', n_samples)
for (i in 1:n_samples) {
sample <- sample(population, sample_size, replace = FALSE)
sample_mean <- mean(sample)
sample_std <- sd(sample) / sqrt(sample_size)
t_value <- qt((1 - conf_level) / 2, df = sample_size - 1, lower.tail = FALSE)
margin_of_error <- t_value * sample_std
sample_means[i] <- sample_mean
upper_cis_mean[i] <- sample_mean + margin_of_error
lower_cis_mean[i] <- sample_mean - margin_of_error
contains_true_mean[i] <- ifelse(true_mean >= lower_cis_mean[i] & true_mean <= upper_cis_mean[i], 1, 0)
}
mean_df <- data.frame(
sample_mean = sample_means,
upper_ci = upper_cis_mean,
lower_ci = lower_cis_mean,
contains_true_mean = contains_true_mean
)
mean_plot <- ggplot(mean_df, aes(x = 1:n_samples, y = sample_mean)) +
geom_point() +
geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci, color = factor(contains_true_mean))) +
geom_hline(yintercept = true_mean, linetype = "dashed", color = "red") +
labs(title = "모집단 평균에 대한 신뢰구간",
x = "표본 번호",
y = "표본 평균") +
scale_color_manual(values = c("0" = "gray", "1" = "blue"),
labels = c("포함 안 함", "포함"),
name = "참값 포함") +
theme_minimal()
print(mean_plot)
cat("모집단 평균에 대한 신뢰구간 포함률:", mean(contains_true_mean), "\n")