R/BRNN.R

Defines functions cv_nnet

Documented in cv_nnet

#' Cross Validate Number of Neurons in Neural Network
#'
#' @param formula a model formula
#' @param data a training data set
#' @param cv.method preferably one of "boot632" (the default), "cv", or "repeatedcv".
#' @param nfolds the number of bootstrap or cross-validation folds to use. defaults to 5.
#' @param nrep the number of repetitions for cv.method = "repeatedcv". defaults to 4.
#' @param folds a vector of pre-set cross-validation or bootstrap folds from caret::createResample or
#' caret::createFolds.
#' @param crit the criterion by which to evaluate the model performance. must be one of "MAE" (the default)
#' or "MSE".
#' @param max.neurons the largest number of neurons to consider.
#' @param select the selection rule to use. Should be one of "best" or "oneSE" (the default).
#' @return
#' a train object
#' @export
#'
cv_nnet = function(formula, data, cv.method = "boot632", nfolds = 5, max.neuron = 10, folds = NULL, nrep = 4, crit = "MAE", select = "oneSE"){

  if (!is.null(folds)) {
    nfolds = NULL
  }

  BRNN <- list(type = "Regression",
                library = "brnn",
                loop = NULL)

  BRNN$parameters <- data.frame(parameter = "neurons",
                                 class = "numeric",
                                 label = "neurons")
  BRNN$max.neuron <- max.neuron
  BRNNGrid <- function(x, y, max.neuron = BRNN$max.neuron, len = length(2:BRNN$max.neuron), search = "grid") {

    ## use grid search:
    if(search == "grid"){
      search = "grid"
    } else {
      search = "grid"
    }

    grid <- expand.grid(neurons = seq(2, max.neuron, by = 1))
    out <- grid
    return(out)
  }

  BRNN$grid <- BRNNGrid

  BRNNFit <- function(x, y, param, ...) {

    brnn::brnn(
      x = as.matrix(x),
      y = as.vector(y),
      neurons = param$neurons,
      verbose = FALSE,
      normalize = FALSE
    )
  }

  BRNN$fit <- BRNNFit
  BRNN$prob <- BRNNFit

  BRNNPred <- function(modelFit, newdata, preProc = NULL, submodels = NULL){
    brnn::predict.brnn(modelFit, newdata)
  }

  BRNN$predict <- BRNNPred

  postRobResamp = function(pred, obs) {

    isNA <- is.na(pred)
    pred <- pred[!isNA]
    obs <- obs[!isNA]
    if (!is.factor(obs) && is.numeric(obs)) {
      if (length(obs) + length(pred) == 0) {
        out <- rep(NA, 2)
      }
      else {
        huber.mean <-  function (y) {
          init.robmu = MASS::hubers(y, k = 3, initmu = median(y), s = sd(y))$mu
          MASS::hubers(y, k = 2.241403, initmu = init.robmu)$mu
        }
        robmse <- huber.mean((pred - obs)^2)
        robmae <- mean(abs(pred - obs))
        out <- c(robmse, robmae)
      }
      names(out) <- c("MSE", "MAE")
    }
    else {
      if (length(obs) + length(pred) == 0) {
        out <- rep(NA, 2)
      }
      else {
        pred <- factor(pred, levels = levels(obs))
        requireNamespaceQuietStop("e1071")
        out <- unlist(e1071::classAgreement(table(obs, pred)))[c("diag",
                                                                 "kappa")]
      }
      names(out) <- c("Accuracy", "Kappa")
    }
    if (any(is.nan(out)))
      out[is.nan(out)] <- NA
    out
  }

  Summary = function (data, lev = NULL, model = NULL)
  {
    if (is.character(data$obs))
      data$obs <- factor(data$obs, levels = lev)
    postRobResamp(data[, "pred"], data[, "obs"])
  }


  if (cv.method == "repeatedcv") {
    fitControl <- trainControl(method = cv.method,
                               number = nfolds,
                               index = folds,
                               repeats = nrep,
                               selectionFunction = select,
                               savePredictions = "all",
                               allowParallel = TRUE,
                               summaryFunction = Summary,
                               search = "grid")
  } else {

    fitControl <- trainControl(method = cv.method,
                               number = nfolds,
                               index = folds,
                               selectionFunction = select,
                               allowParallel = TRUE,
                               savePredictions = "all",
                               summaryFunction = Summary,
                               search = "grid")
  }


  fitted.models <- train(formula, data,
                         method = BRNN,
                         metric = crit,
                         tuneLength = tunlen,
                         maximize = FALSE,
                         preProcess = "range",
                         trControl = fitControl)

  return(fitted.models)

}
abnormally-distributed/cvreg documentation built on May 3, 2020, 3:45 p.m.