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 [2025/09/09 21:10] – [output] hkimscilr:sampling_distribution [2025/09/10 08:43] (current) – [qnorm] hkimscil
Line 313: Line 313:
 </code> </code>
 ====== output ====== ====== output ======
 +<WRAP group> 
 +<WRAP column half>
 <code> <code>
- 
- 
 > rm(list=ls()) > rm(list=ls())
 > rnorm2 <- function(n,mean,sd){  > rnorm2 <- function(n,mean,sd){ 
 +   mean+sd*scale(rnorm(n))  +   mean+sd*scale(rnorm(n)) 
-+ } 
- 
-> se <- function(sample) { 
-+   sd(sample)/sqrt(length(sample)) 
 + } + }
  
Line 330: Line 325:
 + } + }
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +....
 +필요한 펑션 
 +  * rnorm2는 평균과 표준편차값을 (mean, sd) 갖는 n 개의 샘플을 랜덤하게 추출하는 것
 +  * ss는 sum of square 값을 구하는 펑션 
 +  * ss는 ss/n-1 의 variance 혹은 mean square값을 구할 때의 분자부분으로
 +  * error 혹은 residual의 제곱의 합이라고 부를 수 있다 ([[:variance]] 참조)
 +
 +</WRAP>
 +</WRAP>
 +
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > ################################ > ################################
 > N.p <- 1000000 > N.p <- 1000000
Line 353: Line 364:
 [1,]  100 [1,]  100
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +  * rnorm2 펑션을 이용하여 p1과 p2 모집단을 구한다.
 +  * 각 모집단의 평균은 100과 120이고, 표준편차는 모두 10이다. 
 +  * 모집단의 크기는 각각 백만이다. 
 +
 +</WRAP>
 +</WRAP>
 +===== pnorm =====
 +모집단 P1의 히스토그램 (histgram of P1)
 +{{:r:pasted:20250910-065721.png}}
 +
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > hist(p1, breaks=100, col=rgb(1,1,1,1)) > hist(p1, breaks=100, col=rgb(1,1,1,1))
 > abline(v=mean(p1),lwd=2) > abline(v=mean(p1),lwd=2)
Line 395: Line 422:
 > pnorm(2)-pnorm(-2) > pnorm(2)-pnorm(-2)
 [1] 0.9544997 [1] 0.9544997
-> pnorm(3)-pnorm(3) +> pnorm(3)-pnorm(-3) 
-[1] 0+[1] 0.9973002
  
 > 1-pnorm(-2)*2 > 1-pnorm(-2)*2
Line 408: Line 435:
 [1] 0.9544997 [1] 0.9544997
  
 +>
 +>
 +>
 +
 +>
 +>
 +>
 +</code>
 +</WRAP>
 +<WRAP column half>
 +....
 +pnorm 펑션 
 +  * 위의 histogram에서 검정색의 선은 p1의 standard deviation값인 10씩 좌우로 그린것 (90과 110)
 +  * 붉은 색선은 80, 120
 +  * 녹색선은 70, 130 선을 말한다
 +  * 고등학교 때, Normal distribution (정상분포의) 경우 
 +  * 평균을 중심으로 위아래로 SD값 하나씩 간 간격의 probability는 (면적은) 68%
 +  * 두 개씩 간 값은 95%
 +  * 세 개씩 간 값은 99%  라고 배웠다. 
 +  * pnorm은 percentage를 구하는 R의 명령어 
 +
 +  * pnorm(m.p1+sd.p1, m.p1, sd.p1) 은 
 +    * 정규분포곡선에서 
 +    * 평균값과 표준편차값을 더한 값 (100+10=110) 을 기준으로 
 +    * 왼쪽의 부분이 몇 퍼센트인가를 구해주는 명령어 
 +    * m.p1+sd.p1은 110이므로 
 +    * 오른 쪽 검정색을 기준으로 왼쪽의 퍼센티지를 묻는 것
 +    * 답은 0.8413447
 +    * pnorm(m.p1-sd.p1, m.p1, sd.p1) - pnorm(90, 100, 10)이므로 
 +    * 왼쪽 검정색 선을 기준으로 왼쪽의 퍼센티지를 묻는 것
 +    * 답은 0.1586553
 +    * 전자에서 후자를 빼 준 값은 두개의 검정색 선의 안쪽 면적으로 묻는 것
 +    * 답은 0.6826895
 +  * 이 0.6826895 이 우리가 배운 68%
 +  * 그렇다면 두 개씩 간 면적은 (붉은 색 선의 안쪽 부분)
 +    * pnorm(m.p1+2*sd.p1, m.p1, sd.p1) - pnorm(m.p1-2*sd.p1, m.p1, sd.p1) = 0.9544997
 +    * 0.9544997 혹은 95.44997% 
 +    * 이것이 우리가 배운 95%
 +  * 마찬가지로 녹색선 가운데 부분은 pnorm(m.p1+3*sd.p1, m.p1, sd.p1) - pnorm(m.p1-3*sd.p1, m.p1, sd.p1) 로 구할 수 있는데 답은
 +  * 0.9973002
 +
 +  * pnorm명령어는 pnorm(score, mean, sd)와 같이 사용하여 퍼센티지값을 구할 수 있는데 
 +  * pnorm(score)만으로 구하면, mean과 sd가 각각 0, 1을 default값으로 하고 생략이 된 것
 +  * 위의 p1 모집단도 두번째 방법으로 구해 볼 수 있는데, 그렇게 하기 위해서 
 +  * p1을 표준점수화 하면 됨 
 +  * 표준점수화 한다는 뜻은 p1집합의 평균과 표준편차 값을 0과 1로 만든다는 것 
 +  * p1 모든 원소를 표준점수화하는 것은 각 점수와 평균점수 간의 차이에 sd가 몇개나 들어가는 지 구하는 것. 즉, 
 +    * $ \dfrac {(\text{score} - \text{m.p1})} {\text{sd.p1}} $ 
 +    * 100점은 표준점수로 0이 된다 $ (100-100) / 10 = 0 $
 +    * 110점은 1
 +    * 90점은 -1
 +    * 115점은 1.5
 +    * 130점은 30/10 = 3 점으로 계산될 수 있다. 
 +  * 그렇다면 위의 pnorm(m.p1+sd.p1, m.p1, sd.p1)은 pnorm(1)과 같은 것 
 +  * pnorm(110, 100, 10) 이고 이 때 110점은 표준점수로 1 이다. 
 +  * 따라서 위의 68%, 95%, 99%는 pnorm(1)-pnorm(-1)과 같이 구할 수 있다.
 +
 +</WRAP>
 +</WRAP>
 +===== z score, 표준점수 =====
 +
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > zscore <- (120-100)/10 > zscore <- (120-100)/10
 > pnorm(zscore)-pnorm(-zscore) > pnorm(zscore)-pnorm(-zscore)
Line 414: Line 505:
 [1] 2 [1] 2
  
-# reminder. +
 > pnorm(-1) > pnorm(-1)
 [1] 0.1586553 [1] 0.1586553
Line 430: Line 521:
 [1] 0.03593032 [1] 0.03593032
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +....
 +</WRAP>
 +</WRAP>
 +
 +{{:r:pasted:20250910-073339.png}}
 +
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > z.p1 <- (p1-mean(p1))/sd(p1) > z.p1 <- (p1-mean(p1))/sd(p1)
 > mean(z.p1) > mean(z.p1)
Line 446: Line 549:
  
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +....
 +p1 모집단의 모든 원소를 표준점수화 하기 
 +  * z.p1 <- (p1-mean(p1))/sd(p1) 
 +  * 평균과 표준편차는 각각 0, 1이 된다 (mean(z.p1), sd(z.p1))
 +  * 그렇다면 이 표준점수화한 분포에서 -1.8과 1.8 사이를 제외한 바깥 쪽 부분의 면적은 (probability는) 
 +  * 1-(p(1.8)-p(-1.8))과 같이 구할 수 있다.
 +  * 답은 약 7% 정도
 +</WRAP>
 +</WRAP>
 +
 +
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > pnorm(1)-pnorm(-1) > pnorm(1)-pnorm(-1)
 [1] 0.6826895 [1] 0.6826895
Line 459: Line 579:
 [1] 0.9973002 [1] 0.9973002
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +....
 +</WRAP>
 +</WRAP>
 +
 +===== qnorm =====
 +{{:r:pasted:20250910-075030.png}}
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > # > #
 > hist(p1, breaks=100, col=rgb(1,1,1,1)) > hist(p1, breaks=100, col=rgb(1,1,1,1))
