Skip to content

Commit

Permalink
Finally made outcome_conversion legal
Browse files Browse the repository at this point in the history
  • Loading branch information
topepo committed Jul 12, 2017
1 parent bf222f2 commit 069f057
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions pkg/caret/NAMESPACE
Expand Up @@ -331,6 +331,7 @@ export(nnetBag)
export(nullModel)
export(nzv)
export(oneSE)
export(outcome_conversion)
export(panel.calibration)
export(panel.lift)
export(panel.lift2)
Expand Down
2 changes: 2 additions & 0 deletions pkg/caret/R/misc.R
Expand Up @@ -553,6 +553,8 @@ get_range <- function(y) {
if(class(y)[1] %in% c("numeric", "integer")) extendrange(y) else extendrange(y[, "time"])
}

#' @rdname caret-internal
#' @export
outcome_conversion <- function(x, lv) {
if(is.factor(x) | is.character(x)) {
if(!is.null(attributes(lv)) && any(names(attributes(lv)) == "ordered" && attr(lv, "ordered")))
Expand Down
8 changes: 4 additions & 4 deletions pkg/caret/R/recipes.R
Expand Up @@ -878,7 +878,7 @@ loo_train_rec <- function(rec, dat, info, method,
## collate the predictions across all the sub-models
predicted <- lapply(predicted,
function(x, lv, dat) {
x <- getFromNamespace("outcome_conversion", "caret")(x, lv = lev)
x <- outcome_conversion(x, lv = lev)
dat$pred <- x
dat
},
Expand All @@ -896,7 +896,7 @@ loo_train_rec <- function(rec, dat, info, method,
predicted <- cbind(predicted, allParam)
## if saveDetails then save and export 'predicted'
} else {
pred_val <- getFromNamespace("outcome_conversion", "caret")(predicted, lv = lev)
pred_val <- outcome_conversion(predicted, lv = lev)
predicted <- ho_data
predicted$pred <- pred_val
if(ctrl$classProbs) predicted <- cbind(predicted, probValues)
Expand Down Expand Up @@ -1083,7 +1083,7 @@ train_rec <- function(rec, dat, info, method, ctrl, lev, testing = FALSE, ...) {
## collate the predictions across all the sub-models
predicted <- lapply(predicted,
function(x, lv, dat) {
x <- getFromNamespace("outcome_conversion", "caret")(x, lv = lev)
x <- outcome_conversion(x, lv = lev)
dat$pred <- x
dat
},
Expand Down Expand Up @@ -1123,7 +1123,7 @@ train_rec <- function(rec, dat, info, method, ctrl, lev, testing = FALSE, ...) {
thisResample <- cbind(allParam, thisResample)

} else {
pred_val <- getFromNamespace("outcome_conversion", "caret")(predicted, lv = lev)
pred_val <- outcome_conversion(predicted, lv = lev)
tmp <- ho_data
tmp$pred <- pred_val
if(ctrl$classProbs) tmp <- cbind(tmp, probValues)
Expand Down
17 changes: 9 additions & 8 deletions pkg/caret/R/workflows.R
@@ -1,9 +1,9 @@

### In this file, there are a lot of functions form caret that are
### references using the explicit namespace operator (:::). For some
### reason, with some parallel processing technologies and foreach,
### In this file, there are a lot of functions from packages that are
### referenced using `getFromNamespace`. For some
### reason, with _some_ parallel processing technologies and foreach,
### functions inside of caret cannot be found despite using the
### ".packages" argument and calling the caret package via library().
### ".packages" argument or even calling the caret package via library().

getOper <- function(x) if(x) `%dopar%` else `%do%`
getTrainOper <- function(x) if(x) `%dopar%` else `%do%`
Expand Down Expand Up @@ -82,6 +82,7 @@ nominalTrainWorkflow <- function(x, y, wts, info, method, ppOpts, ctrl, lev, tes
if(!(length(ctrl$seeds) == 1 && is.na(ctrl$seeds))) set.seed(ctrl$seeds[[iter]][parm])

loadNamespace("caret")

if(ctrl$verboseIter) progress(printed[parm,,drop = FALSE],
names(resampleIndex), iter)

Expand Down Expand Up @@ -202,7 +203,7 @@ nominalTrainWorkflow <- function(x, y, wts, info, method, ppOpts, ctrl, lev, tes
## collate the predicitons across all the sub-models
predicted <- lapply(predicted,
function(x, y, wts, lv, rows) {
x <- getFromNamespace("outcome_conversion", "caret")(x, lv = lev)
x <- outcome_conversion(x, lv = lev)
out <- data.frame(pred = x, obs = y, stringsAsFactors = FALSE)
if(!is.null(wts)) out$weights <- wts
out$rowIndex <- rows
Expand Down Expand Up @@ -288,7 +289,7 @@ nominalTrainWorkflow <- function(x, y, wts, info, method, ppOpts, ctrl, lev, tes
thisResample <- cbind(allParam, thisResample)

} else {
if(is.factor(y)) predicted <- getFromNamespace("outcome_conversion", "caret")(predicted, lv = lev)
if(is.factor(y)) predicted <- outcome_conversion(predicted, lv = lev)
tmp <- data.frame(pred = predicted,
obs = y[holdoutIndex],
stringsAsFactors = FALSE)
Expand Down Expand Up @@ -517,7 +518,7 @@ looTrainWorkflow <- function(x, y, wts, info, method, ppOpts, ctrl, lev, testing
## collate the predictions across all the sub-models
predicted <- lapply(predicted,
function(x, y, wts, lv, rows) {
x <- getFromNamespace("outcome_conversion", "caret")(x, lv = lev)
x <- outcome_conversion(x, lv = lev)
out <- data.frame(pred = x, obs = y, stringsAsFactors = FALSE)
if(!is.null(wts)) out$weights <- wts
out$rowIndex <- rows
Expand All @@ -539,7 +540,7 @@ looTrainWorkflow <- function(x, y, wts, info, method, ppOpts, ctrl, lev, testing
predicted <- cbind(predicted, allParam)
## if saveDetails then save and export 'predicted'
} else {
predicted <- getFromNamespace("outcome_conversion", "caret")(predicted, lv = lev)
predicted <- outcome_conversion(predicted, lv = lev)
predicted <- data.frame(pred = predicted,
obs = y[holdoutIndex],
stringsAsFactors = FALSE)
Expand Down
3 changes: 3 additions & 0 deletions pkg/caret/man/caret-internal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 069f057

Please sign in to comment.