# [R] Bootstrap standard errors

Rman ben.mahoney at tesco.net
Sat Mar 6 00:48:25 CET 2010

```I have this coding to help me work out the bootstrap standard errord for the
following array/matrix
dat <-
matrix(c(8,23,14,13,9,11,25,25,14,11,4,25,19,7,13,1,8,16,2,6,3,4,6,5,7),
nrow = 5, ncol=5, byrow=TRUE)

the function is:

mybootstrap <- function(dat, nr, nc, m)
{
# dat is a two dimentional array
# nr is the number of rows in the table
# nc is the number of columns in the table
# m is the number of bootstrap simulations
x <- dat[,1]
# Tries scored combinations (0-0, 0-1,...)
y <- dat[,2]
# Frequencies
n <- sum(y)
r <- nr * nc
v <- matrix(0., r, m)
prob <- y / sum(y)
z <- vector(mode = "numeric", length = r)
for (k in 1.:m){
y <-sample(x, n, replace = T, prob)
for (i in 1.:r){
z[i] <- length(y[y == x[i]])
}
w <- matrix(0., nr, nc)
for(i in 1:nr){
w[i, 1:nc] <- z[(nc * (i - 1) + 1) : (nc * i)]
}
rsum <- apply(w, 1., sum)
csum <- apply(w, 2., sum)
u <- matrix(0., nr, nc)
for(i in 1.:nr){
for (j in 1.:nc){
if(w[i,j] > 0.)
u[i,j] <- (w[i,j] * 100.* n) / (rsum[i] * csum[j])
}
}
a <- u[1, ]
for(i in 2:nr){
a <- c(a, u[i, ])
}
v[, k] <- a
}
stderr <- apply (v, 1., var)
return(sqrt(stderr))
}

mybootstrap(dat, 5, 5, 500)

there are no errors as such but the numbers produced are too small,
incorrect and are reroduced. I am expecting numbers such as 18 or 21 etc not
0.00004