About/R

Bootstrap 예제

2018.02.13 14:43

2018 02 교육세션 자료 및 방법론 결과 비교


## 2. Bootstrap


boot.R

LASSO example.R


# 평균 
set.seed(1000)

D = runif(100)
origin.mean = mean(D)
b = 1000
n = length(D)

boot.samples = matrix(sample(D, size = b * n, replace = TRUE), b, n)
boot.statistics = apply(boot.samples, 1, mean)

boot.se = sd(boot.statistics)

ci = (1.96 * boot.se)
boot.ci = round(origin.mean, 1) + c(-1, 1) * ci # original mean 을 사용함에 주의!

boot.mean = function(x,b,binwidth=NULL) {
	n = length(x)
	boot.samples = matrix( sample(x,size=n*b,replace=TRUE), b, n)
	boot.statistics = apply(boot.samples,1,mean)
	se = sd(boot.statistics)
	
	interval = mean(x) + c(-1,1)*1.96*se
	return( list(boot.statistics = boot.statistics, interval=interval, se=se) )
}

my.boot1 = boot.mean(D, b = 1000)
hist(my.boot1[[1]])

set.seed(1000)
library(boot)
my.mean = function(x, indices) {
return( mean( x[indices] ) )
}
my.boot = boot(D, my.mean, 1000)


티스토리 툴바