User Tools

Site Tools


estimated_standard_deviation

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
estimated_standard_deviation [2026/03/10 05:31] – [실험적, R에서 시뮬레이션으로 이해] hkimscilestimated_standard_deviation [2026/03/11 01:56] (current) – [실험적, R에서 시뮬레이션으로 이해] hkimscil
Line 57: Line 57:
 ====== 실험적, R에서 시뮬레이션으로 이해 ====== ====== 실험적, R에서 시뮬레이션으로 이해 ======
 아래 output의 코멘트를 읽을 것 아래 output의 코멘트를 읽을 것
-<tabbed> +<tabbox rs01
-  * estimated_standard_deviation:rscript01 +<code> 
-  * *estimated_standard_deviation:rout01 +rm(list=ls())
-</tabbed>+
  
-===== output =====+rnorm2 <- function(n,mean,sd){  
 +  mean+sd*scale(rnorm(n))  
 +
 + 
 +# set.seed(191) 
 +nx <- 1000 
 +mx <- 50 
 +sdx <- mx * 0.1 
 +sdx  # 5 
 +x <- rnorm2(nx, mx, sdx) 
 +# x <- rnorm2(1000, 50, 5) 와 동일 
 + 
 +mean(x) 
 +sd(x) 
 +length(x) 
 +hist(x) 
 + 
 +x.span <- seq(from mean(x)-3*sd(x),  
 +              to mean(x)+3*sd(x),  
 +              by .1) 
 +x.span 
 + 
 +residuals <- function(x, v) { 
 +  return(x - v) 
 +
 + 
 +# sum of square residual 값을  
 +# 구하는 펑션 
 +ssr <- function(x, v) {  
 +  residuals <- (x - v) 
 +  return(sum(residuals^2)) 
 +
 + 
 +#  mean square residual 값을  
 +# 구하는 펑션 (mean square  
 +# residual variance) 
 +msr <- function(x, v) { 
 +  residuals <- (x - v) 
 +  return((mean(residuals^2))) 
 +
 + 
 +ssrs <- c() # sum of square residuals 
 +msrs <- c() # mean square residuals variance 
 +vs <- c() # the value of v in (x - v) 
 + 
 +# x.span의 값들을 v값으로 삼아 sum(x-x.span)^2 처럼 구하면 
 +# SS값을 구한 것이 된다. 우리가 배운 SS값은 x.span의 값으로  
 +# 샘플의 평균을 사용했을 때의 residual 값이다. x.span은  
 +# 샘플의 평균을 중심으로 여러가지 값을 사용하는 것을 가정한다. 
 + 
 +for (i in x.span) { 
 +  res.x <- residuals(x,i) 
 +  msr.x <- msr(x,i) 
 +  msrs <- append(msrs, msr.x) 
 +  vs <- append(vs, i) 
 +
 +# 아래 plot은 SS값들이나 (두번째는) MS값들을 v값이 변화함에  
 +# 따라서 (x.span의 범위에 따라서 변화) 어떻게 변화하는지  
 +# 구한 것 
 + 
 +plot(msrs) 
 + 
 +# v값이 x.span에 따라서 변화하여 대입되었을 때의 
 +# MS값들을 (msr 펑션으로 구한 mean square값) 
 +# 모아 놓은 값이 msrs 
 +msrs  
 + 
 +# 아래는 위에서 계산한 msr 값들을 저장한 msrs값들 중에서 최소값이  
 +# 되는 것을 찾은 것. 우리는 이것이 샘플의 평균임을 안다.  
 +min(msrs) 
 +# 최소값일 때의 위치 (msrs에서 몇번째인지) 
 +min.pos.msrs <- which(msrs == min(msrs)) 
 +min.pos.msrs 
 +# msr 최소값이 구해졌을 때 사용된 v값 
 +vs[min.pos.msrs] 
 +</code> 
 +<tabbox ro01>
 <WRAP group> <WRAP group>
-<WRAP half column>+<WRAP column 55%>
 <code> <code>
 > rm(list=ls()) > rm(list=ls())
Line 91: Line 166:
 </code> </code>
 </WRAP> </WRAP>
-<WRAP half column>+<WRAP column 35%>
 SS = sum(x-mean(x))^2 인데, mean(x)을 즉, x집합의 평균을, x 원소값을 예측하는데 (빼는데) 사용하면 SS값이 최소값이 된다고 하였다. 이것을 R에서 simulation으로 알아보기 위해서 mean(x) 대신에 다른 숫자들을 넣어보려고 한다. 이를 v라고 하면 sum(x-v)^2이라는 SS값들을 구해서 비교하려는 것이다. 대입할 숫자들은 (v) mean(x) +- 3 sd(x) 를 범위로 하고, 그 범위의 시작 숫자에서 (시작은 mean(x)-3sd(x)가 된다) 0.1씩 증가시키면서 대입하고, 각 숫자마다 (처음 숫자는 35, 다음 숫자는 35.1 . . . ) SS값을 구해서 저장하여 그것을 그래프로 그려보고 최소값이 어떤 것인지 보는 것이 진행하려는 작업이다.  SS = sum(x-mean(x))^2 인데, mean(x)을 즉, x집합의 평균을, x 원소값을 예측하는데 (빼는데) 사용하면 SS값이 최소값이 된다고 하였다. 이것을 R에서 simulation으로 알아보기 위해서 mean(x) 대신에 다른 숫자들을 넣어보려고 한다. 이를 v라고 하면 sum(x-v)^2이라는 SS값들을 구해서 비교하려는 것이다. 대입할 숫자들은 (v) mean(x) +- 3 sd(x) 를 범위로 하고, 그 범위의 시작 숫자에서 (시작은 mean(x)-3sd(x)가 된다) 0.1씩 증가시키면서 대입하고, 각 숫자마다 (처음 숫자는 35, 다음 숫자는 35.1 . . . ) SS값을 구해서 저장하여 그것을 그래프로 그려보고 최소값이 어떤 것인지 보는 것이 진행하려는 작업이다. 
  
Line 100: Line 175:
  
 <WRAP group> <WRAP group>
-<WRAP half column>+<WRAP column 55%>
 <code> <code>
 > x.span <- seq(from = mean(x)-3*sd(x),  > x.span <- seq(from = mean(x)-3*sd(x), 
Line 142: Line 217:
 </code> </code>
 </WRAP> </WRAP>
-<WRAP half column>+<WRAP column 35%>
 x-mean(x) = residual = error  x-mean(x) = residual = error 
 sum(residual^2) = SS (sum of square) sum(residual^2) = SS (sum of square)
Line 154: Line 229:
  
 <WRAP group> <WRAP group>
-<WRAP half column>+<WRAP column 55%>
 <code> <code>
  
Line 178: Line 253:
 </code> </code>
 </WRAP> </WRAP>
-<WRAP half column>+<WRAP column 35%>
   * 이 후 쓸 function들. (x-v) = residual이라고 부르니까 이 residual을 모으는 function   * 이 후 쓸 function들. (x-v) = residual이라고 부르니까 이 residual을 모으는 function
   * function ssr = x 집합과 v값 (x.span의 한 숫자)를 인수를 주었을 때 구할 수 있는 Sum of Square값들 (실제로는 사용하지 않는다. 대신 msr 펑션으로 MS값을 구한다).   * function ssr = x 집합과 v값 (x.span의 한 숫자)를 인수를 주었을 때 구할 수 있는 Sum of Square값들 (실제로는 사용하지 않는다. 대신 msr 펑션으로 MS값을 구한다).
Line 186: Line 261:
  
 <WRAP group> <WRAP group>
-<WRAP half column>+<WRAP column 55%>
 <code> <code>
 > ssrs <- c() # sum of square residuals > ssrs <- c() # sum of square residuals
Line 211: Line 286:
 </code> </code>
 </WRAP> </WRAP>
-<WRAP half column>+ 
 +<WRAP column 35%>
 comment comment
   * x.span의 처음값인 35.1을 넣어서 (x-v)를 구한 후    * x.span의 처음값인 35.1을 넣어서 (x-v)를 구한 후 
Line 228: Line 304:
  
 <WRAP group> <WRAP group>
-<WRAP half column>+<WRAP column 55%>
 <code> <code>
 > # v값이 x.span에 따라서 변화하여 대입되었을 때의 > # v값이 x.span에 따라서 변화하여 대입되었을 때의
Line 298: Line 374:
 </code> </code>
 </WRAP> </WRAP>
-<WRAP half column>+<WRAP column 35%>
 comment comment
   * msrs값에 저장된 msr값들 (mean square residual값들) 중에서   * msrs값에 저장된 msr값들 (mean square residual값들) 중에서
Line 308: Line 384:
  
 <WRAP group> <WRAP group>
-<WRAP half column>+<WRAP column 55%>
 <code> <code>
 > # 아래는 위에서 계산한 msr 값들을 저장한 msrs값들 중에서 최소값이 되는 것을 찾은  > # 아래는 위에서 계산한 msr 값들을 저장한 msrs값들 중에서 최소값이 되는 것을 찾은 
Line 334: Line 410:
 </code> </code>
 </WRAP> </WRAP>
-<WRAP half column>+<WRAP column 35%>
 comment comment
   * msrs 의 min(msrs)값을 찾는다 24.975   * msrs 의 min(msrs)값을 찾는다 24.975
Line 346: Line 422:
 </WRAP> </WRAP>
  
 +
 +</tabbox>
 +
 +
 +===== 미분으로 기울기가 최소값이 될 때를 찾는 법 =====
  
 다음으로 MS값을 구하는 식인  다음으로 MS값을 구하는 식인 
estimated_standard_deviation.1773120669.txt.gz · Last modified: by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki