User Tools

Site Tools


r:sampling_distribution

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
r:sampling_distribution [2026/03/22 23:06] hkimscilr:sampling_distribution [2026/03/24 10:17] (current) hkimscil
Line 62: Line 62:
 </WRAP> </WRAP>
 </WRAP> </WRAP>
-pnorm +===== pnorm ===== 
 + 
 <WRAP group> <WRAP group>
 <WRAP column half> <WRAP column half>
Line 182: Line 183:
 </WRAP> </WRAP>
 </WRAP> </WRAP>
-z score, 표준점수 +===== z score, 표준점수 ===== 
 <WRAP group> <WRAP group>
 <WRAP column half> <WRAP column half>
Line 260: Line 262:
  
 ---- ----
-qnorm + 
 +===== qnorm =====
 <WRAP group> <WRAP group>
 <WRAP column half> <WRAP column half>
Line 369: Line 372:
 </WRAP> </WRAP>
 </WRAP> </WRAP>
- 
 ---- ----
-distribution of sample means + 
 +<code> 
 +pnorm(110, 100, 10, lower.tail = F)  
 +pnorm((110-100)/10, lower.tail = F) 
 +pnorm(1, lower.tail = F) 
 +1-pnorm(1) 
 +pnorm(-1) 
 + 
 +1-(pnorm(-1)*2) 
 +</code> 
 +===== distribution of sample means ===== 
 + 
 아래는 모두 같은 의미이다.  아래는 모두 같은 의미이다. 
   * distribution of sample means   * distribution of sample means
Line 465: Line 478:
 <code> <code>
 > mean(means) > mean(means)
-[1] 99.99912+[1] 100.0025
 > sd(means) > sd(means)
-[1] 3.164792+[1] 3.167479
 > var(means) > var(means)
-[1] 10.01591+[1] 10.03292
 </code> </code>
   * 위와 같다. 이 값은 샘플평균들의 평균이 무엇인가와 (mean(means))   * 위와 같다. 이 값은 샘플평균들의 평균이 무엇인가와 (mean(means))
Line 697: Line 710:
 </WRAP> </WRAP>
 </WRAP> </WRAP>
 +
 +===== Hypothesis test =====
  
 <WRAP group> <WRAP group>
Line 748: Line 763:
 > # 즉 n = ssize 크기의 샘플을 구했을 때  > # 즉 n = ssize 크기의 샘플을 구했을 때 
 > # 104.7489 점 오른쪽 그리고, > # 104.7489 점 오른쪽 그리고,
-> # 이에 대응하는 95.25113 지점 왼쪽의 +> # 이에 대응하는 95.27504 지점 왼쪽의 
 > # 평균이 나오는 probability는  > # 평균이 나오는 probability는 
-> # 0.1331684 +> # 0.1351325 
  
 </code> </code>
Line 757: Line 772:
 .... ....
   * 만약에 내가 한 샘플을 취해서 평균값을 살펴보니    * 만약에 내가 한 샘플을 취해서 평균값을 살펴보니 
-  * m.sample.i.got 값이었다고 하자 (104.7383).+  * m.sample.i.got 값이었다고 하자 (104.725).
   * 이 값보다 큰 값이거나 아니면    * 이 값보다 큰 값이거나 아니면 
   * 이 값에 해당하는 평균 반대편 값보다 작은 값이 값이    * 이 값에 해당하는 평균 반대편 값보다 작은 값이 값이 
   * 나올 확률은 무엇인가?    * 나올 확률은 무엇인가? 
   * 즉, 녹색선과 연두색 선 바깥 쪽 부분의 probability 값은?   * 즉, 녹색선과 연두색 선 바깥 쪽 부분의 probability 값은?
-  * 아래처럼 구해서 13.4% 정도가 된다+  * 아래처럼 구해서 13.5% 정도가 된다
   * 즉, 모집단 p1에서    * 즉, 모집단 p1에서 
   * 무작위로 샘플링을 하여 (see [[:sampling]] and probability sampling)   * 무작위로 샘플링을 하여 (see [[:sampling]] and probability sampling)
   * s.size의 (10) 샘플을 취했는데 그 샘플의 평균이    * s.size의 (10) 샘플을 취했는데 그 샘플의 평균이 
