1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
|
#' A General Framework For Bagging
#' @aliases bag.default bag bagControl predict.bag ldaBag plsBag nbBag ctreeBag svmBag nnetBag
#'
#' @description \code{bag} provides a framework for bagging classification or regression models. The user can provide their own functions for model building, prediction and aggregation of predictions (see Details below).
#'
#'
#' @param x a matrix or data frame of predictors
#' @param y a vector of outcomes
#' @param B the number of bootstrap samples to train over.
#' @param bagControl a list of options.
#' @param \dots arguments to pass to the model function
#' @param fit a function that has arguments \code{x}, \code{y} and \code{...} and produces a model object #' that can later be used for prediction. Example functions are found in \code{ldaBag}, \code{plsBag}, #' \code{nbBag}, \code{svmBag} and \code{nnetBag}.
#' @param predict a function that generates predictions for each sub-model. The function should have #' arguments \code{object} and \code{x}. The output of the function can be any type of object (see the #' example below where posterior probabilities are generated. Example functions are found in \code{ldaBag}#' , \code{plsBag}, \code{nbBag}, \code{svmBag} and \code{nnetBag}.)
#' @param aggregate a function with arguments \code{x} and \code{type}. The function that takes the output #' of the \code{predict} function and reduces the bagged predictions to a single prediction per sample. #' the \code{type} argument can be used to switch between predicting classes or class probabilities for #' classification models. Example functions are found in \code{ldaBag}, \code{plsBag}, \code{nbBag}, #' \code{svmBag} and \code{nnetBag}.
#' @param downSample logical: for classification, should the data set be randomly sampled so that each #' class has the same number of samples as the smallest class?
#' @param oob logical: should out-of-bag statistics be computed and the predictions retained?
#' @param allowParallel a parallel backend is loaded and available, should the function use it?
#' @param vars an integer. If this argument is not \code{NULL}, a random sample of size \code{vars} is taken of the predictors in each bagging iteration. If \code{NULL}, all predictors are used.
#' @param object an object of class \code{bag}.
#' @param newdata a matrix or data frame of samples for prediction. Note that this argument must have a non-null value
#' @param digits minimal number of \emph{significant digits}.
#'
#' @details The function is basically a framework where users can plug in any model in to assess
#' the effect of bagging. Examples functions can be found in \code{ldaBag}, \code{plsBag}
#' , \code{nbBag}, \code{svmBag} and \code{nnetBag}.
#' Each has elements \code{fit}, \code{pred} and \code{aggregate}.
#'
#' One note: when \code{vars} is not \code{NULL}, the sub-setting occurs prior to the \code{fit} and #' \code{predict} functions are called. In this way, the user probably does not need to account for the #' change in predictors in their functions.
#'
#' When using \code{bag} with \code{\link{train}}, classification models should use \code{type = "prob"} #' inside of the \code{predict} function so that \code{predict.train(object, newdata, type = "prob")} will #' work.
#'
#' If a parallel backend is registered, the \pkg{foreach} package is used to train the models in parallel.
#'
#' @return
#' \code{bag} produces an object of class \code{bag} with elements
#' \item{fits }{a list with two sub-objects: the \code{fit} object has the actual model fit for that #' bagged samples and the \code{vars} object is either \code{NULL} or a vector of integers corresponding to which predictors were sampled for that model}
#' \item{control }{a mirror of the arguments passed into \code{bagControl}}
#' \item{call }{the call}
#' \item{B }{the number of bagging iterations}
#' \item{dims }{the dimensions of the training set}
#'
#' @author Max Kuhn
#'
#' @examples
#' ## A simple example of bagging conditional inference regression trees:
#' data(BloodBrain)
#'
#' ## treebag <- bag(bbbDescr, logBBB, B = 10,
#' ## bagControl = bagControl(fit = ctreeBag$fit,
#' ## predict = ctreeBag$pred,
#' ## aggregate = ctreeBag$aggregate))
#'
#'
#'
#'
#' ## An example of pooling posterior probabilities to generate class predictions
#' data(mdrr)
#'
#' ## remove some zero variance predictors and linear dependencies
#' mdrrDescr <- mdrrDescr[, -nearZeroVar(mdrrDescr)]
#' mdrrDescr <- mdrrDescr[, -findCorrelation(cor(mdrrDescr), .95)]
#'
#' ## basicLDA <- train(mdrrDescr, mdrrClass, "lda")
#'
#' ## bagLDA2 <- train(mdrrDescr, mdrrClass,
#' ## "bag",
#' ## B = 10,
#' ## bagControl = bagControl(fit = ldaBag$fit,
#' ## predict = ldaBag$pred,
#' ## aggregate = ldaBag$aggregate),
#' ## tuneGrid = data.frame(vars = c((1:10)*10 , ncol(mdrrDescr))))
#'
#' @keywords models
#'
#' @export
"bag" <-
function(x, ...)
UseMethod("bag")
#' @rdname bag
#' @export
bagControl <- function(
fit = NULL, predict = NULL, aggregate = NULL, downSample = FALSE,
oob = TRUE, allowParallel = TRUE)
{
list(fit = fit,
predict = predict,
aggregate = aggregate,
downSample = downSample,
oob = oob,
allowParallel = allowParallel)
}
#' @rdname bag
#' @method bag default
#' @export
"bag.default" <-
function(x, y, B = 10, vars = ncol(x), bagControl = NULL, ...)
{
funcCall <- match.call(expand.dots = TRUE)
if(is.null(bagControl)) stop("Please specify 'bagControl' with the appropriate functions")
if(!is.null(vars) && vars < 1) stop("vars must be an integer > 0")
if(bagControl$downSample & is.numeric(y)) {
warning("down-sampling with regression... downSample changed to FALSE")
bagControl$downSample <- FALSE
}
if(is.null(bagControl$fit) | is.null(bagControl$predict) |
is.null(bagControl$aggregate)) {
stop("The control arguments 'fit', 'predict' and 'aggregate' should have non-NULL values")
}
fitter <- function(index, x, y, ctrl, v, ...)
{
subX <- x[index,, drop = FALSE]
subY <- y[index]
if(!is.null(v))
{
if(v > ncol(x)) v <- ncol(x)
subVars <- sample(1:ncol(subX), ceiling(v))
subX <- subX[, subVars, drop = FALSE]
} else subVars <- NULL
if(ctrl$downSample)
{
freaks <- table(subY)
smallFreak <- min(freaks)
splitUp <- split(seq(along.with = subY), subY)
splitUp <- lapply(splitUp,
sample,
size = smallFreak)
keepers <- unlist(splitUp)
subX <- subX[keepers,,drop = FALSE]
subY <- subY[keepers]
}
fit <- ctrl$fit(subX, subY, ...)
if(ctrl$oob)
{
pred <- ctrl$predict(fit, x[-unique(index), subVars, drop = FALSE])
if(is.vector(pred))
{
out <- data.frame(pred = pred, obs = y[-unique(index)])
} else {
out <- as.data.frame(pred, stringsAsFactors = TRUE)
out$obs <- y[-unique(index)]
if(is.factor(y) & !(any(names(out) == "pred")))
{
## Try to detect class probs and make a pred factor
if(all(levels(y) %in% names(out)))
{
pred <- apply(out[, levels(y)], 1, which.max)
pred <- factor(levels(y)[pred], levels = levels(y))
out$pred <- pred
}
}
}
out$key <- paste(sample(letters, 10, replace = TRUE), collapse = "")
} else out <- NULL
list(fit = fit,
vars = subVars,
oob = out)
}
btSamples <- createResample(y, times = B)
`%op%` <- if(bagControl$allowParallel) `%dopar%` else `%do%`
btFits <- foreach(iter = seq(along.with = btSamples),
.verbose = FALSE,
.packages = "caret",
.errorhandling = "stop") %op%
fitter(btSamples[[iter]], x = x, y = y, ctrl = bagControl, v = vars, ...)
structure(
list(fits = btFits,
control = bagControl,
call = funcCall,
B = B,
vars = vars,
smallClass = min(table(y)),
dims = dim(x)),
class = "bag")
}
#' @importFrom stats contrasts model.matrix model.response model.weights na.omit
#' @export
"bag.formula" <-
function (formula, data = NULL,..., subset, weights, na.action = na.omit)
{
funcCall <- match.call(expand.dots = TRUE)
if (!inherits(formula, "formula"))
stop("method is only for formula objects")
m <- match.call(expand.dots = FALSE)
mIndex <- match(c("formula", "data", "subset", "weights", "na.action"), names(m), 0)
m <- m[c(1, mIndex)]
m$... <- m$B <- m$vars <- m$bagControl <- NULL
m$na.action <- na.action
m[[1]] <- as.name("model.frame")
m <- eval(m, parent.frame())
Terms <- attr(m, "terms")
attr(Terms, "intercept") <- 0
y <- model.response(m)
w <- model.weights(m)
x <- model.matrix(Terms, m)
cons <- attr(x, "contrast")
xint <- match("(Intercept)", colnames(x), nomatch = 0)
if (xint > 0) x <- x[, -xint, drop = FALSE]
out <- bag.default(x, y, ...)
out$call <- funcCall
out
}
#' @rdname bag
#' @method predict bag
#' @importFrom stats predict
#' @export
"predict.bag" <-
function(object, newdata = NULL, ...)
{
if(is.null(newdata)) stop("please provide a data set for prediction")
predictor <- function(obj, x, ctrl)
{
if(!is.null(obj$vars)) x <- x[, obj$vars, drop = FALSE]
pred <- ctrl$predict(obj$fit, x)
}
btPred <- lapply(object$fit, predictor, x = newdata, ctrl = object$control)
object$control$aggregate(btPred, ...)
}
#' @rdname bag
#' @method print bag
#' @export
print.bag <- function (x, ...)
{
printCall(x$call)
cat("\nB:", x$B,"\n")
cat("Training data:", x$dims[2], "variables and", x$dims[1], "samples\n")
cat(ifelse(is.null(x$vars) || x$dims[2] == x$vars,
"All variables were used in each model",
paste("Each model used", x$vars, "random",
ifelse(x$vars == 1, "variable", "variables"), "predictors")))
cat('\n')
if(x$control$downSample)
{
cat("Training data was down-sampled to balance the classes to",
x$smallClass, "samples per class\n\n")
}
invisible(x)
}
#' @rdname bag
#' @method summary bag
#' @importFrom stats quantile
#' @export
"summary.bag" <-
function(object, ...)
{
hasPred <- any(names(object$fits[[1]]$oob) == "pred")
if(object$control$oob & hasPred)
{
## to avoid a 'no visible binding for global variable' warning
key <- NULL
oobData <- lapply(object$fits, function(x) x$oob)
oobData <- do.call("rbind", oobData)
oobResults <- ddply(oobData, .(key), defaultSummary)
oobResults$key <- NULL
oobStat <- apply(oobResults, 2,
function(x) quantile(x,
na.rm = TRUE,
probs = c(0, 0.025, .25, .5, .75, .975, 1)))
rownames(oobStat) <- paste(format(as.numeric(format(gsub("%", "", rownames(oobStat))))),
"%", sep = "")
B <- nrow(oobResults)
} else {
oobStat <- NULL
B <- NULL
}
out <- list(oobStat = oobStat, call = object$call, B = B)
class(out) <- "summary.bag"
out
}
#' @rdname bag
#' @method print summary.bag
#' @export
"print.summary.bag" <-
function(x, digits = max(3, getOption("digits") - 3), ...)
{
printCall(x$call)
if(!is.null(x$oobStat))
{
cat("Out of bag statistics (B = ", x$B, "):\n\n", sep = "")
print(x$oobStat, digits = digits)
} else cat("No out of bag statistics\n")
cat("\n")
}
#' @rdname bag
#' @importFrom stats median predict
#' @export
ldaBag <- list(fit = function(x, y, ...)
{
loadNamespace("MASS")
MASS::lda(x, y, ...)
},
pred = function(object, x)
{
if(!is.data.frame(x)) x <- as.data.frame(x, stringsAsFactors = TRUE)
predict(object, x)$posterior
},
aggregate = function(x, type = "class")
{
## The class probabilities come in as a list of matrices
## For each class, we can pool them then average over them
pooled <- x[[1]] * NA
n <- nrow(pooled)
classes <- colnames(pooled)
for(i in 1:ncol(pooled))
{
tmp <- lapply(x, function(y, col) y[,col], col = i)
tmp <- do.call("rbind", tmp)
pooled[,i] <- apply(tmp, 2, median)
}
pooled <- apply(pooled, 1, function(x) x/sum(x))
if(n != nrow(pooled)) pooled <- t(pooled)
if(type == "class")
{
out <- factor(classes[apply(pooled, 1, which.max)],
levels = classes)
} else out <- as.data.frame(pooled, stringsAsFactors = TRUE)
out
})
#' @rdname bag
#' @importFrom stats median predict
#' @export
plsBag <- list(fit = function(x, y, ...)
{
loadNamespace("pls")
caret::plsda(x, y, ...)
},
pred = function(object, x)
{
if(!is.data.frame(x)) x <- as.data.frame(x, stringsAsFactors = TRUE)
predict(object, x, type = "prob")[,,]
},
aggregate = function(x, type = "class")
{
pooled <- x[[1]] * NA
classes <- colnames(pooled)
for(i in 1:ncol(pooled))
{
tmp <- lapply(x, function(y, col) y[,col], col = i)
tmp <- do.call("rbind", tmp)
pooled[,i] <- apply(tmp, 2, median)
}
if(type == "class")
{
out <- factor(classes[apply(pooled, 1, which.max)],
levels = classes)
} else out <- as.data.frame(pooled, stringsAsFactors = TRUE)
out
})
#' @rdname bag
#' @importFrom stats median predict
#' @export
nbBag <- list(fit = function(x, y, ...)
{
loadNamespace("klaR")
klaR::NaiveBayes(x, y, usekernel = TRUE, fL = 2, ...)
},
pred = function(object, x)
{
if(!is.data.frame(x)) x <- as.data.frame(x, stringsAsFactors = TRUE)
as.data.frame(predict(object, x)$posterior, stringsAsFactors = TRUE)
},
aggregate = function(x, type = "class")
{
pooled <- x[[1]] * NA
classes <- colnames(pooled)
for(i in 1:ncol(pooled))
{
tmp <- lapply(x, function(y, col) y[,col], col = i)
tmp <- do.call("rbind", tmp)
pooled[,i] <- apply(tmp, 2, median)
}
if(type == "class")
{
out <- factor(classes[apply(pooled, 1, which.max)],
levels = classes)
} else out <- as.data.frame(pooled, stringsAsFactors = TRUE)
out
})
#' @rdname bag
#' @importFrom stats median
#' @export
ctreeBag <- list(fit = function(x, y, ...)
{
loadNamespace("party")
data <- as.data.frame(x, stringsAsFactors = TRUE)
data$y <- y
party::ctree(y~., data = data)
},
pred = function(object, x)
{
if(!is.data.frame(x)) x <- as.data.frame(x, stringsAsFactors = TRUE)
obsLevels <- levels(object@data@get("response")[,1])
if(!is.null(obsLevels))
{
rawProbs <- party::treeresponse(object, x)
probMatrix <- matrix(unlist(rawProbs), ncol = length(obsLevels), byrow = TRUE)
out <- data.frame(probMatrix)
colnames(out) <- obsLevels
rownames(out) <- NULL
} else out <- unlist(party::treeresponse(object, x))
out
},
aggregate = function(x, type = "class")
{
if(is.matrix(x[[1]]) | is.data.frame(x[[1]]))
{
pooled <- x[[1]] & NA
classes <- colnames(pooled)
for(i in 1:ncol(pooled))
{
tmp <- lapply(x, function(y, col) y[,col], col = i)
tmp <- do.call("rbind", tmp)
pooled[,i] <- apply(tmp, 2, median)
}
if(type == "class")
{
out <- factor(classes[apply(pooled, 1, which.max)],
levels = classes)
} else out <- as.data.frame(pooled, stringsAsFactors = TRUE)
} else {
x <- matrix(unlist(x), ncol = length(x))
out <- apply(x, 1, median)
}
out
})
#' @rdname bag
#' @importFrom stats median predict
#' @export
svmBag <- list(fit = function(x, y, ...)
{
loadNamespace("kernlab")
out <- kernlab::ksvm(as.matrix(x), y, prob.model = is.factor(y), ...)
out
},
pred = function(object, x)
{
if(is.character(lev(object)))
{
out <- predict(object, as.matrix(x), type = "probabilities")
colnames(out) <- lev(object)
rownames(out) <- NULL
} else out <- predict(object, as.matrix(x))[,1]
out
},
aggregate = function(x, type = "class")
{
if(is.matrix(x[[1]]) | is.data.frame(x[[1]]))
{
pooled <- x[[1]] & NA
classes <- colnames(pooled)
for(i in 1:ncol(pooled))
{
tmp <- lapply(x, function(y, col) y[,col], col = i)
tmp <- do.call("rbind", tmp)
pooled[,i] <- apply(tmp, 2, median)
}
if(type == "class")
{
out <- factor(classes[apply(pooled, 1, which.max)],
levels = classes)
} else out <- as.data.frame(pooled, stringsAsFactors = TRUE)
} else {
x <- matrix(unlist(x), ncol = length(x))
out <- apply(x, 1, median)
}
out
})
#' @rdname bag
#' @importFrom stats median predict
#' @export
nnetBag <- list(fit = function(x, y, ...)
{
loadNamespace("nnet")
factorY <- is.factor(y)
if(factorY) y <- class2ind(y)
out <- nnet::nnet(x, y, linout = !factorY, trace = FALSE, ...)
out$classification <- factorY
out
},
pred = function(object, x)
{
out <- predict(object, x, type= "raw")
if(object$classification)
{
colnames(out) <- colnames(object$fitted.values)
rownames(out) <- NULL
} else out <- predict(object, x, type= "raw")[,1]
out
},
aggregate = function(x, type = "class")
{
if(is.matrix(x[[1]]) | is.data.frame(x[[1]]))
{
pooled <- x[[1]] & NA
classes <- colnames(pooled)
for(i in 1:ncol(pooled))
{
tmp <- lapply(x, function(y, col) y[,col], col = i)
tmp <- do.call("rbind", tmp)
pooled[,i] <- apply(tmp, 2, median)
}
if(type == "class")
{
out <- factor(classes[apply(pooled, 1, which.max)],
levels = classes)
} else out <- as.data.frame(pooled, stringsAsFactors = TRUE)
} else {
x <- matrix(unlist(x), ncol = length(x))
out <- apply(x, 1, median)
}
out
})
|