User Tools

Site Tools


b:head_first_statistics:estimating_populations_and_samples

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
b:head_first_statistics:estimating_populations_and_samples [2024/11/06 08:05] – [Expectation of samples proportions (Ps)] hkimscilb:head_first_statistics:estimating_populations_and_samples [2024/11/11 08:23] (current) – [Recap] hkimscil
Line 231: Line 231:
  
 </code> </code>
-이 샘플의 평균?+그런데 교재는 이 이항분포를 비율로 (proportion) 생각하므로,은 방식으로 Red gumball의 비율로 바꿔서 보면
 <code> <code>
-set.seed(101+# 아래처럼 n으로 (100개의 검볼이 총 숫자이므로)  
-> mean(rbinom(100, 100, 1/4)+> # 나눠주면 비율을 구할 수 있다 
-[1] 25.28+> proportions.of.rg <- numbers.of.red.gumball/
 +> ps.k <- proportions.of.rg 
 +> ps.k 
 +   [1] 0.18 0.27 0.27 0.22 0.23 0.26 0.23 0.26 0.25 0.30 0.27 0.28 0.32 0.24 0.26 
 +  [16] 0.29 0.22 0.24 0.18 0.27 0.33 0.22 0.27 0.31 0.29 0.19 0.24 0.24 0.27 0.24 
 +  [31] 0.23 0.21 0.21 0.25 0.31 0.21 0.29 0.16 0.31 0.24 0.24 0.28 0.23 0.24 0.22 
 +  [46] 0.19 0.31 0.28 0.20 0.19 0.24 0.27 0.28 0.24 0.28 0.27 0.25 0.27 0.26 0.29 
 +  [61] 0.29 0.26 0.36 0.29 0.27 0.16 0.23 0.30 0.32 0.22 0.32 0.26 0.29 0.29 0.22 
 +  [76] 0.18 0.22 0.27 0.33 0.27 0.28 0.28 0.34 0.15 0.32 0.23 0.24 0.20 0.16 0.27 
 +  [91] 0.31 0.27 0.21 0.22 0.29 0.24 0.22 0.19 0.18 0.20 0.17 0.24 0.30 0.27 0.23 
 + [106] 0.19 0.17 0.28 0.37 0.20 0.18 0.26 0.30 0.30 0.34 0.30 0.25 0.23 0.26 0.24 
 + [121] 0.20 0.19 0.25 0.22 0.29 0.25 0.25 0.27 0.19 0.27 0.23 0.22 0.23 0.26 0.25 
 + [136] 0.25 0.32 0.25 0.27 0.32 0.22 0.32 0.23 0.30 0.21 0.25 0.27 0.17 0.24 0.21 
 + [151] 0.24 0.26 0.33 0.20 0.22 0.26 0.28 0.25 0.30 0.33 0.27 0.30 0.26 0.23 0.39 
 + [166] 0.23 0.31 0.18 0.26 0.27 0.34 0.25 0.28 0.31 0.35 0.28 0.29 0.32 0.27 0.31 
 + [181] 0.28 0.25 0.22 0.23 0.15 0.22 0.20 0.26 0.21 0.22 0.16 0.23 0.22 0.31 0.24 
 + [196] 0.27 0.31 0.21 0.24 0.26 0.26 0.22 0.22 0.34 0.19 0.30 0.22 0.28 0.25 0.24 
 + [211] 0.29 0.25 0.25 0.16 0.27 0.23 0.25 0.32 0.18 0.22 0.25 0.25 0.24 0.24 0.21 
 + [226] 0.32 0.20 0.28 0.29 0.22 0.23 0.22 0.25 0.21 0.27 0.22 0.24 0.29 0.24 0.22 
 + [241] 0.30 0.22 0.21 0.17 0.25 0.23 0.21 0.27 0.22 0.22 0.25 0.22 0.29 0.24 0.26 
 + [256] 0.32 0.28 0.20 0.22 0.22 0.27 0.26 0.22 0.24 0.31 0.18 0.27 0.29 0.28 0.17 
 + [271] 0.27 0.33 0.23 0.33 0.25 0.32 0.26 0.23 0.19 0.21 0.20 0.23 0.15 0.19 0.23 
 + [286] 0.26 0.27 0.28 0.23 0.24 0.35 0.27 0.30 0.23 0.25 0.24 0.31 0.23 0.20 0.22 
 + [301] 0.22 0.26 0.21 0.22 0.26 0.28 0.26 0.23 0.21 0.13 0.29 0.27 0.21 0.34 0.28 
 + [316] 0.24 0.19 0.26 0.27 0.25 0.23 0.27 0.25 0.19 0.29 0.18 0.28 0.21 0.27 0.28 
 + [331] 0.28 0.22 0.22 0.20 0.20 0.25 0.27 0.17 0.16 0.27 0.32 0.23 0.18 0.28 0.31 
 + [346] 0.29 0.21 0.27 0.27 0.30 0.21 0.25 0.20 0.25 0.26 0.30 0.26 0.21 0.15 0.29 
 + [361] 0.22 0.21 0.16 0.25 0.25 0.27 0.26 0.27 0.28 0.21 0.27 0.24 0.25 0.24 0.39 
 + [376] 0.24 0.28 0.33 0.20 0.26 0.24 0.27 0.20 0.31 0.27 0.27 0.20 0.21 0.31 0.25 
 + [391] 0.22 0.22 0.30 0.34 0.27 0.23 0.21 0.25 0.20 0.24 0.29 0.19 0.30 0.27 0.33 
 + [406] 0.22 0.29 0.30 0.22 0.29 0.26 0.24 0.18 0.26 0.36 0.26 0.23 0.24 0.22 0.32 
 + [421] 0.33 0.16 0.24 0.28 0.24 0.25 0.29 0.31 0.28 0.28 0.29 0.26 0.24 0.25 0.28 
 + [436] 0.27 0.24 0.31 0.25 0.31 0.33 0.26 0.26 0.24 0.33 0.28 0.20 0.23 0.22 0.23 
 + [451] 0.22 0.30 0.25 0.25 0.23 0.27 0.27 0.23 0.24 0.28 0.24 0.28 0.23 0.22 0.26 
 + [466] 0.30 0.26 0.27 0.21 0.23 0.23 0.27 0.26 0.23 0.25 0.30 0.25 0.24 0.22 0.28 
 + [481] 0.18 0.23 0.18 0.16 0.27 0.26 0.18 0.25 0.27 0.22 0.20 0.19 0.27 0.25 0.31 
 + [496] 0.27 0.22 0.21 0.24 0.24 0.26 0.23 0.23 0.29 0.27 0.23 0.25 0.20 0.21 0.21 
 + [511] 0.27 0.25 0.22 0.29 0.28 0.21 0.21 0.24 0.27 0.24 0.28 0.19 0.14 0.32 0.27 
 + [526] 0.22 0.24 0.35 0.26 0.28 0.28 0.26 0.25 0.25 0.19 0.26 0.24 0.20 0.19 0.28 
 + [541] 0.25 0.25 0.24 0.21 0.30 0.27 0.30 0.20 0.22 0.26 0.31 0.26 0.20 0.20 0.27 
 + [556] 0.25 0.26 0.18 0.30 0.20 0.29 0.16 0.38 0.26 0.22 0.29 0.22 0.30 0.26 0.19 
 + [571] 0.27 0.24 0.29 0.29 0.25 0.19 0.23 0.24 0.24 0.23 0.25 0.31 0.18 0.24 0.33 
 + [586] 0.27 0.25 0.27 0.29 0.28 0.24 0.23 0.24 0.28 0.20 0.24 0.30 0.24 0.21 0.20 
 + [601] 0.25 0.24 0.24 0.30 0.22 0.26 0.23 0.25 0.21 0.21 0.24 0.27 0.18 0.20 0.22 
 + [616] 0.30 0.25 0.23 0.27 0.26 0.23 0.23 0.28 0.18 0.29 0.27 0.25 0.32 0.26 0.15 
 + [631] 0.22 0.24 0.21 0.34 0.23 0.23 0.18 0.29 0.23 0.27 0.28 0.23 0.37 0.20 0.17 
 + [646] 0.25 0.11 0.21 0.28 0.22 0.28 0.25 0.22 0.25 0.21 0.18 0.20 0.27 0.30 0.24 
 + [661] 0.28 0.23 0.30 0.31 0.24 0.23 0.37 0.19 0.27 0.32 0.25 0.27 0.28 0.29 0.22 
 + [676] 0.26 0.26 0.20 0.22 0.25 0.24 0.19 0.27 0.21 0.32 0.27 0.31 0.29 0.24 0.24 
 + [691] 0.29 0.29 0.25 0.22 0.34 0.23 0.18 0.33 0.18 0.23 0.24 0.26 0.18 0.20 0.23 
 + [706] 0.30 0.28 0.26 0.34 0.17 0.33 0.30 0.32 0.30 0.22 0.28 0.19 0.19 0.23 0.23 
 + [721] 0.20 0.23 0.21 0.31 0.30 0.20 0.24 0.23 0.23 0.28 0.26 0.34 0.27 0.33 0.31 
 + [736] 0.20 0.25 0.12 0.25 0.20 0.20 0.25 0.27 0.24 0.29 0.26 0.22 0.30 0.26 0.28 
 + [751] 0.28 0.27 0.23 0.18 0.28 0.22 0.21 0.27 0.22 0.26 0.21 0.22 0.27 0.24 0.19 
 + [766] 0.27 0.29 0.37 0.30 0.27 0.25 0.30 0.19 0.22 0.22 0.28 0.32 0.22 0.33 0.26 
 + [781] 0.20 0.31 0.23 0.24 0.24 0.26 0.24 0.30 0.17 0.21 0.20 0.22 0.20 0.17 0.24 
 + [796] 0.22 0.24 0.23 0.23 0.24 0.23 0.16 0.16 0.17 0.23 0.27 0.29 0.26 0.16 0.21 
 + [811] 0.34 0.19 0.25 0.25 0.28 0.32 0.17 0.22 0.26 0.23 0.23 0.24 0.22 0.22 0.14 
 + [826] 0.30 0.25 0.33 0.26 0.25 0.31 0.28 0.30 0.21 0.19 0.17 0.19 0.21 0.16 0.21 
 + [841] 0.26 0.21 0.29 0.27 0.31 0.32 0.19 0.22 0.24 0.25 0.25 0.24 0.23 0.30 0.21 
 + [856] 0.22 0.19 0.20 0.21 0.20 0.21 0.28 0.19 0.26 0.28 0.26 0.29 0.28 0.26 0.21 
 + [871] 0.31 0.32 0.31 0.22 0.23 0.25 0.27 0.26 0.22 0.27 0.30 0.24 0.25 0.23 0.27 
 + [886] 0.25 0.24 0.24 0.30 0.29 0.26 0.32 0.29 0.23 0.24 0.20 0.26 0.26 0.22 0.22 
 + [901] 0.19 0.23 0.33 0.18 0.27 0.26 0.28 0.18 0.26 0.24 0.24 0.26 0.27 0.17 0.26 
 + [916] 0.23 0.27 0.25 0.32 0.20 0.22 0.23 0.25 0.25 0.24 0.28 0.20 0.19 0.22 0.20 
 + [931] 0.22 0.24 0.17 0.19 0.22 0.17 0.19 0.27 0.27 0.28 0.29 0.18 0.24 0.30 0.26 
 + [946] 0.34 0.26 0.24 0.25 0.24 0.29 0.28 0.29 0.23 0.24 0.21 0.24 0.23 0.23 0.29 
 + [961] 0.19 0.29 0.30 0.33 0.25 0.30 0.32 0.23 0.30 0.27 0.17 0.20 0.21 0.24 0.36 
 + [976] 0.21 0.26 0.30 0.26 0.25 0.22 0.21 0.38 0.21 0.24 0.21 0.25 0.21 0.32 0.20 
 + [991] 0.29 0.24 0.19 0.21 0.32 0.26 0.27 0.18 0.21 0.20 
 +
 +</code> 
 +위의 비율의 기댓값을 (평균을구한다는 것이 교재가 하는 이야기 
 +<code> 
 +mean.ps.k <- mean(ps.k) 
 +> mean.ps.k 
 +[1] 0.24893
  
 </code> </code>
-그런데 위의 이야기는 샘플의 숫자가 100 이 아닌 무한대라면 나타나는 평균을 말한다. 실제 무한대를 새뮬래션해 볼 수는 없으므로 k를 1억으로 들어 평균을 구해보면 아래와 같이 25가 된다. +위의 결과를 histogram으로 그려보면  
 +<code> 
 +hist(ps.k) 
 +</code> 
 +이는 평균이 0.25에 (p값에) 근접하는 값이 된다. 교재의 p값이 되는 것은 k가 무한대로 큰 값을 가질 때의 야기.  
 +아래는 k를 1000번이 아닌 1000000번 (백번일 때의 이야기). 평균비율이 0.25가 된다. 
 <code> <code>
 > set.seed(101) > set.seed(101)
-mean(rbinom(100000000, 1001/4)) +k <- 1000000 
-[1] 25.0001+> n <- 100 
 +> p <- 1/4 
 +> q <- 1-p 
 +> numbers.of.red.gumball <- rbinom(k, n, p) 
 +> # 아래처럼 n으로 (100개의 검볼이 총 숫자이므로 
 +> # 나눠주면 비율을 구할 수 있다 
 +> proportions.of.rg <- numbers.of.red.gumball/
 +> ps.k <- proportions.of.rg 
 +> mean.ps.k <- mean(ps.k) 
 +> mean.ps.k 
 +[1] 0.2500217
  
 </code> </code>
-위의 이야기를 visual하게 생각해보면  +{{:b:head_first_statistics:pasted:20241106-081710.png}} 
-<code> +
-set.seed(101) +
-k <- 10000 +
-n <- 100 +
-p <- 1/4 +
-q <- 1-p +
-numbers.of.red.gumball <- rbinom(k, n, p) +
-head(numbers.of.red.gumball) +
-proportions.of.rg <- numbers.of.red.gumball/+
-head(proportions.of.rg) +
-mean(proportions.of.rg) +
-hist(proportions.of.rg) +
-</code> +
-{{:b:head_first_statistics:pasted:20241104-080847.png}}+
  
 ^ references  ^ ^ references  ^
Line 268: Line 346:
  
 ===== What about variance ===== ===== What about variance =====
 +그렇다면 위의 분포에서의 분산값은 얼마가 될까? 그리고 표준편차값은 얼마가 될까?
 \begin{eqnarray*} \begin{eqnarray*}
-Var(\text{probability of sample proportions}& = & Var(P_{s}) \\+\text{Variance of sample proportions} & = & Var(P_{s}) \\
 & = & Var\left(\frac{X}{n}\right) \\ & = & Var\left(\frac{X}{n}\right) \\
 & = & \frac {Var(X)}{n^{2}} \\ & = & \frac {Var(X)}{n^{2}} \\
 & = & \frac {npq}{n^{2}} \\ & = & \frac {npq}{n^{2}} \\
-& = & \frac {pq}{n}  +& = & \frac {pq}{n} \\
-\end{eqnarray*} +
- +
-\begin{eqnarray*}+
 \text{Standard deviation of sample proportions} & = & \sqrt{\frac{pq}{n}} \\ \text{Standard deviation of sample proportions} & = & \sqrt{\frac{pq}{n}} \\
 & = & \text{Standard error of sample proportions}  & = & \text{Standard error of sample proportions} 
 \end{eqnarray*} \end{eqnarray*}
 +우리는 위의 Standard deviation of sample proportions를 특별하게 standard error라고 부른다. 
  
-이를 종합하면, Sample proportions 들에 대한 기대값과 분산은 각각 아래와 같다 (그림 참조).+종합하면, Sample proportions 들에 대한 기대값과 분산은 각각 아래와 같다 (그림 참조).
  
 $$E(P_{s}) = p \qquad\qquad\qquad Var(P_{s}) = \displaystyle \frac{pq}{n}$$ $$E(P_{s}) = p \qquad\qquad\qquad Var(P_{s}) = \displaystyle \frac{pq}{n}$$
Line 289: Line 366:
  
 continuity correction: $$\pm \frac{1}{2n}$$ continuity correction: $$\pm \frac{1}{2n}$$
 +
 +R에서의 simulation을 계속해서 보면 
 +<code>
 +> # variance?
 +> var.cal <- var(ps.k)
 +> var.value <- (p*q)/n
 +> var.cal
 +[1] 0.001869001
 +> var.value
 +[1] 0.001875
 +
 +> # standard deviation 
 +> sd.cal <- sqrt(var.cal)
 +> sd.value <- sqrt(var.value)
 +> sd.cal
 +[1] 0.04323195
 +> sd.value 
 +[1] 0.04330127
 +> se <- sd.value 
 +> # 우리는 standard deviation of sample
 +> # proportions 를 standard error라고 
 +> # 부른다
 +
 +</code>
 +위의 se는 standard deviation의 일종이므로 그 특성을 갖는다 (68, 95, 99%). 따라서 Red gumball의 비율이 1/4임을 알고 있을 때, n=100개의 gumball을 샘플링하면 (한번), red gumball의 비율은 p를 (0.25) 중심으로 위아래도 2*se 범위의 값이 나올 확률이 95%임을 안다는 것이 된다. 위에서 계산해보면; 
 +
 +<code>
 +# 위의 histogram 에서 mean 값은 이론적으로
 +p
 +# standard deviation값은 
 +se
 +
 +# 우리는 평균값에서 +- 2*sd.cal 구간이 95%인줄 안다. 
 +se2 <- se * 2
 +# 즉, 아래 구간이 
 +lower <- p-se2
 +upper <- p+se2
 +lower
 +upper
 +
 +hist(ps.k)
 +abline(v=lower, col=2, lwd=2)
 +abline(v=upper, col=2, lwd=2)
 +
 +</code>
 +즉 아래의 그래프에서 
 +{{:b:head_first_statistics:pasted:20241106-084520.png}}
 +lower: 0.1633975와 (16.33975%) upper: 0.3366025 사이에서 (33.66025%) red gumaball의 비율이 나올 확률이 95%라는 이야기. 
 +
 +그렇다면 만약에 30% 이상이 red gumball일 확률은 무엇이라는 질문이라면 
 +우리는 X ~ B(100, 1/4)에서 도출되는 
 +X ~ N(p, se) 에서 P(X>_0.3)을 구하는 질문이므로 
 +1-pnorm(0.295, p, se) 가 답이 되겠다. 
 +1-pnorm(0.295, p, se) 
 +[1] 0.1493488
  
 ===== Exercise ===== ===== Exercise =====
Line 490: Line 622:
  
 </code> </code>
 +====== Recap ====== 
 +Distribution of **Sample** <fc #ff0000>**P**</fc>roportion<fc #ff0000>**s**</fc>, <fc #ff0000>$Ps$</fc>, 
 +when sampling n entities (repeatedly) from a population whose proportion is p. 
 +\begin{eqnarray*} 
 +Ps & \sim & N(p,  \frac{pq}{n}) \\ 
 +\text{hence, } \\ 
 +\text{standard deviation of} \\  
 +\text{sample proportions} & = & \sqrt{\frac{pq}{n}} 
 +\end{eqnarray*} 
 +Distribution of **Sample** <fc #ff0000>Means, $\overline{X}$</fc>  
 +when sampling a sample whose size is n from a population whose mean is $\mu$ and variance is $\sigma^2$. 
 +\begin{eqnarray*} 
 +\overline{X} & \sim & N(\mu,  \frac{\sigma^2}{n}) \\ 
 +\text{hence, } \\ 
 +\text{standard deviation of} \\  
 +\text{sample means} & = &  \sqrt{\frac{\sigma^2}{n}} \\ 
 +& = &  \frac{\sigma}{\sqrt{n}} 
 +\end{eqnarray*}
b/head_first_statistics/estimating_populations_and_samples.1730847942.txt.gz · Last modified: 2024/11/06 08:05 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki