# [R] Resampling to find Confidence intervals

Dieter Menne dieter.menne at menne-biomed.de
Tue Jan 4 17:24:02 CET 2011

```
Axolotl9250 wrote:
>
> ...
> resampled_ecoli = sample(ecoli, 500, replace=T)
> coefs = (coef(lm(MIC. ~ 1 + Challenge + Cleaner + Replicate,
> data=resampled_ecoli)))
> sd(coefs)
>
> ...
>
Below a simplified and self-consistent version of your code, and some
changes

Dieter

# resample
d = data.frame(x=rnorm(10))
d\$y = d\$x*3+rnorm(10,0.01)

# if you do this, you only get ONE bootstrap sample
d1 = d[sample(1:nrow(d),10,TRUE),]
d1.coef = coef(lm(y~x,data=d1))
d1.coef
# No error below, because you compute the sd of (Intercept) and slope
# but result is wrong!
sd(d1.coef)

# We have to do this over and over
# Check ?replicate for a more R-ish approach....
nsamples = 1000
allboot = NULL
for (i in 1:1000) {
d1 = d[sample(1:nrow(d),10,TRUE),]
d1.coef = coef(lm(y~x,data=d1))
allboot = rbind(allboot,d1.coef) # Not very efficient, preallocate!
}
head(allboot) # display first of nsamples lines
apply(allboot,2,mean) # Compute mean
apply(allboot,2,sd) # compute sd
# After you are sure you understood the above, you might try package boot.

--
View this message in context: http://r.789695.n4.nabble.com/Resampling-to-find-Confidence-intervals-tp3172867p3173846.html
Sent from the R help mailing list archive at Nabble.com.

```