Line 509: Line 641:
  
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +....
 +  * qnorm는 pnorm의 반대값을 구하는 명령어
 +  * 히스토그램에서 검정 색 부분의 바깥 쪽 부분은 32%이고 왼 쪽의 것은 이것의 반인 16% 이다. 
 +  * 이 16% 에 해당하는 표준점수가 무엇인가를 묻는 질문이 ''qnorm(.32/2)'' 혹은 ''qnorm(.32/2, 0, 1)''
 +  * 원점수일 경우에 대입해서 물어본다면 ''qnorm(.32/2, 100, 10)''과 같이 하면 된다 (혹은 qnorm(.32/2, m.p1, sd.p1) ). 
 +  * 위의 명령어에 대한 답은 우리가 가장 쉽게 이해한 방법에 의하면 
 +  * -1 혹은 90 이렇게 될 것이다. 
 +  * 하지만 위에서 봤던 것처럼 pnorm(1)-pnorm(-1)이 정확히 
 +  * 0.6826895 인 것처럼, 우리가 이해한 방법은 정확한 값을 구해주지는 않는다. 
 +  * 마찬가지로 qnorm(0.05/2)와 qnorm(1-(0.05/2))는 
 +  * 표준편차 값인 1이 왼쪽으로 두개 내려가고 오른 쪽으로 두개 올라간 값의 quotient값을 구하는 것이므로 
 +  * 즉, 위의 histogram에서 붉은 색 부분의 바깥 쪽 면적에 해당하는 점수를 물어보는 것이므로 
 +  * -2와 2라고 대답해도 되지만 정확한 답은 아래와 같다 (1.96). 
 +<code>
 +> qnorm(0.05/2)
 +[1] -1.959964
 +> qnorm(1-(0.05/2))
 +[1] 1.959964
 +
 +</code>
 +  * 이에 해당하는 원점수 (p1 원소에 해당하는) 값은 80과 120이 아니라 아래와 같을 것이다. 
 +<code>
 +> qnorm(0.05/2, 100, 10)
 +[1] 80.40036
 +> qnorm(1-(0.05/2), 100, 10)
 +[1] 119.5996
 +
 +</code>
 +</WRAP>
 +</WRAP>
 +
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > ################################ > ################################
 > hist(p1, breaks=50, col = rgb(1, 0, 0, 0.5), > hist(p1, breaks=50, col = rgb(1, 0, 0, 0.5),
Line 516: Line 685:
 > abline(v=mean(p2), col="violet", lwd=3) > abline(v=mean(p2), col="violet", lwd=3)
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +</WRAP>
 +</WRAP>
 +
 +===== distribution of sample means =====
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > s.size <- 10 > s.size <- 10
  
Line 530: Line 709:
 [1]  99.64273 107.15516 103.81192 103.12311 105.88372 [1]  99.64273 107.15516 103.81192 103.12311 105.88372
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +....
 +  * s.size는 10으로 우선 고정하고 
 +  * 한 샘플을 취하여 그 평균값을 means.temp 메모리에 add한다 (저장하는 것이 아니라 means.temp에 붙힌다). 
 +  * 그 다음 샘플의 평균을 다시 means.temp에 저장한다
 +  * 그 다음 . . . 모두 5번을 하고 출력을 해본다
 +
 +</WRAP>
 +</WRAP>
 +
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > iter <- 1000000 > iter <- 1000000
 > # means <- c() > # means <- c()
Line 548: Line 742:
 > abline(v=mean(means), col="red", lwd=2) > abline(v=mean(means), col="red", lwd=2)
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +....
 +</WRAP>
 +</WRAP>
 +{{:r:pasted:20250910-080946.png}}
 +
 +
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > # now we want to get sd of this distribution > # now we want to get sd of this distribution
 > lo1 <- mean(means)-se.s > lo1 <- mean(means)-se.s
Line 565: Line 771:
 +        lwd=2) +        lwd=2)
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +....
 +  * 위 백만개의 샘플평균이 모인 집합의 히스토그램을 그리고 
 +  * 그 집합의 표준편차값을 수직선으로 표시하기 위해서 
 +  * mean(means) +- se.s 와 같은 방법을 쓴 후 그래프로 그린다. 
 +  * 아래에서 선 하나씩의 길이는 means 집합의 (distribution of sample means)
 +  * 표준편차값이다. 
 +  * 이 표준편차 값을 위에서 sd(means)로 구한 후에 se.s로 저장한 적이 있다. 
 +  * 그리고 그 값은 3.161886 이었다. 
 +<code>
 +> se.s
 +[1] 3.161886
 +</code>
 +
 +
 +</WRAP>
 +</WRAP>
 +{{:r:pasted:20250910-081358.png}}
 +
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > # meanwhile . . . . > # meanwhile . . . .
 > se.s > se.s
Line 575: Line 805:
  
 > # sd of sample means (sd(means)) > # sd of sample means (sd(means))
