Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw multiple Bootstrap curves in R

Tags:

r

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

like image 973
Rick Kim Avatar asked Nov 06 '12 04:11

Rick Kim


1 Answers

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.

like image 120
David Robinson Avatar answered Oct 17 '22 22:10

David Robinson