Skip to content

Commit

Permalink
fixed a bug in using BoxCox
Browse files Browse the repository at this point in the history
For issue #177
  • Loading branch information
topepo committed Jul 4, 2015
1 parent f97e126 commit 34dfc7c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ release_process/Open_Data_Science_Conference/caret.snm
release_process/Open_Data_Science_Conference/caret.toc

release_process/Open_Data_Science_Conference/caret.vrb

models/files/.RData

models/parseModels.Rout
6 changes: 3 additions & 3 deletions pkg/caret/R/preProcess.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ preProcess.default <- function(x, method = c("center", "scale"),
if(any(method == "BoxCox"))
{
if(verbose) cat("Estimating Box-Cox transformations for the predictors...")
bc <- lapply(x,
bc <- lapply(as.data.frame(x),
BoxCoxTrans,
fudge = fudge,
na.rm = na.remove,
Expand All @@ -97,7 +97,7 @@ preProcess.default <- function(x, method = c("center", "scale"),
out
}
if(verbose) cat("Estimating Yeo-Johnson transformations for the predictors...")
yj <- lapply(x, yjWrap, numUnique = numUnique)
yj <- lapply(as.data.frame(x), yjWrap, numUnique = numUnique)
if(verbose) cat(" applying them to training data\n")
## Find a better way of doing this
lam <- unlist(lapply(yj, function(x) if(class(x) == "powerTransform") x$lambda else NA))
Expand All @@ -115,7 +115,7 @@ preProcess.default <- function(x, method = c("center", "scale"),
if(any(method == "expoTrans"))
{
if(verbose) cat("Estimating exponential transformations for the predictors...")
et <- lapply(x, expoTrans.default, numUnique = numUnique)
et <- lapply(as.data.frame(x), expoTrans.default, numUnique = numUnique)
if(verbose) cat(" applying them to training data\n")
for(i in seq(et)) x[,i] <- predict(et[[i]], x[,i])
} else et <- NULL
Expand Down
31 changes: 31 additions & 0 deletions pkg/caret/tests/testthat/test_BoxCox.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
context('Box Cox transformations')

###################################################################
## Generate data and do BC using the source function to get
## expected results

library(MASS)

set.seed(1)
dat <- matrix(runif(30), ncol = 3)
dat[,1] <- exp(dat[,1])
colnames(dat) <- paste0("x", 1:3)

exp_lambdas <- rep(NA, 3)
for(i in 1:ncol(dat)) {
tmp <- as.data.frame(dat)[,i,drop = FALSE]
names(tmp)[1] <- "x"
tmp_bc <- boxcox(x ~ 1, data = tmp, plotit = FALSE, lambda = seq(-2, 2, by = .1))
exp_lambdas[i] <- tmp_bc$x[which.max(tmp_bc$y)]
}

check_BoxCox <- function(x, expected = NULL) {
pp1 <- preProcess(x, method = "BoxCox")
obs_lambdas1 <- unlist(lapply(pp1$bc, function(x) x$lambda))
names(obs_lambdas1) <- NULL
expect_equal(obs_lambdas1, expected)
}

check_BoxCox(dat, expected = exp_lambdas)
check_BoxCox(as.data.frame(dat), expected = exp_lambdas)

0 comments on commit 34dfc7c

Please sign in to comment.