Skip to content

Instantly share code, notes, and snippets.

@jpablo
Created April 29, 2011 06:28
Show Gist options
  • Save jpablo/947944 to your computer and use it in GitHub Desktop.
Save jpablo/947944 to your computer and use it in GitHub Desktop.
bootstrap in R
# Los datos (ejemplo)
d = c(2,4,6,6,7,11,13,13,14,15,19,23,24,27,28,28,28,30,31,43)
####
dm <- mean(d)
ds <- sd(d)
n <- length(d)
T <- function() {
m <- sample(d, n, 1)
(mean(m) - dm)*sqrt(n) / sd(m)
}
B <- 999
L = round(0.025 * B)
U = round(0.975 * B)
# El intervalo de 95 % confianza para el bootstrap
b <- sort(replicate(B, T()))
c( dm - b[U] * ds / sqrt(n), dm - b[L] * ds / sqrt(n) )
# El mismo intervalo asumiendo normalidad
tn <- abs(qt(.05 / 2, n-1))
c( dm - tn * ds / sqrt(n), dm + tn * ds / sqrt(n) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment