(This article was first published on

**Yet Another Blog in Statistical Computing » S+/R**, and kindly contributed to R-bloggers)While coding ensemble methods in data mining with R, e.g. bagging, we often need to generate many data and models objects with sequential names. Below is a quick example how to use **assign()** function to generate many prediction objects on the fly and then retrieve these predictions with **mget()** to do the model averaging.

data(Boston, package = "MASS") for (i in 1:10) { set.seed(i) smp <- Boston[sample(1:nrow(Boston), nrow(Boston), replace = TRUE), ] glm <- glm(medv ~ ., data = smp) prd <- predict(glm, Boston) ### ASSIGN A LIST OF SEQUENTIAL NAMES TO PREDICTIONS ### assign(paste("p", i, sep = ""), prd) } ### RETURN NAMED OBJECTS TO A LIST ### plist <- mget(paste('p', 1:i, sep = '')) ### AGGREGATE ALL PREDICTIONS ### pcols <- do.call('cbind', plist) pred_medv <- rowSums(pcols) / i ### A SIMPLE FUNCTION CALCULATION R-SQUARE ### r2 <- function(y, yhat) { ybar <- mean(y) r2 <- sum((yhat - ybar) ^ 2) / sum((y - ybar) ^ 2) return(r2) } print(r2(Boston$medv, pred_medv)) # OUTPUT: # [1] 0.7454225

To

**leave a comment**for the author, please follow the link and comment on their blog:**Yet Another Blog in Statistical Computing » S+/R**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...