Draw some Bootstrap curves in R

I am wondering how to draw these multiple bootstrap curves in R. My codes are similar to

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")) 

Several Bootstrap curves are similar to fig. 8.2 bottom left in this book. ESL

enter image description here

And the data called Bone Mineral Density can be obtained from this site: data

Direct file link: here

+6
source share
1 answer

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

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

So, in order to complete the download, in accordance with the instructions in the book, you produce random rows from the data with the replacement and call plot.spline on the re-selected data:

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

Thus, you can use this function to run separate charts for men and women:

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

Final result:

enter image description here

Note. This code will generate a few warnings (not errors) that look like this:

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

This is due to re-fetching the bootstrap. smooth.spline uses cross-validation to determine the number of degrees of freedom to give a spline, but he prefers not to do this with duplicate x values ​​(since there will always be a re-fetch bootstrap there). You can get around this by choosing your own number of degrees of freedom, but this is probably suitable for this purpose.

+6
source

All Articles