-> # is sqrt(var(s1)/s.size) or  
-> # sd(s1) / sqrt(s.size)  
 > # = se.s > # = se.s
  
Line 631: Line 859:
  
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +....
 +  * 그런데 이 값은 (se.s = 3.161886)
 +  * se.z 를 구하는 방법과 거의 같은 값을 갖는다 3.162278
 +<code>
 +> se.z <- sqrt(var(p1)/s.size)
 +> se.z <- c(se.z)
 +> se.z
 +[1] 3.162278
 +</code>
 +  * 사실, 우리가 백만 번의 샘플을 취해서 구한 means 집합의 평균과 표준편차 값은 
 +  * 만약에 백만 번이 아니라 무한 대로 더 큰 숫자를 사용한다고 하면
 +  * 위의 se.z 값을 구하는 식의 값을 갖게 된다. 이것을 말로 풀어서 설명하면
 +  * 샘플평균들의 집합에서 표준편차 값은 
 +  * 원래 모집단의 분산값을 샘플사이즈로 나누어준 값에 제곱근을 씌워서 구할 수 있다이다. 
 +
 +  * 즉, 샘플평균을 모은 집합의 분산값은 그 샘플이 추출된 원래 population의 분산값을 샘플크기로 (sample size) 나누어 준 값이다. 
 +  * 즉, '' var(means) = var(p1) / s.size ''
 +  * 따라서, ''std(means) = sqrt(var(p1) / s.size) ''
 +  * 더하여 그 샘플평균 집합의 평균 값은 population의 평균값이 된다 
 +  * 즉, '' mean(means) = mean(p1) ''
 +
 +  * 따라서 lo, hi에 해당하는 means분포의 값을 mean(means) +- sd(means)로 구했었는데, 
 +  * 샘플평균의 분포를 무한대 번을 했다고 하면 사실 이 값은 
 +  * mean(p1) +- se.z 로 구하는 것이 정확할 것이다. 
 +  * 여기서 ''se.z = sqrt(var(p1)/s.size))''
 +  * loz1 - hiz1, loz2 - hiz2 값들은 이렇게 구한 값들이다. 
 +참고 
 +<code>
 +
 +> lo1 <- mean(means)-se.s
 +> hi1 <- mean(means)+se.s
 +> lo2 <- mean(means)-2*se.s
 +> hi2 <- mean(means)+2*se.s
 +> lo3 <- mean(means)-3*se.s
 +> hi3 <- mean(means)+3*se.s
 +
 +</code>
 +</WRAP>
 +</WRAP>
 +
 +
 +{{:r:pasted:20250910-083710.png}}
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > hist(means,  > hist(means, 
 +      xlim = c(mean(means)-5*sd(means), mean(means)+10*sd(means)),  +      xlim = c(mean(means)-5*sd(means), mean(means)+10*sd(means)), 
Line 654: Line 930:
 [1]  91 109 [1]  91 109
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +....
 +</WRAP>
 +</WRAP>
 +
 +{{:r:pasted:20250910-084125.png}}
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > m.sample.i.got <- mean(means)+ 1.5*sd(means) > m.sample.i.got <- mean(means)+ 1.5*sd(means)
 > m.sample.i.got > m.sample.i.got
Line 686: Line 973:
 [1] 104.7383 [1] 104.7383
  
 +</code>
 +</WRAP>
 +<WRAP column half>
 +....
 +  * 만약에 내가 한 샘플을 취해서 평균값을 살펴보니 
 +  * m.sample.i.got 값이었다고 하자 (104.7383).
 +  * 이 값보다 큰 값이거나 아니면 
 +  * 이 값에 해당하는 평균 반대편 값보다 작은 값이 값이 
 +  * 나올 확률은 무엇인가? 
 +  * 즉, 녹색선과 연두색 선 바깥 쪽 부분의 probability 값은?
 +  * 아래처럼 구해서 13.4% 정도가 된다
 +<code>
 +> 2 * pnorm(m.sample.i.got, mean(p1), sd(means), lower.tail = F)
 +[1] 0.1339882
 +</code>
 +</WRAP>
 +</WRAP>
 +
 +
 +<WRAP group>
 +<WRAP column half>
 +<code>
 > ### one more time  > ### one more time 
 > mean(p2) > mean(p2)
Line 721: Line 1030:
 [1] 1.51207e-10 [1] 1.51207e-10
  
-  
 </code> </code>
 +</WRAP>
 +<WRAP column half>
 +....
 +</WRAP>
 +</WRAP>
 +
 +
r/sampling_distribution.1757419846.txt.gz · Last modified: 2025/09/09 21:10 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki