Take the 2-minute tour ×
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It's 100% free, no registration required.

Please let me know my mistake. I expected bootstrapping a standard normal data produces a mean of zero and standard deviation of 1, but my example does not. rm(list=ls())

Create random data with a mean of zero and standard deviation of one

V1 <- rnorm(9, mean=0, sd=1); V2 <- rnorm(9, mean=0, sd=1); V3 <- rnorm(9, mean=0, sd=1)
V4 <- rnorm(9, mean=0, sd=1); V5 <- rnorm(9, mean=0, sd=1); V6 <- rnorm(9, mean=0, sd=1)
V7 <- rnorm(9, mean=0, sd=1); V8 <- rnorm(9, mean=0, sd=1)

charDataDiff <- as.data.frame(cbind(V1,V2,V3,V4,V5,V6,V7,V8));charDataDiff

rename the columns

colnames(charDataDiff) <- c("s380","s390","s400","s410","s420","s430","s440","s450"); charDataDiff

patchId      <- c("C", "B", "A", "A", "B", "C", "A", "B", "C"); patchId

provide an ordering variable

idColor      <- c("C", "B", "A")

Put all the pieces together to create the data

charDataDiff <- cbind.data.frame(patchId,charDataDiff); charDataDiff

Split the data by patchId

patchSpectrumBiasSplit <- split(charDataDiff, charDataDiff$patchId)

Order the data by idColor

patchSpectrumBiasOrdered <- patchSpectrumBiasSplit[idColor]

Remove the first column

patchSpectrumBias <- lapply(patchSpectrumBiasOrdered, "[", 2:9)

Bootstrap

sampleOne   <- function(x) x[sample(seq_len(nrow(x)), replace = TRUE), ]
sampleBoot  <- function(x, n) replicate(n, sampleOne(x), simplify = FALSE)
applyMedian <- function(l) do.call(rbind, lapply(l, apply, 2, median))
k <- lapply(lapply(patchSpectrumBias, sampleBoot, n = 1000), applyMedian)

Calculate the mean, I expected the mean to be zero following the law of large numbers.

bootMeansBias <- do.call(rbind, lapply(k, apply, 2, mean));bootMeansBias

Calculate the standard deviation, I expected the standard deviation to be one

standardError <- do.call(rbind, lapply(k, apply, 2, sd));standardError
share|improve this question
1  
Crossposted at stackoverflow.com/questions/13532997/… –  MattBagg Nov 23 '12 at 18:06
    
@Matt The cross-post now has been closed and at least one statistically interesting answer has been offered here, so this looks like the right place for this thread to remain. Thanks for flagging it! –  whuber Nov 23 '12 at 20:54
add comment

2 Answers

You could never expect the mean to be exactly 0 if you were using the law of large numbers. You're not using large numbers in your sample, only your bootstrap. Your bootstrap will build up a representation out of the sample. You're just highlighting a problem in bootstrapping, that the results you get depend on the sample. So, a sample of 9 is very unlikely to have a mean of 0 and can deviate quite a bit from it. A sample of 3 is even worse. Bootstrapping won't fix this.

share|improve this answer
add comment

Thank all, I modified my code and realized my mistake. the new code is as follows:

rm(list=ls())
n <-2000 

Create random data with a mean of zero and standard deviation of one

a <- lapply(1:8, function(x)cbind(rnorm(n)))
b <- as.data.frame(do.call(cbind,a ))

rename the columns

names(b) <- c("s380","s390","s400","s410","s420","s430","s440","s450")

Create a second random data

c <- lapply(1:8, function(x)cbind(rnorm(n)))

rename the columns

d <- as.data.frame(do.call(cbind, c))

names(d) <- c("s380","s390","s400","s410","s420","s430","s440","s450"); str(d)

Create a list of the above data

e <- list(b,d)

Bootstrap

sampleOne   <- function(x) x[sample(seq_len(nrow(x)), replace = TRUE), ]
sampleBoot  <- function(x, nn) replicate(n, sampleOne(x), simplify = FALSE)
applyMean <- function(l) do.call(rbind, lapply(l, apply, 2, mean))
k <- lapply(lapply(e, sampleBoot, nn = 100), applyMean)

calculate the mean, it is almost zero

bootMeansBias <- round(do.call(rbind, lapply(k, apply, 2, mean)),3); bootMeansBias

calculate the standard deviation, it is almost 1

standardError <- round(do.call(rbind, lapply(k, apply, 2, sd)),3);standardError
standardDeviation <- standardError*sqrt(n); standardDeviation

Thank you all

Ragy Isaac

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.