Skip to content

Commit

Permalink
Update funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
shangzhi-hong committed Jun 25, 2020
1 parent 53c6e4b commit 496aa6a
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ RoxygenNote: 7.1.0
Depends:
R (>= 3.5.0),
mice (>= 3.9.0),
ranger (>= 0.12.3)
ranger (>= 0.12.1)
Suggests:
testthat (>= 2.1.0),
knitr,
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## RfEmpImp 2.1.5
* Add new wrapper function to recover dependency on `ranger (>= 0.12.1)` and
to fix parameter passing
* New independent `mice.impute.rfpred.norm` function, and thus simplified
`mice.impute.rfpred.emp` function
* Bypass collinearity and constant checks in `mice` for `imp.*` functions
Expand All @@ -11,7 +13,6 @@ with the terminal nodes
* Add new function `conv.factor` for converting variables
* Update dependency for `mice (>= 3.9.0)` to avoid collinearity checks during
imputation
* Update dependency for `ranger (>= 0.12.3)` to fix parameter passing
* Update documentations

## RfEmpImp 2.0.3
Expand Down
12 changes: 9 additions & 3 deletions R/mice.impute.rfnode.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,19 @@ mice.impute.rfnode <- function(
}
xMis <- x[wy, , drop = FALSE]
# Output in-bag list when using conditional distribution
# Let ranger handle unused arguments after v0.12.3
rfObj <- suppressWarnings(ranger(x = xObs,
# TODO: Let ranger handle unused arguments after v0.12.3
# rfObj <- suppressWarnings(ranger(x = xObs,
# y = yObs,
# num.trees = num.trees.node,
# keep.inbag = use.node.cond.dist,
# num.threads = num.threads,
# ...))
rfObj <- rangerCallerSafe(x = xObs,
y = yObs,
num.trees = num.trees.node,
keep.inbag = use.node.cond.dist,
num.threads = num.threads,
...))
...)
# Get Nodes for training and test set
nodeObjMis <- predict(rfObj, data = xMis, type = "terminalNodes")
nodeObjObs <- predict(rfObj, data = xObs, type = "terminalNodes")
Expand Down
36 changes: 30 additions & 6 deletions R/mice.impute.rfpred.cate.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,43 @@ mice.impute.rfpred.cate <- function(
xMis <- x[wy, , drop = FALSE]
if (isTRUE(forest.vote.cate)) {
# Construct predictions based on major votes, less variant
rangerObj <- suppressWarnings(ranger(
# TODO: Let ranger handle unused arguments after v0.12.3
# rangerObj <- suppressWarnings(ranger(
# x = xObs,
# y = yObs,
# oob.error = FALSE,
# num.trees = num.trees.cate,
# num.threads = num.threads,
# ...))
rangerObj <- rangerCallerSafe(
x = xObs,
y = yObs,
oob.error = FALSE,
num.trees = num.trees.cate,
num.threads = num.threads,
...))
...)
impVal <- predictions(predict(rangerObj, xMis))
} else if (isTRUE(use.pred.prob.cate)) {
# Construct predictions based on probabilities
# Suppress warnings like:
# "Dropped unused factor level(s) in dependent variable"
# Let ranger handle unused arguments after v0.12.3
rangerObjProb <- suppressWarnings(ranger(
# TODO: Let ranger handle unused arguments after v0.12.3
# rangerObjProb <- suppressWarnings(ranger(
# x = xObs,
# y = yObs,
# probability = TRUE,
# oob.error = FALSE,
# num.trees = num.trees.cate,
# num.threads = num.threads,
# ...))
rangerObjProb <- rangerCallerSafe(
x = xObs,
y = yObs,
probability = TRUE,
oob.error = FALSE,
num.trees = num.trees.cate,
num.threads = num.threads,
...))
...)
misPredMat <- predictions(predict(rangerObjProb, xMis))
yLevels <- colnames(misPredMat)
impValChar <- apply(
Expand All @@ -149,7 +165,15 @@ mice.impute.rfpred.cate <- function(
impVal <- factor(x = impValChar, levels = levels(y))
} else {
# Imputation using random draws from predictions of trees
rangerObj <- ranger(
# TODO: Let ranger handle unused arguments after v0.12.3
# rangerObj <- suppressWarnings(ranger(
# x = xObs,
# y = yObs,
# oob.error = FALSE,
# num.trees = num.trees.cate,
# num.threads = num.threads,
# ...))
rangerObj <- rangerCallerSafe(
x = xObs,
y = yObs,
oob.error = FALSE,
Expand Down
11 changes: 9 additions & 2 deletions R/mice.impute.rfpred.emp.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,20 @@ mice.impute.rfpred.emp <- function(
}
xMis <- x[wy, , drop = FALSE]
# Let ranger handle unused arguments after v0.12.3
rfObj <- suppressWarnings(ranger(
# rfObj <- suppressWarnings(ranger(
# x = xObs,
# y = yObs,
# oob.error = TRUE,
# num.trees = num.trees.cont,
# num.threads = num.threads,
# ...))
rfObj <- rangerCallerSafe(
x = xObs,
y = yObs,
oob.error = TRUE,
num.trees = num.trees.cont,
num.threads = num.threads,
...))
...)
misPredVal <- predictions(predict(rfObj, xMis))
# Get empirical OOB prediction errors
oobErrEmp <- yObs - rfObj[["predictions"]]
Expand Down
24 changes: 14 additions & 10 deletions R/mice.impute.rfpred.norm.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,20 @@ mice.impute.rfpred.norm <- function(
}
xMis <- x[wy, , drop = FALSE]
# Let ranger handle unused arguments after v0.12.3
rfObj <- suppressWarnings(
ranger(
x = xObs,
y = yObs,
oob.error = TRUE,
num.trees = num.trees.cont,
num.threads = num.threads,
...
)
)
# rfObj <- suppressWarnings(ranger(
# x = xObs,
# y = yObs,
# oob.error = TRUE,
# num.trees = num.trees.cont,
# num.threads = num.threads,
# ...))
rfObj <- rangerCallerSafe(
x = xObs,
y = yObs,
oob.error = TRUE,
num.trees = num.trees.cont,
num.threads = num.threads,
...)
misPredVal <- predictions(predict(rfObj, xMis))
if (norm.err.cont) {
oobMse <- NULL
Expand Down
14 changes: 14 additions & 0 deletions R/rangerCallerSafe.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#' Remove unnecessary arguments for \code{ranger} function
#'
#' This function serves as an workaround for ranger function.
# Will let ranger handle unused arguments after v0.12.3.
#'
#' @param ... Parameters to pass down.
#'
#' @return Constructed \code{ranger} object.
rangerCallerSafe <- function(...) {
params <- list(...)
validName <- setdiff(names(formals(ranger)), "...")
params <- params[names(params) %in% validName]
return(suppressWarnings(do.call(what = ranger, args = params)))
}
17 changes: 17 additions & 0 deletions man/rangerCallerSafe.Rd

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

1 change: 1 addition & 0 deletions tests/testthat/test-Imp-Pred-Continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,4 @@ test_that("mice.impute.rfpred.norm works for continuous variables", {
}
}
})

0 comments on commit 496aa6a

Please sign in to comment.