-  * 104.7383 점보다 크거나 혹은  +  * 104.725 점보다 크거나 혹은  
-  * 95.26505 점보다 작을 확률은 13.4% 이다. +  * 95.27504 점보다 작을 확률은 13.5% 이다. 
  
 <code> <code>
Line 777: Line 792:
  
 <code> <code>
-2 * pnorm(m.sample.i.gotmean(p1), sd(means), lower.tail = F) +> (m.sample.i.got mean(p1))/se.z 
-[1] 0.13371 +         [,1] 
-> (m.sample.i.got - mean(p1))/sd(means) +[1,] 1.494165 
-[1] 1.499631 +> z.score <- (m.sample.i.got - mean(p1))/se.z
-> z.score <- (m.sample.i.got - mean(p1))/sd(means)+
 > pnorm(z.score, lower.tail = FALSE) > pnorm(z.score, lower.tail = FALSE)
-[1] 0.06685502+           [,1
 +[1,] 0.06756624
 > 2 * pnorm(z.score, lower.tail = FALSE) > 2 * pnorm(z.score, lower.tail = FALSE)
-[1] 0.13371 +          [,1
-+[1,] 0.1351325
 </code> </code>
   * 위처럼 z score를 구해서 pnorm으로 probability를 보는 것을 z-test 라고 한다.    * 위처럼 z score를 구해서 pnorm으로 probability를 보는 것을 z-test 라고 한다. 
Line 792: Line 807:
 </WRAP> </WRAP>
 ---- ----
-Last one . . . Important +
 <WRAP group> <WRAP group>
 <WRAP column half> <WRAP column half>
Line 875: Line 890:
  
 이 probability level이 어느정도나 작아야 이 샘플이 p1에서 나오지 않고 p2에서 나왔다고 판단할 수 있을까? 관습적으로 5/100를 기준으로 해서 이 범위보다 작게 되면 p1의 모집단에서 나온 샘플이 아닌 것으로 판단하게 된다. 이 논리에 따라서, (평균을 107.9235 값을 갖는)이 샘플은, p1에서 나오기 힘든 샘플이라고 판단 된다.  이 probability level이 어느정도나 작아야 이 샘플이 p1에서 나오지 않고 p2에서 나왔다고 판단할 수 있을까? 관습적으로 5/100를 기준으로 해서 이 범위보다 작게 되면 p1의 모집단에서 나온 샘플이 아닌 것으로 판단하게 된다. 이 논리에 따라서, (평균을 107.9235 값을 갖는)이 샘플은, p1에서 나오기 힘든 샘플이라고 판단 된다. 
 +
 +  * R에는 z-test 펑션이 없다. 현실에서는 전체 모집단의 평균을 알고 있는 경우는 많지만 표준편차까지 알고 있는 경우는 많지 않다. 그래서 많이 쓰이지 않는 편이다. 
 +  * 모집단의 평균과 표준편차를 알고 있다고 하면, 우리는 R에서 z test를 하는 절차는 
 +  * n = n 일 경우의 샘플링분포에서 se 를 구한다
 +    * se = sigma / sqrt(n)
 +  * 테스트할 점수의 z score를 구한다.
 +    * diff = test.score - mean.of.population
 +    * z.score = diff / se 
 +  * z score 보다 큰 점수나 -z score 보다 작은 점수가 나올 확률를 위의 샘플링 분포에 구한다.
 +    * p.value = 2 * pnorm(z.score, lower.tail=F)
 +  * z.score와 p.vallue로 테스트점수가 모집단에서 나왔는지 나올 수 없는지 (나오기 어려운지를) 판단한다.
 {{:r:pasted:20250910-135442.png?400}} {{:r:pasted:20250910-135442.png?400}}
  
Line 884: Line 910:
 <WRAP column half> <WRAP column half>
 <code> <code>
 +> ###################################################
 +> # 다른 케이스 --> t-test 케이스
 > # what if we do not know the sd of  > # what if we do not know the sd of 
 > # population? > # population?
Line 892: Line 920:
 > # of normal distribution (z). > # of normal distribution (z).
 > diff > diff
-[1] 7.95152+[1] 7.923527
 > se.t <- sqrt(var(sample.i.got.p2)/s.size) > se.t <- sqrt(var(sample.i.got.p2)/s.size)
 > t.cal <- diff/se.t > t.cal <- diff/se.t
 > t.cal > t.cal
-[1] 2.417997+[1] 2.914599
 > pt(t.cal, df=s.size-1, lower.tail = F) > pt(t.cal, df=s.size-1, lower.tail = F)
-[1] 0.01936882+[1] 0.008591086
 > pt(t.cal, df=s.size-1, lower.tail = F)*2 > pt(t.cal, df=s.size-1, lower.tail = F)*2
-[1] 0.03873764+[1] 0.01718217
 > prob.t <- pt(t.cal, df=s.size-1, lower.tail = F)*2 > prob.t <- pt(t.cal, df=s.size-1, lower.tail = F)*2
  
Line 906: Line 934:
 > z.cal > z.cal
          [,1]          [,1]
-[1,] 2.514492+[1,] 2.505639
 > se.z > se.z
          [,1]          [,1]
Line 912: Line 940:
 > prob.z > prob.z
            [,1]            [,1]
-[1,] 0.01192042+[1,] 0.01222302
  
 > t.cal > t.cal
-[1] 2.417997+[1] 2.914599
 > se.t > se.t
-[1] 3.288473+[1] 2.718566
 > prob.t > prob.t
-[1] 0.03873764+[1] 0.01718217
  
 > t.test(sample.i.got.p2, mu=mean(p1)) > t.test(sample.i.got.p2, mu=mean(p1))
Line 926: Line 954:
  
 data:  sample.i.got.p2 data:  sample.i.got.p2
-t = 2.418, df = 9, p-value = 0.03874+t = 2.9146, df = 9, p-value = 0.01718
 alternative hypothesis: true mean is not equal to 100 alternative hypothesis: true mean is not equal to 100
 95 percent confidence interval: 95 percent confidence interval:
- 100.5125 115.3906+ 101.7737 114.0733
 sample estimates: sample estimates:
 mean of x  mean of x 
- 107.9515 + 107.9235  
 + 
 + 
 </code> </code>
 </WRAP> </WRAP>
 <WRAP half column> <WRAP half column>
 [[:t-test]] 중에서 2번째 케이스 [[:t-test]] 중에서 2번째 케이스
 +  * 모집단의 평균은 알지만 표준편차 정보는 없는 경우이다. 
 +  * 똑같은 논리로 생각을 해서 모집단의 샘플링분포를 (distribution of sample means) 머리에 두고
 +  * se값을 구한다. 이 때의 se 값은 
 +    * ''se.cal <- sqrt( s^2/n )''값으로 구한다. 
 +    * sigma 대신에 s를 사용한 것에 주목
 +  * z.score에 해당하는 t.score를 구한다. 
 +    * 테스트점수와 모집단 평균의 차이를 구한 후 (''diff = test.score - mean(p1)'')
 +    * se.cal 값으로 나눠준다. 
 +    * ''t.cal <- diff / se.cal''
 +  * t.cal 값 이상, 반대편 점수의 이하가 나올 확률을 구한다. 
 +    * 이 때, 모집단의 표준편차를 사용해서 z.score를 구하지 않았으므로 
 +    * 그리고, 이 probability는 샘플의 크기 n에 영향을 받으므로 n의 크기에 따라서 변화하는 probability distribution을 사용한다. 
 +      * p.value <- pt(t.score, df=n-1, lower.tail=F) * 2
 +  * t.cal과 p.value로 테스트점수가 나올 가능성을 판단하여 가설을 기각하거나 채택한다 (검증한다). 
 </WRAP> </WRAP>
 </WRAP> </WRAP>
  
 <WRAP group> <WRAP group>
 +[[:types of error]]
 +[[:hypothesis testing]]
 +
 <WRAP half column> <WRAP half column>
 <code> <code>
->  +> # 그렇다면 
-> # 그렇다면 모집단에 대한 파라미터 없이+
 > # 두 샘플의 평균과 표준편차만 알고 > # 두 샘플의 평균과 표준편차만 알고
 > # 모집단들의 파라미터는 모를 때는? > # 모집단들의 파라미터는 모를 때는?
Line 955: Line 1001:
 > s1 <- sample(p1, s.size, replace = T) > s1 <- sample(p1, s.size, replace = T)
 > s2 <- sample(p2, s.size, replace = T) > s2 <- sample(p2, s.size, replace = T)
 +> s1
 + [1] 100.46855  97.67673  94.48868  97.51996  94.28300 113.78623  95.44617
 + [8]  92.91108  97.46601 100.66815 113.94452  81.20774  93.77529  93.64388
 +[15] 111.43702 104.34787  98.82403 100.93909 111.08048 110.12275  94.68502
 +[22] 100.14734  80.23712  88.02110 118.89602  99.46530 106.92325  97.13274
 +[29] 106.40479 107.44831
 +> s2
 + [1] 120.28360 105.80657 116.28974 106.63910 108.66632 107.51868 123.66436
 + [8]  96.06523 111.53770 108.78226 115.22628 104.60990 105.07035 114.90022
 +[15] 103.92860 105.06587 113.77416 108.07776 111.92824 112.01554 104.34549
 +[22] 100.51113  84.75464  96.36697 117.09381 116.29935 117.20402 109.61657
 +[29] 106.46650 105.86733
 +
 > mean(s1) > mean(s1)
-[1] 98.97605+[1] 100.1133
 > mean(s2) > mean(s2)
-[1] 110.8606+[1] 108.6125
 > sd(s1) > sd(s1)
-[1] 10.29997+[1] 9.132472
 > sd(s2) > sd(s2)
-[1] 12.28259+[1] 7.916677
  
 > ss(s1) > ss(s1)
-[1] 3076.589+[1] 2418.659
 > ss(s2) > ss(s2)
-[1] 4374.997+[1] 1817.54
 > df1 <- s.size - 1 > df1 <- s.size - 1
 > df2 <- s.size - 1 > df2 <- s.size - 1
Line 975: Line 1034:
 > t.ts.cal <- diff/se.tst > t.ts.cal <- diff/se.tst
 > t.ts.cal > t.ts.cal
-[1] 4.060845+[1] 3.851705
 > pt(t.ts.cal, df=df1+df2, lower.tail = F)*2 > pt(t.ts.cal, df=df1+df2, lower.tail = F)*2
-[1] 0.0001484894+[1] 0.0002954557
  
 > t.test(s2, s1, var.equal=T) > t.test(s2, s1, var.equal=T)
Line 984: Line 1043:
  
 data:  s2 and s1 data:  s2 and s1
-t = 4.0608, df = 58, p-value = 0.0001485+t = 3.8517, df = 58, p-value = 0.0002955
 alternative hypothesis: true difference in means is not equal to 0 alternative hypothesis: true difference in means is not equal to 0
 95 percent confidence interval: 95 percent confidence interval:
-  6.026262 17.742752+  4.082229 12.916309
 sample estimates: sample estimates:
 mean of x mean of y  mean of x mean of y 
-110.86056  98.97605 + 108.6125  100.1133 
  
- 
  
 </code> </code>
Line 1000: Line 1058:
 </WRAP> </WRAP>
 </WRAP> </WRAP>
-====== Hypothesis e.g ====== 
  
-<WRAP group> 
-위에서 p2의 parameter에 대해서 잘 모른다는 점에 주목하라. 그리고 아래 시나리오를 상상하라.  
-어느 한 모집단의 IQ 평균이 100 이고 표준편차가 10 임을 알고 있다. 확률과통계 교수는 머리가 좋아지는 약을 개발하여 이를 팔아보려고 하고 있다. 이를 위해서 확통교수는 25명의 학생에게 머리가 좋아지는 약을 복용하도록 한 후에 IQ를 측정하였다. 그런데, 그 IQ 평균이 105.45 이다. 이 점수를 가지고 약의 효과가 있는지 검증을 해보력고 한다.  
-<tabbox rs.eg01> 
-<code> 
-m.s <- 105.45 
-m.p <- 100 
-sd.p <- 10 
-v.p <- 100 
-s.sz <- 25 
-s <- rnorm2(s.sz, m.s, sd.p) 
-s 
-se <- sqrt(v.p/s.sz) 
-se 
-diff <- m.s - m.p  
-diff 
-t.cal <- diff / se 
-df.s <- s.sz-1 
-2*pt(t.cal, df=df.s,lower.tail = F) 
-t.test(s, mu=m.p) 
-</code> 
-<tabbox ro.eg01> 
-<code> 
-> m.s <- 105.45 
-> m.p <- 100 
-> sd.p <- 10 
-> v.p <- 100 
-> s.sz <- 25 
-> s <- rnorm2(s.sz, m.s, sd.p) 
-> s 
-           [,1] 
- [1,] 102.84774 
- [2,] 114.05839 
- [3,] 101.95736 
- [4,] 122.88205 
- [5,] 106.27718 
- [6,] 116.96923 
- [7,] 101.78039 
- [8, 81.37328 
- [9,] 112.28533 
-[10,] 108.26472 
-[11,] 100.31204 
-[12,]  89.84322 
-[13,] 109.29927 
-[14,] 116.19203 
-[15,]  98.35046 
-[16,] 105.84416 
-[17,] 104.34385 
-[18,] 119.76468 
-[19,] 104.44693 
-[20,]  93.43272 
-[21,]  95.42572 
-[22,] 107.99983 
-[23,] 119.88284 
-[24,]  95.36440 
-[25,] 107.05218 
-attr(,"scaled:center") 
-[1] -0.2689983 
-attr(,"scaled:scale") 
-[1] 0.7604765 
-> se <- sqrt(v.p/s.sz) 
-> se 
-[1] 2 
-> diff <- m.s - m.p  
-> diff 
-[1] 5.45 
-> t.cal <- diff / se 
-> df.s <- s.sz-1 
-> 2*pt(t.cal, df=df.s,lower.tail = F) 
-[1] 0.01180885 
-> t.test(s, mu=m.p) 
- 
- One Sample t-test 
- 
-data:  s 
-t = 2.725, df = 24, p-value = 0.01181 
-alternative hypothesis: true mean is not equal to 100 
-95 percent confidence interval: 
- 101.3222 109.5778 
-sample estimates: 
-mean of x  
-   105.45  
- 
- 
-</code> 
-</tabbox> 
-</WRAP> 
  
 ====== R script and output ====== ====== R script and output ======
Line 2388: Line 2358:
 </tabbox> </tabbox>
  
 +====== exercise.1 ======
 +위에서 p2의 parameter에 대해서 잘 모른다는 점에 주목하라. 그리고 아래 시나리오를 상상하라. 
 +
 +  - 어느 한 모집단의 IQ 평균이 100 이고 표준편차가 10 임을 알고 있다. 확률과통계 교수는 머리가 좋아지는 약을 개발하여 이를 팔아보려고 하고 있다. 이를 위해서 확통교수는 25명의 학생에게 머리가 좋아지는 약을 복용하도록 한 후에 IQ를 측정하였다. 그런데, 그 IQ 평균이 106.45 이다. 이 점수를 가지고 약의 효과가 있는지 검증을 해보력고 한다. 
 +  - 똑 같은 경우이지만 모집단의 평균을 100으로 추정하고 있지 표준편차는 모르는 상태이다. 그런데, 25명에게 약을 복용시키고 IQ를 측정하니 점수가 105.50이 나왔고, 표준편차는 9.4였다. 이 점수로 약의 효과가 있었는지 검증을 하려 한다. 
r/sampling_distribution.1774220807.txt.gz · Last modified: by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki