6

I am wondering how to draw these multiple bootstrap curves in R. The codes of mine is like

dat2 <- read.delim("bone.data", sep ="\t", header= TRUE)
y <- dat2[,4]
x <- dat2[,2]
plot(x,y,xlab="age",ylab="BMD",col=ifelse(dat2[,3]=="female","red","blue"))

The multiple Bootstrap Curves are like Fig 8.2 bottom left one in this book. ESL

enter image description here

And the data named Bone Mineral Density could be get from this website: data

The direct link to the file being: here

Florent
  • 12,310
  • 10
  • 49
  • 58
Rick Kim
  • 85
  • 1
  • 6
  • 4
    What multiple bootstrap curves? Please make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and your question clear. – mnel Nov 06 '12 at 04:45
  • No code in the book or at the book's website. Are you familiar with package:boot? – IRTFM Nov 06 '12 at 07:01

1 Answers1

6

You can plot a spline curve using smooth.spline and lines:

plot.spline = function(x, y, ...) {
    s = smooth.spline(x, y, cv=TRUE)
    lines(predict(s), ...)
}

So to perform bootstrapping, as per the instructions in the book, you sample random rows from the data with replacement, and call plot.spline on the resampled data:

bootstrap.curves = function(dat, nboot, ...) {
    for (i in 1:nboot) {
        subdata = dat[sample(NROW(dat), replace=TRUE), ]
        plot.spline(subdata$age, subdata$spnbmd, ...)
    }
}

You can thus use this function to run separate plots for male and female:

bootstrap.curves(dat2[dat2$gender == "female", ], 10, col="red")
bootstrap.curves(dat2[dat2$gender == "male", ], 10, col="blue")

End result:

enter image description here

Note: This code will produce a number of warnings (not errors) that look like:

1: In smooth.spline(x, y, cv = TRUE) :
  crossvalidation with non-unique 'x' values seems doubtful

This is because of the bootstrap resampling. smooth.spline uses cross validation to decide on the number of degrees of freedom to give a spline, but it prefers not to do so with duplicate x values (as there effectively always will be with bootstrap resampling). You could get around this by choosing your own number of degrees of freedom, but this is probably fine for this purpose.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • I see from testing that setting `cv=NA` in the `smooth.spline` call does away with the warnings and doesn't seem to affect the smoothing operation. After reading the help I'm a bit clueless as to any potential problems using this method would introduce. Any thoughts? – thelatemail Nov 07 '12 at 02:56
  • @thelatemail: When I run it with `cv=NA` all the curves are replaced by straight lines- that doesn't happen for you? Similarly, setting `cv=FALSE` leads to far too many degrees of freedom, and the curves are far too jagged. – David Robinson Nov 07 '12 at 03:57
  • I have no idea what I did, but somewhere I have confused functions. I am getting the same results as you now. Chalk it up to a long couple of weeks. – thelatemail Nov 07 '12 at 05:00
  • Happens all the time. I'm guessing this is the way that ELS did it, so I do think it's the right choice. – David Robinson Nov 07 '12 at 05:03