[This article was first published on R snippets, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Together with Mateusz Zawisza I have just published a new book in Polish on building predictive models in GNU R. It can be bought at Oficyna Wydawnicza SGH. The book presents complete examples of basic data mining processes.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Although the book is in Polish, sources of all procedures used in it, which are available on my website, can be used without the book. Here is a simplified code from exercise 4.5 presenting neural network bagging:
library(nnet)< o:p>
set.seed(1)< o:p>
SAMPLE_SIZE <- 256< o:p>
X <- seq(-2, 2, length.out = SAMPLE_SIZE)< o:p>
TRUE_Y <- X ^ 2 / 2 + sin(4 * X)< o:p>
y <- TRUE_Y + 2 * rnorm(SAMPLE_SIZE)< o:p>
GetBootstrapPrediction <- function() {< o:p>
bootstrap.indices <- sample(SAMPLE_SIZE, replace = T)< o:p>
bootstrap.sample.y <- y[bootstrap.indices]< o:p>
bootstrap.sample.x <- X[bootstrap.indices]< o:p>
bootstrap.model <- nnet(bootstrap.sample.y ~bootstrap.sample.x,< o:p>
lin = T, size = 4, trace = FALSE, maxit = 10 ^ 6)< o:p>
return(predict(bootstrap.model, data.frame(bootstrap.sample.x = X)))< o:p>
}< o:p>
progress.bar <- winProgressBar(“Progress in %”, “0% done”, 0, 1, 0)< o:p>
BOOTSTRAP_REPLICATIONS <- 1024< o:p>
bootstrap.predictions <- rep(0, SAMPLE_SIZE)< o:p>
for (i in 1:BOOTSTRAP_REPLICATIONS) {< o:p>
bootstrap.predictions <-bootstrap.predictions +< o:p>
GetBootstrapPrediction()< o:p>
percentage <- i / BOOTSTRAP_REPLICATIONS< o:p>
setWinProgressBar(progress.bar, percentage, “Progress in %”,< o:p>
sprintf(“%d%% done”, round(100 * percentage)))< o:p>
}< o:p>
close(progress.bar)< o:p>
plot(X, y,xlim = c(-2, 2), ylim = c(-5, 6))< o:p>
lines(X, TRUE_Y, lwd = 4) < o:p>
lines(X, bootstrap.predictions /BOOTSTRAP_REPLICATIONS,lwd = 3, col = 3)
It produces the following graph. Circles represent training data, black line is true relationship and green line is prediction from bagging procedure:
To leave a comment for the author, please follow the link and comment on their blog: R snippets.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.